Skip to content

Instantly share code, notes, and snippets.

@cmd-ntrf
Created March 6, 2012 20:25
Show Gist options
  • Save cmd-ntrf/1988770 to your computer and use it in GitHub Desktop.
Save cmd-ntrf/1988770 to your computer and use it in GitHub Desktop.
varOr + numpy multinomial
def varOr(toolbox, population, lambda_, cxpb, mutpb):
assert (cxpb + mutpb) <= 1.0, ("The sum of the crossover and mutation "
"probabilities must be smaller or equal to 1.0.")
ncx, nmut, nrep = numpy.random.multinomial(lambda_, (cxpb, mutpb, 1-cxpb-mutpb))
offcx = map(toolbox.clone, tools.selRandom(population, 2*ncx))
offmut = map(toolbox.clone, tools.selRandom(population, nmut))
offrep = map(toolbox.clone, tools.selRandom(population, nrep))
for ind1, ind2 in offcx:
toolbox.mate(ind1, ind2)
del ind1.fitness.values, ind2.fitness.values
offcx = offcx[::2]
for ind in offmut:
toolbox.mutate(ind)
del ind.fitness.values
return random.shuffle(offcx + offmut + offrep)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment