function run() { $data = array(); $adam = new HelloWorld(); $eve = new HelloWorld(); $ga = new GA(); $ga->population = array($adam, $eve); add_data_for_chart($ga->population); $ga->fitness_function = array('HelloWorldProgram', 'fitness'); //Uses the 'total' function as fitness function $ga->num_couples = 1; //4 couples per generation (when possible) $ga->death_rate = 2; //No kills per generation $ga->generations = 50; //Executes 100 generations $ga->crossover_functions = array('string' => 'avg_char_code'); //Array with functions, like $property=>$func $ga->mutation_function = 'char_gradual_change'; //Uses the 'inc' function as mutation function $ga->mutation_rate = 60; //10% mutation rate $ga->evolve(); //Run add_data_for_chart($ga->population); //add_data_for_chart(GA::select($ga->population, $ga->fitness_function, 1)); //The best $this->show_stats($ga); }
{ 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); //add_data_for_chart($ga->population, 'start'); $ga->fitness_function = 'total'; //Uses the 'total' function as fitness function $ga->num_couples = 4; //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 add_data_for_chart($ga->population); //add_data_for_chart(GA::select($ga->population,'total', 1), 'best'); //The best