//TSP -> NN -> Generations( g, ForkJoin ( n, GA -> 2-OPT ) ) -> TSP' void Pipeline2(tsp_class& tsp_instance, unsigned int number_of_tasks, unsigned int number_of_generations) { #pragma region "PipelineConfiguration" auto a = Args<General_args_type>(make_General_args(number_of_generations, number_of_tasks)); auto sa = Args<SA_args_type>(); auto aco = Args<ACO_args_type>(); auto ga = Args<GA_args_type>(make_GA_args(1000, 10, 5, 50000, 10, 0.9)); const char* pipeline_description = "TSP -> NN -> Generations( g, ForkJoin ( n, GA -> 2-OPT ) ) -> TSP'"; display_args(pipeline_description, a, sa, aco, ga); auto g = a[0].number_of_iterations_or_generations; auto n = a[0].number_of_tasks_in_parallel; auto _TSP = TSP(just(tsp_instance)); auto _DisplayInput = Display("TSP INPUT", DisplayFlags::All); auto _NN = Measure(NN(), Display("NEAREST NEIGHBOUR", DisplayFlags::EmitMathematicaGraphPlot)); auto _GA_2OPT = Chain(GA(ga[0].population_size, ga[0].mutation_percentage, ga[0].group_size, ga[0].number_of_generations, ga[0].nearby_cities, ga[0].nearby_cities_percentage), _2OPT()); auto _ForkJoin = [](unsigned int n, TSP::transformer_type map_fun){ return Measure(ForkJoin(n, map_fun)); }; auto _DisplayOutput = Display("TSP OUTPUT", DisplayFlags::EmitMathematicaGraphPlot); #pragma endregion //TSP -> NN -> Generations( g, ForkJoin ( n, GA -> 2-OPT ) ) -> TSP' auto result = _TSP .map(_DisplayInput) .map(_NN) .map(Generations(g, _ForkJoin(n, _GA_2OPT))) .map(_DisplayOutput); }
/* Make a new environment on the stack. The prevsize parameter is the count of objects already on the stack that become part of the environment that is being created, and extrasize are the additional elements on the stack that are to be added. These additional elements have the initial value CUNBOUND. */ void __arc_mkenv(arc *c, value thr, int prevsize, int extrasize) { value *envstart; int i, esofs; /* Add the extra environment entries */ for (i=0; i<extrasize; i++) CPUSH(thr, CUNBOUND); /* Add the count */ CPUSH(thr, INT2FIX(prevsize+extrasize)); envstart = TSP(thr); CPUSH(thr, TENVR(thr)); esofs = envstart - TSBASE(thr); TSFN(thr) = TSP(thr); /* start of stack after env */ SENVR(thr, ((value)((long)(esofs << 4) | ENV_FLAG))); }
//TSP -> NN -> Generations( g, ForkJoin ( n, SA -> 2-OPT ) ) -> TSP' void Pipeline1(tsp_class& tsp_instance, unsigned int number_of_tasks, unsigned int number_of_generations) { #pragma region "PipelineConfiguration" auto a = Args<General_args_type>(make_General_args(number_of_generations, number_of_tasks)); auto sa = Args<SA_args_type>(make_SA_args(1000.0, 0.00001, 0.999, 400)); auto aco = Args<ACO_args_type>(); auto ga = Args<GA_args_type>(); const char* pipeline_description = "TSP -> NN -> Generations( g, ForkJoin ( n, SA -> 2-OPT ) ) -> TSP'"; display_args(pipeline_description, a, sa, aco, ga); auto g = a[0].number_of_iterations_or_generations; auto n = a[0].number_of_tasks_in_parallel; auto _TSP = TSP(just(tsp_instance)); auto _DisplayInput = Display("TSP INPUT", DisplayFlags::All); auto _NN = Measure(NN(), Display("NEAREST NEIGHBOUR", DisplayFlags::EmitMathematicaGraphPlot)); auto _SA_2OPT = Chain(SA(sa[0].initial_temperature, sa[0].stopping_criteria_temperature, sa[0].decreasing_factor, sa[0].monte_carlo_steps), _2OPT()); auto _ForkJoin = [](unsigned int n, TSP::transformer_type map_fun){ return Measure(ForkJoin(n, map_fun)); }; auto _DisplayOutput = Display("TSP OUTPUT", DisplayFlags::EmitMathematicaGraphPlot); #pragma endregion //TSP -> NN -> Generations( g, ForkJoin ( n, SA -> 2-OPT ) ) -> TSP' auto result = _TSP .map(_DisplayInput) .map(_NN) .map(Generations(g, _ForkJoin(n, _SA_2OPT))) .map(_DisplayOutput); }
int main(int argc, char **argv){ srand(time(NULL)); start(); glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(1, 1); glutInitWindowSize(700, 700); glutCreateWindow("TREE SEARCH"); init(); glutDisplayFunc(display); glutIdleFunc(idle); glutReshapeFunc(reshape); printf("Procesando...\n"); double time0 = omp_get_wtime(); TSP(t1); double time1 = omp_get_wtime(); printf("Costo: %f\n",best_tour->cost); printf("stack: %d\n",stack->max); /**Impresion del tiempo que tardo el algoritmo para determinar las * posiciones y velocidades de las particulas ***/ printf("duracion: %f\n",time1-time0); glutMainLoop(); return 0; }
int main(int argc, char **argv){ srand(time(NULL)); start(); //disconnect(); glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(1, 1); glutInitWindowSize(700, 700); glutCreateWindow("TREE SEARCH"); init(); glutDisplayFunc(display); glutIdleFunc(idle); glutReshapeFunc(reshape); omp_init_lock(&best_tour_lock); printf("Procesando...\n"); double time0 = omp_get_wtime(); partition(stacks,t1); #pragma omp parallel for schedule(static, 1) for (int i = 0; i < thread_count; i++){ TSP((void *) stacks[i]); } omp_destroy_lock(&best_tour_lock); double time1 = omp_get_wtime(); printf("Costo: %f\n",best_tour->cost); /**Impresion del tiempo que tardo el algoritmo para determinar las * posiciones y velocidades de las particulas ***/ printf("duracion: %f\n",time1-time0); glutMainLoop(); return 0; }
Cost tsp(const Subset subset, const int i) { Subset without = subset ^ (1 << i); Cost minimum = numeric_limits<Cost>::max(); for(int j=0; j<n_nodes; j++) { if(j==i || (without & (1 << j)) == 0) continue; Cost v = TSP(without, j); v += distances[i][j]; if(v < minimum) minimum = v; } return tsp_memoize[subset][i] = minimum; }
/* Move n elements from the top of stack, overwriting the current environment. Points the stack pointer to just above the last element moved. Does not do this if current environment is on the heap. It will, in either case, set the environment register to the parent environment (possibly leaving the heap environment as garbage to be collected) This is essentially used to support tail calls and tail recursion. When this mechanism is used, the new arguments are on the stack, over the previous environment. It will move all of the new arguments onto the old environment and adjust the stack pointer appropriately. When control is transferred to the tail called function the call to __arc_mkenv will turn the stuff on the stack into a new environment. */ void __arc_menv(arc *c, value thr, int n) { value *src, *dest, *penv; value parentenv; int i; /* do nothing if we have no env */ if (NIL_P(TENVR(thr))) return; parentenv = nextenv(c, thr, TENVR(thr)); /* We should only bother if both the present environment and its parent (which should be superseded) are both on the stack. */ if (TYPE(TENVR(thr)) == T_ENV) { /* Run the write barrier on the environment that is about to be overwritten. Required for most incremental and concurrent GC algorithms. */ penv = SENV_PTR(TSBASE(thr), TENVR(thr)); for (i=0; i<SENV_COUNT(penv); i++) __arc_wb(*envval(c, thr, 0, i), CNIL); /* source of our copy is the last value pushed on the stack */ src = TSP(thr)+1; /* Destination of our copy is the (n-1)th element of the environment. May be larger or smaller than the actual size of the environment. Doesn't matter, as long as it remains inside the stack! */ dest = envval(c, thr, 0, n-1); /* use memmove because the new environment might be larger than the old one. */ memmove(dest, src, n*sizeof(value)); /* new stack pointer points to just outside previous one */ TSP(thr) = dest-1; } /* If the current environment was on the heap, none of this black magic needs to be done. It is enough to just set the environment register to point to the parent environment. */ SENVR(thr, parentenv); }
int TSP(int pos, int bitmask) { if (bitmask == (1<<vertices)-1){ return costmatrix[pos][0]; } if (memo[pos][bitmask] == -1){ memo[pos][bitmask] = 2000000; for (int i = 0; i < vertices; i++) { if (i != pos && (bitmask & (1 << i)) == 0){ memo[pos][bitmask] = std::min(memo[pos][bitmask], costmatrix[pos][i] + TSP(i, bitmask | (1 << i))); } } } return memo[pos][bitmask]; // Storing the best dist for the set of traveled cities and untraveled ones. }
/*teste la fonction TPS, retourne la liste de sommets : "1, 6, 2, 4, 7, 8, 5, 9, 10, 11, 14, 3, 13, 12" dans le fichier chemin.txt*/ void test_TSP () { FILE* fichier = NULL; fichier = fopen("testVerticesCoordinates.txt", "r"); if (fichier==NULL) { printf("probleme d'ouverture de testVerticesCoordinates.txt"); exit(1); } Matrice matrice_poids = matrice_of_coordonnees (fichier); double distance = 0.; FILE* chemin = fopen ("chemin.txt", "w"); TSP(matrice_poids,chemin); fprintf(chemin,"\n"); fclose (chemin); fclose(fichier); }
//TSP -> NN -> Generations( g, ForkJoin ( n, ACO -> 2-OPT ) ) -> TSP' void Pipeline3(tsp_class& tsp_instance, unsigned int number_of_tasks, unsigned int number_of_generations) { #pragma region "PipelineConfiguration" auto a = Args<General_args_type>(make_General_args(number_of_generations, number_of_tasks)); auto sa = Args<SA_args_type>(); auto ga = Args<GA_args_type>(); const int aco_iterations = static_cast<int>(tsp_instance.cities.size() * 100); const ants_type::size_type number_of_ants = tsp_instance.cities.size(); const double BASE_PHEROMONE = 1.0f / static_cast<double>(tsp_instance.cities.size()); const double ALPHA = 1.0; const double BETA = 1.0; const double RHO = 0.9; const double QVAL = 70; auto aco = Args<ACO_args_type>(make_ACO_args(aco_iterations, number_of_ants, BASE_PHEROMONE, ALPHA, BETA, RHO, QVAL)); const char* pipeline_description = "TSP -> NN -> Generations( g, ForkJoin ( n, ACO -> 2-OPT ) ) -> TSP'"; display_args(pipeline_description, a, sa, aco, ga); auto g = a[0].number_of_iterations_or_generations; auto n = a[0].number_of_tasks_in_parallel; auto _TSP = TSP(just(tsp_instance)); auto _DisplayInput = Display("TSP INPUT", DisplayFlags::All); auto _NN = Measure(NN(), Display("NEAREST NEIGHBOUR", DisplayFlags::EmitMathematicaGraphPlot)); auto _ACO_2OPT = Chain(ACO(aco[0].aco_iterations, aco[0].number_of_ants, aco[0].base_pheromone, aco[0].favor_pheromone_level_over_distance, aco[0].favor_distance_over_pheromone_level, aco[0].value_for_intensification_and_evaporation, aco[0].pheronome_distribution), _2OPT()); auto _ForkJoin = [](unsigned int n, TSP::transformer_type map_fun){ return Measure(ForkJoin(n, map_fun)); }; auto _DisplayOutput = Display("TSP OUTPUT", DisplayFlags::EmitMathematicaGraphPlot); #pragma endregion //TSP -> NN -> Generations( g, ForkJoin ( n, ACO -> 2-OPT ) ) -> TSP' auto result = _TSP .map(_DisplayInput) .map(_NN) .map(Generations(g, _ForkJoin(n, _ACO_2OPT))) .map(_DisplayOutput); }
int TSP(int state, int n, int last) { if(state == 0) return 0; if(DP[state][last] != -oo) return DP[state][last]; int i; long long max = -oo, tmp; for(i = 0; i <= n; i++) { if((state&(1<<i)) != 0 && last != i) { tmp = TSP(state-(1<<last), n, i); tmp -= map[A[i]][A[last]]; tmp += AD[last]; if(max < tmp) max = tmp; } } if(state == (1<<last)) max = -map[A[last]][0]+AD[last]; if(best < max-map[A[last]][0]) best = max-map[A[last]][0]; DP[state][last] = max; return DP[state][last]; }
TSP make_TSP(const tsp_class& tsp_instance) { return TSP(just(tsp_instance)); }
int main (int argc, char *argv[]){ int continuer = 10; #ifdef GUI_ENABLE int afficher; #endif char nom_file[1000]; Matrice mat = NULL; FILE *input = NULL; FILE *output = stdout; FILE *nullstream = stdout; nullstream = fopen ("nullstream", "w"); if (nullstream == NULL){ printf("probleme a l'ouverture du fichier\n"); exit(1); } if(argc >= 2){ output = fopen (argv[1], "w"); if (output == NULL){ printf("probleme a l'ouverture du fichier\n"); exit(1); } } while (continuer){ printf("TSP : selectionner l'action a executer :\n"); printf("\t1 : generer une liste de ville\n"); printf("\t2 : appliquer l'algorithme prim à un fichier de ville\n"); #ifdef GUI_ENABLE printf("\t3 : afficher une liste de ville (aucune action possible -> efficace pour de grand nombre de villes)\n"); #endif printf("\t0 : quitter le programme\n"); scanf("%d", &continuer); switch(continuer){ case 1 : continuer = generation(nom_file, &mat); break; case 2 : printf("entrer le nom du fichier :\n"); scanf("%s", nom_file); input = fopen (nom_file, "r"); if (input == NULL) { printf("probleme a l'ouverture du fichier\n"); exit(1); } mat = matrice_of_coordonnees(input); fclose(input); TSP (mat,output); fprintf(output,"\n"); continuer = 13; break; #ifdef GUI_ENABLE case 3 : printf("entrer le nom du fichier :\n"); scanf("%s", nom_file); affichage_gui(nom_file); break; #endif case 0 : break; default : continuer = 10; break; } #ifdef GUI_ENABLE /*propose l'affichage graphique, et initialise la matrice en fonction des choix fait par l'utilisateur*/ afficher = 0; switch(continuer){ case 13: case 12: case 11: do{ printf("voulez vous afficher graphiquement le resultat ? (oui = 1, non = 0) "); scanf("%d", &afficher); }while (afficher != 0 && afficher != 1); break; } if (afficher){ printf("\ncommande :\nla pression des touches suivantes permet d'afficher ou masquer : \n" "c(ycle) : le chemin calculé par l'algorithme\n" "t(ree) : l'arbre couvrant minimum\n" "s(ommet) : les sommets representant les villes\n" "Appuyez sur esc pour fermer l'interface graphique\n"); switch(continuer){ case 12://un fichier liste de ville (nom_file) vient d'etre cree. input = fopen(nom_file, "r"); if (input == NULL) { printf("probleme a l'ouverture du fichier\n"); exit(1); } mat = matrice_of_coordonnees(input); fclose(input); //break omis volontairement case 11:// la matrice est initialisee, il suffit d'afficher le cycle TSP (mat, nullstream); fprintf(nullstream,"\n"); //break omis volontairement case 13: gui(mat); break; } } #endif if (mat){ liberer_mat(mat); mat = NULL; } } fclose(nullstream); if (argc >= 2)fclose(output); return 0; }