Problem_Representation_Type const operator()( Chromosome_Args ... ch_args ) { //initialize first generation randomly for ( std::size_t i = 0; i != population_per_generation; ++i ) current_population.push_back( chromosome_type( ch_args... ) ); //std::cerr << "\ninitialized " << population_per_generation << " chromosomes.\n"; std::size_t debug_counter = 0; best_fitness = std::numeric_limits<Fitness_Type>::max(); srand ( unsigned ( time (NULL) ) ); //for random_shuffle Fitness_Type total_fit; Problem_Representation_Type pr_0; Problem_Representation_Type pr_1; Problem_Representation_Type pr_2; Problem_Representation_Type pr_3; std::ofstream elite_out( "elite.dat" ); std::ofstream elite_fit( "elite_fit.dat"); for (;;) { //evaluate //feng::for_each( current_population.begin(), current_population.end(), [](chromosome_type& chrom) { Evaluation_Method()(Chromosome_Problem_Translation_Method()(chrom)); }); //std::cerr << "\nevaluating........"; #if 0 for ( auto& ch : current_population ) if ( ch.modification_after_last_evaluation_flag ) { Chromosome_Problem_Translation_Method()( *(ch.chrom), pr ); ch.fit = Evaluation_Method()(pr); //Evaluation_Method()(Chromosome_Problem_Translation_Method()(ch)); ch.modification_after_last_evaluation_flag = false; } #endif #if 1 std::thread t0( parallel_evaluator(), current_population.begin(), current_population.begin()+(current_population.size()/4), &pr_0 ); std::thread t1( parallel_evaluator(), current_population.begin()+(current_population.size()/4), current_population.begin()+(current_population.size()/2), &pr_1 ); std::thread t2( parallel_evaluator(), current_population.begin()+(current_population.size()/2), current_population.begin()+(current_population.size()*3/4), &pr_2 ); std::thread t3( parallel_evaluator(), current_population.begin()+(current_population.size()*3/4), current_population.end(), &pr_3 ); //parallel_evaluator()( current_population.begin()+(current_population.size()*3/4), current_population.end(), &pr_3 ); t0.join(); t1.join(); t2.join(); t3.join(); #endif //using nth_element? //mutate duplicated chromosome? std::sort( current_population.begin(), current_population.end() ); auto const elite_one = *(current_population.begin()); Chromosome_Problem_Translation_Method()( *(elite_one.chrom), pr ); //keep the best individual of the current generation if ( elite_one.fit < best_fitness ) { debug_counter++; best_fitness = elite_one.fit; best_one = pr; elite_out << best_one; elite_fit << best_fitness; if ( debug_counter & 0xf ) { elite_out << "\n"; elite_fit << "\n"; } else { elite_out << std::endl; elite_fit << std::endl; } } current_population.resize( std::distance( current_population.begin(), std::unique( current_population.begin(), current_population.end() )) ); if ( current_population.size() < population_per_generation ) { //generate someone randomly and evaluate for ( std::size_t i = current_population.size(); i != population_per_generation; ++i ) { auto ch = chromosome_type( ch_args...); Chromosome_Problem_Translation_Method()( *(ch.chrom), pr ); ch.fit = Evaluation_Method()(pr); ch.modification_after_last_evaluation_flag = false; current_population.push_back( ch ); } } else { //shuffle the resize std::random_shuffle( current_population.begin(), current_population.end() ); current_population.resize( population_per_generation ); } std::sort( current_population.begin(), current_population.end() ); //total_fit = 0; //for( auto& ch : current_population ) // total_fit += ch.fit; //std::cout.precision(20); //std::cout << total_fit << "\n"; //if timeout, return output elite auto& t_manager = feng::singleton<time_manager>::instance(); if ( t_manager.is_timeout() ) { std::cerr << "\nthe residual for the best individual is " << best_fitness; elite_out.close(); elite_fit.close(); return best_one; //return pr; } //std::cerr << "\nElite of " << debug_counter++ << " is \n" << pr; //std::cerr << "\nElite of " << debug_counter << " is \n" << *(elite_one.chrom); //select auto& xpm = feng::singleton<xover_probability_manager<>>::instance(); std::size_t const selection_number = xpm(population_per_generation); //selected_population_for_xover_father.resize(selection_number); //selected_population_for_xover_mother.resize(selection_number); selected_population_for_xover_father.clear(); selected_population_for_xover_mother.clear(); //std::cerr << "\nselecting............." << selection_number; auto& xs_manager = feng::singleton<xover_selection_manager>::instance(); for ( std::size_t i = 0; i != selection_number; ++i ) { //selected_population_for_xover_father[i] = current_population[xs_manager()]; //selected_population_for_xover_mother[i] = current_population[xs_manager()]; const std::size_t select1 = xs_manager(); const std::size_t select2 = xs_manager(); //selected_population_for_xover_father.push_back(current_population[xs_manager()]); //selected_population_for_xover_mother.push_back(current_population[xs_manager()]); selected_population_for_xover_father.push_back(current_population[select1]); selected_population_for_xover_mother.push_back(current_population[select2]); } //std::cerr << "\nselectedted"; //xover #if 0 //not working as selected population for xover are pure reference of current populaiton for ( std::size_t i = 0; i != selection_number; ++i ) feng::for_each( selected_population_for_xover_father[i].begin(), selected_population_for_xover_father[i].end(), selected_population_for_xover_mother[i].begin(), Chromosome_Xover_Method() ); current_population.reserve( population_per_generation + selection_number + selection_number ); //append newly generated genes into current population current_population.insert( current_population.begin()+population_per_generation, selected_population_for_xover_father.begin(), selected_population_for_xover_father.end() ); current_population.insert( current_population.begin()+population_per_generation+selection_number, selected_population_for_xover_mother.begin(), selected_population_for_xover_mother.end() ); #endif //std::cerr << "\nxover............."; for ( std::size_t i = 0; i != selection_number; ++i ) { chromosome_type son( ch_args... ); chromosome_type daughter( ch_args... ); feng::for_each( selected_population_for_xover_father[i].begin(), selected_population_for_xover_father[i].end(), selected_population_for_xover_mother[i].begin(), son.begin(), daughter.begin(), Chromosome_Xover_Method() ); current_population.push_back( son ); current_population.push_back( daughter ); } //mark new generation not evaluated feng::for_each( current_population.begin()+population_per_generation, current_population.end(), [](chromosome_type& ch) { ch.modification_after_last_evaluation_flag = true; } ); //std::cerr << "\nxovered"; //mutate //std::cerr << "\nmutating"; auto& mpm = feng::singleton<mutation_probability_manager<>>::instance(); mpm.reset(); for ( auto& chromosome : current_population ) if ( mpm.should_mutate_current_gene() ) { for ( auto& gene : chromosome ) Chromosome_Mutation_Method()(gene); //makr muated ones as not evaluated chromosome.modification_after_last_evaluation_flag = true; } //std::cerr << "\nmutated"; //goto evaluate } assert( !"Should never reach here!!" ); return Problem_Representation_Type(); }