POIvec filterPOIs(const POIvec &all, int count, float tabuScale, const Matrix &M) { float minx = 1000000000, maxx = -1000000000, miny = 1000000000, maxy = -1000000000; for(int i=0; i<(int)all.size(); i++) { POI p = M * all[i]; minx = std::min(minx, p.x); maxx = std::max(maxx, p.x); miny = std::min(miny, p.y); maxy = std::max(maxy, p.y); } minx -= 10; miny -= 10; maxx += 10; maxy += 10; int w = ceilf(maxx - minx), h = ceilf(maxy - miny); Image tabu(w,h); tabu.fill(0); POIvec selected; for(int i=0; i<(int)all.size() && (int)selected.size() < count; i++) { POI p = M * all[i]; int x = roundf(p.x - minx), y = roundf(p.y - miny); if(tabu[y][x]) continue; selected.push_back(p); float R = tabuScale / p.val; int iR = (int)ceilf(R); for (int dy=-iR; dy<=iR; dy++) for (int dx=-iR; dx<=iR; dx++) if (dx*dx+dy*dy <= (int)(R*R) && tabu.inside(x+dx, y+dy)) tabu[y+dy][x+dx] = 1; } return selected; }
void testear( int maxN, int step, int cantTests ) { ofstream Oexacto; Oexacto.open("exacto.data"); ofstream Ogoloso; Ogoloso.open("goloso.data"); ofstream Olocal; Olocal.open("local.data"); ofstream Olocal2; Olocal2.open("local2.data"); ofstream Otabu; Otabu.open("tabu.data"); ofstream Otabu2; Otabu2.open("tabu2.data"); for (int n=step; n<=maxN; n+=step) { cout << "n = " << n; double acum_e = 0; double acum_g = 0; double acum_l = 0; double acum_l2 = 0; double acum_t = 0; double acum_t2 = 0; double elmax_e = 0; double elmax_g = 0; double elmax_l = 0; double elmax_l2 = 0; double elmax_t = 0; double elmax_t2 = 0; double elmin_e = 1E+37; double elmin_g = 1E+37; double elmin_l = 1E+37; double elmin_l2 = 1E+37; double elmin_t = 1E+37; double elmin_t2 = 1E+37; double todos_e[cantTests]; double todos_g[cantTests]; double todos_l[cantTests]; double todos_l2[cantTests]; double todos_t[cantTests]; double todos_t2[cantTests]; for (int k=0; k<cantTests; k++) { grafo g = grafo(n); g.generar_aristas_aleatorias(); Solucion si = make_pair(vector<bool>(n,false),0); double duracion; iniciarTimer(); exacto(g); duracion = finalizarTimer(); acum_e += duracion; todos_e[k] = duracion; if (duracion > elmax_e) elmax_e = duracion; if (duracion < elmin_e) elmin_e = duracion; iniciarTimer(); goloso(g); duracion = finalizarTimer(); acum_g += duracion; todos_g[k] = duracion; if (duracion > elmax_g) elmax_g = duracion; if (duracion < elmin_g) elmin_g = duracion; iniciarTimer(); if (n>=2) local(g,si,2); duracion = finalizarTimer(); acum_l += duracion; todos_l[k] = duracion; if (duracion > elmax_l) elmax_l = duracion; if (duracion < elmin_l) elmin_l = duracion; iniciarTimer(); if (n>=3) local(g,si,3); duracion = finalizarTimer(); acum_l2 += duracion; todos_l2[k] = duracion; if (duracion > elmax_l2) elmax_l2 = duracion; if (duracion < elmin_l2) elmin_l2 = duracion; iniciarTimer(); tabu(g,si,n,n,1); duracion = finalizarTimer(); acum_t += duracion; todos_t[k] = duracion; if (duracion > elmax_t) elmax_t = duracion; if (duracion < elmin_t) elmin_t = duracion; iniciarTimer(); if (n>=2) tabu(g,si,n,n,2); duracion = finalizarTimer(); acum_t2 += duracion; todos_t2[k] = duracion; if (duracion > elmax_t2) elmax_t2 = duracion; if (duracion < elmin_t2) elmin_t2 = duracion; } cout << endl; double media; double desvioEstandar; float index; media = acum_e/cantTests; desvioEstandar = 0; for (int l =0; l<cantTests; ++l) { double d = todos_e[l]-media; desvioEstandar += (d*d); } desvioEstandar = sqrt(desvioEstandar/(cantTests-1)); index = n - 0.5*step; Oexacto << index << " " << fixed << media << " " << fixed << media-desvioEstandar << " " << fixed << media + desvioEstandar << " " << fixed << elmax_e << " " << fixed << elmin_e << endl; media = acum_g/cantTests; desvioEstandar = 0; for (int l =0; l<cantTests; ++l) { double d = todos_g[l]-media; desvioEstandar += (d*d); } desvioEstandar = sqrt(desvioEstandar/(cantTests-1)); index = n - 0.25*step; Ogoloso << index << " " << fixed << media << " " << fixed << media-desvioEstandar << " " << fixed << media + desvioEstandar << " " << fixed << elmax_g << " " << fixed << elmin_g << endl; media = acum_l/cantTests; desvioEstandar = 0; for (int l =0; l<cantTests; ++l) { double d = todos_l[l]-media; desvioEstandar += (d*d); } desvioEstandar = sqrt(desvioEstandar/(cantTests-1)); index = n + 0.25*step; Olocal << index << " " << fixed << media << " " << fixed << media-desvioEstandar << " " << fixed << media + desvioEstandar << " " << fixed << elmax_l << " " << fixed << elmin_l << endl; media = acum_l2/cantTests; desvioEstandar = 0; for (int l =0; l<cantTests; ++l) { double d = todos_l2[l]-media; desvioEstandar += (d*d); } desvioEstandar = sqrt(desvioEstandar/(cantTests-1)); index = n + 0.5*step; Olocal2 << index << " " << fixed << media << " " << fixed << media-desvioEstandar << " " << fixed << media + desvioEstandar << " " << fixed << elmax_l2 << " " << fixed << elmin_l2 << endl; media = acum_t/cantTests; desvioEstandar = 0; for (int l =0; l<cantTests; ++l) { double d = todos_t[l]-media; desvioEstandar += (d*d); } desvioEstandar = sqrt(desvioEstandar/(cantTests-1)); index = n - 0.15*step; Otabu << index << " " << fixed << media << " " << fixed << media-desvioEstandar << " " << fixed << media + desvioEstandar << " " << fixed << elmax_t << " " << fixed << elmin_t << endl; media = acum_t2/cantTests; desvioEstandar = 0; for (int l =0; l<cantTests; ++l) { double d = todos_t2[l]-media; desvioEstandar += (d*d); } desvioEstandar = sqrt(desvioEstandar/(cantTests-1)); index = n + 0.15*step; Otabu2 << index << " " << fixed << media << " " << fixed << media-desvioEstandar << " " << fixed << media + desvioEstandar << " " << fixed << elmax_t2 << " " << fixed << elmin_t2 << endl; } Oexacto.close(); Ogoloso.close(); Olocal.close(); Olocal2.close(); Otabu.close(); Otabu2.close(); system("gnuplot super_test.gnuplot"); }
int main(int argc, char *argv[]) { if (argc <= 1) { std::cout << "Error: Please inform options and filename." << std::endl; printHelp(argv[0]); return 1; } std::string option = argv[1]; if (option == "--test") { testRun(); return 0; } if (option == "--help") { printHelp(argv[0]); return 0; } tsp::TSPLibData data; std::ifstream file(argv[2]); if (!file.is_open()) { std::cout << "Error: cannot open file." << std::endl; printHelp(argv[0]); return 0; } data.load(file); if (option == "--only-constructive") { tsp::TSPNearestNeighborConstruct nn(data); tsp::TSPTour tour = nn.run(); if (tour.isValid()) { tour.print(std::cout); } return 0; } if (option == "--localsearch") { tsp::TSPNearestNeighborConstruct nn(data); tsp::TSPTour tour = nn.run(); tsp::TSP2opt local(data, tour); tour = local.run(); if (tour.isValid()) { tour.print(std::cout); } return 0; } if (option == "--grasp") { tsp::TSPGrasp grasp(data, 100); tsp::TSPTour tourGrasp = grasp.run(); if ( tourGrasp.isValid() ) { tourGrasp.print(std::cout); } return 0; } if (option == "--tabu") { tsp::TSPTabu tabu(data, 100, 10, 10); tsp::TSPTour tourTabu = tabu.run(); if ( tourTabu.isValid() ) { tourTabu.print(std::cout); } return 0; } return 0; }