Explanation of Genetic algorithms
Steps in genetic algorithms
Framework DEAP
population
individual (chromosome)
gene
generate initial popolutaion evaluate initial population (Selection) select parents for mating (Mating)take parent and generate children
(Mutation)mutate selected individual or offspring
replace individuals with offsprings
usually is randomly generated
does not have to fulfill all criteria
alternative approaches:
load existing solution
only feasible solution in first generation
(drawing placeholder)
and many others
(drawing placeholder)
(drawing placeholder)
(drawing placeholder)
* apply selection again *
keep population size the same
expanding population and then reducing
offsprings only
half parents / half offsprings
probably the best Python framework to play with genetic algorithms
import random
from deap import base
from deap import creator
from deap import tools
IND_SIZE = 5
creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0))
creator.create("Individual", list, fitness=creator.FitnessMin)
toolbox = base.Toolbox()
toolbox.register("attr_float", random.random)
toolbox.register("individual", tools.initRepeat, creator.Individual,
toolbox.attr_float, n=IND_SIZE)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
ind = toolbox.individual()
pop = toolbox.population(n=50)
from deap import algorithms
algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=50)
from deap import base
from deap import tools
toolbox = base.Toolbox()
def evaluateInd(individual):
# Do some computation
return result,
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.2)
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("evaluate", evaluateInd)
for g in range(NGEN):
# Select the next generation individuals
offspring = toolbox.select(pop, len(pop))
# Clone the selected individuals
offspring = map(toolbox.clone, offspring)
# Apply crossover on the offspring
for child1, child2 in zip(offspring[::2], offspring[1::2]):
if random.random() < CXPB:
toolbox.mate(child1, child2)
del child1.fitness.values
del child2.fitness.values
# Apply mutation on the offspring
for mutant in offspring:
if random.random() < MUTPB:
toolbox.mutate(mutant)
del mutant.fitness.values
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
# The population is entirely replaced by the offspring
pop[:] = offspring
from deap import algorithms
for g in range(NGEN):
# Select and clone the next generation individuals
offspring = map(toolbox.clone, toolbox.select(pop, len(pop)))
# Apply crossover and mutation on the offspring
offspring = algorithms.varAnd(offspring, toolbox, CXPB, MUTPB)
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
# The population is entirely replaced by the offspring
pop[:] = offspring
for g in range(NGEN):
# Select the next generation individuals
offspring = toolbox.select(pop, len(pop))
# Clone the selected individuals
offspring = map(toolbox.clone, offspring)
# Apply crossover on the offspring
for child1, child2 in zip(offspring[::2], offspring[1::2]):
if random.random() < CXPB:
toolbox.mate(child1, child2)
del child1.fitness.values
del child2.fitness.values
# Apply mutation on the offspring
for mutant in offspring:
if random.random() < MUTPB:
toolbox.mutate(mutant)
del mutant.fitness.values
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
# The population is entirely replaced by the offspring
pop[:] = offspring
Initialization | Crossover | Mutation | Selection |
---|---|---|---|
initRepeat() | cxOnePoint() | mutGaussian() | selTournament() |
initIterate() | cxTwoPoint() | mutShuffleIndexes() | selRoulette() |
initCycle() | cxUniform() | mutFlipBit() | selNSGA2() |
cxPartialyMatched() | mutPolynomialBounded() | selNSGA3() | |
cxUniformPartialyMatched() | mutUniformInt() | selSPEA2() | |
cxOrdered() | mutESLogNormal() | selRandom() | |
cxBlend() | selBest() | ||
cxESBlend() | selWorst() | ||
cxESTwoPoint() | selTournamentDCD() | ||
cxSimulatedBinary() | selDoubleTournament() | ||
cxSimulatedBinaryBounded() | selStochasticUniversalSampling() | ||
cxMessyOnePoint() | selLexicase() | ||
selEpsilonLexicase() | |||
selAutomaticEpsilonLexicase() |