In: Computer Science
import numpy as np
def biochild(m, biomother, biofather):
# child is generated by randomly combining part of the child's information ??????h?? and ??????h??.
n = len(biomother)
# randomly choose b from 1 to n
b = np.random.randint(1, n)
# The first part of the child list will be made up of the first ? bits of ??????h?? and the second part by the last ? - ? bits of the ??????h??.
child = []
for i in range(b):
child.append(biomother[i])
for i in range(b, n):
child.append(biofather[i])
# After generating child, each bit in the list is considered for mutation.
# For each bit of child, with probability ? the bit is “mutated” by
for i in range(n):
# being replaced by its inverse (If the bit is 0 it is replaced by 1, and if it is 1 it is replaced by 0).
p = np.random.uniform(0, 1)
if p <= m:
if child[i] == 0:
child[i] = 1
if child[i] == 1:
child[i] = 0
# Finally, the function returns the list child.
return child
# For example: ??????h?? = [1,0,0,1,0,1] and ??????h?? = [1,1,1,0,0,1]
print(biochild(0.5, [1, 0, 0, 1, 0, 1], [1, 1, 1, 0, 0, 1]))
.
Screenshot:
Output:
.