コード例 #1
0
  @Override
  public BinaryGenome crossover(BinaryGenome parent1, BinaryGenome parent2, Random rand) {
    BinaryGenome child =
        new BinaryGenome(parent1.geneCount, parent1.geneSize, parent1.getDeepCopy());
    int crossoverIndex;

    if (child.geneCount == 2) {
      crossoverIndex = 1;
    } else {
      crossoverIndex = rand.nextInt(parent1.geneCount - 1) + 1;
    }

    // System.out.println("Crossover index: " + crossoverIndex);

    for (int geneIndex = crossoverIndex; geneIndex < child.geneCount; geneIndex++) {
      child.setGene(parent2.getGene(geneIndex), geneIndex);
    }
    return child;
  }