Compare commits

..

1 Commits

Author SHA1 Message Date
Claudio Maggioni a9ad56ad45 sumission take2 2020-12-22 23:02:33 +01:00
3 changed files with 5 additions and 46 deletions

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,12 +214,11 @@ 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]);
@ -247,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

@ -36,7 +36,7 @@ def run(show_plots=False, verbose=False):
solver.duration])
with open("sol/" + prob_instance.name + ".sol", "w") as f:
print(map(lambda x: x + 1, solution), file=f)
print(list(map(lambda x: x + 1, solution)), file=f)
if show_plots:
solver.plot_solution()