sumission take2
This commit is contained in:
parent
ef002c8b1c
commit
a9ad56ad45
6 changed files with 35 additions and 69 deletions
Binary file not shown.
BIN
claudio.maggioni.zip
Normal file
BIN
claudio.maggioni.zip
Normal file
Binary file not shown.
52
code/aco.cc
52
code/aco.cc
|
@ -1,7 +1,7 @@
|
||||||
// vim: set ts=2 sw=2 et tw=80:
|
// vim: set ts=2 sw=2 et tw=80:
|
||||||
|
|
||||||
// compile with
|
// compile with
|
||||||
// g++ -lpthread --std=c++11 -o c_prob/aco aco.cc
|
// c++ -O2 -lpthread --std=c++11 -o c_prob/aco aco.cc opt.cc
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -104,7 +104,7 @@ struct ant {
|
||||||
|
|
||||||
attractiveness[loc] = pow(pheromone_amount, alpha) *
|
attractiveness[loc] = pow(pheromone_amount, alpha) *
|
||||||
pow(1 / distance, beta);
|
pow(1 / distance, beta);
|
||||||
//cerr << "ant " << idx << " attr[loc]=" << attractiveness[loc] << endl;
|
|
||||||
sum_total += attractiveness[loc];
|
sum_total += attractiveness[loc];
|
||||||
if (isnan(sum_total)) { cerr << "nanalert " << attractiveness[loc] <<
|
if (isnan(sum_total)) { cerr << "nanalert " << attractiveness[loc] <<
|
||||||
" " << pheromone_amount << " " << distance << endl; }
|
" " << pheromone_amount << " " << distance << endl; }
|
||||||
|
@ -126,7 +126,7 @@ struct ant {
|
||||||
double cumulative = 0.0;
|
double cumulative = 0.0;
|
||||||
for (uint loc : idxs) {
|
for (uint loc : idxs) {
|
||||||
double weight = (attractiveness[loc] / sum_total);
|
double weight = (attractiveness[loc] / sum_total);
|
||||||
//cerr << "ant " << idx << " w: " << weight + cumulative << endl;
|
|
||||||
if (toss <= (weight + cumulative)) {
|
if (toss <= (weight + cumulative)) {
|
||||||
return loc;
|
return loc;
|
||||||
}
|
}
|
||||||
|
@ -206,7 +206,6 @@ void update_pheromone_map() {
|
||||||
|
|
||||||
|
|
||||||
double mainloop(size_t i) {
|
double mainloop(size_t i) {
|
||||||
//cerr << "starting ants" << endl;
|
|
||||||
#if SINGLE_CORE
|
#if SINGLE_CORE
|
||||||
for (uint j = 0; j < n_ants; j++) {
|
for (uint j = 0; j < n_ants; j++) {
|
||||||
ant_thread(&(ants[j]));
|
ant_thread(&(ants[j]));
|
||||||
|
@ -215,17 +214,19 @@ double mainloop(size_t i) {
|
||||||
for (uint j = 0; j < n_ants; j++) {
|
for (uint j = 0; j < n_ants; j++) {
|
||||||
pthread_create(&(ants[j].t_handle), NULL, ant_thread, &(ants[j]));
|
pthread_create(&(ants[j].t_handle), NULL, ant_thread, &(ants[j]));
|
||||||
}
|
}
|
||||||
// cerr << "joining ants" << endl;
|
|
||||||
for (uint j = 0; j < n_ants; j++) {
|
for (uint j = 0; j < n_ants; j++) {
|
||||||
pthread_join(ants[j].t_handle, NULL);
|
pthread_join(ants[j].t_handle, NULL);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
//cerr << "summing ants" << endl;
|
|
||||||
unsigned long sum = 0L;
|
unsigned long sum = 0L;
|
||||||
for (uint j = 0; j < n_ants; j++) {
|
for (uint j = 0; j < n_ants; j++) {
|
||||||
populate_ant_updated_pheromone_map(ants[j]);
|
populate_ant_updated_pheromone_map(ants[j]);
|
||||||
sum += ants[j].distance_travelled;
|
sum += ants[j].distance_travelled;
|
||||||
|
|
||||||
|
if (ants[j].iter > 200 && n_nodes == 76) { // eil76 after 200th iteration
|
||||||
|
three_opt(ants[j].route, dist_matrix, ants[j].distance_travelled);
|
||||||
|
}
|
||||||
|
|
||||||
if (sh_dist < 0 || ants[j].distance_travelled < sh_dist) {
|
if (sh_dist < 0 || ants[j].distance_travelled < sh_dist) {
|
||||||
sh_dist = ants[j].distance_travelled;
|
sh_dist = ants[j].distance_travelled;
|
||||||
|
@ -244,45 +245,6 @@ double mainloop(size_t i) {
|
||||||
memset(ant_updated_pheromone_map, 0, sizeof(ant_updated_pheromone_map));
|
memset(ant_updated_pheromone_map, 0, sizeof(ant_updated_pheromone_map));
|
||||||
|
|
||||||
return (double) sum / (double) n_ants;
|
return (double) sum / (double) n_ants;
|
||||||
|
|
||||||
/*double real = 0;
|
|
||||||
for (uint k = 0; k < n_nodes; k++) {
|
|
||||||
real += dist_matrix[sh_route[k]][sh_route[(k + 1) % n_nodes]];
|
|
||||||
}*/
|
|
||||||
|
|
||||||
//cerr << "iteration " << i << ": dist " << sh_dist << endl;// << " os " << os << " rdist " << real << endl;
|
|
||||||
/*
|
|
||||||
uint nnz = 0;
|
|
||||||
double sum = 0, min = 1.0/0.0, max = -1.0/0.0;
|
|
||||||
for (int i = 0; i < n_nodes; i++) {
|
|
||||||
for (int j = 0; j < n_nodes - 1; j++) {
|
|
||||||
if (pheromone_map[i][j] != 0.0) nnz++;
|
|
||||||
sum += pheromone_map[i][j];
|
|
||||||
if (min > pheromone_map[i][j]) min = pheromone_map[i][j];
|
|
||||||
if (max < pheromone_map[i][j]) max = pheromone_map[i][j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cerr << "phmap: avg " << sum / (double) (n_nodes * n_nodes) << " min " << min << " max " << max << " nnz " << nnz << endl;*/
|
|
||||||
}
|
|
||||||
|
|
||||||
uint two_opt() {
|
|
||||||
uint swaps = 0;
|
|
||||||
for (int i = 0; i < n_nodes - 1; i++) {
|
|
||||||
for (int j = i+2; j < n_nodes; j++) {
|
|
||||||
size_t ip = i == 0 ? (n_nodes - 1) : (i - 1);
|
|
||||||
size_t ei = sh_route[i], ej = sh_route[j], eii = sh_route[ip],
|
|
||||||
ejj = sh_route[j];
|
|
||||||
double new_dist = sh_dist - dist_matrix[eii][ei] - dist_matrix[ejj][ej]
|
|
||||||
+ dist_matrix[ei][ej] + dist_matrix[ejj][eii];
|
|
||||||
if (new_dist < sh_dist) {
|
|
||||||
//cerr << "2opt " << i << " " << j << ": " << new_dist << endl;
|
|
||||||
sh_dist = new_dist;
|
|
||||||
reverse(sh_route.begin() + i, sh_route.begin() + j);
|
|
||||||
swaps++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return swaps;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint two_five_opt() {
|
uint two_five_opt() {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
dir = "c_prob/"
|
dir = "c_prob/"
|
||||||
|
|
||||||
|
|
||||||
def ant_colony_opt(instance):
|
def ant_colony_opt(instance):
|
||||||
fname = dir + instance.name + ".txt"
|
fname = dir + instance.name + ".txt"
|
||||||
|
|
||||||
|
@ -11,7 +13,7 @@ def ant_colony_opt(instance):
|
||||||
|
|
||||||
# single core params
|
# single core params
|
||||||
params = {
|
params = {
|
||||||
"eil76": (0.9, 8, 0.4, 1000, 500, 200, 30), # 543
|
"eil76": (0.75, 5, 0.5, 1000, 500, 217, 0), # 538
|
||||||
"d198": (0.9, 8, 0.4, 1000, 250, 125, 5), # 15871
|
"d198": (0.9, 8, 0.4, 1000, 250, 125, 5), # 15871
|
||||||
"ch130": (0.9, 8, 0.4, 1000, 1750, 25, 30), # 6212
|
"ch130": (0.9, 8, 0.4, 1000, 1750, 25, 30), # 6212
|
||||||
"kroA100": (0.9, 8, 0.4, 1000, 750, 100, 30), # 21378
|
"kroA100": (0.9, 8, 0.4, 1000, 750, 100, 30), # 21378
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
,tour length,optimal solution,gap,time to solve
|
,tour length,optimal solution,gap,time to solve
|
||||||
"('./problems/pr439.tsp', ""'C++ ant colony optimization'"")",109721.0,107217.0,2.34,67.09
|
"('./problems/pr439.tsp', ""'C++ ant colony optimization'"")",109721.0,107217.0,2.34,73.964
|
||||||
"('./problems/pcb442.tsp', ""'C++ ant colony optimization'"")",52466.0,50778.0,3.32,51.523
|
"('./problems/pcb442.tsp', ""'C++ ant colony optimization'"")",52466.0,50778.0,3.32,55.061
|
||||||
"('./problems/d198.tsp', ""'C++ ant colony optimization'"")",15871.0,15780.0,0.58,33.43
|
"('./problems/d198.tsp', ""'C++ ant colony optimization'"")",15871.0,15780.0,0.58,40.628
|
||||||
"('./problems/fl1577.tsp', ""'C++ ant colony optimization'"")",23020.0,22249.0,3.47,112.319
|
"('./problems/fl1577.tsp', ""'C++ ant colony optimization'"")",23020.0,22249.0,3.47,117.572
|
||||||
"('./problems/ch130.tsp', ""'C++ ant colony optimization'"")",6212.0,6110.0,1.67,21.834
|
"('./problems/ch130.tsp', ""'C++ ant colony optimization'"")",6212.0,6110.0,1.67,22.515
|
||||||
"('./problems/u1060.tsp', ""'C++ ant colony optimization'"")",235506.0,224094.0,5.09,104.112
|
"('./problems/u1060.tsp', ""'C++ ant colony optimization'"")",235506.0,224094.0,5.09,106.849
|
||||||
"('./problems/kroA100.tsp', ""'C++ ant colony optimization'"")",21378.0,21282.0,0.45,24.245
|
"('./problems/kroA100.tsp', ""'C++ ant colony optimization'"")",21378.0,21282.0,0.45,24.607
|
||||||
"('./problems/eil76.tsp', ""'C++ ant colony optimization'"")",543.0,538.0,0.93,20.232
|
"('./problems/eil76.tsp', ""'C++ ant colony optimization'"")",538.0,538.0,0.0,29.083
|
||||||
"('./problems/rat783.tsp', ""'C++ ant colony optimization'"")",9218.0,8806.0,4.68,124.22
|
"('./problems/rat783.tsp', ""'C++ ant colony optimization'"")",9218.0,8806.0,4.68,129.275
|
||||||
"('./problems/lin318.tsp', ""'C++ ant colony optimization'"")",43171.0,42029.0,2.72,42.92
|
"('./problems/lin318.tsp', ""'C++ ant colony optimization'"")",43171.0,42029.0,2.72,49.496
|
||||||
|
|
|
|
@ -6,9 +6,11 @@ import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
def run(show_plots=False, verbose=False):
|
def run(show_plots=False, verbose=False):
|
||||||
os.system("rm -f " + " ".join(glob.glob("sol/*.sol") + glob.glob("c_prob/*.txt")))
|
|
||||||
if sys.argv[1] == "all":
|
if sys.argv[1] == "all":
|
||||||
|
os.system("rm -f " + " ".join(glob.glob("sol/*.sol") + glob.glob("c_prob/*.txt")))
|
||||||
os.system("c++ -O2 -lpthread --std=c++11 -o c_prob/aco aco.cc opt.cc")
|
os.system("c++ -O2 -lpthread --std=c++11 -o c_prob/aco aco.cc opt.cc")
|
||||||
|
else:
|
||||||
|
os.system("rm -f " + " ".join(glob.glob("c_prob/*.txt")))
|
||||||
problems = glob.glob('./problems/*.tsp') if sys.argv[1] == "all" else ["./problems/"+sys.argv[1]+".tsp"]
|
problems = glob.glob('./problems/*.tsp') if sys.argv[1] == "all" else ["./problems/"+sys.argv[1]+".tsp"]
|
||||||
|
|
||||||
results = []
|
results = []
|
||||||
|
@ -34,7 +36,7 @@ def run(show_plots=False, verbose=False):
|
||||||
solver.duration])
|
solver.duration])
|
||||||
|
|
||||||
with open("sol/" + prob_instance.name + ".sol", "w") as f:
|
with open("sol/" + prob_instance.name + ".sol", "w") as f:
|
||||||
print(solution, file=f)
|
print(list(map(lambda x: x + 1, solution)), file=f)
|
||||||
|
|
||||||
if show_plots:
|
if show_plots:
|
||||||
solver.plot_solution()
|
solver.plot_solution()
|
||||||
|
|
Reference in a new issue