int main( ){ srand( time_seed() ); individual* i; i = create_individual(); print_individual( i, 0 ); evaluate_individual( i ); print_individual( i, 0 ); }
// extends population size to lambda using mutations based on (lamda/mu) factor void es::create_pop(unsigned int gen) { try { boost::format gen_dir(eap::run_directory+"gen%04d"); boost::format input_path(eap::run_directory + "gen%04d/ind%09d"); boost::filesystem::create_directory(str(gen_dir % gen)); std::vector<individual_ptr> new_pop; if (gen != 0) { for (unsigned int i=0; i<m_mu; ++i) { new_pop.push_back(create_individual(str(input_path % gen % i) + "a%02d.nec", m_pop[i]->m_positions)); } } else if (gen == 0) { for (unsigned int i=0; i<m_mu; ++i) { new_pop.push_back(m_pop[i]); } } for (unsigned int i=0; i<m_mu; ++i) { unsigned int counter = 0; while (counter<m_mulambda_factor) { int id = m_mu + i*m_mulambda_factor + counter; std::vector<position_ptr> placements = mutate_pos_once(m_pop[i]->m_positions); new_pop.push_back(create_individual(str(input_path % gen % id) + "a%02d.nec", placements)); counter++; } } m_pop = new_pop; } catch (...) { throw; } }
population* create_population( ) { int x; population* pop; pop = ( population* ) malloc( sizeof( population ) ); if( pop == 0 ) { printf( "Malloc Failed\n" ); exit( 1 ); } pop->individuals = ( individual** ) malloc( sizeof( individual* ) * POP_SIZE ); if( pop->individuals == 0 ) { printf( "Malloc Failed\n" ); exit( 1 ); } for( x = 0; x < POP_SIZE; x++ ) { pop->individuals[x] = create_individual( ); } pop->size = POP_SIZE; pop->gen = 0; }
std::vector<Individual> read_individuals(std::string dose_path) { gz::igzstream dose_file( dose_path.c_str( ) ); std::vector<Individual> individual_list; std::string line; while( std::getline( dose_file, line ) ) { std::vector<std::string> splitted_line = split_line( line ); Individual individual = create_individual( splitted_line ); individual_list.push_back( individual ); } return individual_list; }
/** * @desc Implements logic for ES runs */ void es::run(unsigned int run_id) { try { if (m_pop.size() > 1) m_pop.erase(m_pop.begin(), m_pop.end()); if (m_pop.size() != 0) throw eap::InvalidStateException("ES: Population size sould be zero"); boost::filesystem::create_directory(std::string(eap::run_directory+"gen0000")); boost::format nec_input(eap::run_directory + "gen%04d/ind%09d"); for (unsigned int i_id=0; i_id<m_mu; ++i_id) { std::vector<position_ptr> placements; for (ant_config_ptr i_ant : m_ant_configs) { int pos; do { pos = eap::rand(0, i_ant->m_positions.size()-1); } while (overlap(placements, i_ant->m_positions[pos])); placements.push_back(i_ant->m_positions[pos]); } m_pop.push_back(create_individual(str(nec_input % 0 % i_id) + "a%02d.nec", placements)); } for (unsigned int i=0; i<m_generations; ++i) { create_pop(i); evaluate_gen(i); std::sort(m_pop.begin(), m_pop.end(), eap::fitness_sort); save_population(m_pop, run_id, i); save_best_nec(m_pop[0], run_id, i); std::cout<<"best "<<m_pop[0]->m_fitness<<"\n"; survivor_selection(); } } catch (...) { throw; } }
vector<Individual> RandomGreedInitialization::run() { vector<int> aimed_time; vector<int> intersections; vector< pair<int, int> > time_intersections; vector<Individual> population; int num_intersections = problem->getNintersections(); vector< vector<int> > scene = problem->getTable(); int num_vehicles = problem->getNvehicles(); int required_time = problem->getRequiredTime(); int nones = problem->getNrsu(); const int INTERSECTION_POOL = (nones*2 <= num_intersections) ? nones*2 : num_intersections; int npop = 0; int sum = 0; int time_vehicle; while(npop < this->size/2) { intersections.clear(); aimed_time.clear(); for(int i = 0; i < num_vehicles; i++) { aimed_time.push_back(required_time); } int genes_count = 0; while(genes_count < nones) { time_intersections.clear(); sum = 0; // cout << "time_intersections\n"; for(int i = 0; i < num_intersections; i++) { sum = 0; for(int j = 0; j < num_vehicles; j++) { time_vehicle = scene[i][j]; if(time_vehicle > aimed_time[j]) time_vehicle = aimed_time[j]; sum += time_vehicle; } time_intersections.push_back(pair<int,int>(i,sum)); } sort(time_intersections.begin(), time_intersections.end(), decreased_pair_sort); // cout << "sort\n"; int chosen; if(npop == 0 && add_greed_solution) { chosen = time_intersections[0].first; } else { while(true) { // cout << "oi\n"; chosen = time_intersections[Randomize::generate_long() % INTERSECTION_POOL].first; if(!exist(intersections, chosen)) { break; } } } // int max = time_intersections[1].first; // cout << "aimed_time\n"; for(int i = 0; i < num_vehicles; i++) { if(scene[chosen][i] != 0) { if(scene[chosen][i] >= aimed_time[i]) { aimed_time[i] = 0; } else { aimed_time[i] = aimed_time[i] - scene[chosen][i]; } } scene[chosen][i] = 0; } genes_count++; intersections.push_back(chosen); } npop++; // cout << "individual creation\n"; population.push_back(create_individual(intersections)); } vector<Individual> random_population; RandomInitialization random_init = RandomInitialization(this->size/2, num_intersections, nones); random_population = random_init.run(); for(unsigned i = 0; i < random_population.size(); i++) { population.push_back(random_population[i]); } return population; }