void Source::reproduction(const gsl_rng *rand){ assign_fitness(); gsl_ran_multinomial(rand, pop_size, pop_size, fitness, off_numbers); next_generation(rand); mutate_population(rand); }
void APG::ConstraintSolver::run() { if ( !m_readyToRun ) { error() << "DANGER WILL ROBINSON! A ConstraintSolver (serial no:" << m_serialNumber << ") tried to run before its QueryMaker finished!"; m_abortRequested = true; return; } if ( m_domain.empty() ) { debug() << "The QueryMaker returned no tracks"; return; } else { debug() << "Domain has" << m_domain.size() << "tracks"; } debug() << "Running ConstraintSolver" << m_serialNumber; emit totalSteps( m_maxGenerations ); // GENETIC ALGORITHM LOOP Population population; quint32 generation = 0; Meta::TrackList* best = NULL; while ( !m_abortRequested && ( generation < m_maxGenerations ) ) { quint32 s = m_constraintTreeRoot->suggestPlaylistSize(); m_suggestedPlaylistSize = (s > 0) ? s : m_suggestedPlaylistSize; fill_population( population ); best = find_best( population ); if ( population.value( best ) < m_satisfactionThreshold ) { select_population( population, best ); mutate_population( population ); generation++; emit incrementProgress(); } else { break; } } debug() << "solution at" << (void*)(best); m_solvedPlaylist = best->mid( 0 ); m_finalSatisfaction = m_constraintTreeRoot->satisfaction( m_solvedPlaylist ); /* clean up */ Population::iterator it = population.begin(); while ( it != population.end() ) { delete it.key(); it = population.erase( it ); } emit endProgressOperation( this ); }
void generate_new_generation(void *arg) { struct thread_arguments_info *info = arg; int population_size = info->population_size; GeneWrapper * population = info->population; GeneWrapper * next_population = info->next_population; int i, num_iterations = NUM_ITERATIONS; GeneWrapper combined_populaton[population_size * 2]; for (i = 0; i < population_size * 2; i++) { GeneWrapper * unitialized_gene = &combined_populaton[i]; unitialized_gene->gene = malloc(sizeof(PacGene)); unitialized_gene->score = 0; } char buffer[51]; bzero(buffer, 51); while (num_iterations-- > 0) { for (i = 0; i < population_size * 2; i++) { NewStringFromGene(population[i % population_size].gene, buffer); SetGeneFromString(buffer, combined_populaton[i].gene); } printf("crossover_population \n"); crossover_population(combined_populaton + population_size, population_size, population, population_size); mutate_population(combined_populaton, population_size, 0.1); // high mutation printf("reduce_population_through_ranking \n"); int reduced_size = population_size * 2; remove_duplicates(combined_populaton, &reduced_size); reduce_population_through_ranking(combined_populaton, population_size, combined_populaton, max(reduced_size, population_size), population, population_size); for (i = 0; i < population_size; i++) { NewStringFromGene(combined_populaton[i].gene, buffer); SetGeneFromString(buffer, population[i].gene); } } for (i = 0; i < population_size * 2; i++) { free(combined_populaton[i].gene); } for (i = 0; i < population_size; i++) { NewStringFromGene(population[i].gene, buffer); SetGeneFromString(buffer, next_population[i].gene); } }
void Colony::reproduction(const gsl_rng *rand){ int k,j,new_pop_size; double pop_fitness; assign_fitness(); pop_fitness = 0.; for(j=0;j<pop_size;j++) { pop_fitness += fitness[j]; } pop_fitness = pop_fitness*max_fitness; new_pop_size = gsl_ran_poisson(rand,pop_fitness); // printf("fitness: %lf\tnew pop size: %i\n", pop_fitness, new_pop_size); if (new_pop_size==0) { printf("extinct\n"); exit(0); } if (new_pop_size>limit) { printf("established\n"); exit(0); } gsl_ran_multinomial(rand, pop_size, new_pop_size, fitness, off_numbers); next_generation(rand); pop_size = new_pop_size; mutate_population(rand); for(j=pop_size;j<limit;j++) { for(k=0; k<n_del; k++) { del_matrix[j][k] = 0; } for(k=0; k<n_env; k++) { env_matrix[j][k] = 0; } } };
void crossover_population_helper(bool * flags, int index, int flag_array_size, GeneWrapper * unitialize_genes, GeneWrapper* p1, GeneWrapper* p2, int * first_uninitialized_gene) { // printf("%d %d \n", index, flag_array_size); int i; if (index == flag_array_size) { char buffer[51]; char p1buffer[51]; char p2buffer[51]; int offsets[NUM_CROSS_OVERS] = {0, 4, 7, 12, 16, 21, 26, 33, 38, 44}; int sizes[NUM_CROSS_OVERS] = {4, 3, 5, 4, 5, 5, 7, 5, 6, 6}; bzero(buffer, 51); bzero(p1buffer, 51); bzero(p2buffer, 51); GeneWrapper * unitialized_gene = &(unitialize_genes[*first_uninitialized_gene]); NewStringFromGene(p2->gene, buffer); NewStringFromGene(p1->gene, p1buffer); NewStringFromGene(p2->gene, p2buffer); for (i = 0; i < flag_array_size; i++) { if (flags[i]) { memcpy(buffer+offsets[i], p1buffer+offsets[i], sizes[i]); } } unitialized_gene->gene = malloc(sizeof(PacGene)); unitialized_gene->score = 0; SetGeneFromString(buffer, unitialized_gene->gene); mutate_population(unitialized_gene, 1, 0.09); *first_uninitialized_gene = (*first_uninitialized_gene) + 1; } else { bool options[2] = {true, false}; for (i = 0; i < 2; i++) { flags[index] = options[i]; crossover_population_helper(flags, index+1, flag_array_size, unitialize_genes, p1, p2, first_uninitialized_gene); } } }
int main(void) { int n; FILE* fptr = fopen("input.in", "r"); assert(fptr != NULL); n = count_lines(fptr); printf("\nNumber of cities: %i\n",n); /*Init cities*/ City* cities[n]; read_cities(fptr, cities); printf("Init cities succesful... \n"); /*Init distances*/ double** distances; distances=calculate_distances(distances,cities, n); printf("Init distances succesful... \n"); /*Init GA config*/ Config config; config.populationSize=1000; config.mutationRate=0.2; config.numGenerations=6000; config.numElitism=1; config.mutationSize=1; config.maxBreeding=10; config.numGenes=n; printf("Init GA config succesful... \n"); /*Init random seed*/ srand ( time(NULL) ); printf("Init random seed succesful... \n"); /*Init population*/ Population population; generate_random_population(&population, distances, &config); printf("Init population succesful... \n"); int numGenerations = 0; /*Start evolution*/ while(numGenerations < config.numGenerations){ numGenerations++; if (numGenerations%1000==0) printf("Shortest path of %ith generation: %i\n", numGenerations, population.path[0].fitness); /*Sort population*/ qsort(population.path, population.numPaths, sizeof(Path), compare_population); /*Breed population*/ simple_breed_population(&population, config.numGenes, distances); /*Mutate population*/ mutate_population(&population, distances, &config); } /*Sort population*/ qsort(population.path, population.numPaths, sizeof(Path), compare_population); printf("Shortest path is %i\n", population.path[0].fitness); int i; for(i=0;i<config.numGenes;i++){ print_city(cities[population.path[0].combination[i]]); } return 0; }