Example #1
0
 public function mutate(Population $population)
 {
     $newPopulation = new Population($population->size());
     for ($populationIndex = 0; $populationIndex < $population->size(); $populationIndex++) {
         $individual = $population->getFittestIndividual($populationIndex);
         for ($geneIndex = 0; $geneIndex < $individual->getChromosomeLength(); $geneIndex++) {
             if ($populationIndex >= $this->elitismCount) {
                 if ($this->mutationRate > Random::generate()) {
                     $newGene = 1;
                     if ($individual->getGene($geneIndex) == 1) {
                         $newGene = 0;
                     }
                     $individual->setGene($geneIndex, $newGene);
                 }
             }
         }
         $newPopulation->setIndividual($populationIndex, $individual);
     }
     return $newPopulation;
 }
 public function mutateUniform(Population $population, Element $element)
 {
     $newPopulation = new Population($population->size());
     for ($populationIndex = 0; $populationIndex < $population->size(); $populationIndex++) {
         $individual = $population->getFittestIndividual($populationIndex);
         $randomIndividual = new Individual($element);
         for ($geneIndex = 0; $geneIndex < $individual->getChromosomeLength(); $geneIndex++) {
             if ($populationIndex >= $this->elitismCount) {
                 if ($this->mutationRate > Random::generate()) {
                     $individual->setGene($geneIndex, $randomIndividual->getGene($geneIndex));
                 }
             }
         }
         $newPopulation->setIndividual($populationIndex, $individual);
     }
     return $newPopulation;
 }