Пример #1
0
 private function mutate()
 {
     // order the populating into most to least fit
     $this->actors = $this->get_best_fit($this->population);
     // get most fit actors to mutate
     $mutators = array_slice($this->actors, 0, $this->breeders);
     // clear out the population and rebuild it from the best fit
     $this->actors = array();
     while (count($this->actors) < $this->population) {
         // mix 'em up
         shuffle($mutators);
         for ($i = 0; $i < $this->breeders; $i++) {
             $actor = new Actor();
             // get code
             $code = $mutators[$i]->get_code();
             // chance of mutation...
             if ($this->mutate_actor()) {
                 // find a random spot in the code
                 $pos = mt_rand(0, strlen($code));
                 $symbol = substr($actor->symbols, mt_rand(0, strlen($actor->symbols) - 1), 1);
                 $code = substr_replace($code, $symbol, $pos, 1);
             }
             // insert the code
             $actor->init($code);
             // Update the population
             $this->actors[] = $actor;
         }
     }
 }