Exemplo n.º 1
0
  /**
   * Randomly select a set of n elements from the passed list until the total size of those elements
   * would exceed the specified maximum size.
   *
   * <p>This is like shuffling the passed list and then selecting the first n elements, except we
   * stop shuffling once the exit criteria has been met.
   */
  static Packing select(int indices[]) {
    Packing selection = new Packing();

    for (int i = 0; i < indices.length - 1; i++) {
      int selIndex = (int) (Math.random() * (indices.length - i) + i);
      int at = indices[selIndex];
      indices[selIndex] = indices[i];
      indices[i] = at;
      selection.add(i);
    }

    return selection;
  }
Exemplo n.º 2
0
  /**
   * Pick a cross-over point within the index range. length/2 points before it come from one array,
   * the other come from the other array until the capacity gets busted.
   */
  Packing breed(Packing other) {
    int crossOver = (int) (Math.random() * array.length);
    Packing offspring = new Packing();

    for (int i = 0; i < array.length / 2; i++) {
      if (array[(i + crossOver) % array.length] != null)
        offspring.add((i + crossOver) % array.length);

      if (array[(i + array.length / 2 + crossOver) % array.length] != null)
        offspring.add((i + array.length / 2 + crossOver) % array.length);
    }

    offspring.mutate();

    return offspring;
  }