function breedNewPop($pop, $newPopSize, $ingredients) { $newPop = array(); foreach ($pop as $survivor) { $newPop[] = $survivor; $newPop[] = mutate($survivor, $ingredients); } if (count($newPop) < $newPopSize) { $newPop = array_merge(generateInitialPopulation($ingredients, $newPopSize - count($newPop)), $newPop); } return $newPop; }
<?php $digit = 10; $population_size = 100; $generations = 100; for ($i = 0; $i < $generations; $i++) { $population = new_population($population_size); $fitness = fitness($population); $fitness = mutate($fitness); echo "Fitness: " . $fitness . "<br>"; if ($fitness == 0) { echo "Done at generation " . $i; break; } } //Creating population function new_population($size) { $population = array(); for ($i = 0; $i < $size; $i++) { $population[] = new Thing(); } return $population; } //Computing fitness function function fitness($population) { $best = 100; for ($i = 0; $i < $population; $i++) { $fitness = abs($population[$i]->digit - $digit); if ($fitness < $best) {
function evolve(&$population) { $indexA = rand(0, count($population) - 1); do { $indexB = rand(0, count($population) - 1); } while($indexB == $indexA); $a = $population[$indexA]; $b = $population[$indexB]; printf("%d : %d\n", $indexA, $indexB); $choose = chooseGenome($a, $b); switch ($choose) { case 1: array_splice($population, $indexB, 1); break; case 2: array_splice($population, $indexA, 1); break; } if(count($population) == 2) { printf("Merging.\n"); $cd = merge($population[0], $population[1]); $population = array_merge($population, $cd); } if(rand(1,5) == 1) { $index = rand(0, count($population) - 1); printf("\t\t\tMutating %d\n", $index); mutate($population[$index]); } }
$sfn = $info['fn']; $sql = "INSERT INTO mutate (id,fn) VALUES ('{$sid}','{$sfn}')"; mysql_query($sql, $db) or die(mysql_error($db)); $sql = "DELETE FROM sorted where id='{$sid}' "; mysql_query($sql, $db) or die(mysql_error($db)); $t++; } $sql = "SELECT * FROM mutate order by RAND() "; $sql = mysql_query($sql, $db) or die(mysql_error($db)); $t = 0; while ($info = mysql_fetch_array($sql)) { $ms[$t] = $info['id']; $t++; } mutate($ms[0], $ms[1]); mutate($ms[2], $ms[3]); $sql = "SELECT * FROM sorted order by RAND() "; $sql = mysql_query($sql, $db) or die(mysql_error($db)); $t = 0; while ($info = mysql_fetch_array($sql)) { $cos[$t] = $info['id']; $t++; } crossover($cos[0], $cos[1]); crossover($cos[2], $cos[3]); crossover($cos[4], $cos[5]); crossover($cos[6], $cos[7]); crossover($cos[8], $cos[9]); crossover($cos[10], $cos[11]); crossover($cos[12], $cos[13]); crossover($cos[14], $cos[15]);
while ($cpop < $POP_SIZE) { // we are going to create the new population by grabbing members of the old population // two at a time via roulette wheel selection. $offspring1 = roulette($totalfitness, $population, $fitness); $offspring2 = roulette($totalfitness, $population, $fitness); // and crossover dependent on the crossover rate $crossresult = crossover($offspring1, $offspring2); if (isset($crossresult[0])) { $offspring1 = $crossresult[0]; } if (isset($crossresult[1])) { $offspring2 = $crossresult[1]; } // now mutate dependen on the mutation rate $offspring1 = mutate($offspring1); $offspring2 = mutate($offspring2); // add the offsprings in the population with fitness at 0 $temppop[$cpop++] = $offspring1; $temppop[$cpop++] = $offspring2; } // Reset fitness $fitness = array(); for ($i = 0; $i < $POP_SIZE; $i++) { $fitness[$i] = 0; } // Copy temp population to population $population = $temppop; if (!$found) { printPopulation($population); } $iterations++;