forked from RatulGhosh/TSP-with-BnB-and-Genetic-Algorithm
/
TSP_Genetic.py
266 lines (185 loc) · 7.99 KB
/
TSP_Genetic.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
#!/usr/bin/env python
# coding: utf-8
# In[25]:
import numpy as np, random, operator, pandas as pd, matplotlib.pyplot as plt
from tqdm import tqdm
import collections
from tsp_solver.greedy import solve_tsp
import os
import time
with open("output_sls.csv", "a") as f:
f.writelines("81688982, 40204942, 32329404\nSLS\nTSP\n")
base_dir = "/data"
file_list = os.listdir(base_dir)
def cmp(c):
return (int(c.split('-')[2])/1000)+(int(c.split('-')[3])/10000000)+(int(c.split('-')[4])/100)+(int(c.split('-')[5])/25)
file_list = sorted([x for x in file_list if x[-3:] == "txt"])
file_list = sorted(file_list, key = cmp)
for file_name in file_list:
with open(os.path.join(base_dir, file_name), "r") as f:
temp = f.readlines()
temp = [x[:-1].split() for x in temp[1:]]
temp = [[float(y) for y in x] for x in temp]
# In[7]:
early_stop = collections.deque(maxlen=100)
# In[20]:
class Fitness:
def __init__(self, route):
self.route = route
self.distance = 0
self.fitness= 0.0
def routeDistance(self):
if self.distance ==0:
pathDistance = 0
for i in range(0, len(self.route)):
fromCity = self.route[i]
toCity = None
if i + 1 < len(self.route):
toCity = self.route[i + 1]
else:
toCity = self.route[0]
pathDistance += temp[fromCity][toCity]
self.distance = pathDistance
return self.distance
def routeFitness(self):
if self.fitness == 0:
self.fitness = 1 / float(self.routeDistance())
return self.fitness
# In[35]:
def createRoute(num_cities):
route = random.sample(list(range(num_cities)), num_cities)
return route
def cmp_key(c):
lst = temp[c]
lst = lst[:c]+lst[c+1:]
return np.argmin(lst)
def create_a_path_n(num_cities):
cities = list(range(num_cities))
city = random.sample(cities,1)[0]
path = [city]
remaining_cities = [rc for rc in cities if rc!=city]
#loop while the list of remaining cities are not empty
while remaining_cities:
#get the minimum distance
city = min(remaining_cities, key=cmp_key)
path.append(city)
remaining_cities.remove(city)
return path
def initialPopulation(popSize, num_cities):
population = []
for i in range(0, popSize-1):
population.append(create_a_path_n(num_cities))
# path = solve_tsp(temp)
# population.append(path)
return population
def rankRoutes(population):
fitnessResults = {}
for i in range(0,len(population)):
fitnessResults[i] = Fitness(population[i]).routeFitness()
return sorted(fitnessResults.items(), key = operator.itemgetter(1), reverse = True)
def selection(popRanked, eliteSize):
selectionResults = []
df = pd.DataFrame(np.array(popRanked), columns=["Index","Fitness"])
df['cum_sum'] = df.Fitness.cumsum()
df['cum_perc'] = 100*df.cum_sum/df.Fitness.sum()
for i in range(0, eliteSize):
selectionResults.append(popRanked[i][0])
for i in range(0, len(popRanked) - eliteSize):
pick = 100*random.random()
for i in range(0, len(popRanked)):
if pick <= df.iat[i,3]:
selectionResults.append(popRanked[i][0])
break
return selectionResults
def matingPool(population, selectionResults):
matingpool = []
for i in range(0, len(selectionResults)):
index = selectionResults[i]
matingpool.append(population[index])
return matingpool
def breed(parent1, parent2):
child = []
childP1 = []
childP2 = []
geneA = int(random.random() * len(parent1))
geneB = int(random.random() * len(parent1))
startGene = min(geneA, geneB)
endGene = max(geneA, geneB)
for i in range(startGene, endGene):
childP1.append(parent1[i])
childP2 = [item for item in parent2 if item not in childP1]
child = childP2[:startGene] + childP1 + childP2[startGene:]
return child
def breedPopulation(matingpool, eliteSize):
children = []
length = len(matingpool) - eliteSize
pool = random.sample(matingpool, len(matingpool))
for i in range(0,eliteSize):
children.append(matingpool[i])
for i in range(0, length):
child = breed(pool[i], pool[len(matingpool)-i-1])
children.append(child)
return children
def mutate(individual, mutationRate):
for swapped in range(len(individual)):
if(random.random() < mutationRate):
swapWith = int(random.random() * len(individual))
city1 = individual[swapped]
city2 = individual[swapWith]
individual[swapped] = city2
individual[swapWith] = city1
return individual
def mutatePopulation(population, mutationRate):
mutatedPop = []
for ind in range(0, len(population)):
mutatedInd = mutate(population[ind], mutationRate)
mutatedPop.append(mutatedInd)
return mutatedPop
def nextGeneration(currentGen, eliteSize, mutationRate, greedy = False):
popRanked = rankRoutes(currentGen)
selectionResults = selection(popRanked, eliteSize)
matingpool = matingPool(currentGen, selectionResults)
children = breedPopulation(matingpool, eliteSize)
nextGeneration = mutatePopulation(children, mutationRate)
if not greedy:
return nextGeneration
path = solve_tsp(temp)
nextGeneration.pop()
nextGeneration.append(path)
return nextGeneration
def geneticAlgorithm(population, popSize, eliteSize, mutationRate, generations):
pop = initialPopulation(popSize, len(population))
progress = []
progress1 = []
last_distance = 1 / rankRoutes(pop)[0][1]
min_distance = last_distance
early_stop.append(last_distance)
progress.append(last_distance)
best_route = pop[rankRoutes(pop)[0][0]]
print("Initial distance: " + str(1 / rankRoutes(pop)[0][1]))
for i in tqdm(range(0, generations)):
if i == 400:
pop = nextGeneration(pop, eliteSize, mutationRate, greedy=True)
else:
pop = nextGeneration(pop, eliteSize, mutationRate)
last_distance = 1 / rankRoutes(pop)[0][1]
#store the best route
if last_distance < min_distance:
min_distance = last_distance
best_route = pop[rankRoutes(pop)[0][0]]
#early stop
progress1.append(abs(last_distance - min(early_stop)))
if abs(last_distance - max(early_stop))<0.00000001 and i > 800:
break
progress.append(last_distance)
early_stop.append(last_distance)
print("Final distance: " + str(min_distance))
return progress, best_route, round(min_distance+temp[best_route[-1]][best_route[0]], 2), i
# In[53]:
start_time = time.time()
progress, best_route, min_distance, num_episodes = geneticAlgorithm(population=temp, popSize=100,
eliteSize=20, mutationRate=0.1/len(temp),
generations=5000)
time_taken = time.time()-start_time
with open("output_sls.csv", "a") as f:
f.writelines(file_name+","+str(round(min_distance, 2))+","+str(num_episodes)+"\n")