-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.py
More file actions
234 lines (189 loc) · 5.83 KB
/
Copy pathtest.py
File metadata and controls
234 lines (189 loc) · 5.83 KB
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
from numpy.random import rand
import numpy as np
from multiprocessing import Pool
import time
import gc
ele_list="ABCDEFGHIJKLMNOPQRSTUVWXYZ "
target="ALPHA CODE X LOAD TEST MAGIC K"*3
#target="ABCDE"
tlen=len(target)
tlen
min_fitness_val=10
#print(dnac(25))
def gfitness_cal(dna):
f = 0
for i in range(tlen):
if (target[i] == dna[i]):
f =f + 1
return f
class gene_func:
def to_char(self, x):
return ele_list[x]
def dnac(self):
return ''.join(list(map(self.to_char, list(map(int, rand(tlen)*27)))))
class gene(gene_func):
#fitness calculation function
def fitness_cal(self):
f = 0
for i in range(tlen):
if (target[i] == self.dna[i]):
f =f + 1
return f
#manulaly create a DNA
def __init__(self,st=None,fit=None):
if st==None:
self.dna = self.dnac()
else:
self.dna = st
if fit==None:
self.fit = self.fitness_cal()
else:
self.fit=fit
#DNA mutation
def mutation(self):
pass
#function for crossover
def cross_over(self):
pass
class Population:
def __init__(self):
self.arr=[]
#minimum required fitness
min_fit=min_fitness_val
#max fitnss return
def cross_connect(self):
amt=int(len(self.arr)/2)
i=0
while i<amt:
try:
g=self.arr[i]
sendc(g.dna)
self.arr[i]=gene(recc())
i+=1
except:
print("error in sockets")
def max_fitness(self):
maxx=0
for i in self.arr:
if i.fit>maxx:
maxx=i.fit
return maxx
#create a new population
def create_population(self,pop_size):
self.arr=[]
i=0
while i<pop_size:
v=gene()
if v.fit>=self.min_fit:
self.arr+=[v]
i+=1
if(i%5==0):
print(i," -- ",v.dna," --- ",v.fit)
def get_gene(self,pop,old_max):
size=len(pop.arr)
min_fitness=int(rand(1)[0]*old_max)
for i in range(1000):
selected_gene=int(rand(1)[0]*size)
#check if the dna quality matches the required mark or the max no of attempts have passed
if pop.arr[selected_gene].fit>=min_fitness or i>200:
# if i>200:
# print("dna quality not found")
return pop.arr[selected_gene].dna
def cross_over_monte(self,old_pop,old_max,st,ed):
dna1=self.get_gene(old_pop,old_max)
dna2=self.get_gene(old_pop,old_max)
#val=int(rand(1)*(tlen-1))
val=int(rand(1)[0]*(ed-st)+st)
return([dna1[:val]+dna2[val:],dna2[:val]+dna1[val:]])
#create a population from crossover
def cross_popx(self,varrs):
#print(pop_size)
sta=time.time()
pop_size,old_pop,max_old,ind,tot=varrs
#print("processor ",ind)
perp=len(old_pop.arr[0].dna)/tot
st=ind*perp
ed=st+perp
#arr=old_pop.arr
carr=[]
i=0
while i<pop_size:
vx=self.cross_over_monte(old_pop,max_old,st,ed)
v=gene(vx[0])
if v.fit>=self.min_fit:
carr+=[v]
i+=1
#print(v.dna," --- ",v.fit)
v=gene(vx[1])
if v.fit>=self.min_fit:
carr+=[v]
i+=1
#print(v.dna," --- ",v.fit)
if ind==0:
print("\ntotal time=",(time.time()-sta))
#print("processor ",ind)
#print(carr)
#print(carr[0].dna)
return carr
def cross_pop(self,pop_size,old_pop,nop=4):
max_old=old_pop.max_fitness()+1
jobs=[]
# for k in range(nop):
# p=mp(self.cross_popx(int(pop_size/nop),old_pop,max_old,k))
# jobs.append(p)
# p.start()
# for proc in jobs:
# proc.join()
p = Pool(nop)
#workers = [p.apply_async(self.cross_popx, args=(int(pop_size/nop),old_pop,max_old,i,nop)) for i in range(nop)]
#for w in workers:
# w.get()
values=p.map(self.cross_popx, [[int(pop_size/nop),old_pop,max_old,i,nop] for i in range(nop)])
#print(len(values[0]))
for i in values:
self.arr+=i
for i in range(0,len(self.arr)):
#print(self.arr[i].dna," ---------- ",self.arr[i].fit)
if self.arr[i].dna==target:
print(self.arr[i].dna)
return (1)
#print(self.arr[i].)
#print(len(pro_dict))
return (0)
#mote carlo method of crossover rejection samaling
def get_unique(p):
ar=[]
for i in p.arr:
ar+=[i.dna]
return(len(set(ar))/len(ar))
if __name__ == '__main__':
print("input population size")
psize=int(input())
cops=Population()
cops.create_population(psize)
while True:
print("enter the number of Population and no of processor to test with")
cr=int(input())
nop=int(input())
print("\n-----------------start-----------------\n")
cop=cops
st=time.time()
for i in range(cr):
print("----------crossover ",i,"---------")
copn=Population()
# k=copn.cross_pop(psize,cop)
print(get_unique(cop))
#print("before-->",len(cop.arr))
k=copn.cross_pop(psize,cop,nop)
print(copn.max_fitness())
#print(get_unique(copn))
#print("after-->",len(cop.arr))
print("printing length of ",len(copn.arr))
if k==1:
print("found")
break
del(cop)
cop=copn
gc.collect()
#print("\ntotal time=",(time.time()-st))
print("\n-----------------end-------------------\n\n")