void reduce_population_through_ranking(GeneWrapper * population_after, int population_size_after, GeneWrapper * population_before, int population_size_before, GeneWrapper * base_population, int base_population_size) { char buffer[51]; int i = population_size_after - 1; int j = population_size_before - 1; GeneWrapper * population_before_copy = NULL; copy_population(&population_before_copy, population_before, population_size_before); rank_compete(population_before_copy, population_size_before, base_population, base_population_size); while (i >= 0) { NewStringFromGene(population_before_copy[j].gene, buffer); SetGeneFromString(buffer, population_after[i].gene); i--; j--; } free_population(population_before_copy, population_size_before); }
void reduce_population_through_competition(GeneWrapper * initial_population, GeneWrapper * new_population, int initial_population_size, int new_population_size) { char buffer[51]; int i = initial_population_size - 1; int j = new_population_size - 1; GeneWrapper * new_population_copy = NULL; copy_population(&new_population_copy, new_population, new_population_size); mutual_compete(new_population_size, new_population_copy); while (i >= 0) { NewStringFromGene(new_population_copy[j].gene, buffer); SetGeneFromString(buffer, initial_population[i].gene); i--; j--; } free_population(new_population_copy, new_population_size); }
/** * Reproduction phase **/ vector<vector<tower>*>* reproduction_iteration(const int nb_children, const float mutation_prob, vector<vector<tower>*>* population, vector<tower>* towers, const int dist, float taux_var) { vector<vector<tower>*>* remaining_parents = copy_population(population); // copy of the population vector<vector<tower>*>* children = new vector<vector<tower>*>(); // allocate vector of children vector<tower>* child = new vector<tower>(); int index_1, index_2; //int max_children = min(nb_children, (int) population->size() / 2); // if nb children > population / 2 can't generate enough children for (int i = 0; i < min(nb_children, (int)remaining_parents->size() / 2); i++) { do { do { index_1 = aliased_select(remaining_parents); // use the heuristic index_2 = aliased_select(remaining_parents); } while (index_2 == index_1); child = mix((*remaining_parents)[index_1], (*remaining_parents)[index_2], dist, towers, taux_var); // check on the remaining_parents } while(child->empty()); if (static_cast <float> (rand()) / static_cast <float> (RAND_MAX) < mutation_prob) { mutate(child, towers, dist, taux_var); } children->push_back(child); if (index_1 < index_2) { remaining_parents->erase(remaining_parents->begin() + index_1); remaining_parents->erase(remaining_parents->begin() + index_2 - 1); } else { remaining_parents->erase(remaining_parents->begin() + index_2); remaining_parents->erase(remaining_parents->begin() + index_1 - 1); } } for (vector<vector<tower>*>::iterator it = remaining_parents->begin(); it != remaining_parents->end(); it++) { delete *it; } delete remaining_parents; return children; }
stResult<TCity> * main_genetic(mySlimTree* SlimTree, TCity * queryObject, stDistance range) { time_t begin, end; begin = time(NULL); srand(time(NULL)); Population * pop = new_population(); Population * aux_pop = new_empty_population(); infeasible(SlimTree, pop); set_genotype(pop); evaluate_fitness(pop, SlimTree, queryObject, range); #if DEBUG print_population(pop ,"Pop", number_of_gerations); #endif while( (number_of_gerations < MAX_NUMBER_OF_GERATIONS) && (delta_fitness > 0.0001) ) { number_of_gerations++; #if DEBUG cout << "Number of Gerations: "<< number_of_gerations << endl; #endif selection_by_tournament(pop, aux_pop); #if DEBUG cout << "Torneio" << endl; #endif crossover_uniform(aux_pop); #if DEBUG cout << "Crossover Uniforme" << endl; #endif mutation(aux_pop); #if DEBUG cout << "Mutação" << endl; #endif set_phenotype(aux_pop); #if DEBUG cout << "Fenotipo" << endl; #endif infeasible(SlimTree, aux_pop); #if DEBUG cout << "Infactibilidade" << endl; #endif set_genotype(aux_pop); #if DEBUG cout << "Fenotipo" << endl; #endif copy_population(aux_pop, pop); #if DEBUG cout << "Copiar aux pop" << endl; #endif evaluate_fitness(pop, SlimTree, queryObject, range); #if DEBUG cout << "Fitness" << endl; #endif old_media_of_fitness = media_of_fitness; media_of_fitness = get_media_of_population(pop); delta_fitness = fabs(media_of_fitness - old_media_of_fitness); } end = time(NULL); #if DEBUG print_population(pop, "PopulatioN", number_of_gerations); print_statistic(pop, begin, end); #endif }