Пример #1
0
  private double getCrossoverRate(IPopulation parents) {
    int population_size = parents.getGenotypes().size();
    if (population_size <= AVG_SIZE) return properties.getCrossoverRate();

    if (population_size >= MAX_SIZE) return 0;

    return properties.getCrossoverRate() * (MAX_SIZE - population_size) / MAX_SIZE;
  }
Пример #2
0
  @Override
  public IPopulation crossover(IPopulation parents) {
    logger.debug("start");
    Population childs = new Population();
    double crossoverRate = getCrossoverRate(parents);
    if (crossoverRate <= 0) return childs;

    Iterator<IGenotype> iter = parents.getGenotypes().iterator();
    while (iter.hasNext()) {
      Genotype parent1 = (Genotype) iter.next();
      if (!iter.hasNext()) continue;
      Genotype parent2 = (Genotype) iter.next();

      if (random.nextDouble() < crossoverRate) childs.addGenotype(helper.create(parent1, parent2));
    }
    logger.debug("end");
    return childs;
  }