def point_mutation(individual, random_state=None):
"""
Randomly pick a gene in individual and mutate it.
The mutation is either rewiring, i.e. changing the inputs, or changing the operator (head of gene)
:param individual: instance of Base
:type individual: instance of Cartesian
:param random_state: an instance of np.random.RandomState, a seed integer or None
:return: new instance of Base
"""
random_state = check_random_state(random_state)
n_terminals = len(individual.pset.terminals)
i = random_state.randint(n_terminals, len(individual) - 1)
el, c, r, l = individual.mapping[i]
gene = l[r]
if isinstance(gene, list):
new_gene = gene[:]
j = random_state.randint(0, len(gene))
if j == 0: # function
new_j = individual.pset.imapping[random_state.choice(individual.pset.operators)]
else: # input
new_j = random_state.choice(individual._valid_inputs[i])
new_gene[j] = new_j
else: # output gene
new_gene = random_state.randint(0, len(individual) - individual.n_out - 1)
new_individual = copy.copy(individual)
new_individual[i] = new_gene
return new_individual
评论列表
文章目录