Commit 61a8d4e5 authored by Grégoire Grzeczkowicz's avatar Grégoire Grzeczkowicz
Browse files

Bit genetic with split

parent df3986f5
......@@ -140,7 +140,7 @@ def neighb_square(sol, scale, domain_width):
# Crossover
########################################################################
def crossover(sol1, sol2):
def rand_crossover(sol1, sol2):
x = []
y = []
sol = sol1 + sol2
......@@ -157,3 +157,37 @@ def crossover(sol1, sol2):
domain[y[i]][x[i]] = 1
return domain
def split_crossover(sol1, sol2):
p1 = np.array(to_sensors(sol1))
p2 = np.array(to_sensors(sol2))
sol = np.concatenate((p1,p2))
x = sol[::2]
m = np.median(x)
r = []
for p in p1:
if p[0] <= m:
r.append(p)
for p in p2:
if p[0] > m:
r.append(p)
if len(r) < len(p1):
q = []
for p in p1:
if p[0] > m:
q.append(p)
for p in p2:
if p[0] <= m:
q.append(p)
for i in np.random.randint(0,len(p),(len(p1)-len(r))):
r.append(q[i])
while len(r) > len(p1):
r.pop(np.random.randint(0,len(r)))
rmatrix = np.zeros((len(sol1),len(sol1)))
for p in r:
rmatrix[p[0],p[1]] = 1
return rmatrix
......@@ -148,18 +148,23 @@ def split_crossover(sol1, sol2):
r.append(sol2[2*i+1])
if len(r) < len(sol1):
p = []
q = []
for i in range(len(sol1)//2):
if sol1[2*i] > m:
p.append(sol1[2*i])
p.append(sol1[2*i+1])
q.append(sol1[2*i])
q.append(sol1[2*i+1])
for i in range(len(sol2)//2):
if sol2[2*i] <= m:
p.append(sol2[2*i])
p.append(sol2[2*i+1])
for i in np.random.randint(0,len(p)//2,(len(sol1)-len(r))//2):
r.append(p[2*i])
r.append(p[2*i+1])
q.append(sol2[2*i])
q.append(sol2[2*i+1])
for i in np.random.randint(0,len(q)//2,(len(sol1)-len(r))//2):
r.append(q[2*i])
r.append(q[2*i+1])
while len(r) > len(sol1):
i = np.random.randint(0,len(r)//2)
r.pop(2*i)
r.pop(2*i)
return r
......@@ -195,7 +195,7 @@ if __name__=="__main__":
make.init(bit.unif,
domain_width = the.domain_width,
nb_sensors = the.nb_sensors),
bit.crossover,
bit.split_crossover,
make.neig(bit.neighb_square,
scale = the.variation_scale,
domain_width = the.domain_width),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment