-
Notifications
You must be signed in to change notification settings - Fork 0
/
ga.py
352 lines (232 loc) · 11.5 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
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 27 17:11:32 2018
@author: Andrei
"""
import random as rnd
import numpy as np
from sympy.combinatorics.graycode import GrayCode
from sympy.combinatorics.graycode import gray_to_bin
from datetime import datetime
class GA_Abuin:
rnd.seed(datetime.now())
def __init__(self, alphabet, pMut=0.1, c='1',pCross=0.1, pUCross = 0, sel='fps', tsSize=2, elC=0, prob='ras', length=16 ):
self.popSize,self.maxGen = 100, 100
self.tsSize = tsSize
self.Kuni=[] # to keep count of K values for uniform crossover
self.eps = 0
self.ind=[]
self.new_pop=[]
self.old_pop=[]
self.fitAll=[]
self.bestChromosome=[] # best Fitness value actually
self.bestChromosomeBin=[] # best Chromosome in binary form
self.alphabet = alphabet
self.pMut= pMut
self.seFun = sel
self.pCross = pCross
self.problem = prob
self.dim = length//16 # determine dimensions of the problem
try:
self.kP =int(c)
self.pUCross = 0
except ValueError:
self.pUCross = pUCross
self.kP=0
self.BigConst = 10.*self.dim+sum([26 for x in range(0, self.dim)])
self.eC = elC
self.length=length
self.pop = self.reinit()
#self.pop = [['1'
# for x in range(self.length)]
# for y in range(self.popSize)
# ]
# if sel == 'fps' :
self.fitnessFun = getattr(self, 'sel_'+sel+'_'+prob)
def reinit(self):
return [[self.alphabet[rnd.randint(0, len(self.alphabet)-1)]
for x in range(self.length)]
for y in range(self.popSize)
]
def ts(self):
print('I\'m in ts')
def maxone(self):
return
def fitness_ras(self,pop):
xdim=self.length//self.dim
fVals=[]
fObj=[]
for i in range(len(pop)):
#xGC=[''.join(pop[i])[j*xdim:(j+1)*xdim] for j in range(0,self.dim)]
#print(xdim)
self.xGC=[gray_to_bin(''.join(pop[i]))[(j*xdim):((j+1)*xdim)] for j in range(0,self.dim)]
#print(pop)
#self.xGC=[(''.join(pop[i]))[(j*xdim):((j+1)*xdim)] for j in range(0,self.dim)]
#print(self.xGC)
x=[int(i,2)/8192.-4. for i in self.xGC]
rhs = [m**2.-10.*np.cos(2.*np.pi*m) for m in x]
f_x = 10.*self.dim+sum(rhs)
fVals.append(f_x)
fObj.append(self.BigConst-f_x)
#print(xGC)
#print(x)
#print(f_x)
#wait = input("PRESS ENTER TO CONTINUE.")
return fVals[:], fObj[:]
def sel_fps_maxone(self, pop):
fitnessVals = [len(list(filter(lambda x: x=='1', pop[x]))) for x in range(0,self.popSize)]
self.totalFitness = sum(fitnessVals)
self.fitProbs = [float(x) /float(self.totalFitness) for x in fitnessVals]
self.ind = np.random.choice([x for x in range(0,self.popSize)],self.popSize, replace=True, p=self.fitProbs)
popNew = [pop[i] for i in self.ind]
self.fitnessVals = [len(list(filter(lambda x: x=='1', popNew[x]))) for x in range(0,self.popSize)]
return popNew
def sel_fps_ras(self, pop):
fitnessVals, ObjVals=self.fitness_ras(pop)
self.totalFitness = sum(ObjVals)
self.fitProbs = [float(x)/float(self.totalFitness) for x in ObjVals]
# print('haha')
self.ind = np.random.choice([x for x in range(0,len(pop))],len(pop), replace=True, p=self.fitProbs)
popNew = [pop[i] for i in self.ind]
self.fitnessVals, self.ObjVals=self.fitness_ras(popNew)
return popNew
def sel_ts_maxone(self, pop):
popNew=[]
for i in range(0,self.popSize):
memSel=[rnd.randint(0,self.popSize-1) for x in range(0,self.tsSize)]
fitVals=[len(list(filter(lambda x: x=='1', pop[x]))) for x in memSel]
maxFit= max(fitVals)
ind = memSel[np.where(np.asarray(fitVals)==maxFit)[0][0]]
popNew.append(pop[ind])
# print(fitVals,maxFit, memSel,self.tsSize)
self.fitnessVals = [len(list(filter(lambda x: x=='1', popNew[x]))) for x in range(0,self.popSize)]
#wait = input("PRESS ENTER TO CONTINUE.")
return popNew
def sel_ts_ras(self, pop):
popNew=[]
for i in range(0,self.popSize):
memSel=[rnd.randint(0,self.popSize-1) for x in range(0,self.tsSize)]
memb=[pop[x] for x in memSel]
fitVals, ObjVals=self.fitness_ras(memb)
maxFit= min(fitVals)
ind = memSel[np.where(np.asarray(fitVals)==maxFit)[0][0]]
popNew.append(pop[ind])
# print(fitVals,maxFit, memSel,self.tsSize)
self.fitnessVals, self.ObjVals=self.fitness_ras(popNew)
#wait = input("PRESS ENTER TO CONTINUE.")
return popNew
def mutate(self, chromosome):
newChromosome=[]
for x in chromosome:
if rnd.random() < self.pMut and x not in '.':
if len(self.alphabet) > 2:
diffSet = list(set(self.alphabet).difference(x))[0]
x = diffSet[rnd.randint(0, len(diffSet)-1)]
else:
x=list(set(self.alphabet).difference(x))[0]
newChromosome.append(x)
return newChromosome
def crossover(self, p1, p2):
off1=[]
off2=[]
crossA = []
if rnd.random()< self.pCross:
if self.kP > 0:
kPs = list({rnd.randint(0,self.length) for x in range(self.kP)})
crossA = kPs[:]
elif self.kP == 0:
kPs=[]
for x in p1:
if rnd.random() < self.pUCross:
kPs.append(rnd.randint(0,self.length))
crossA = list(set(kPs))
self.Kuni.append(len(crossA))
# print("kPS")
# print(kPs)
#if self.kP >0: print(crossA,len(crossA),self.Kuni, self.kP)
crossA.sort()
if(crossA and crossA[0] ==0):
p1,p2 = p2,p1
else:
crossA.insert(0,0)
if crossA[-1] != self.length: crossA.insert(len(kPs), self.length)
crossA.sort()
#self.Kuni=self.Kuni.append(len(crossA))
for i in range(len(crossA)-1):
indx1=crossA[i]
indx2=crossA[i+1]
if i % 2 ==0:
off1.extend(p1[indx1:indx2])
off2.extend(p2[indx1:indx2])
else:
off1.extend(p2[indx1:indx2])
off2.extend(p1[indx1:indx2])
if not off1:
off1=p1[:]
off2=p2[:]
return off1, off2
def elPop(self,pop,fVals):
elpop = []
temp1=[]
temp2=[]
fitVals = fVals[:]
if self.problem == 'maxone' : fitVals.sort(reverse=True)
if self.problem == 'ras' : fitVals.sort(reverse=False)
elChoose=fitVals[:self.eC]
for el in elChoose:
temp1.extend([fitVals[np.where(np.asarray(fVals)==el)[0][j]] for j in range(len(np.where(np.asarray(fVals)==el)[0]))])
temp2.extend([np.where(np.asarray(fVals)==el)[0][j] for j in range(len(np.where(np.asarray(fVals)==el)[0]))])
ind = temp2[0:self.eC]
elpop=[pop[i] for i in ind]
#print elpop
#print fitVals
#wait = input("PRESS ENTER TO CONTINUE.")
return elpop
def runMain(self):
self.old_pop=self.pop[:]
self.new_pop=self.fitnessFun(self.old_pop)
if self.problem == 'maxone':
self.bestChromosome.append(max(self.fitnessVals))
mem = self.new_pop[np.where(np.asarray(self.fitnessVals)==max(self.fitnessVals))[0][0]]
self.bestChromosomeBin.append(''.join(mem))
if self.problem == 'ras':
self.bestChromosome.append(min(self.fitnessVals))
mem = self.new_pop[np.where(np.asarray(self.fitnessVals)==max(self.fitnessVals))[0][0]]
self.bestChromosomeBin.append(gray_to_bin(''.join(mem)))
if self.eC > 0:
ind=rnd.randint(0,self.popSize)
self.new_pop[ind:ind+self.eC]=self.elPop(self.old_pop,self.fitnessVals)
cnt=0
while True:
# self.new_pop=self.fitnessFun(self.old_pop)
# self.bestChromosome.append(max(self.fitnessVals))
cnt=cnt+1
# print(cnt)
self.fitAll.append(self.fitnessVals)
if self.problem == 'maxone' or self.problem == 'ras':
if cnt == self.maxGen: #or max(self.fitnessVals)==self.length:
break
crosMates = [(i,i+1) for i in range(0,self.popSize,2)]
for i in range(len(crosMates)):
m = crosMates[i][0]
p = crosMates[i][1]
self.new_pop[m], self.new_pop[p] = self.crossover(self.new_pop[m],self.new_pop[p])
for i in range(0,self.popSize):
self.new_pop[i]=self.mutate(self.new_pop[i])
self.old_pop= self.new_pop[:]
self.new_pop=self.fitnessFun(self.new_pop)
#if self.problem == 'maxone':
# self.fitnessVals = [len(list(filter(lambda x: x=='1', self.new_pop[x]))) for x in range(0,self.popSize)]
# if self.problem == 'ras':
if self.eC > 0:
ind=rnd.randint(0,self.popSize)
self.new_pop[ind:ind+self.eC]=self.elPop(self.new_pop,self.fitnessVals)
if self.problem == 'maxone':
self.bestChromosome.append(max(self.fitnessVals))
mem = self.new_pop[np.where(np.asarray(self.fitnessVals)==max(self.fitnessVals))[0][0]]
self.bestChromosomeBin.append(''.join(mem))
if self.problem == 'ras':
self.bestChromosome.append(min(self.fitnessVals))
mem = self.new_pop[np.where(np.asarray(self.fitnessVals)==min(self.fitnessVals))[0][0]]
#self.bestChromosomeBin.append(''.join(mem))
self.bestChromosomeBin.append(gray_to_bin(''.join(mem)))