-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtsp.py
268 lines (185 loc) · 8.61 KB
/
tsp.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
268
from random import random, randrange, shuffle
from pytsp.core import (CompressedAnnealing, GeneticAlgorithm,
SimulatedAnnealing, cached, jarvis)
class TravellingSalesman(SimulatedAnnealing, GeneticAlgorithm):
class Traits(SimulatedAnnealing.Traits, GeneticAlgorithm.Traits):
class Mutate(GeneticAlgorithm.Traits.Mutate):
def random_swap(self, elements):
neighbor = elements[:]
i, j = randrange(1, len(elements) -
1), randrange(1, len(elements) - 1)
neighbor[i], neighbor[j] = neighbor[j], neighbor[i]
return neighbor
def shift_1(self, elements):
neighbor = elements[:]
i, j = randrange(1, len(elements) -
1), randrange(1, len(elements) - 1)
neighbor.insert(j, neighbor.pop(i))
return neighbor
def reverse_random_sublist(self, elements):
neighbor = elements[:]
i = randrange(1, len(elements) - 1)
j = randrange(1, len(elements) - 1)
i, j = min([i, j]), max([i, j])
neighbor[i:j] = neighbor[i:j][::-1]
return neighbor
class Crossover(GeneticAlgorithm.Traits.Crossover):
def cut_and_stitch(self, individual_a, individual_b):
individual_a, individual_b
offspring = individual_a[1:len(individual_a) // 2]
for b in individual_b[1:-1]:
if b not in offspring:
offspring.append(b)
return [individual_a[0]] + offspring + [individual_b[0]]
class Select(GeneticAlgorithm.Traits.Select):
def random_top_half(self, population):
return population[randrange(0, len(population) // 2)]
class Fitness(GeneticAlgorithm.Traits.Fitness):
def inverse_cost(self, individual):
return 1.0 / self.cost(individual)
def unweighted_mst(self, individual):
v = len(individual) - 1
return ((v * v) - v + 1) / self.cost(individual)
def weighted_mst(self, individual):
return self.heuristic(individual) / self.cost(individual)
class Metric:
def euclidean(self, p1, p2):
return (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2
def manhattan(self, p1, p2):
return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])
class Heuristic:
@cached
def kruskal(self, route):
edges = []
for u in route[:-1]:
for v in route[:-1]:
if u != v:
edges.append((u, v, self.metric(u, v)))
edges.sort(key=lambda edge: edge[2])
cost, components = 0, {v: set([v]) for v in route}
for u, v, d in edges:
if not components[u].intersection(components[v]):
cost += d
components[u] = components[u].union(components[v])
components[v] = components[u]
for root, component in components.items():
if u in component or v in component:
for vertex in component:
components[root] = components[root].union(
components[vertex])
return cost
class Criterion:
def angle(self, c, b, a):
from math import degrees, atan2
return degrees(
atan2(c[1]-b[1], c[0]-b[0]) - atan2(a[1]-b[1], a[0]-b[0])
)
def eccentricity(self, a, b, c):
d1 = self.metric(a, b)
d2 = self.metric(b, c)
d3 = self.metric(a, c)
return d3 / (d1 + d2)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def cost(self, route):
return sum([
self.metric(route[i], route[i + 1])
for i in range(len(route) - 1)
])
def nearest_neighbor(self, *args, **kwargs):
depot, cities = args[0], args[1]
route, remaining = [depot], cities[:]
while len(remaining) > 0:
nearest = (0, self.metric(route[-1], remaining[0]))
for i in range(1, len(remaining)):
city = remaining[i]
distance = self.metric(route[-1], city)
if distance < nearest[1]:
nearest = (i, distance)
route.append(remaining[nearest[0]])
del remaining[nearest[0]]
return route + [depot], self.cost(route + [depot])
def convex_hull(self, *args, **kwargs):
depot, cities = args[0], args[1]
route = jarvis([depot] + cities)
inner = set([depot] + cities).difference(set(route))
while inner:
best, best_i, best_value = None, -1, float("-inf")
for candidate in inner:
for i in range(len(route) - 1):
value_candidate = self.criterion(
route[i], candidate, route[i + 1]
)
if value_candidate > best_value:
best_value = value_candidate
best = candidate
best_i = i
inner.remove(best)
route = route[:best_i + 1] + [best] + route[best_i + 1:]
while route[0] != depot:
route.insert(0, route.pop())
return route + [depot], self.cost(route + [depot])
def opt_2(self, *args, **kwargs):
depot, cities = args[0], args[1]
def reverse_sublist(elements, i, j):
copy = elements[:]
copy[i:j] = copy[i:j][::-1]
return copy
route = cities[:]
cost = self.cost([depot] + route + [depot])
for i in range(0, len(route) - 1):
for j in range(i + 1, len(route)):
candidate = reverse_sublist(route, i, j)
candidate_cost = self.cost([depot] + candidate + [depot])
if candidate_cost < cost:
return self.opt_2(depot, candidate)
return [depot] + route + [depot], self.cost([depot] + route + [depot])
def simulated_annealing(self, *args, **kwargs):
depot, cities = args[0], args[1]
return SimulatedAnnealing.fit(self, [depot] + cities + [depot])
def genetic_algorithm(self, *args, **kwargs):
depot, cities = args[0], args[1]
fittest = GeneticAlgorithm.fit(self, [depot] + cities + [depot])
return fittest, self.cost(fittest)
class TravellingSalesmanTimeWindows(TravellingSalesman, CompressedAnnealing):
class Traits(TravellingSalesman.Traits, CompressedAnnealing.Traits):
class Fitness(TravellingSalesman.Traits.Fitness):
def inverse_cost(self, individual):
c = 0.5 * self.cost(individual) + 0.5 * self.penalty(individual)
return 1.0 / c
def unweighted_mst(self, individual):
v = len(individual) - 1
c = 0.5 * self.cost(individual) + 0.5 * self.penalty(individual)
return ((v * v) - v + 1) / c
def weighted_mst(self, individual):
c = 0.5 * self.cost(individual) + 0.5 * self.penalty(individual)
return self.heuristic(individual) / c
class Service:
pass
class Timewindow:
pass
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def partial_cost(self, a, b):
return self.service(a) + self.metric(a, b)
def cost(self, route):
return sum([
self.partial_cost(route[i], route[i + 1])
for i in range(len(route) - 1)
])
def partial_penalty(self, arrival, a, b):
arrival += self.partial_cost(a, b)
beg, end = self.timewindow(b)
start_of_service = max(arrival, beg)
penalty = max(0, start_of_service + self.service(b) - end)
return arrival, penalty
def penalty(self, route):
arrival, penalty = 0, 0
for i in range(len(route) - 1):
arrival, p = self.partial_penalty(arrival, route[i], route[i + 1])
penalty += p
return penalty
def compressed_annealing(self, *args, **kwargs):
depot, cities = args[0], args[1]
fittest = CompressedAnnealing.fit(self, [depot] + cities + [depot])
return fittest, self.cost(fittest)