int main(int argc, char **argv) { population *pop=NULL; /* Population structure. */ char *beststring=NULL; /* Human readable form of best solution. */ size_t beststrlen=0; /* Length of beststring. */ random_seed(23091975); pop = ga_genesis_char( 120, /* const int population_size */ 1, /* const int num_chromo */ (int) strlen(target_text), /* const int len_chromo */ struggle_generation_hook, /* GAgeneration_hook generation_hook */ NULL, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ struggle_score, /* GAevaluate evaluate */ ga_seed_printable_random, /* GAseed seed */ struggle_adaptation, /* GAadapt adapt */ ga_select_one_sus, /* GAselect_one select_one */ ga_select_two_sus, /* GAselect_two select_two */ ga_mutate_printable_singlepoint_drift, /* GAmutate mutate */ ga_crossover_char_allele_mixing, /* GAcrossover crossover */ NULL, /* GAreplace replace */ NULL /* vpointer User data */ ); ga_population_set_parameters( pop, /* population *pop */ GA_SCHEME_LAMARCK_CHILDREN, /* const ga_scheme_type scheme */ GA_ELITISM_PARENTS_DIE, /* const ga_elitism_type elitism */ 0.8, /* const double crossover */ 0.05, /* const double mutation */ 0.0 /* const double migration */ ); if ( ga_evolution( pop, 1000 )<1000 ) { printf("The evolution was stopped because the termination criteria were met.\n" ); } else { printf("The evolution was stopped because the maximum number of generations were performed.\n" ); } printf( "The final solution with score %f was:\n", ga_get_entity_from_rank(pop,0)->fitness ); beststring = ga_chromosome_char_to_string(pop, ga_get_entity_from_rank(pop,0), beststring, &beststrlen); printf("%s\n", beststring); printf( "Total number of fitness evaluations: %ld\n", evaluation_count ); ga_extinction(pop); s_free(beststring); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { int i; /* Loop over runs. */ population *pop=NULL; /* Population of solutions. */ char *beststring=NULL; /* Human readable form of best solution. */ size_t beststrlen=0; /* Length of beststring. */ for (i=0; i<50; i++) { random_seed(i); pop = ga_genesis_char( 120, /* const int population_size */ 1, /* const int num_chromo */ (int) strlen(target_text), /* const int len_chromo */ NULL, /* GAgeneration_hook generation_hook */ NULL, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ struggle_score, /* GAevaluate evaluate */ ga_seed_printable_random, /* GAseed seed */ NULL, /* GAadapt adapt */ ga_select_one_sus, /* GAselect_one select_one */ ga_select_two_sus, /* GAselect_two select_two */ ga_mutate_printable_singlepoint_drift, /* GAmutate mutate */ ga_crossover_char_allele_mixing, /* GAcrossover crossover */ NULL, /* GAreplace replace */ NULL /* vpointer User data */ ); ga_population_set_parameters( pop, /* population *pop */ GA_SCHEME_DARWIN, /* const ga_scheme_type scheme */ GA_ELITISM_PARENTS_DIE, /* const ga_elitism_type elitism */ 0.9, /* double crossover */ 0.2, /* double mutation */ 0.0 /* double migration */ ); ga_evolution_threaded( pop, /* population *pop */ 500 /* const int max_generations */ ); printf( "The final solution with seed = %d was:\n", i ); beststring = ga_chromosome_char_to_string(pop, ga_get_entity_from_rank(pop,0), beststring, &beststrlen); printf("%s\n", beststring); printf( "With score = %f\n", ga_entity_get_fitness(ga_get_entity_from_rank(pop,0)) ); ga_extinction(pop); } s_free(beststring); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { population *pop=NULL; /* Population of solutions. */ char *beststring=NULL; /* Human readable form of best solution. */ size_t beststrlen=0; /* Length of beststring. */ entity *solution; /* Solution to problem. */ int num_iterations; /* Number of iterations required. */ random_seed(23091975); pop = ga_genesis_char( 100, /* const int population_size */ 1, /* const int num_chromo */ strlen(target_text), /* const int len_chromo */ NULL, /* GAgeneration_hook generation_hook */ NULL, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ struggle_score, /* GAevaluate evaluate */ struggle_seed, /* GAseed seed */ NULL, /* GAadapt adapt */ NULL, /* GAselect_one select_one */ NULL, /* GAselect_two select_two */ NULL, /* GAmutate mutate */ NULL, /* GAcrossover crossover */ NULL, /* GAreplace replace */ NULL /* vpointer User data */ ); ga_population_set_search_parameters(pop, struggle_scan_chromosome); solution = ga_get_free_entity(pop); num_iterations = ga_search( pop, /* population *pop */ solution /* entity *entity */ ); printf( "The final solution was:\n"); beststring = ga_chromosome_char_to_string(pop, solution, beststring, &beststrlen); printf("%s\n", beststring); printf( "With score = %f\n", ga_entity_get_fitness(solution) ); printf( "This required %d iterations\n", num_iterations); ga_extinction(pop); s_free(beststring); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { population *pop=NULL; /* Population of solutions. */ char *filename_in=NULL; /* Input filename. */ char *filename_out=NULL; /* Output filename. */ int i; /* Loop variable over command-line arguments. */ int generations=10; /* Number of generations to perform. */ char *beststring=NULL; /* Human readable form of best solution. */ size_t beststrlen=0; /* Length of beststring. */ random_seed(42); /* * Parse command-line. Expect '-i FILENAME' for a population to read, * otherwise a new population will be created. * '-o FILENAME' is absolutely required to specify a file to write to. * If we don't get these, then we will write the options. */ if (argc<2) { write_usage(); exit(0); } for (i=1; i<argc; i++) { if (strcmp(argv[i], "-i")==0) { /* Read pop. */ i++; if (i==argc) { printf("Input filename not specified.\n"); write_usage(); exit(0); } filename_in = argv[i]; printf("Input filename set to \"%s\"\n", filename_in); } else if (strcmp(argv[i], "-o")==0) { /* Out pop. */ i++; if (i==argc) { printf("Output filename not specified.\n"); write_usage(); exit(0); } filename_out = argv[i]; printf("Output filename set to \"%s\"\n", filename_out); } else if (strcmp(argv[i], "-n")==0) { /* Number of generations requested. */ i++; if (i==argc) { printf("Number of generations not specified.\n"); write_usage(); exit(0); } generations = atoi(argv[i]); printf("Number of generations set to %d.\n", generations); } else { /* Error parsing args. */ printf("Unable to parse command-line argument \"%s\"\n", argv[i]); write_usage(); exit(0); } } /* * Check that we had the required inputs. */ if (filename_out == NULL) { printf("No output filename was specified.\n"); write_usage(); exit(0); } /* * Read or create population. */ if (filename_in == NULL) { pop = ga_genesis_char( 40, /* const int population_size */ 1, /* const int num_chromo */ strlen(target_text), /* const int len_chromo */ NULL, /* GAgeneration_hook generation_hook */ NULL, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ struggle_score, /* GAevaluate evaluate */ ga_seed_printable_random, /* GAseed seed */ NULL, /* GAadapt adapt */ ga_select_one_roulette, /* GAselect_one select_one */ ga_select_two_roulette, /* GAselect_two select_two */ ga_mutate_printable_singlepoint_drift, /* GAmutate mutate */ ga_crossover_char_allele_mixing, /* GAcrossover crossover */ NULL, /* GAreplace replace */ NULL /* vpointer User data */ ); ga_population_set_parameters( pop, /* population *pop */ GA_SCHEME_DARWIN, /* const ga_scheme_type scheme */ GA_ELITISM_PARENTS_SURVIVE, /* const ga_elitism_type elitism */ 1.0, /* double crossover */ 0.1, /* double mutation */ 0.0 /* double migration */ ); } else { pop = ga_population_read(filename_in); pop->evaluate = struggle_score; /* Custom functions can't be saved and * therefore "pop->evaluate" must be * defined manually. Likewise, if a * custom crossover routine was used, for * example, then that would also need * to be manually defined here. */ } ga_evolution( pop, /* population *pop */ generations /* const int max_generations */ ); printf("The final solution with seed = %d was:\n", i); beststring = ga_chromosome_char_to_string(pop, ga_get_entity_from_rank(pop,0), beststring, &beststrlen); printf("%s\n", beststring); printf("With score = %f\n", ga_entity_get_fitness(ga_get_entity_from_rank(pop,0)) ); ga_population_write(pop, filename_out); printf("Population has been saved as \"%s\"\n", filename_out); ga_extinction(pop); s_free(beststring); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { int i; /* Loop over populations. */ population *pop[GA_STRUGGLE_NUM_POPS]; /* Array of populations. */ population *slavepop; /* Population for slave calculations. */ char *beststring=NULL; /* Human readable form of best solution. */ size_t beststrlen=0; /* Length of beststring. */ int rank; /* MPI rank. */ MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); printf("Process %d initialised (rank %d)\n", getpid(), rank); random_seed(42); if (rank != 0) { /* This is a slave process. */ /* * A population is created so that the callbacks are defined. Evolution doesn't * occur with this population, so population_size can be zero. In such a case, * no entities are ever seeded, so there is no significant overhead. * Strictly, several of these callbacks are not needed on the slave processes, but * their definition doesn't have any adverse effects. */ slavepop = ga_genesis_char( 0, /* const int population_size */ 1, /* const int num_chromo */ (int) strlen(target_text), /* const int len_chromo */ NULL, /* GAgeneration_hook generation_hook */ NULL, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ struggle_score, /* GAevaluate evaluate */ ga_seed_printable_random, /* GAseed seed */ NULL, /* GAadapt adapt */ ga_select_one_sus, /* GAselect_one select_one */ ga_select_two_sus, /* GAselect_two select_two */ ga_mutate_printable_singlepoint_drift, /* GAmutate mutate */ ga_crossover_char_allele_mixing, /* GAcrossover crossover */ NULL, /* GAreplace replace */ NULL /* vpointer User data */ ); printf("DEBUG: Attaching process %d\n", rank); ga_attach_mpi_slave( slavepop ); /* The slaves halt here until ga_detach_mpi_slaves(), below, is called. */ } else { /* * This is the master process. Other than calling ga_evolution_mpi() instead * of ga_evolution(), there are no differences between this code and the usual * GAUL invocation. */ for (i=0; i<GA_STRUGGLE_NUM_POPS; i++) { pop[i] = ga_genesis_char( 80, /* const int population_size */ 1, /* const int num_chromo */ (int) strlen(target_text), /* const int len_chromo */ NULL, /* GAgeneration_hook generation_hook */ NULL, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ struggle_score, /* GAevaluate evaluate */ ga_seed_printable_random, /* GAseed seed */ NULL, /* GAadapt adapt */ ga_select_one_sus, /* GAselect_one select_one */ ga_select_two_sus, /* GAselect_two select_two */ ga_mutate_printable_singlepoint_drift, /* GAmutate mutate */ ga_crossover_char_allele_mixing, /* GAcrossover crossover */ NULL, /* GAreplace replace */ NULL /* vpointer User data */ ); ga_population_set_parameters( pop[i], GA_SCHEME_DARWIN, GA_ELITISM_PARENTS_DIE, 0.75, 0.25, 0.001 ); } ga_evolution_archipelago_mpi( GA_STRUGGLE_NUM_POPS, pop, 250 ); for (i=0; i<GA_STRUGGLE_NUM_POPS; i++) { printf( "The best solution on island %d with score %f was:\n", i, ga_get_entity_from_rank(pop[i],0)->fitness ); beststring = ga_chromosome_char_to_string(pop[i], ga_get_entity_from_rank(pop[i],0), beststring, &beststrlen); printf("%s\n", beststring); ga_extinction(pop[i]); } s_free(beststring); ga_detach_mpi_slaves(); /* Allow all slave processes to continue. */ } MPI_Finalize(); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { population *popd=NULL; /* Population for Darwinian evolution. */ population *popb=NULL; /* Population for Baldwinian evolution. */ population *popl=NULL; /* Population for Lamarckian evolution. */ char *beststring=NULL; /* Human readable form of best solution. */ size_t beststrlen=0; /* Length of beststring. */ random_seed(23091975); popd = ga_genesis_char( 150, /* const int population_size */ 1, /* const int num_chromo */ (int) strlen(target_text), /* const int len_chromo */ NULL, /* GAgeneration_hook generation_hook */ NULL, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ struggle_score, /* GAevaluate evaluate */ ga_seed_printable_random, /* GAseed seed */ struggle_adaptation, /* GAadapt adapt */ ga_select_one_sus, /* GAselect_one select_one */ ga_select_two_sus, /* GAselect_two select_two */ ga_mutate_printable_singlepoint_drift, /* GAmutate mutate */ ga_crossover_char_allele_mixing, /* GAcrossover crossover */ NULL, /* GAreplace replace */ NULL /* vpointer User data */ ); ga_population_set_parameters( popd, /* population *pop */ GA_SCHEME_DARWIN, /* const ga_scheme_type scheme */ GA_ELITISM_PARENTS_DIE, /* const ga_elitism_type elitism */ 0.9, /* const double crossover */ 0.1, /* const double mutation */ 0.0 /* const double migration */ ); /* * Make exact copies of the populations, except modify * their evolutionary schemes. */ popb = ga_population_clone(popd); ga_population_set_scheme(popb, GA_SCHEME_BALDWIN_CHILDREN); popl = ga_population_clone(popd); ga_population_set_scheme(popl, GA_SCHEME_LAMARCK_CHILDREN); /* * Evolve each population in turn. */ ga_evolution( popd, /* population *pop */ 600 /* const int max_generations */ ); printf( "The final solution with Darwinian evolution with score %f was:\n", ga_get_entity_from_rank(popd,0)->fitness ); beststring = ga_chromosome_char_to_string(popd, ga_get_entity_from_rank(popd,0), beststring, &beststrlen); printf("%s\n", beststring); ga_evolution( popb, /* population *pop */ 300 /* const int max_generations */ ); printf( "The final solution with Baldwinian evolution with score %f was:\n", ga_get_entity_from_rank(popb,0)->fitness ); beststring = ga_chromosome_char_to_string(popb, ga_get_entity_from_rank(popb,0), beststring, &beststrlen); printf("%s\n", beststring); ga_evolution( popl, /* population *pop */ 300 /* const int max_generations */ ); printf( "The final solution with Lamarckian evolution with score %f was:\n", ga_get_entity_from_rank(popl,0)->fitness ); beststring = ga_chromosome_char_to_string(popl, ga_get_entity_from_rank(popl,0), beststring, &beststrlen); printf("%s\n", beststring); /* Deallocate population structures. */ ga_extinction(popd); ga_extinction(popb); ga_extinction(popl); /* Deallocate string buffer. */ s_free(beststring); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { population *pop; /* Population of solutions. */ fitting_data_t data; /* Training data. */ FILE * fData; random_seed(time(NULL)); double params_d[4]; double x; // fitting if(argc >= 3 && strcmp(argv[1], "fit") == 0) { pop = ga_genesis_char( 100, /* const int population_size */ 1, /* const int num_chromo */ 3, /* const int len_chromo */ fitting_generation_callback, /* GAgeneration_hook generation_hook */ NULL, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ fitting_score, /* GAevaluate evaluate */ //fitting_seed, /* GAseed seed */ ga_seed_char_random, NULL, /* GAadapt adapt */ //ga_select_one_linearrank, ga_select_one_random, /* GAselect_one select_one */ ga_select_two_random, /* GAselect_two select_two */ ga_mutate_char_singlepoint_randomize,/* GAmutate mutate */ ga_crossover_char_allele_mixing,/* GAcrossover crossover */ NULL, /* GAreplace replace */ NULL /* vpointer User data */ ); ga_population_set_parameters( pop, /* population *pop */ GA_SCHEME_DARWIN, /* const ga_scheme_type scheme */ GA_ELITISM_PARENTS_DIE, /* const ga_elitism_type elitism */ 0.8, /* double crossover */ 0.8, /* double mutation */ 0.0 /* double migration */ ); fData = fopen("data", "r"); debug_message("Begin Get Data\n"); get_data(fData, &data); pop->data = &data; debug_message("Done Get Data\n"); debug_message("Begin Evolution\n"); ga_evolution( pop, /* population *pop */ 200 /* const int max_generations */ ); debug_message("Done Evolution\n"); ga_extinction(pop); } // plot else if(argc >= 5 && strcmp(argv[1], "plot")==0) { params_d[0] = E_GIVEN; params_d[1] = atof(argv[2]); params_d[2] = atof(argv[3]); params_d[3] = atof(argv[4]); for(x = 0; x < 10; x += 0.01) { fprintf(stdout, "%lf %lf\n", x, target_function(x, params_d)); } } else { fprintf(stdout, "usage: %s [plot/fit] [params]\n", argv[0]); } }