Exemple #1
0
    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