From 061b9f7d320bdd4778ca96581dee50ac4aed8069 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Mon, 14 Dec 2020 10:38:07 +0100 Subject: [PATCH] uncommitted changes --- AI_cup_2020_studentname.xls | Bin 29184 -> 29184 bytes aco.cc | 29 ++++++++++++++++++++++++----- aco_gambardella.txt | 6 ++++++ src/ant_colony.py | 30 ++++++++++++++++++------------ src/two_opt.py | 2 +- 5 files changed, 49 insertions(+), 18 deletions(-) create mode 100644 aco_gambardella.txt diff --git a/AI_cup_2020_studentname.xls b/AI_cup_2020_studentname.xls index 5b951f477f3deb9de22ef19e13e8cccc74fd8fcc..eb8d3ba07ce29307d8ca655eb7d40095c621dea1 100644 GIT binary patch delta 21 dcmZp8!r1VHaYIf4Tf(%dPdm?SE-L6_1ORb(3Pu0` delta 21 dcmZp8!r1VHaYIf4+k^`zd@PP^E-L6_1ORYo3Gn~` diff --git a/aco.cc b/aco.cc index 207d701..9acd2e0 100644 --- a/aco.cc +++ b/aco.cc @@ -219,8 +219,7 @@ void update_pheromone_map() { } -void mainloop() { - for (size_t i = 0; i < n_iterations; i++) { +double mainloop(size_t i) { srand(i); //cerr << "starting ants" << endl; for (uint j = 0; j < n_ants; j++) { @@ -232,9 +231,12 @@ void mainloop() { } //cerr << "summing ants" << endl; uint os = 0; + unsigned long sum = 0L; for (uint j = 0; j < n_ants; j++) { os += ants[j].other; populate_ant_updated_pheromone_map(ants[j]); + sum += ants[j].distance_travelled; + if (sh_dist < 0 || ants[j].distance_travelled < sh_dist) { sh_dist = ants[j].distance_travelled; @@ -252,12 +254,14 @@ void mainloop() { 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; + //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; @@ -270,7 +274,6 @@ void mainloop() { } } cerr << "phmap: avg " << sum / (double) (n_nodes * n_nodes) << " min " << min << " max " << max << " nnz " << nnz << endl;*/ - } } uint two_opt() { @@ -353,6 +356,7 @@ uint two_five_opt() { #define buffersize 100000 int main(int argc, char** argv) { + unsigned started = time(NULL); if (argc < 8) { cerr << argv[0] << " [n_nodes] [n_iter] [n_ants] [alpha] [beta] [evap] [weight]" << endl; return 1; @@ -400,7 +404,22 @@ int main(int argc, char** argv) { init_aco(); // scan and parse - mainloop(); + + unsigned limit = 175; + unsigned last_time = started; + unsigned last_delta = 0; + for (size_t i = 0; i < n_iterations; i++) { + double d = mainloop(i); + unsigned now = time(NULL); + if ((now - started) + last_delta > limit) { + cerr << "out of time" << endl; + break; + } + cerr << "iter: " << i << " - dist: " << sh_dist << " - avg: " + << d << " - elapsed: " << now - started << endl; + last_delta = now - last_time; + last_time = now; + } //for (uint k = 0; k < n_nodes; k++) { // cerr << "p: " << sh_route[k] << ": " << dist_matrix[sh_route[k]][sh_route[(k + 1) % n_nodes]] << endl; diff --git a/aco_gambardella.txt b/aco_gambardella.txt new file mode 100644 index 0000000..5206464 --- /dev/null +++ b/aco_gambardella.txt @@ -0,0 +1,6 @@ +ants = 10 +alpha = 0.1 +rho = 0.1 +q0 = 0.95 = 1 - (15/nCities) +beta = b1 = 1 + diff --git a/src/ant_colony.py b/src/ant_colony.py index bac47c9..c8a6be6 100644 --- a/src/ant_colony.py +++ b/src/ant_colony.py @@ -24,6 +24,15 @@ import os # kroA100=21665 eil76=550 lin318=43675 # Separate run: fl1577=24238 (132s) rat783=9389 (174s) +# Run #3 +# alpha, beta, evap, weight = (0.1, 1, 0.1, 1 - 15/instance.nPoints) +# ants, loops = (800, 75) if instance.nPoints > 1100 \ +# else (800, 100) if instance.nPoints > 1000 \ +# else (800, 300) if instance.nPoints > 700 \ +# else (800, 750) if instance.nPoints > 500 \ +# else (800, 1000) if instance.nPoints > 300 \ +# else (800, 1250) if instance.nPoints > 195 else (800, 1500) + dir = "/Users/maggicl/Git/AI2020BsC/c_prob/" def ant_colony_opt(instance): fname = dir + instance.name + ".txt" @@ -34,22 +43,19 @@ def ant_colony_opt(instance): for i in range(instance.nPoints): print(" ".join(map(str, instance.dist_matrix[i])), file=f) - alpha, beta, evap, weight = (0.9, 8, 0.4, 100_000) - ants, loops = (1000,7) if instance.nPoints > 1100 \ - else (600,15) if instance.nPoints > 1000 \ - else (750,30) if instance.nPoints > 700 \ - else (975,40) if instance.nPoints > 500 \ - else (1000,50) if instance.nPoints > 300 \ - else (1100,70) if instance.nPoints > 195 else (2700, 140) + alpha, beta, evap, weight = (0.9, 6, 0.6, 100_000) + ants, loops = (400, 100_000) if instance.nPoints > 1100 \ + else (600, 15) if instance.nPoints > 1000 \ + else (750, 30) if instance.nPoints > 700 \ + else (975, 40) if instance.nPoints > 500 \ + else (1000, 50) if instance.nPoints > 300 \ + else (1100, 70) if instance.nPoints > 195 else (2700, 140) # Call C++ program cmd = dir + "aco " + str(instance.nPoints) + " " + str(loops) + " " + str(ants) + \ - " " + str(alpha) + " " + str(beta) + " " + str(evap) + " " + str(weight) +" < " + fname + " &2>/dev/null" + " " + str(alpha) + " " + str(beta) + " " + str(evap) + " " + str(weight) + \ + " < " + fname + " &2>/dev/null" print(cmd) solution = eval(os.popen(cmd).read()) solution.append(solution[0]) return solution - -if __name__ == "__main__": - ant_colony_opt(ProblemInstance("../problems/eil76.tsp")) - diff --git a/src/two_opt.py b/src/two_opt.py index cf01ae9..4e6da3f 100644 --- a/src/two_opt.py +++ b/src/two_opt.py @@ -25,7 +25,7 @@ def swap2opt(tsp_sequence, i, j): def gain(i, j, tsp_sequence, matrix_dist): old_link_len = (matrix_dist[tsp_sequence[i], tsp_sequence[i - 1]] + matrix_dist[ - tsp_sequence[j], tsp_sequence[j + 1]])x + tsp_sequence[j], tsp_sequence[j + 1]]) changed_links_len = (matrix_dist[tsp_sequence[j], tsp_sequence[i - 1]] + matrix_dist[ tsp_sequence[i], tsp_sequence[j + 1]]) return - old_link_len + changed_links_len