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); }
function run() { $data = array(); $adam = new Digit(); $eve = new Digit(); $ga = new GA(); $ga->population = array($adam, $eve); //add_data_for_chart($ga->population); $ga->fitness_function = array('DigitProgram', 'fitness'); //Uses the 'total' function as fitness function $ga->num_couples = 1; //4 couples per generation (when possible) $ga->death_rate = 2; //2 kills per generation $ga->generations = 50; //Executes 100 generations $ga->crossover_functions = array('digit' => 'crossover'); //Array with functions, like $property=>$func $ga->mutation_function = 'mutation'; //Uses the 'inc' function as mutation function $ga->mutation_rate = 60; //10% mutation rate $ga->evolve(); //Run echo "<pre>"; //print_r($ga); echo "</pre>"; //add_data_for_chart($ga->population); //add_data_for_chart(GA::select($ga->population, $ga->fitness_function, 1)); //The best //$this->show_stats($ga); }
//This will be the mutation function. Just increments the property. function inc($x) { return $x + 1; } //This will be the fitness function. Is just the sum of all properties. function total($obj) { return $obj->p1 + $obj->p2; } $t1 = new Test(1, 4); $t2 = new Test(3, 2); $ga = new GA(); $ga->population = array($t1, $t2); $ga->fitness_function = 'total'; //Uses the 'total' function as fitness function $ga->num_couples = 1; //1 couple per generation $ga->death_rate = 0; //1 death per generation $ga->generations = 10; //Executes 10 generations $ga->crossover_functions = 'max'; //Uses the 'max' (built-in) function as crossover function $ga->mutation_function = 'inc'; //Uses the 'inc' function as mutation function $ga->mutation_rate = 1; //1% mutation rate $ga->evolve(); //Run debug($ga->population);