private static function poolSelection($pop) { // Create a pool population $pool = new population(algorithm::$poolSize, false); for ($i = 0; $i < algorithm::$poolSize; $i++) { $randomId = rand(0, $pop->size() - 1); //Get a random individual from anywhere in the population $pool->saveIndividual($i, $pop->getIndividual($randomId)); } // Get the fittest $fittest = $pool->getFittest(); return $fittest; }
echo "\n \\/ \\/ \\/ \\/ \\/ "; echo "\n-----------------------------------------------"; echo "\nUniformRate (crosssover point where to break gene string) :" . algorithm::$uniformRate; echo "\nmutationRate (what % of genes change for each mutate) :" . algorithm::$mutationRate; echo "\nPoolSize (crossover # of individuals to select in each pool ):" . algorithm::$poolSize; echo "\nInitial population # individuals:" . $initial_population_size; echo "\nelitism (keep best individual each generation true=1) :" . algorithm::$elitism; // Set a candidate solution static class fitnesscalc::setSolution($solution_phrase); echo "\nMax Fitness is :" . fitnesscalc::getMaxFitness(); echo "\n-----------------------------------------------"; // Create an initial population $time1 = microtime(true); $myPop = new population($initial_population_size, true); // Evolve our population until we reach an optimum solution while ($myPop->getFittest()->getFitness() > fitnesscalc::getMaxFitness()) { $generationCount++; $most_fit = $myPop->getFittest()->getFitness(); $myPop = algorithm::evolvePopulation($myPop); //create a new generation if ($most_fit < $most_fit_last) { // echo " *** MOST FIT ".$most_fit." Most fit last".$most_fit_last; echo "\n Generation: " . $generationCount . " (Stagnant:" . $generation_stagnant . ") Fittest: " . $most_fit . "/" . fitnesscalc::getMaxFitness(); echo " Best: " . $myPop->getFittest(); $most_fit_last = $most_fit; $generation_stagnant = 0; //reset stagnant generation counter } else { $generation_stagnant++; } //no improvement increment may want to end early