Example #1
0
 /**
  * @param $population Population
  * @param $target Person
  * @return Population
  */
 public static function evolve($population, $target, $depth)
 {
     $id = 1;
     $newPopulation = new Population();
     if (self::$ELITISM) {
         $newPopulation->addPerson($population->getFittest($target));
     }
     $pool = Algorithm::selection($population, $target);
     //        var_dump('pool');
     //        var_dump($pool);
     //Do crossover here
     for ($i = 0; $i < count($pool) - 1; $i++) {
         for ($j = $i + 1; $j < count($pool); $j++) {
             $child = Algorithm::crossover($pool[$i], $pool[$j]);
             $child->setId($depth . '_' . $id);
             $id++;
             //                var_dump('CHILD');
             //                var_dump($child);
             $newPopulation->addPerson($child);
         }
     }
     //Do mutations here
     return $newPopulation;
 }
Example #2
0
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
namespace GAHelloWorld;

require_once 'src/GAHelloWorld/Chromosome.php';
require_once 'src/GAHelloWorld/Population.php';
$p = new Population('Hello, World!');
for ($x = 1; $x <= 16789; $x++) {
    $best = reset($p->population);
    printf("Generation %d: %s\n", $x, $best->gene);
    if ($best->fitness == 0) {
        exit("Best match found");
    }
    $p->evolve();
}
exit("Unable to find the best match");
Example #3
0
$p5->setSkinColor(new SkinColor(0.5));
$p5->setId($generationLevel . '_' . $personId);
$personId++;
$p6 = new Person();
$p6->setEyesColor(new EyesColor(EyesColor::MARK_BLUE));
$p6->setHairColor(new HairColor(HairColor::MARK_BLONDE));
$p6->setSkinColor(new SkinColor(0.5));
$p6->setId($generationLevel . '_' . $personId);
$personId++;
/*$p7 = new Person();
$p7->setEyesColor(new EyesColor(EyesColor::MARK_GREEN));
$p7->setHairColor(new HairColor(HairColor::MARK_BLACK));
$p7->setSkinColor(new SkinColor(0.2));
$p7->setId($generationLevel.'_'.$personId);
$personId++;*/
$population = new Population();
$population->setPersons([$p1, $p2, $p3, $p4, $p5, $p6]);
$generationLevel++;
$solution = $population->getFittest($target);
?>

<section>
    <h1>Initial Population</h1>
    <table>
        <tr>
            <th>View</th>
            <th>Person</th>
            <th>Genes</th>
            <th>Fitness Value</th>
        </tr>
        <?php 
Example #4
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;
 }