int main(int argc, char * argv[]){ Population * pop, * selected; Individual * best_solution; int generation_num; initParameters(argc, argv); pop = genSeededPopulation(POP_SIZE); #if (defined DIVERSITY) printGeneComposition(pop); #endif determineFitness(pop); sortByFitness(pop); generation_num = 0; while(generation_num < MAX_NUM_GENERATIONS){ #if (defined VERBOSE || defined DIVERSITY) fprintf(stdout, "\n----------------- GENERATION %d -----------------\n", generation_num + 1); printPopulation(pop); #endif // FIX - use function pointers instead of flags + if statement if(selection_type == 1) selected = tournamentSelection(pop); else selected = randomSelection(pop); // FIX - use function pointers instead of flags + if statement evolvePopulation(selected, crossover_type, mutation_type); determineFitness(selected); // FIX - use function pointers instead of flags + if statement if(replacement_type == 1) pop = replaceAll(pop, selected); else pop = retainBest(pop, selected); generation_num++; } fprintf(stdout, "\nFINAL RESULT:\n"); printPopulation(pop); fprintf(stdout, "\nBEST SOLUTION:\n"); best_solution = findBest(pop); printIndividual(best_solution); freePopulation(pop); freeParameters(); return EXIT_SUCCESS; }
bool Population::checkForWinner() { // compute fitness of all chromosome if (config.selectionType == "fitness-proportional") { _totalFitness = 0; for (const auto &candidate : _population) _totalFitness += candidate.getFitness(); } // sort them by fitness sortByFitness(); // test if the best candidate solution solves the problem return _problem->test(_population.front().getStrand()); }