Ejemplo n.º 1
0
 public static function evolvePopulation($pop)
 {
     $newPopulation = new population($pop->size(), false);
     // Keep our best individual
     if (algorithm::$elitism) {
         $newPopulation->saveIndividual(0, $pop->getFittest());
     }
     // Crossover population
     $elitismOffset = 0;
     if (algorithm::$elitism) {
         $elitismOffset = 1;
     } else {
         $elitismOffset = 0;
     }
     // Loop over the population size and create new individuals with
     // crossover
     for ($i = $elitismOffset; $i < $pop->size(); $i++) {
         $indiv1 = algorithm::poolSelection($pop);
         $indiv2 = algorithm::poolSelection($pop);
         $newIndiv = algorithm::crossover($indiv1, $indiv2);
         $newPopulation->saveIndividual($i, $newIndiv);
     }
     // Mutate population
     for ($i = $elitismOffset; $i < $newPopulation->size(); $i++) {
         algorithm::mutate($newPopulation->getIndividual($i));
     }
     return $newPopulation;
 }