PHP Classes

File: example/example2.php

Recommend this page to a friend!
  Classes of ryan silalahi   Simple Genetic Algorithm   example/example2.php   Download  
File: example/example2.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Simple Genetic Algorithm
Implement genetic algorithm to optimize population
Author: By
Last change:
Date: 9 years ago
Size: 1,086 bytes
 

Contents

Class file image Download
<?php

/*
    this example show how we can override one or more method to make more suitable genetic algorithm
*/

require __DIR__ . '/../vendor/autoload.php'; // composer autoload

class CustomGeneticAlgorithm extends \SimpleGeneticAlgorithm\SimpleGeneticAlgorithm{
   
    public function
crossover(){
       
$new_population = array();
        for(
$i = 0; $i < $this->options['population']; $i++){
           
// get random parents
           
$a = $this->population[array_rand($this->population, 1)]['chromosome'];
           
$b = $this->population[array_rand($this->population, 1)]['chromosome'];
           
           
           
// make separator from a & b
           
$sp = rand(0, strlen($a) - 1);
           
           
// get some part from a, and some part from b
           
$child = substr($a, 0, $sp) . substr($b, $sp, strlen($b));
           
           
$new_population[] = array(
               
'chromosome' => $child,
               
'fitness' => 0,
            );
        }
       
       
$this->population = $new_population;
    }
   
}

$ga = new CustomGeneticAlgorithm(array(
   
'mutation' => 25, // 25%
   
'goal' => 'Astari Ghaniarti',
   
   
'delay' => 50, // ms, if debug is false, then delay forced to 0
   
'debug' => true,
));

$ga->run();