I'm building parent selection of genetic algorithm with roulette wheel in python.
acc['left']
is the left boundary and acc['right']
is the right boundary of probability cumulative for each individu. rw
is random numbers of roulette wheel, while n_rw
is the the number of rw
.
This is the acc
dataframe:
accuracy rank prob_fitness left right
0 0.825152 6.0 0.109091 0.000000 0.109091
1 0.839545 9.0 0.163636 0.109091 0.272727
2 0.807727 2.5 0.045455 0.272727 0.318182
3 0.840000 10.0 0.181818 0.318182 0.500000
4 0.807727 2.5 0.045455 0.500000 0.545455
5 0.820152 4.0 0.072727 0.545455 0.618182
6 0.832576 8.0 0.145455 0.618182 0.763636
7 0.821364 5.0 0.090909 0.763636 0.854545
8 0.802727 1.0 0.018182 0.854545 0.872727
9 0.829091 7.0 0.127273 0.872727 1.000000
And this is the rw
:
'array([ 0.89676, 0.8007 , 0.35212, 0.08043, 0.51044, 0.61213, 0.3392 , 0.96687, 0.2554 , 0.97215])'
I'm trying to determine which one will be the parent candidate with this code. But it does't work.
acc['parent'] = np.zeros(pop_size)
o = 0
b = 0
while o < pop_size:
o = o+1
while b < n_rw:
acc['parent'] = (rw[b] > acc['left'][o] & rw[b] <= acc['right'][o])
if acc['parent'] == True:
b = n_rw
else:
b = b+1
acc
It results in:
TypeError: unsupported operand type(s) for &: 'numpy.float64' and 'numpy.float64'
Can you help me please? Thanks in advance
In your case, something like this should do the job:
acc['parent'] = np.full(pop_size, False, dtype=bool)
o = 0
while o < pop_size:
b = 0
while b < n_rw:
acc.loc[o,'parent'] = ((rw[b] > acc['left'][o]) & (rw[b] <= acc['right'][o]))
Now you are writing into the DataFrame and actually changing the values that were previously initiated.
if acc.loc[o,'parent'] == True:
break
else:
b = b+1
o = o+1
print acc
However, I'm not sure what your code is doing.