def crossover(self, ind):
"""Used by the evolution process to generate a new individual.
Notes
-----
This is a tweaked version of the classical DE crossover
algorithm, the main difference that candidate parameters are
generated using a lognormal distribution. Bound handling is
achieved by resampling where the candidate solution exceeds +/-1
Parameters
----------
Returns
-------
y: deap individual
An individual representing a candidate solution, to be
assigned a fitness.
"""
if self._params['neighbours']:
a, b, c = random.sample([self.population[i]
for i in ind.neighbours], 3)
else:
a, b, c = random.sample(self.population, 3)
y = self.toolbox.clone(a)
y.ident = ind.ident
y.neighbours = ind.neighbours
del y.fitness.values
# y should now be a copy of ind with the vector elements from a
ident = random.randrange(len(self._params['value_means']))
for i, value in enumerate(y):
if i == ident or random.random() < self._params['cxpb']:
entry = a[i] + random.lognormvariate(-1.2, 0.5) * \
self._params['diff_weight'] * (b[i] - c[i])
tries = 0
while abs(entry) > 1.0:
tries += 1
entry = a[i] + random.lognormvariate(-1.2, 0.5) * \
self._params['diff_weight'] * (b[i] - c[i])
if tries > 10000:
entry = a[i]
y[i] = entry
return y
评论列表
文章目录