int main(int argc, char **argv) { population *pop; /* Population of solutions. */ entity *solution; /* Optimised solution. */ random_seed(23091975); pop = ga_genesis_double( 50, /* const int population_size */ 1, /* const int num_chromo */ 4, /* const int len_chromo */ NULL, /* GAgeneration_hook generation_hook */ test_iteration_callback, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ test_score, /* GAevaluate evaluate */ test_seed, /* GAseed seed */ NULL, /* GAadapt adapt */ NULL, /* GAselect_one select_one */ NULL, /* GAselect_two select_two */ ga_mutate_double_singlepoint_drift, /* GAmutate mutate */ NULL, /* GAcrossover crossover */ NULL, /* GAreplace replace */ NULL /* vpointer User data */ ); ga_population_set_simplex_parameters( pop, /* population *pop */ 4, /* const int num_dimensions */ 0.5, /* const double Initial step size. */ test_to_double, /* const GAto_double to_double */ test_from_double /* const GAfrom_double from_double */ ); /* Evaluate and sort the initial population members (i.e. select best of 50 random solutions. */ ga_population_score_and_sort(pop); /* Use the best population member. */ solution = ga_get_entity_from_rank(pop, 0); ga_simplex( pop, /* population *pop */ solution, /* entity *solution */ 10000 /* const int max_iterations */ ); ga_extinction(pop); exit(EXIT_SUCCESS); }
boolean wildfire_ga_callback(int generation, population *pop) { double average, stddev; /* Statistics. */ fprintf(stderr, "%f\n", ga_entity_get_fitness(ga_get_entity_from_rank(pop,0))); if (generation > 0) { ga_population_score_and_sort(pop); ga_fitness_mean_stddev(pop, &average, &stddev); printf( "%d: Best %d Average %f Stddev %f\n", generation, (int) ga_entity_get_fitness(ga_get_entity_from_rank(pop,0)), average, stddev ); } else { printf( "Best random solution has score %d\n", (int) ga_entity_get_fitness(ga_get_entity_from_rank(pop,0)) ); } return TRUE; /* Always TRUE, so search doesn't finish. */ }