I am implementing a genetic algorithm. I take the classes from here, but I don't understand how to give input to these classes. I have dynamic inputs. I have three inputs that need to be optimized. If any one knows kindly provide me the guidelines.
Here is the code
<?php
class GA {
var $population; //Objects array (same classes)
var $fitness_function; //The fitness function name (string)
var $crossover_functions; //The crossover function name (string) or array
var $mutation_function; //The mutation function name (string)
var $mutation_rate; //Mutation rate per child (%)
var $generations; //Number of generations
var $num_couples; //Number of couples for each generation
var $death_rate; //Number of killed objects for each generation
function crossover($parent1,$parent2,$cross_functions)
{
$class = get_class($parent1);
if ($class != get_class($parent2))
return false;
if (!is_array($cross_functions)) {
$cross_function = $cross_functions;
$cross_functions = array();
}
$child = new $class();
$properties = get_object_vars($parent1);
foreach ($properties as $propertie => $value)
{
if ($cross_function) $cross_functions[$propertie] = $cross_function;
if (function_exists($cross_functions[$propertie]))
$child->$propertie = $cross_functions[$propertie]($parent1->$propertie,$parent2->$propertie);
}
return $child;
}
function mutate(&$object,$mutation_function) {
$properties = get_object_vars($object);
foreach ($properties as $propertie => $value) {
$object->$propertie = $mutation_function($object->$propertie);
}
}
function fitness($object,$fitness_function) {
return $fitness_function($object);
}
//PRIVATE
function best($a, $b) {
if ($a[1] == $b[1]) return 0;
return ($a[1] < $b[1]) ? 1 : -1;
}
function select($objects,$fitness_function,$n=2) {
foreach ($objects as $object) {
$selection[] = array($object,$fitness_function($object));
}
usort($selection,array("GA", "best"));
$selection = array_slice($selection,0,$n);
foreach ($selection as $selected) {
$winners[] = $selected[0];
}
return $winners;
}
//PRIVATE
function worst($a, $b) {
if ($a[1] == $b[1]) return 0;
return ($a[1] < $b[1]) ? -1 : 1;
}
function kill(&$objects,$fitness_function,$n=2) {
foreach ($objects as $object) {
$selection[] = array($object,$fitness_function($object));
}
usort($selection,array("GA", "worst"));
$selection = array_slice($selection,0,count($selection)-$n);
$objects = array();
foreach ($selection as $selected) {
$objects[] = $selected[0];
}
}
//PRIVATE
function mass_crossover($objects,$cross_functions) {
foreach ($objects as $object) {
if (!$obj1) $obj1 = $object;
else {
$children[] = $this->crossover($obj1,$object,$this->crossover_functions);
$obj1 = null;
}
}
return $children;
}
//PRIVATE
function mass_mutation(&$objects) {
foreach($objects as $key => $object) {
if (rand(1,100) <= $this->mutation_rate) $this->mutate($objects[$key],$this->mutation_function);
}
}
function evolve() {
for ($i=0;$i<$this->generations;$i++) {
$couples = $this->select($this->population,$this->fitness_function,2*min($this->num_couples,floor(count($this->population)/2)));
$children = $this->mass_crossover($couples,$this->crossover_functions);
$this->mass_mutation($children);
$this->population = array_merge($this->population,$children);
$this->kill($this->population,$this->fitness_function,min($this->death_rate,count($this->population)-2));
}
}
}
?>
here gaexample2.php
<?php
require_once('ga.php');
class Human {
var $strength;
var $dexterity;
var $resistance;
var $intelligence;
function Human($strength=0,$dexterity=0,$resistance=0,$intelligence=0)
{
$this->strength = $strength;
$this->dexterity = $dexterity;
$this->resistance = $resistance;
$this->intelligence = $intelligence;
}
}
function debug($x) {
echo "<pre style='border: 1px solid black'>";
print_r($x);
echo '</pre>';
}
//This will be the mutation function. Just increments the property.
function inc($x) {
return $x+1;
}
//This will be the crossover function. Is just the average of all properties.
function avg($a,$b) {
return round(($a+$b)/2);
}
//This will be the fitness function. Is just the sum of all properties.
function total($obj) {
return $obj->strength + $obj->dexterity + $obj->resistance + $obj->intelligence;
}
$adam = new Human(4,2,3,1);
$eve = new Human(1,4,2,3);
$ga = new GA();
$ga->population = array($adam,$eve);
debug($ga->population);
$ga->fitness_function = 'total'; //Uses the 'total' function as fitness function
$ga->num_couples = 1; //4 couples per generation (when possible)
$ga->death_rate = 0; //No kills per generation
$ga->generations = 100; //Executes 100 generations
$ga->crossover_functions = 'avg'; //Uses the 'avg' function as crossover function
$ga->mutation_function = 'inc'; //Uses the 'inc' function as mutation function
$ga->mutation_rate = 10; //10% mutation rate
$ga->evolve(); //Run
debug($ga->population);
debug(GA::select($ga->population,'total',1)); //The best
?>
can someone help in showing the sepearte outputs of every step
The way to send values to a class is using the constructor of the class in which you can retrieve the parameters and assign it to the class variables.
Read more about constructors here.
It would be something like:
class GA {
var $population; //Objects array (same classes)
var $fitness_function; //The fitness function name (string)
var $crossover_functions; //The crossover function name (string) or array
var $mutation_function; //The mutation function name (string)
var $mutation_rate; //Mutation rate per child (%)
var $generations; //Number of generations
var $num_couples; //Number of couples for each generation
var $death_rate; //Number of killed objects for each generation
//$params would be the array of params to receive
function __construct($params) {
$this->population = $params['population'];
$this->fitness_function = $params['fitness_function'];
...
}
...
}