-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
196 lines (138 loc) · 4.89 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
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
#!/usr/bin/env python
# coding: utf-8
# ### Import libraries
# In[1]:
import json
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import random
from math import sqrt
import operator
import time
# %matplotlib notebook
# In[2]:
class Location:
def __init__(self, x, y):
self.x = x
self.y = y
def calcDistance(self, c):
return sqrt((self.x - c.x)**2 + (self.y - c.y)**2)
def dispLoc(self):
return (self.x, self.y)
# return '('+str(self.x)+','+str(self.y)+')'
# ### Generate Initial Population from set of locations
# In[3]:
def generateInitialPopulation(locations, population_size):
initial_population = []
for i in range(0, population_size):
initial_population.append(random.sample(locations, len(locations)))
return initial_population
# ### Selection
# In[4]:
def calcFitness(locations):
score = 0.0
for i in range(len(locations)):
f = 0.0
if i == len(locations)-1:
f = locations[i].calcDistance(locations[0])
else:
f = locations[i].calcDistance(locations[i+1])
score += f
score = (1/score)
return score
# In[5]:
def Selection(population, elite_size):
fitness_values = {}
selection_pool = []
selection_pool_index = []
for i in range(len(population)):
fitness_values[i] = calcFitness(population[i])
sorted_fitness = sorted(fitness_values.items(), key = operator.itemgetter(1), reverse = True)
df = pd.DataFrame(np.array(sorted_fitness), columns=["Index","Fitness"])
df['cum_sum'] = df.Fitness.cumsum()
df['cum_perc'] = 100*df.cum_sum/df.Fitness.sum()
for i in range(0, elite_size):
selection_pool.append(population[sorted_fitness[i][0]])
for i in range(0, len(sorted_fitness) - elite_size):
pick = 100*random.random()
for i in range(0, len(sorted_fitness)):
if pick <= df.iat[i,3]:
selection_pool.append(population[sorted_fitness[i][0]])
break
return selection_pool
# In[6]:
def Mating(sel_pool, elite_size):
offsprings = sel_pool[:elite_size]
new_pool = random.sample(sel_pool, len(sel_pool)) #retain the best routes using ellitism
for i in range(0, len(sel_pool)-elite_size):
a = int(random.random()*len(new_pool[i]))
b = int(random.random()*len(new_pool[len(sel_pool)-i-1]))
new_off = new_pool[i][min(a,b):max(a,b)]
new_off += [i for i in new_pool[len(sel_pool)-i-1] if i not in new_off]
offsprings.append(new_off)
return offsprings
# In[7]:
def Mutate(offsprings, mutation_rate):
for i in range(len(offsprings)):
for j in range(len(offsprings[i])):
if(random.random() < mutation_rate):
rand = int(random.random()*len(offsprings[i]))
temp = offsprings[i][rand]
offsprings[i][rand] = offsprings[i][j]
offsprings[i][j] = temp
return offsprings
# In[8]:
new_pop=[]
def TSA_GA(num_location, population_size, elite_size, mutation_rate, num_generations):
location = []
dist = []
# Generate random locations dataset
for i in range(num_location):
(x, y) = (int(random.random()*100), int(random.random()*100))
location.append(Location(x, y))
population = generateInitialPopulation(location, population_size)
print("Initial Distance:", str(1/calcFitness(population[0])))
for i in range(num_generations):
sel = Selection(population, elite_size)
dist.append((1/calcFitness(sel[0])))
offsprings = Mating(sel, elite_size)
new_pop = Mutate(offsprings, mutation_rate)
population = new_pop
print("Final Distance:", str(1/calcFitness(offsprings[0])))
print("Best route:")
for i in sel[0]:
print(i.dispLoc())
return dist, sel[0]
# In[9]:
with open('config.json') as json_data_file:
data = json.load(json_data_file)
dist, path = TSA_GA(data['num_location'], data['population_size'], data['elite_size'], data['mutation_rate'], data['num_generations'])
path = [i.dispLoc() for i in path]
# In[10]:
def plotGraph(dist, generation):
plt.plot(dist)
plt.ylabel('Distance')
plt.xlabel('Generation')
plt.show()
plotGraph(dist, generation = data['num_generations'])
# In[55]:
x = []
y = []
for i in path:
x.append(i[0])
y.append(i[1])
fig = plt.figure(figsize=(10, 8))
plt.scatter(x, y, marker ='o', s=50)
plt.scatter(x[0], y[0], marker ='o', color='green', s=200)
plt.scatter(x[-1], y[-1], marker ='o', color='red', s=200)
plt.ylabel('Y co-ordinate')
plt.xlabel('X co-ordinate')
plt.title("Shortest Path")
for i in range(len(path)):
if i == len(path) - 1:
plt.plot([path[i][0], path[0][0]], [path[i][1], path[0][1]])
else:
plt.plot([path[i][0], path[i+1][0]], [path[i][1], path[i+1][1]])
plt.pause(0.5)
plt.show()