This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
AICup/code/aco/ant_colony.py

36 lines
1.3 KiB
Python
Raw Normal View History

import os
2020-12-14 09:38:07 +00:00
2020-12-21 14:46:50 +00:00
dir = "c_prob/"
def ant_colony_opt(instance):
fname = dir + instance.name + ".txt"
# write .mat file for C++
2020-12-20 18:39:46 +00:00
with open(fname, "w") as f:
for i in range(instance.nPoints):
print(" ".join(map(str, instance.dist_matrix[i])), file=f)
# single core params
params = {
"eil76": (0.9, 8, 0.4, 1000, 500, 200, 30), # 543
2020-12-21 14:46:50 +00:00
"d198": (0.9, 8, 0.4, 1000, 250, 125, 5), # 15871
2020-12-20 18:39:46 +00:00
"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
2020-12-21 15:52:19 +00:00
"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
2020-12-20 18:39:46 +00:00
}
alpha, beta, evap, weight, ants, loops, optruns = params[instance.name]
# Call C++ program
cmd = dir + "aco " + str(instance.nPoints) + " " + str(loops) + " " + str(ants) + \
2020-12-21 14:46:50 +00:00
" " + 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])
return solution