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
2020-12-21 15:46:50 +01:00

36 lines
1.4 KiB
Python

import os
dir = "c_prob/"
def ant_colony_opt(instance):
fname = dir + instance.name + ".txt"
# write .mat file for C++
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
"d198": (0.9, 8, 0.4, 1000, 250, 125, 5), # 15871
"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
#"fl1577": (0.9, 8, 0.4, 1000, 50, 9, 3), # 24145
"fl1577": (0.9, 8, 0.4, 1000, 50, 10, 5), # 24145
}
alpha, beta, evap, weight, ants, loops, optruns = params[instance.name]
# Call C++ program
cmd = dir + "aco " + str(instance.nPoints) + " " + str(loops) + " " + str(ants) + \
" " + 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