Compare commits
1 Commits
a9ad56ad45
...
0d5f32ffa2
Author | SHA1 | Date | |
---|---|---|---|
0d5f32ffa2 |
Binary file not shown.
49
code/aco.cc
49
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
|
||||||
// c++ -O2 -lpthread --std=c++11 -o c_prob/aco aco.cc opt.cc
|
// g++ -lpthread --std=c++11 -o c_prob/aco aco.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,6 +206,7 @@ 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]));
|
||||||
@ -214,11 +215,12 @@ 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]);
|
||||||
@ -245,6 +247,45 @@ 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() {
|
||||||
|
@ -36,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(list(map(lambda x: x + 1, solution)), file=f)
|
print(map(lambda x: x + 1, solution), file=f)
|
||||||
|
|
||||||
if show_plots:
|
if show_plots:
|
||||||
solver.plot_solution()
|
solver.plot_solution()
|
||||||
|
Reference in New Issue
Block a user