-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcROSSOVER.py
executable file
·46 lines (40 loc) · 1.33 KB
/
cROSSOVER.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
#!/usr/bin/env python3
import numpy as np
## ===================================== ##
# Take 2 individuals in the population
# and then interchange 2 parts from 2
# individuals to create 2 new individuals
## ===================================== ##
'''
Parameters:
In: parent1 : (nr_binary_var, )
parent2 : (nr_binary_var, )
nr_binary_var : scalar value is 0 or 1
prob_crossover: scalar between (0, 1)
Out: child1 : (nr_binary_var, )
child2 : (nr_binary_var, )
'''
def crossover(parent1,parent2,nr_binary_var,prob_crossover):
np.random.seed()
point_xover=int(np.rint(np.random.rand()*nr_binary_var))
first_part=parent1[0:point_xover]
second_part=parent2[point_xover:]
child1=np.concatenate([first_part,second_part])
first_part=parent2[0:point_xover]
second_part=parent1[point_xover:]
child2=np.concatenate([first_part,second_part])
if np.random.rand()>prob_crossover:
child1=parent1
if np.random.rand()>prob_crossover:
child2=parent2
return child1, child2
'''
# Verify function crossover
prob_crossover=0.85
nr_binary_var=10
parent1=np.array(np.arange(0,nr_binary_var))
parent2=np.array(np.arange(10,2*nr_binary_var))
print('Two parents are:\n ', parent1,'\n',parent2)
child1, child2 = crossover(parent1,parent2,nr_binary_var,prob_crossover)
print('Two new child are:\n ', child1,'\n', child2)
'''