sumission take2

This commit is contained in:
Claudio Maggioni 2020-12-22 22:39:26 +01:00
parent ef002c8b1c
commit a9ad56ad45
6 changed files with 35 additions and 69 deletions

Binary file not shown.

BIN
claudio.maggioni.zip Normal file

Binary file not shown.

View file

@ -1,7 +1,7 @@
// vim: set ts=2 sw=2 et tw=80:
// 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 <stdlib.h>
@ -104,7 +104,7 @@ struct ant {
attractiveness[loc] = pow(pheromone_amount, alpha) *
pow(1 / distance, beta);
//cerr << "ant " << idx << " attr[loc]=" << attractiveness[loc] << endl;
sum_total += attractiveness[loc];
if (isnan(sum_total)) { cerr << "nanalert " << attractiveness[loc] <<
" " << pheromone_amount << " " << distance << endl; }
@ -126,7 +126,7 @@ struct ant {
double cumulative = 0.0;
for (uint loc : idxs) {
double weight = (attractiveness[loc] / sum_total);
//cerr << "ant " << idx << " w: " << weight + cumulative << endl;
if (toss <= (weight + cumulative)) {
return loc;
}
@ -206,7 +206,6 @@ void update_pheromone_map() {
double mainloop(size_t i) {
//cerr << "starting ants" << endl;
#if SINGLE_CORE
for (uint j = 0; j < n_ants; j++) {
ant_thread(&(ants[j]));
@ -215,17 +214,19 @@ double mainloop(size_t i) {
for (uint j = 0; j < n_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++) {
pthread_join(ants[j].t_handle, NULL);
}
#endif
//cerr << "summing ants" << endl;
unsigned long sum = 0L;
for (uint j = 0; j < n_ants; j++) {
populate_ant_updated_pheromone_map(ants[j]);
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) {
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));
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() {

View file

@ -1,6 +1,8 @@
import os
dir = "c_prob/"
def ant_colony_opt(instance):
fname = dir + instance.name + ".txt"
@ -11,24 +13,24 @@ def ant_colony_opt(instance):
# single core params
params = {
"eil76": (0.9, 8, 0.4, 1000, 500, 200, 30), # 543
"d198": (0.9, 8, 0.4, 1000, 250, 125, 5), # 15871
"ch130": (0.9, 8, 0.4, 1000, 1750, 25, 30), # 6212
"kroA100": (0.9, 8, 0.4, 1000, 750, 100, 30), # 21378
"lin318": (0.9, 8, 0.4, 1000, 500, 32, 30), # 43171
"pcb442": (0.9, 8, 0.4, 1000, 450, 24, 3), # 52466
"pr439": (0.9, 8, 0.4, 1000, 1000, 15, 3), # 109721
"rat783": (0.9, 8, 0.4, 1000, 300, 28, 4), # 9218
"u1060": (0.9, 8, 0.4, 1000, 350, 8, 10), # 235506
"fl1577": (0.9, 8, 0.4, 1000, 50, 10, 7), # 23020
"eil76": (0.75, 5, 0.5, 1000, 500, 217, 0), # 538
"d198": (0.9, 8, 0.4, 1000, 250, 125, 5), # 15871
"ch130": (0.9, 8, 0.4, 1000, 1750, 25, 30), # 6212
"kroA100": (0.9, 8, 0.4, 1000, 750, 100, 30), # 21378
"lin318": (0.9, 8, 0.4, 1000, 500, 32, 30), # 43171
"pcb442": (0.9, 8, 0.4, 1000, 450, 24, 3), # 52466
"pr439": (0.9, 8, 0.4, 1000, 1000, 15, 3), # 109721
"rat783": (0.9, 8, 0.4, 1000, 300, 28, 4), # 9218
"u1060": (0.9, 8, 0.4, 1000, 350, 8, 10), # 235506
"fl1577": (0.9, 8, 0.4, 1000, 50, 10, 7), # 23020
}
alpha, beta, evap, weight, ants, loops, optruns = params[instance.name]
# Call C++ program
cmd = dir + "aco " + str(instance.nPoints) + " " + str(loops) + " " + str(ants) + \
" " + str(alpha) + " " + str(beta) + " " + str(evap) + " " + str(weight) + \
" " + str(optruns) + " < " + fname + " &2>/dev/null"
" " + str(alpha) + " " + str(beta) + " " + str(evap) + " " + str(weight) + \
" " + str(optruns) + " < " + fname + " &2>/dev/null"
print(cmd)
solution = eval(os.popen(cmd).read())
solution.append(solution[0])

View file

@ -1,11 +1,11 @@
,tour length,optimal solution,gap,time to solve
"('./problems/pr439.tsp', ""'C++ ant colony optimization'"")",109721.0,107217.0,2.34,67.09
"('./problems/pcb442.tsp', ""'C++ ant colony optimization'"")",52466.0,50778.0,3.32,51.523
"('./problems/d198.tsp', ""'C++ ant colony optimization'"")",15871.0,15780.0,0.58,33.43
"('./problems/fl1577.tsp', ""'C++ ant colony optimization'"")",23020.0,22249.0,3.47,112.319
"('./problems/ch130.tsp', ""'C++ ant colony optimization'"")",6212.0,6110.0,1.67,21.834
"('./problems/u1060.tsp', ""'C++ ant colony optimization'"")",235506.0,224094.0,5.09,104.112
"('./problems/kroA100.tsp', ""'C++ ant colony optimization'"")",21378.0,21282.0,0.45,24.245
"('./problems/eil76.tsp', ""'C++ ant colony optimization'"")",543.0,538.0,0.93,20.232
"('./problems/rat783.tsp', ""'C++ ant colony optimization'"")",9218.0,8806.0,4.68,124.22
"('./problems/lin318.tsp', ""'C++ ant colony optimization'"")",43171.0,42029.0,2.72,42.92
"('./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,55.061
"('./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,117.572
"('./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,106.849
"('./problems/kroA100.tsp', ""'C++ ant colony optimization'"")",21378.0,21282.0,0.45,24.607
"('./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,129.275
"('./problems/lin318.tsp', ""'C++ ant colony optimization'"")",43171.0,42029.0,2.72,49.496

1 tour length optimal solution gap time to solve
2 ('./problems/pr439.tsp', "'C++ ant colony optimization'") 109721.0 107217.0 2.34 67.09 73.964
3 ('./problems/pcb442.tsp', "'C++ ant colony optimization'") 52466.0 50778.0 3.32 51.523 55.061
4 ('./problems/d198.tsp', "'C++ ant colony optimization'") 15871.0 15780.0 0.58 33.43 40.628
5 ('./problems/fl1577.tsp', "'C++ ant colony optimization'") 23020.0 22249.0 3.47 112.319 117.572
6 ('./problems/ch130.tsp', "'C++ ant colony optimization'") 6212.0 6110.0 1.67 21.834 22.515
7 ('./problems/u1060.tsp', "'C++ ant colony optimization'") 235506.0 224094.0 5.09 104.112 106.849
8 ('./problems/kroA100.tsp', "'C++ ant colony optimization'") 21378.0 21282.0 0.45 24.245 24.607
9 ('./problems/eil76.tsp', "'C++ ant colony optimization'") 543.0 538.0 538.0 0.93 0.0 20.232 29.083
10 ('./problems/rat783.tsp', "'C++ ant colony optimization'") 9218.0 8806.0 4.68 124.22 129.275
11 ('./problems/lin318.tsp', "'C++ ant colony optimization'") 43171.0 42029.0 2.72 42.92 49.496

View file

@ -6,9 +6,11 @@ import os
import sys
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":
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")
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"]
results = []
@ -34,7 +36,7 @@ def run(show_plots=False, verbose=False):
solver.duration])
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:
solver.plot_solution()