-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGA.py
414 lines (280 loc) · 11.9 KB
/
GA.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import math
import random
from operator import itemgetter
class block:
def __init__(self, Name, X, Y, Value):
self.name = Name
self.value = Value
self.x = X
self.y = Y
def getX(self):
return self.x
def getY(self):
return self.y
def getValue(self):
return self.value
def setValue(self, Value):
self.value = Value
def getName(self):
return self.name
def distanceTo(self, block1):
distX = int(math.fabs(self.getX() - block1.getX()))
distY = int(math.fabs(self.getY() - block1.getY()))
return math.sqrt(distX * distX + distY * distY)
def print(self):
print(self.getName(), "(", self.getX(), ",", self.getY(), "):", self.value)
################################################
class RouteManager:
destRoutes = []
@staticmethod
def addBlock(block):
RouteManager.destRoutes.append(block)
@staticmethod
def getBlock(index):
return RouteManager.destRoutes.__getitem__(index)
@staticmethod
def numberOfBlocks():
return RouteManager.destRoutes.__len__()
####################################################
class Route:
def __init__(self, start, end, Size):
self.route = []
for i in range(Size):
self.route.append(None)
self.route[0] = start
self.route[Size - 1] = end
self.distance = 0
self.fitness = 0
def generateIndividual(self):
for i in range(1, self.routeSize() - 1):
tempBlock = RouteManager.getBlock(random.randint(0, RouteManager.numberOfBlocks() - 1))
while self.containBlock(tempBlock):
tempBlock = RouteManager.getBlock(random.randint(0, RouteManager.numberOfBlocks() - 1))
self.setBlock(i, tempBlock)
rest = self.route[1:self.route.__len__() - 1]
random.shuffle(rest)
self.route = [self.route[0]] + rest + [self.route[self.route.__len__() - 1]]
def containBlock(self, thisBlock):
for i in range(self.routeSize()):
if (self.getBlock(i) != None):
tempName = self.getBlock(i).getName()
tempX = self.getBlock(i).getX()
tempY = self.getBlock(i).getY()
tempValue = self.getBlock(i).getValue()
if (
tempX == thisBlock.getX() and tempY == thisBlock.getY() and tempName == thisBlock.getName() and tempValue == thisBlock.getValue()):
return True
return False
def setBlock(self, routePosition, Block):
self.route.__setitem__(routePosition, Block)
self.fitness = 0
self.distance = 0
def getBlock(self, routePosition):
return self.route.__getitem__(routePosition)
def routeSize(self):
return self.route.__len__()
def getDistance(self):
if (self.distance == 0):
routeDist = 0
for i in range(self.routeSize()):
fromBlock = self.getBlock(i)
destBlock = self.getBlock(0)
if (fromBlock.getValue() == "B"):
routeDist += 300
if (fromBlock.getValue() == "S"):
routeDist += 50
if (i + 1 < self.routeSize()):
destBlock = self.getBlock(i + 1)
routeDist += int(fromBlock.distanceTo(destBlock))
self.distance = int(routeDist)
return self.distance
def getFitness(self):
if (self.fitness == 0):
self.fitness = 1 / self.getDistance()
return self.fitness
def getRoute(self):
return self.route
def setRoute(self, routeArray):
self.route.clear()
self.route = []
self.route = routeArray
self.distance = 0
self.fitness = 0
def PrintMe(self):
for i in range(self.routeSize()):
self.getBlock(i).print()
###################################################
class population:
def __init__(self, populationSize, initialise, start=0, goal=0, RouteSize=0):
self.routes = []
for i in range(populationSize):
self.routes.append(None)
if initialise:
for i in range(self.populationSize()):
tmpRoute = Route(start, goal, RouteSize)
tmpRoute.generateIndividual()
self.saveRoute(i, tmpRoute)
def saveRoute(self, index, route1):
self.routes.__setitem__(index, route1)
def getRoute(self, index):
return self.routes.__getitem__(index)
def populationSize(self):
return self.routes.__len__()
def sort_on_fitness(self):
temp = []
for i in range(self.populationSize()):
route = self.getRoute(i)
temp2 = []
temp2.append(route)
temp2.append(route.getFitness())
temp.append(temp2)
temp = sorted(temp, key=itemgetter(1), reverse=True)
for i in range(self.populationSize()):
self.saveRoute(i, temp[i][0])
def routeExists(self, route1):
exists = False
for i in range(self.populationSize()):
route = self.getRoute(i)
if (route != None):
exists = True
for j in range(1, route.routeSize() - 1):
bl = route.getBlock(j)
bl2 = route1.getBlock(j)
if bl.getName() == bl2.getName() and bl.getX() == bl2.getX() and bl.getY() == bl2.getY() and bl.getValue() == bl2.getValue():
exists = True
else:
exists = False
break
if exists:
return exists
return exists
def getFittest(self):
fittestRoute = self.getRoute(0)
for i in range(1, self.populationSize()):
if fittestRoute.getFitness() <= self.getRoute(i).getFitness():
fittestRoute = self.getRoute(i)
return fittestRoute
#################################################
class GA:
mutationRate = 0.015
tournamentSize = 5
elitism = True
@staticmethod
def evolvePopulation(pop):
newPop = population(pop.populationSize() * 2, False)
# elitismOffSet=0
# if(GA.elitism):
# elitismOffSet = 1
# for i in range(elitismOffSet):
# newPop.saveRoute(i,pop.getRoute(i))
for i in range(pop.populationSize()):
newPop.saveRoute(i, pop.getRoute(i))
# for i in range(elitismOffSet,newPop.populationSize()):
for i in range(pop.populationSize(), newPop.populationSize()):
Parent1 = GA.tournamentSelection(pop)
Parent2 = GA.tournamentSelection(pop)
child = GA.crossOver(Parent1, Parent2)
while newPop.routeExists(child) is True:
Parent1 = GA.tournamentSelection(pop)
Parent2 = GA.tournamentSelection(pop)
child = GA.crossOver(Parent1, Parent2)
newPop.saveRoute(i, child)
# for i in range(elitismOffSet,newPop.populationSize()):
# GA.mutate(newPop.getRoute(i))
for i in range(pop.populationSize(), newPop.populationSize()):
GA.mutate(newPop.getRoute(i))
newPop.sort_on_fitness()
newPop2 = population(pop.populationSize(), False)
for i in range(pop.populationSize()):
route = newPop.getRoute(i)
newPop2.saveRoute(i, route)
return newPop2
@staticmethod
def crossOver(route1, route2): # 2 cut crossover
child = Route(route1.getBlock(0), route1.getBlock(route1.routeSize() - 1), route1.routeSize())
startPos = int(random.random() * route1.routeSize())
endPos = int(random.random() * route2.routeSize())
for i in range(1, child.routeSize() - 1):
if (startPos < endPos and i > startPos and i < endPos):
if route1.getBlock(i).getValue != "B":
child.setBlock(i, route1.getBlock(i))
elif (startPos > endPos):
if not (i < startPos and i > endPos):
if route1.getBlock(i).getValue != "B":
child.setBlock(i, route1.getBlock(i))
for j in range(route2.routeSize()):
if child.containBlock(route2.getBlock(j)) is False:
for k in range(child.routeSize()):
if child.getBlock(k) == None:
child.setBlock(k, route2.getBlock(j))
break
return child
@staticmethod # 1 cut crossover
def crossOver2(route1, route2):
child = Route(route1.getBlock(0), route1.getBlock(route1.routeSize() - 1), route1.routeSize())
startPos = random.randint(1, route1.routeSize() - 1)
for i in range(1, startPos):
if i <= startPos:
# if route1.getBlock(i).getValue != "B":
child.setBlock(i, route1.getBlock(i))
for j in range(startPos, child.routeSize()):
if child.containBlock(route2.getBlock(j)) is False:
for k in range(child.routeSize()):
if child.getBlock(k) == None:
child.setBlock(k, route2.getBlock(j))
break
for k in range(child.routeSize()):
if child.getBlock(k) == None:
for j in range(startPos, child.routeSize()):
if child.containBlock(route1.getBlock(j)) is False:
child.setBlock(k, route1.getBlock(j))
break
return child
@staticmethod
def mutate(myRoute):
for routePos1 in range(1, myRoute.routeSize() - 1):
if random.random() < GA.mutationRate:
routePos2 = int(myRoute.routeSize() * random.random())
while routePos2 == 0 or routePos2 == (myRoute.routeSize() - 1):
routePos2 = int(myRoute.routeSize() * random.random())
c1 = myRoute.getBlock(routePos1)
c2 = myRoute.getBlock(routePos2)
myRoute.setBlock(routePos2, c1)
myRoute.setBlock(routePos1, c2)
@staticmethod
def tournamentSelection(pop):
popTournament = population(GA.tournamentSize, False)
for i in range(GA.tournamentSize):
randomID = random.randint(0, pop.populationSize() - 1)
popTournament.saveRoute(i, pop.getRoute(randomID))
fittestRoute = popTournament.getFittest()
return fittestRoute
class Ambulance:
def __init__(self, Start=0, End=0, isFree=True):
self.start = RouteManager.getBlock(Start)
self.end = RouteManager.getBlock(End)
self.route = population(5, False)
self.isFreeFlag = isFree
def setStart(self, Start):
self.start = RouteManager.getBlock(Start)
def getStart(self):
return self.start
def setEnd(self, End):
self.end = RouteManager.getBlock(End)
def getEnd(self):
return self.end
def setAllRoutes(self, Population1):
# self.route=population(1,False)
self.route = Population1
def getAllRoutes(self):
return self.route
def setRoute(self, index, newRoute):
self.route.__setitem__(index, newRoute)
def getRoute(self, index):
return self.route[index]
def setisFree(self, isFree):
self.isFreeFlag = isFree
def getisFree(self):
return self.isFreeFlag
def addRoute(self, route):
self.route.append(route)