// 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 if ($generation_stagnant > algorithm::$max_generation_stagnant) { echo "\n-- Ending TOO MANY (" . algorithm::$max_generation_stagnant . ") stagnant generations unchanged. Ending APPROX solution below \n..)"; break; } } //end of while loop //we're done
static function setSolution($newSolution) { // Loop through each character of our string and save it in our string array fitnesscalc::$solution = str_split($newSolution); // print_r(fitnesscalc::$solution); }
//keeps track of lowest time in seconds $generationCount = 0; $generation_stagnant = 0; $most_fit = 0; $most_fit_last = 400; $response = array(); //holdse the JSON object to be returned $response['done'] = false; //assume not done // Set a candidate solution static class fitnesscalc::setSolution($solution_phrase); // 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()) { $response['stagnant'] = 0; $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; $response['generation'] = $generationCount; $response['stagnant'] = $generation_stagnant; $response['best_fittest_value'] = $most_fit; $response['best_individual'] = "" . $myPop->getFittest(); $most_fit_last = $most_fit; $generation_stagnant = 0; //reset stagnant generation counter $time2 = microtime(true);