private static function mutate($indiv) { // Loop through genes for ($i = 0; $i < $indiv->size(); $i++) { if (algorithm::random() <= algorithm::$mutationRate) { $gene = individual::$characters[rand(0, strlen(individual::$characters) - 1)]; // Create random gene $indiv->setGene($i, $gene); //substitute the gene into the individual } } }
/** * Glycemic Control Algorithm Test bench * * Creates algorithm object, sets algorithm parameters, runs algorithm and returns the algorithm object. * * @param $testNumber - * @param $a1c - current a1c value * @param $a1clast - last a1c value * @param $a1cTarget - a1c target value * @param $symptoms - Type II diabetes symptoms (true/false) * @param $allergies - patient drug allergies * @param $med1 - previous medicine 1 * @param $med2 - previous medicine 2 * @param $med3 - previous medicine 3 * @param $medhistory - patient medical history */ function algTest($testNumber, $a1c, $a1clast, $a1cTarget, $symptoms, $allergies, $med1, $med2, $med3, $medhistory) { echo "<br>"; echo "Test Number "; echo $testNumber; echo "<br>"; $alg1 = new algorithm(); // set A1c values $alg1->setA1C($a1c); $alg1->setA1Clast($a1clast); $alg1->setA1CTarget($a1cTarget); $alg1->setSymptoms($symptoms); // Set allergies $alg1->setAllergies($allergies); // Set medicines $alg1->setMedicine1($med1); $alg1->setMedicine2($med2); $alg1->setMedicine3($med3); // set medical history $alg1->setMedhistory($medhistory); // print a1c values $alg1->printA1C(); $alg1->printAllergies(); echo "<br>"; // run algorithm and print results $alg1->gcAlgorithm(); echo "Results:"; echo "<br>"; $alg1->printResults(); return $alg1; }
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;