-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
138 lines (118 loc) · 3.71 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import argparse
import csv
import sys
import pathlib
import math
import os
import webbrowser
import tsp_aco
from graph import parse_graph_from_file
def main(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument("--file", "-F", default="worldcitiespop-part1.txt")
parser.add_argument("--country", "-C", default="hu")
parser.add_argument("-n", type=int, default=6)
parser.add_argument("-m", type=int, default=2)
parser.add_argument("--iterations", "-I", type=int)
parser.add_argument("--n-probas", "-p", type=int)
parser.add_argument("--features", default="")
parser.add_argument("--out", "-O")
parser.add_argument(
"--format", "-f", choices=["readable", "csv", "numpy"], default="csv"
)
parser.add_argument("--show", "-S", action="store_true")
opts = parser.parse_args(argv)
city_names, coordinates, distmat = parse_graph_from_file(
opts.file, opts.country, opts.n
)
if opts.out == "-":
out = sys.stdout
elif opts.out:
out = open(opts.out, "w")
else:
out = None
if out is not None:
printers = {"readable": print_readable, "csv": print_csv, "numpy": print_numpy}
printers[opts.format](out, city_names, distmat)
if opts.out != "-":
out.close()
graph_name = os.path.split(opts.file)[1].rpartition(".")[0]
params_15n_general = dict(
evaporation=0.001,
n_probas=100,
max_iteration=500,
)
params_15n_for_seed777 = dict(
evaporation=0.0001,
n_probas=1000,
max_iteration=50,
)
params_30n = dict(
evaporation=0.00001,
n_probas=500,
max_iteration=300,
)
params = params_30n
max_iterations = params.pop("max_iteration")
# heuristic = tsp_aco.HeuristicV1(
# distances=distmat,
# n_salesmans=opts.m,
# herdness=0.5,
# greedness=0.5,
# evaporation=0.001,
# n_probas=1000,
# use_jump_balancing=True,
# pheromone_update_scale=1,
# )
heuristic = tsp_aco.HeuristicV2(
distances=distmat,
n_salesmans=opts.m,
herdness=1,
greedness=2,
pheromone_update_scale=0.0001,
use_jump_balancing=False,
**params
)
best_cost, best_paths = tsp_aco.solve_tsp(
distances=distmat,
# callback=tsp_aco.GenerateVisualSvg(f"frames/test", coordinates),
callback=tsp_aco.GenerateVisualSvg(f"frames/v2_{graph_name}", coordinates),
max_iteration=max_iterations,
heuristic=heuristic,
)
# distmat_norm = sum(map(sum, distmat)) / (len(distmat) ** 2)
for path in best_paths:
cost = tsp_aco.path_len(path, distmat) #/ distmat_norm
print(f"{cost:.4f}:: {' -> '.join(str(i) for i in path)}")
if opts.show:
url = (
pathlib.Path(__file__)
.absolute()
.parent.joinpath("visualizer.html")
.as_uri()
)
webbrowser.open(url)
def print_readable(out, names, distmap):
printmat = [["", *names]]
for name, row in zip(names, distmap):
printmat.append([name, *("{:.2f}".format(d) for d in row)])
colw = max(map(len, (x for r in printmat for x in r)))
for row in printmat:
out.write(" ".join(r.ljust(colw) for r in row))
out.write("\n")
def print_csv(out, names, distmap):
writer = csv.writer(out)
writer.writerow(names)
for line in distmap:
writer.writerow(map(str, line))
def print_numpy(out, names, distmap):
import numpy
numpy.savetxt(out, distmap)
main(
"-C x -F graphs/1-30,gauss,1.csv "
"-n 30 -m 3 -p 1000 -I 5 "
# "--features JUMP_BALANCING"
"".split()
)
# if __name__ == "__main__":
# main()