uncommitted changes
This commit is contained in:
parent
55fa9b5d64
commit
061b9f7d32
5 changed files with 49 additions and 18 deletions
Binary file not shown.
29
aco.cc
29
aco.cc
|
@ -219,8 +219,7 @@ void update_pheromone_map() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void mainloop() {
|
double mainloop(size_t i) {
|
||||||
for (size_t i = 0; i < n_iterations; i++) {
|
|
||||||
srand(i);
|
srand(i);
|
||||||
//cerr << "starting ants" << endl;
|
//cerr << "starting ants" << endl;
|
||||||
for (uint j = 0; j < n_ants; j++) {
|
for (uint j = 0; j < n_ants; j++) {
|
||||||
|
@ -232,9 +231,12 @@ void mainloop() {
|
||||||
}
|
}
|
||||||
//cerr << "summing ants" << endl;
|
//cerr << "summing ants" << endl;
|
||||||
uint os = 0;
|
uint os = 0;
|
||||||
|
unsigned long sum = 0L;
|
||||||
for (uint j = 0; j < n_ants; j++) {
|
for (uint j = 0; j < n_ants; j++) {
|
||||||
os += ants[j].other;
|
os += ants[j].other;
|
||||||
populate_ant_updated_pheromone_map(ants[j]);
|
populate_ant_updated_pheromone_map(ants[j]);
|
||||||
|
sum += 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;
|
||||||
|
@ -252,12 +254,14 @@ void mainloop() {
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
/*double real = 0;
|
/*double real = 0;
|
||||||
for (uint k = 0; k < n_nodes; k++) {
|
for (uint k = 0; k < n_nodes; k++) {
|
||||||
real += dist_matrix[sh_route[k]][sh_route[(k + 1) % n_nodes]];
|
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;
|
uint nnz = 0;
|
||||||
double sum = 0, min = 1.0/0.0, max = -1.0/0.0;
|
double sum = 0, min = 1.0/0.0, max = -1.0/0.0;
|
||||||
|
@ -271,7 +275,6 @@ void mainloop() {
|
||||||
}
|
}
|
||||||
cerr << "phmap: avg " << sum / (double) (n_nodes * n_nodes) << " min " << min << " max " << max << " nnz " << nnz << endl;*/
|
cerr << "phmap: avg " << sum / (double) (n_nodes * n_nodes) << " min " << min << " max " << max << " nnz " << nnz << endl;*/
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
uint two_opt() {
|
uint two_opt() {
|
||||||
uint swaps = 0;
|
uint swaps = 0;
|
||||||
|
@ -353,6 +356,7 @@ uint two_five_opt() {
|
||||||
#define buffersize 100000
|
#define buffersize 100000
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
unsigned started = time(NULL);
|
||||||
if (argc < 8) {
|
if (argc < 8) {
|
||||||
cerr << argv[0] << " [n_nodes] [n_iter] [n_ants] [alpha] [beta] [evap] [weight]" << endl;
|
cerr << argv[0] << " [n_nodes] [n_iter] [n_ants] [alpha] [beta] [evap] [weight]" << endl;
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -400,7 +404,22 @@ int main(int argc, char** argv) {
|
||||||
|
|
||||||
init_aco();
|
init_aco();
|
||||||
// scan and parse
|
// 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++) {
|
//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;
|
// cerr << "p: " << sh_route[k] << ": " << dist_matrix[sh_route[k]][sh_route[(k + 1) % n_nodes]] << endl;
|
||||||
|
|
6
aco_gambardella.txt
Normal file
6
aco_gambardella.txt
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
ants = 10
|
||||||
|
alpha = 0.1
|
||||||
|
rho = 0.1
|
||||||
|
q0 = 0.95 = 1 - (15/nCities)
|
||||||
|
beta = b1 = 1
|
||||||
|
|
|
@ -24,6 +24,15 @@ import os
|
||||||
# kroA100=21665 eil76=550 lin318=43675
|
# kroA100=21665 eil76=550 lin318=43675
|
||||||
# Separate run: fl1577=24238 (132s) rat783=9389 (174s)
|
# 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/"
|
dir = "/Users/maggicl/Git/AI2020BsC/c_prob/"
|
||||||
def ant_colony_opt(instance):
|
def ant_colony_opt(instance):
|
||||||
fname = dir + instance.name + ".txt"
|
fname = dir + instance.name + ".txt"
|
||||||
|
@ -34,8 +43,8 @@ def ant_colony_opt(instance):
|
||||||
for i in range(instance.nPoints):
|
for i in range(instance.nPoints):
|
||||||
print(" ".join(map(str, instance.dist_matrix[i])), file=f)
|
print(" ".join(map(str, instance.dist_matrix[i])), file=f)
|
||||||
|
|
||||||
alpha, beta, evap, weight = (0.9, 8, 0.4, 100_000)
|
alpha, beta, evap, weight = (0.9, 6, 0.6, 100_000)
|
||||||
ants, loops = (1000,7) if instance.nPoints > 1100 \
|
ants, loops = (400, 100_000) if instance.nPoints > 1100 \
|
||||||
else (600, 15) if instance.nPoints > 1000 \
|
else (600, 15) if instance.nPoints > 1000 \
|
||||||
else (750, 30) if instance.nPoints > 700 \
|
else (750, 30) if instance.nPoints > 700 \
|
||||||
else (975, 40) if instance.nPoints > 500 \
|
else (975, 40) if instance.nPoints > 500 \
|
||||||
|
@ -44,12 +53,9 @@ def ant_colony_opt(instance):
|
||||||
|
|
||||||
# Call C++ program
|
# Call C++ program
|
||||||
cmd = dir + "aco " + str(instance.nPoints) + " " + str(loops) + " " + str(ants) + \
|
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)
|
print(cmd)
|
||||||
solution = eval(os.popen(cmd).read())
|
solution = eval(os.popen(cmd).read())
|
||||||
solution.append(solution[0])
|
solution.append(solution[0])
|
||||||
return solution
|
return solution
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
ant_colony_opt(ProblemInstance("../problems/eil76.tsp"))
|
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ def swap2opt(tsp_sequence, i, j):
|
||||||
|
|
||||||
def gain(i, j, tsp_sequence, matrix_dist):
|
def gain(i, j, tsp_sequence, matrix_dist):
|
||||||
old_link_len = (matrix_dist[tsp_sequence[i], tsp_sequence[i - 1]] + 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[
|
changed_links_len = (matrix_dist[tsp_sequence[j], tsp_sequence[i - 1]] + matrix_dist[
|
||||||
tsp_sequence[i], tsp_sequence[j + 1]])
|
tsp_sequence[i], tsp_sequence[j + 1]])
|
||||||
return - old_link_len + changed_links_len
|
return - old_link_len + changed_links_len
|
||||||
|
|
Reference in a new issue