PV226 ML: Genetic Algorithms

Content of this session

Explanation of Genetic algorithms

Steps in genetic algorithms

Framework DEAP

So what is genetic algoritm?

  • member of a group called Evolutionary Algorithms
  • inspired by the natural process of selection
  • use concept of apopulation where individuals are mating and mutate to generate offsprings

No Free Launch Theorem

Common terms

population

individual (chromosome)

gene

Algorithm

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

Initial population

usually is randomly generated

does not have to fulfill all criteria

 

alternative approaches:

load existing solution

only feasible solution in first generation

(drawing placeholder)

Selection

  • roulette wheel selection
  • turn-based selection
  • elitism selection
  • tournament selection

 

and many others

(drawing placeholder)

Mating

  • one-point crossover
  • two-point crossover
  • uniform
  • ordered cross over
  • ...

(drawing placeholder)

Mutation

  • gaussian
  • flip
  • uniform
  • shuffle
  • ...

(drawing placeholder)

Next generation

 * apply selection again *

keep population size the same

expanding population and then reducing

offsprings only

half parents / half offsprings

DEAP

probably the best Python framework to play with genetic algorithms

Creating individual

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)

Simple EA

from deap import algorithms

algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=50)

Toolbox

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)

Writing algorithm

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

Or using varAnd

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

Toolbox

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()

Any questions?

Alternatives

  • Evolution Strategies
  • Differential Evolution
  • Particle Swarm Optimization
  • Q-Learning (Deep Q Learning)
Made with Slides.com