-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
267 lines (224 loc) · 6.79 KB
/
common.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Common functionality for travelling-salesman-problem algorithms.
"""
import time
import random
from math import sqrt
class Node(object):
"""Abstract TSP Node."""
def __init__(self, *args, **kwargs):
pass
def get_travel_costs(self, other_node):
"""
Returns the costs for travelling from this node to the given node.
Raises NotImplemented unless overwritten.
"""
raise NotImplemented
def get_nearest_neighbor(self, candidates):
"""
Calculates the travel costs for each of the given candidate nodes
and returns the candidate with the minimum costs along with the
according minimum costs. This means it returns a 2-tuple:
(nearest_neighbor, nearest_distance)
"""
nearest_neighbor = candidates[0]
nearest_distance = self.get_travel_costs(candidates[0])
for i in range(1, len(candidates)):
candidate_distance = self.get_travel_costs(candidates[i])
if candidate_distance < nearest_distance:
nearest_distance = candidate_distance
nearest_neighbor = candidates[i]
return (nearest_neighbor, nearest_distance)
class CoordinateNode(Node):
"""
TSP Node for the metric TSP with a X- and a Y-coordinate.
The distance between 2 CoordinateNodes is calculated using
Pythagoras' theorem (and converting the result to integer for
TSPLIB compatibility).
"""
def __init__(self, x, y, *args, **kwargs):
self.x = x
self.y = y
def get_travel_costs(self, other_node):
return int(sqrt((other_node.x - self.x)**2 + (other_node.y - self.y)**2))
#return sqrt((other_node.x - self.x)**2 + (other_node.y - self.y)**2)
def __str__(self):
return "CN(%s, %s)" % (self.x, self.y)
class Route(list):
def get_total_costs(self):
"""Returns the total travel costs for this route."""
sum = 0
for i in range(len(self) - 1):
sum += self[i].get_travel_costs(self[i + 1])
sum += self[len(self) - 1].get_travel_costs(self[0])
return sum
class TSPAlgorithm(object):
"""Abstract TSPAlgorithm."""
def __init__(self, nodes):
self.nodes = tuple(nodes)
self.t_started = 0
self.t_end = 0
def run(self):
"""Returns a Route containing all the nodes which have been
handed over to the constructor."""
raise NotImplemented
def save_start_time(self):
"""Children should execute this method each time run() is called."""
self.t_started = time.time()
def save_end_time(self):
"""Children should execute this method after each time run() has been
executed."""
self.t_end = time.time()
def get_runtime(self):
"""Calculates the time difference between the most recent calls of
save_start_time() and save_end_time()."""
return round(self.t_end - self.t_started, 3)
def generate_random_nodes(count, seed=0, max_size=500):
"""
Generates count nodes with coordinates between 0 and max_size.
Uses seed to initialize the random number generator.
Returns a list of nodes.
"""
random.seed(seed)
nodes = []
for i in range(count):
nodes.append(CoordinateNode(random.randint(0, max_size), random.randint(0, max_size)))
return nodes
def load_nodes_from_tsplib_file(filename):
"""
Loads all nodes specified in the given tsplib-file.
Only EUC_2D-format is supported.
"""
nodes = []
with open(filename, "r") as fh:
for line in fh:
line = line.strip()
assert len(line) > 0
if line.startswith("EDGE_WEIGHT_TYPE"):
assert line.endswith("EUC_2D"), \
"only EUC_2D instances can be loaded"
continue
if line[0].isdigit():
index, x, y = [float(i) for i in filter(lambda x: len(x) > 0, line.strip().split(" "))]
nodes.append(CoordinateNode(x, y))
elif line == "EOF":
break
return nodes
optimal_solutions = {
'a280': 2579,
'ali535': 202339,
'att48': 10628,
'att532': 27686,
'bayg29': 1610,
'bays29': 2020,
'berlin52': 7542,
'bier127': 118282,
'brazil58': 25395,
'brd14051': 469385,
'brg180': 1950,
'burma14': 3323,
'ch130': 6110,
'ch150': 6528,
'd1291': 50801,
'd15112': 1573084,
'd1655': 62128,
'd18512': 645238,
'd198': 15780,
'd2103': 80450,
'd493': 35002,
'd657': 48912,
'dantzig42': 699,
'dsj1000': 18660188,
'eil101': 629,
'eil51': 426,
'eil76': 538,
'fl1400': 20127,
'fl1577': 22249,
'fl3795': 28772,
'fl417': 11861,
'fnl4461': 182566,
'fri26': 937,
'gil262': 2378,
'gr120': 6942,
'gr137': 69853,
'gr17': 2085,
'gr202': 40160,
'gr21': 2707,
'gr229': 134602,
'gr24': 1272,
'gr431': 171414,
'gr48': 5046,
'gr666': 294358,
'gr96': 55209,
'hk48': 11461,
'kroA100': 21282,
'kroA150': 26524,
'kroA200': 29368,
'kroB100': 22141,
'kroB150': 26130,
'kroB200': 29437,
'kroC100': 20749,
'kroD100': 21294,
'kroE100': 22068,
'lin105': 14379,
'lin318': 42029,
'linhp318': 41345,
'nrw1379': 56638,
'p654': 34643,
'pa561': 2763,
'pcb1173': 56892,
'pcb3038': 137694,
'pcb442': 50778,
'pla33810': 66048945,
'pla7397': 23260728,
'pla85900': 142382641,
'pr1002': 259045,
'pr107': 44303,
'pr124': 59030,
'pr136': 96772,
'pr144': 58537,
'pr152': 73682,
'pr226': 80369,
'pr2392': 378032,
'pr264': 49135,
'pr299': 48191,
'pr439': 107217,
'pr76': 108159,
'rat195': 2323,
'rat575': 6773,
'rat783': 8806,
'rat99': 1211,
'rd100': 7910,
'rd400': 15281,
'rl11849': 923288,
'rl1304': 252948,
'rl1323': 270199,
'rl1889': 316536,
'rl5915': 565530,
'rl5934': 556045,
'si1032': 92650,
'si175': 21407,
'si535': 48450,
'st70': 675,
'swiss42': 1273,
'ts225': 126643,
'tsp225': 3916,
'u1060': 224094,
'u1432': 152970,
'u159': 42080,
'u1817': 57201,
'u2152': 64253,
'u2319': 234256,
'u574': 36905,
'u724': 41910,
'ulysses16': 6859,
'ulysses22': 7013,
'usa13509': 19982859,
'vm1084': 239297,
'vm1748': 336556
}
def tsplib_get_optimal_solution(tsp_instance):
global optimal_solutions
return optimal_solutions[tsp_instance]