Ejemplo n.º 1
0
Archivo: ga.php Proyecto: piiskop/pstk
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
    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;