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) { population *pop=NULL; /* Population of solutions. */ char *beststring=NULL; /* Human readable form of best solution. */ size_t beststrlen=0; /* Length of beststring. */ random_seed(20092004); pop = ga_genesis_integer( 200, /* const int population_size */ 1, /* const int num_chromo */ 100, /* 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 */ all5s_score, /* GAevaluate evaluate */ ga_seed_integer_random, /* GAseed seed */ NULL, /* GAadapt adapt */ ga_select_one_sus, /* GAselect_one select_one */ ga_select_two_sus, /* GAselect_two select_two */ ga_mutate_integer_singlepoint_drift, /* GAmutate mutate */ ga_crossover_integer_singlepoints, /* GAcrossover crossover */ NULL, /* GAreplace replace */ NULL /* vpointer User data */ ); ga_population_set_allele_min_integer(pop, 0); ga_population_set_allele_max_integer(pop, 10); ga_population_set_parameters( pop, /* population *pop */ GA_SCHEME_DARWIN, /* const ga_scheme_type scheme */ GA_ELITISM_PARENTS_SURVIVE, /* const ga_elitism_type elitism */ 0.8, /* double crossover */ 0.05, /* double mutation */ 0.0 /* double migration */ ); ga_evolution( pop, /* population *pop */ 250 /* const int max_generations */ ); /* Display final solution. */ printf("The final solution was:\n"); beststring = ga_chromosome_integer_to_string(pop, ga_get_entity_from_rank(pop,0), beststring, &beststrlen); printf("%s\n", beststring); printf("With score = %f\n", ga_get_entity_from_rank(pop,0)->fitness); /* Free memory. */ 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) { int i; /* 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++) { if (pop) ga_extinction(pop); random_seed(424242*i); pop = ga_genesis_integer( 50, /* const int population_size */ 1, /* const int num_chromo */ 25, /* const int len_chromo */ NULL, /*pingpong_ga_callback,*/ /* GAgeneration_hook generation_hook */ NULL, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ pingpong_score, /* GAevaluate evaluate */ pingpong_seed, /* GAseed seed */ NULL, /* GAadapt adapt */ ga_select_one_randomrank, /* GAselect_one select_one */ ga_select_two_randomrank, /* GAselect_two select_two */ pingpong_mutate, /* GAmutate mutate */ pingpong_crossover, /* 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 */ 0.5, /* double crossover */ 0.5, /* double mutation */ 0.0 /* double migration */ ); ga_evolution( pop, /* population *pop */ 200 /* const int max_generations */ ); pingpong_ga_callback(i, pop); } printf("The final solution found was:\n"); beststring = ga_chromosome_integer_to_string(pop, ga_get_entity_from_rank(pop,0), beststring, &beststrlen); printf("%s\n", beststring); ga_extinction(pop); s_free(beststring); exit(EXIT_SUCCESS); }
static boolean struggle_generation_hook(int generation, population *pop) { static double sum_best_fitnesses=0.0; /* Sum of best fitness score at each generation. */ double average, stddev; /* Simple stats. */ sum_best_fitnesses += ga_get_entity_from_rank(pop,0)->fitness; /* * Display statistics every 20th generation. */ if (generation%20 == 0) { printf("Generation = %d\n", generation); printf("Number of evaluations = %ld\n", evaluation_count); printf("Best fitness = %f\n", ga_get_entity_from_rank(pop,0)->fitness); ga_fitness_mean_stddev(pop, &average, &stddev); printf("Mean fitness = %f, with standard deviation = %f\n", average, stddev); if (generation>0) printf("Average best fitness for entire run = %f\n", sum_best_fitnesses/generation); } /* * Stop if we have the exact solution. */ if (!strncmp(target_text, (char *)ga_get_entity_from_rank(pop,0)->chromosome[0], strlen(target_text))) { printf("Exact solution has been found!\n"); return FALSE; } /* * Stop if the population has converged. */ if (!strncmp((char *)ga_get_entity_from_rank(pop,0)->chromosome[0], (char *)ga_get_entity_from_rank(pop,pop->size-1)->chromosome[0], strlen(target_text))) { printf("Solutions have converged!\n"); return FALSE; } return TRUE; /* TRUE indicates that evolution should continue. */ }
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. */ }
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); }
static boolean test_generation_callback(int generation, population *pop) { entity *this_entity = ga_get_entity_from_rank(pop, 0); /* The best entity. */ printf( "%d: A = %f B = %f C = %f D = %f (fitness = %f) pop_size %d\n", generation, ((double *)this_entity->chromosome[0])[0], ((double *)this_entity->chromosome[0])[1], ((double *)this_entity->chromosome[0])[2], ((double *)this_entity->chromosome[0])[3], ga_entity_get_fitness(this_entity), pop->size ); return TRUE; }
boolean pingpong_ga_callback(int generation, population *pop) { int i; /* Team member. */ int score[25]; /* Scores. */ int loss=0; /* Number of matches lost. */ double lossscore=0; /* Average score in lost matches. */ entity *best; /* Top ranked solution. */ best = ga_get_entity_from_rank(pop, 0); for (i=0; i<25; i++) { score[i] = (((int *)best->chromosome[0])[i] - i)*4 + 2; if (score[i] > 0) { loss++; lossscore += score[i]; } } lossscore /= loss; printf( "%d: %f %d %d %d %d %d %d %d %d %d\n", generation, best->fitness, ((int *)best->chromosome[0])[0], ((int *)best->chromosome[0])[1], ((int *)best->chromosome[0])[2], ((int *)best->chromosome[0])[3], ((int *)best->chromosome[0])[4], ((int *)best->chromosome[0])[5], ((int *)best->chromosome[0])[6], ((int *)best->chromosome[0])[7], ((int *)best->chromosome[0])[8] ); printf( " %d %d %d %d %d %d %d %d %d Ave. loss = %f wins = %d\n", score[0], score[1], score[2], score[3], score[4], score[5], score[6], score[7], score[8], lossscore, 25-loss ); printf( " orig %d current %d\n", pop->orig_size, pop->size); return TRUE; /* If this was to return FALSE, then the GA would terminate. */ }
boolean polynomial_generation_callback(int generation, population *pop) { entity *entity; /* Best ranked entity. */ /* * This is a easy method for implementing randomly selected * scaling factor (F in original paper) for each generation, as * suggested in: * * Karaboga D., Okdem, S. "A simple and global optimization algorithm * for engineering problems: differential evolution algorithm", * Elec. Engin. 12:53-60 (2004). * * Uncomment, if desired. */ /* pop->de_params->weighting_factor = random_double_range(-2.0, 2.0); */ /* * Write rank 1 solution every tenth generation. Note, that this is * not neccesarily the best solution because DE doesn't require the * population to be sorted, as genetic algorithms usually do. */ if ( generation%10 == 0) { entity = ga_get_entity_from_rank(pop, 0); printf( "%d: A = %f B = %f C = %f D = %f (fitness = %f)\n", generation, ((double *)entity->chromosome[0])[0], ((double *)entity->chromosome[0])[1], ((double *)entity->chromosome[0])[2], ((double *)entity->chromosome[0])[3], ga_entity_get_fitness(entity) ); } return TRUE; }
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. */ int i=0; /* Loop variable over strategies. */ entity *entity; /* Best ranked entity. */ random_seed(23091975); log_init(LOG_NORMAL, NULL, NULL, FALSE); while ( strategy[i].label != NULL ) { if ( strategy[i].weighting_factor != strategy[i].weighting_factor2 ) { printf( "Strategy %s ; C = %f ; F = rand( %f, %f )\n", strategy[i].label, strategy[i].crossover_factor, strategy[i].weighting_factor, strategy[i].weighting_factor2 ); } else { printf( "Strategy %s ; C = %f ; F = %f\n", strategy[i].label, strategy[i].crossover_factor, strategy[i].weighting_factor ); } pop = ga_genesis_double( 40, /* const int population_size */ 1, /* const int num_chromo */ 4, /* const int len_chromo */ polynomial_generation_callback,/* GAgeneration_hook generation_hook */ NULL, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ polynomial_score, /* GAevaluate evaluate */ polynomial_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_differentialevolution_parameters( pop, strategy[i].strategy, strategy[i].crossover, strategy[i].num_perturbed, strategy[i].weighting_factor, strategy[i].weighting_factor2, strategy[i].crossover_factor ); ga_differentialevolution( pop, /* population *pop */ 50 /* const int max_generations */ ); entity = ga_get_entity_from_rank(pop, 0); printf( "Final: A = %f B = %f C = %f D = %f (fitness = %f)\n", ((double *)entity->chromosome[0])[0], ((double *)entity->chromosome[0])[1], ((double *)entity->chromosome[0])[2], ((double *)entity->chromosome[0])[3], ga_entity_get_fitness(entity) ); ga_extinction(pop); i++; } exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { int i; /* Runs. */ int j; /* Loop variable. */ population *pop=NULL; /* Population of solutions. */ int map[WILDFIRE_X_DIMENSION*WILDFIRE_Y_DIMENSION]; /* Map. */ int count=0; /* Number of cisterns. */ random_seed(23091975); pop = ga_genesis_integer( 100, /* const int population_size */ 1, /* const int num_chromo */ WILDFIRE_X_DIMENSION*WILDFIRE_Y_DIMENSION,/* const int len_chromo */ wildfire_ga_callback, /* GAgeneration_hook generation_hook */ NULL, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ wildfire_score, /* GAevaluate evaluate */ wildfire_seed, /* GAseed seed */ NULL, /* GAadapt adapt */ ga_select_one_roulette_rebased, /* GAselect_one select_one */ ga_select_two_roulette_rebased, /* GAselect_two select_two */ wildfire_mutate_flip, /* GAmutate mutate */ wildfire_crossover, /* 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 */ 0.8, /* double crossover */ 0.2, /* double mutation */ 0.0 /* double migration */ ); ga_evolution_forked( pop, /* population *pop */ 250 /* const int max_generations */ ); printf( "Best solution, with score %d, was:\n", (int) ga_entity_get_fitness(ga_get_entity_from_rank(pop,0)) ); /* Decode chromsome, and count number of cisterns. */ for(i=0; i<WILDFIRE_X_DIMENSION*WILDFIRE_Y_DIMENSION; i++) { map[i] = ((int *)ga_get_entity_from_rank(pop,0)->chromosome[0])[i]; if (map[i]) count++; } printf("%d cisterns\n", count); for (i=0; i<WILDFIRE_Y_DIMENSION; i++) { for (j=0; j<WILDFIRE_X_DIMENSION; j++) { printf("%s ", ((int *)ga_get_entity_from_rank(pop,0)->chromosome[0])[i*WILDFIRE_X_DIMENSION+j]?"X":"-"); } printf("\n"); } wildfire_simulation(map, TRUE); printf("\n"); wildfire_simulation(map, TRUE); printf("\n"); wildfire_simulation(map, TRUE); printf("\n"); wildfire_simulation(map, TRUE); printf("\n"); ga_extinction(pop); exit(EXIT_SUCCESS); }
int testVdwGA(){ //--------------------- protein ------------------------------------- string fileNameA ( "/mnt/2t/xushutan/project/gold_dock/1a9u_diverse500/1A9U_protein.mol2" ) ; Molecular molA( fileNameA ); molA = readMolecularFile( fileNameA ).front(); mol = molA; bindingAtomVec = getBindingSiteAtoms( mol, bindingCenter, bindingDiameter ); if(1){ cout<<"binding site atoms:"<<bindingAtomVec.size()<<endl; for( size_t i=0; i<bindingAtomVec.size(); i++ ){ bindingAtomVec[i].print(); } } vector<Atom> protHbAcceptorAtoms = goldPar.getHydrogenBondAcceptorAtoms( bindingAtomVec ); vector<Atom> protHbDonorAtoms = goldPar.getHydrogenBondDonorAtoms( bindingAtomVec ); if(0){ cout<<"prot acceptor:"<<protHbAcceptorAtoms.size()<<endl; for( size_t i=0; i<protHbAcceptorAtoms.size(); i++ ){ protHbAcceptorAtoms[i].print(); } cout<<"prot donor:"<<protHbDonorAtoms.size()<<endl; for( size_t i=0; i<protHbDonorAtoms.size(); i++ ){ protHbDonorAtoms[i].print(); } } //---------------------------- ligand ----------------------------------- string fileNameB = "/mnt/2t/xushutan/project/gold_dock/1a9u_diverse500/ligand_corina.mol2"; Molecular molB; molB = readMolecularFile( fileNameB ).front(); vector<Atom> ligHbAcceptorAtoms = goldPar.getHydrogenBondAcceptorAtoms( molB.get_atomVec() ); vector<Atom> ligHbDonorAtoms = goldPar.getHydrogenBondDonorAtoms( molB.get_atomVec() ); if(0){ cout<<"lig acceptor:"<<ligHbAcceptorAtoms.size()<<endl; for( size_t i=0; i<ligHbAcceptorAtoms.size(); i++ ){ ligHbAcceptorAtoms[i].print(); } cout<<"lig donor:"<<ligHbDonorAtoms.size()<<endl; for( size_t i=0; i<ligHbDonorAtoms.size(); i++ ){ ligHbDonorAtoms[i].print(); } cout<<"-----------"<<endl; } Coord ligand_N25( 0.1835, 0.8981, 0.1207 ); Coord standardLigand_NC3( 3.576, 15.169, 28.381 ); Coord direction = standardLigand_NC3 - ligand_N25 ; double distance = getCoordDis( standardLigand_NC3, ligand_N25 ); ligand = translateMol( molB, direction, distance ); ligand.print(); rotableBondVec = getRotableBonds( molB ); if(1){ cout<<"rotableBondNum:"<<rotableBondVec.size()<<endl; for( size_t i=0; i<rotableBondVec.size(); i++ ){ rotableBondVec[i].print(); } } population *pop=NULL; /* The population of solutions. */ int chromoLength = 7 + rotableBondVec.size(); cout<<"chromolength:"<< rotableBondVec.size()<<endl;; random_seed( 10000 ); pop = ga_genesis_double( 6, // population size 1, // num_chromo: ligand position, rotation and bond rotation 7 + rotableBondVec.size(), // chromo length, here 7 is position 3 + rotation 3 + translate distance 1 NULL, NULL, NULL, NULL, docking_score, // ga_seed_printable_random, initialChromo, NULL, ga_select_one_sus, /* GAselect_one select_one */ ga_select_two_sus, /* GAselect_two select_two */ ga_mutate_double_multipoint, /* GAmutate mutate */ ga_crossover_double_mean, /* GAcrossover crossover */ NULL, /* GAreplace replace */ NULL /* void * userdata */ ); ga_population_set_parameters( pop, /* population *pop */ GA_SCHEME_DARWIN, /* const ga_class_type class */ GA_ELITISM_PARENTS_DIE, /* const ga_elitism_type elitism */ 0.9, /* double crossover */ 0.2, /* double mutation */ 0.0 /* double migration */ ); ga_evolution( pop, /* population *pop */ int(2) /* const int max_generations */ ); printf( "Fitness score = %f\n", ga_get_entity_from_rank(pop,0)->fitness); ga_extinction(pop); /* Deallocates all memory associated with the population and it's entities. */ exit(EXIT_SUCCESS); }