56 lines
2.1 KiB
Python
56 lines
2.1 KiB
Python
|
from src.io_tsp import ProblemInstance
|
||
|
import os
|
||
|
# OOT = out-of-time
|
||
|
|
||
|
# Run #0 unknown
|
||
|
|
||
|
# Run #1
|
||
|
# alpha = 0.5, beta = 7, evap = 0.5, weight = instance.best_sol
|
||
|
# if instance.nPoints > 1000: ants = 550 loops = 15
|
||
|
# elif instance.nPoints > 195: ants = 1000 loops = 55
|
||
|
# else: ants = 2500 loops = 150
|
||
|
# pr439=112263 pcb442=53699 d198=16199 fl1577=<OOT> ch130=6332 u1060=247703
|
||
|
# kroA100=21737 eil76=554 rat783=<OOT> lin318=45580
|
||
|
|
||
|
# Run #2
|
||
|
# alpha, beta, evap, weight = (0.9, 8, 0.4, 100_000)
|
||
|
# ants, loops = (900, 6) if instance.nPoints > 1100 \
|
||
|
# else (600, 15) if instance.nPoints > 1000 \
|
||
|
# else (750, 30) if instance.nPoints > 700 \
|
||
|
# else (975, 40) if instance.nPoints > 500 \
|
||
|
# else (1000, 50) if instance.nPoints > 300 \
|
||
|
# else (1100, 70) if instance.nPoints > 195 else (2700, 140)
|
||
|
# pr439=111322 pcb442=52176 d198=16177 ch130=6269 u1060=246085
|
||
|
# kroA100=21665 eil76=550 lin318=43675
|
||
|
# Separate run: fl1577=24238 (132s) rat783=9389 (174s)
|
||
|
|
||
|
dir = "/Users/maggicl/Git/AI2020BsC/c_prob/"
|
||
|
def ant_colony_opt(instance):
|
||
|
fname = dir + instance.name + ".txt"
|
||
|
|
||
|
# write .mat file for C++
|
||
|
if not os.path.exists(fname):
|
||
|
with open(fname, "w") as f:
|
||
|
for i in range(instance.nPoints):
|
||
|
print(" ".join(map(str, instance.dist_matrix[i])), file=f)
|
||
|
|
||
|
alpha, beta, evap, weight = (0.9, 8, 0.4, 100_000)
|
||
|
ants, loops = (1000,7) if instance.nPoints > 1100 \
|
||
|
else (600,15) if instance.nPoints > 1000 \
|
||
|
else (750,30) if instance.nPoints > 700 \
|
||
|
else (975,40) if instance.nPoints > 500 \
|
||
|
else (1000,50) if instance.nPoints > 300 \
|
||
|
else (1100,70) if instance.nPoints > 195 else (2700, 140)
|
||
|
|
||
|
# Call C++ program
|
||
|
cmd = dir + "aco " + str(instance.nPoints) + " " + str(loops) + " " + str(ants) + \
|
||
|
" " + str(alpha) + " " + str(beta) + " " + str(evap) + " " + str(weight) +" < " + fname + " &2>/dev/null"
|
||
|
print(cmd)
|
||
|
solution = eval(os.popen(cmd).read())
|
||
|
solution.append(solution[0])
|
||
|
return solution
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
ant_colony_opt(ProblemInstance("../problems/eil76.tsp"))
|
||
|
|