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/aco/ant_colony.py

37 lines
1.4 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
"pr439": (0.9, 8, 0.4, 1000, 450, 24, 2), # 11734
"rat783": (0.9, 8, 0.4, 1000, 450, 24, 2), # 9232
"u1060": (0.9, 8, 0.4, 1000, 350, 6, 3), # 238025
2020-12-21 14:46:50 +00:00
#"fl1577": (0.9, 8, 0.4, 1000, 50, 9, 3), # 24145
"fl1577": (0.9, 8, 0.4, 1000, 50, 10, 5), # 24145
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