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 16:52:19 +01:00

35 lines
1.3 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, 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
}
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