int main(int argc,char ** argv){ int * visitado,otimo; Tour pop[TAMPOP], * melhor; struct tms before,after; if(argc != 3){ printf("USO : %s 'instancia' 'otimo' \n\n",argv[0]); return 1; } otimo = atoi(argv[2]); times(&before); read(argv[1],d,n); visitado = new_int(n); init_int(visitado,n); pop[0] = rand_tour(); pop[1] = rand_tour(); createSet(); LinKernighan(pop[0]); LinKernighan(pop[1]); pop[2] = new_tour(); pop[2]->c = new_int(n); pop[2]->pos = new_int(n); pop[3] = new_tour(); pop[3]->c = new_int(n); pop[3]->pos = new_int(n); PMX(pop[2],pop[3],pop[0],pop[1]); LinKernighan(pop[2]); LinKernighan(pop[3]); printf("PAI 1 : \n"); print_tour(pop[0]); printf("PAI 2 : \n"); print_tour(pop[1]); printf("FILHO 1 : \n"); print_tour(pop[2]); printf("FILHO 2 : \n"); print_tour(pop[3]); melhor = &pop[0]; //print_tour(melhor); times(&after); double tempo = (double)(after.tms_utime - before.tms_utime)/(double)100; double gap = ((double)((*(melhor))->cost-otimo)/(double) otimo) * 100.0; printf("%s\t%.2lfs\t%d\t%d\t%.3lf\n",argv[1],tempo,otimo,(*(melhor))->cost,gap); //printf("System time: %ld seconds\n", after.tms_stime - before.tms_stime); return 0; }
/** * Runs the NN_TSP algorithm. * * @param[in,out] pop input/output pagmo::population to be evolved. */ void nn_tsp::evolve(population &pop) const { const problem::base_tsp* prob; //check if problem is of type pagmo::problem::base_tsp try { prob = &dynamic_cast<const problem::base_tsp &>(pop.problem()); } catch (const std::bad_cast& e) { pagmo_throw(value_error,"Problem not of type pagmo::problem::tsp, nn_tsp can only be called on problem::tsp problems"); } // Let's store some useful variables. const problem::base::size_type Nv = prob->get_n_cities(); //create individuals decision_vector best_tour(Nv); decision_vector new_tour(Nv); //check input parameter if (m_start_city < -1 || m_start_city > static_cast<int>(Nv-1)) { pagmo_throw(value_error,"invalid value for the first vertex"); } size_t first_city, Nt; if(m_start_city == -1){ first_city = 0; Nt = Nv; } else{ first_city = m_start_city; Nt = m_start_city+1; } int length_best_tour, length_new_tour; size_t nxt_city, min_idx; std::vector<int> not_visited(Nv); length_best_tour = 0; //main loop for (size_t i = first_city; i < Nt; i++) { length_new_tour = 0; for (size_t j = 0; j < Nv; j++) { not_visited[j] = j; } new_tour[0] = i; std::swap(not_visited[new_tour[0]],not_visited[Nv-1]); for (size_t j = 1; j < Nv-1; j++) { min_idx = 0; nxt_city = not_visited[0]; for (size_t l = 1; l < Nv-j; l++) { if(prob->distance(new_tour[j-1], not_visited[l]) < prob->distance(new_tour[j-1], nxt_city) ) { min_idx = l; nxt_city = not_visited[l];} } new_tour[j] = nxt_city; length_new_tour += prob->distance(new_tour[j-1], nxt_city); std::swap(not_visited[min_idx],not_visited[Nv-j-1]); } new_tour[Nv-1] = not_visited[0]; length_new_tour += prob->distance(new_tour[Nv-2], new_tour[Nv-1]); length_new_tour += prob->distance(new_tour[Nv-1], new_tour[0]); if(i == first_city || length_new_tour < length_best_tour){ best_tour = new_tour; length_best_tour = length_new_tour; } } //change representation of tour population::size_type best_idx = pop.get_best_idx(); switch( prob->get_encoding() ) { case problem::base_tsp::FULL: pop.set_x(best_idx,prob->cities2full(best_tour)); break; case problem::base_tsp::RANDOMKEYS: pop.set_x(best_idx,prob->cities2randomkeys(best_tour,pop.get_individual(best_idx).cur_x)); break; case problem::base_tsp::CITIES: pop.set_x(best_idx,best_tour); break; } } // end of evolve