64
Evolutionary Algorithms: Perfecting the Art of “Good Enough” Liz Sander

Evolutionary Algorithms: Perfecting the Art of "Good Enough"

  • Upload
    pydata

  • View
    118

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

Evolutionary Algorithms:Perfecting the Art of “Good

Enough”

Liz Sander

Page 2: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

Source: wikipedia.org

Page 3: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

Source: fishbase.org

Page 4: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

Source: youtube.com

Page 5: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

Sometimes, we can’t find the best solution.

But most of the time, we don’t need to.

Let’s focus on finding an answer that’s goodenough!

Page 6: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

Sometimes, we can’t find the best solution.

But most of the time, we don’t need to.

Let’s focus on finding an answer that’s goodenough!

Page 7: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

Sometimes, we can’t find the best solution.

But most of the time, we don’t need to.

Let’s focus on finding an answer that’s goodenough!

Page 8: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

Evolutionary Algorithms

Page 9: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

Evolutionary Algorithms

Page 10: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

“Heuristic Optimizers with a RandomComponent”

Page 11: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

“Heuristic Optimizers with a RandomComponent”

Page 12: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

Heuristic:

Optimizer:

Random Component:

Page 13: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

Heuristic: Rule of thumb

Optimizer:

Random Component:

Page 14: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

Heuristic: Rule of thumb

Optimizer: Maximizing/minimizing a function(objective function, cost function, fitnessfunction)

Random Component:

Page 15: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

Heuristic: Rule of thumb

Optimizer: Maximizing/minimizing a function(objective function, cost function, fitnessfunction)

Random Component: Non-deterministic

Page 16: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

WHY HEURISTIC?

There are methods that guarantee we find thetrue optimum...

if you meet the assumptions.

Gradient descent:

• Convex• Differentiable

Page 17: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

WHY HEURISTIC?

There are methods that guarantee we find thetrue optimum... if you meet the assumptions.

Gradient descent:

• Convex• Differentiable

Page 18: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

WHY HEURISTIC?

There are methods that guarantee we find thetrue optimum... if you meet the assumptions.

Gradient descent:

• Convex• Differentiable

Page 19: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

WHY HEURISTIC?

There are methods that guarantee we find thetrue optimum... if you meet the assumptions.

Gradient descent:

• Convex

• Differentiable

Page 20: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

WHY HEURISTIC?

There are methods that guarantee we find thetrue optimum... if you meet the assumptions.

Gradient descent:

• Convex• Differentiable

Page 21: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

WHY HEURISTIC?

Page 22: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

WHY HEURISTIC?

Page 23: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

WHAT ARE WE OPTIMIZING?

• Often high-dimensional (many inputs, oneoutput)

• Nearby solutions are of similar quality• USPS: Minimize distance• Zebrafish scheduling: Minimize conflicts• Skyrim looting: Maximize value

Page 24: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

WHAT ARE WE OPTIMIZING?

• Often high-dimensional (many inputs, oneoutput)

• Nearby solutions are of similar quality

• USPS: Minimize distance• Zebrafish scheduling: Minimize conflicts• Skyrim looting: Maximize value

Page 25: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

WHAT ARE WE OPTIMIZING?

• Often high-dimensional (many inputs, oneoutput)

• Nearby solutions are of similar quality• USPS: Minimize distance

• Zebrafish scheduling: Minimize conflicts• Skyrim looting: Maximize value

Page 26: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

WHAT ARE WE OPTIMIZING?

• Often high-dimensional (many inputs, oneoutput)

• Nearby solutions are of similar quality• USPS: Minimize distance• Zebrafish scheduling: Minimize conflicts

• Skyrim looting: Maximize value

Page 27: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

WHAT ARE WE OPTIMIZING?

• Often high-dimensional (many inputs, oneoutput)

• Nearby solutions are of similar quality• USPS: Minimize distance• Zebrafish scheduling: Minimize conflicts• Skyrim looting: Maximize value

Page 28: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

FITNESS FUNCTION

Weaver & Knight 2014

Page 29: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

FITNESS FUNCTION# example inputssolution = [1, 0, 1, 1, 0]weights = [1, 2, .5, 4, 1] #fixedvalues = [40, 25, 10, 30, 15] #fixedmax_weight = 5

def Fitness(knapsack, weights, values, max_weight):’’’Calculate the fitness of a knapsack of items.’’’tot_weight = 0tot_value = 0for i, item in enumerate(knapsack):

if item:tot_weight += weights[i]tot_value += values[i]

if tot_weight > max_weight:return 0

else:return tot_value

Page 30: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

HILL CLIMBER

Page 31: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

HILL CLIMBER

Page 32: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

HILL CLIMBER

Page 33: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

HILL CLIMBER

X

Page 34: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

HILL CLIMBER

Page 35: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

HILL CLIMBER

Page 36: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

HILL CLIMBER

Page 37: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

HILL CLIMBER

• Gets stuck in local optima• Fast!• Almost no tuning

Page 38: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

WHY HEURISTIC?

Page 39: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

HILL CLIMBER: INITIALIZATION

import random

def InitializeSol(items):’’’Random starting knapsack.items: int, number of items in knapsack.’’’knapsack = [0] * itemsfor i in range(len(knapsack)):

knapsack[i] = random.randint(0,1)return knapsack

Page 40: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

HILL CLIMBER: MUTATION

import randomimport copy

def Mutate(knapsack):’’’Mutate a solution by flipping one bit.’’’toSwap = random.randint(0, len(knapsack)-1)if knapsack[toSwap] == 0:

knapsack[toSwap] = 1else:

knapsack[toSwap] = 0return knapsack

Page 41: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

HILL CLIMBERimport randomfrom Initialize import InitializeSolfrom Fitness import Fitnessfrom Mutate import Mutate

def HillClimber(steps, weights, values, max_wt, seed):random.seed(seed) # reproducibility!best = InitializeSol(len(weights))bestFit = Fitness(best, weights, values, max_wt)for i in range(steps):

# take a stepcandidate = Mutate(best)candidateFit = Fitness(candidate, weights,

values, max_wt)if candidateFit > bestFit:

best = candidatebestFit = candidateFit

return best

Page 42: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

SIMULATED ANNEALING

Hill climbing with a changing temperature

Temperature: probability of accepting a bad stepHot: accept many bad steps (more random)Cold: accept fewer bad steps (less random)

Hot Cold

Random Walk Hill Climber

Page 43: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

SIMULATED ANNEALING

Hill climbing with a changing temperature

Temperature: probability of accepting a bad stepHot: accept many bad steps (more random)Cold: accept fewer bad steps (less random)

Hot Cold

Random Walk Hill Climber

Page 44: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

SIMULATED ANNEALING

Hill climbing with a changing temperature

Temperature: probability of accepting a bad stepHot: accept many bad steps (more random)Cold: accept fewer bad steps (less random)

Hot Cold

Random Walk Hill Climber

Page 45: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

SIMULATED ANNEALING

Page 46: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

SIMULATED ANNEALING

Page 47: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

SIMULATED ANNEALING

Page 48: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

SIMULATED ANNEALING

• Exploration and exploitation• Still very fast• More tuning: cooling schedules, reheating,

and variants

Page 49: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

TUNING

It’s hard.

Do a grid search probably.

Page 50: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

TUNING

It’s hard.

Do a grid search probably.

Page 51: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

TUNING

It’s hard.

Do a grid search probably.

Page 52: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

EVOLUTIONARY ALGORITHMS

Population

Page 53: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

EVOLUTIONARY ALGORITHMS

1.Selection

Page 54: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

EVOLUTIONARY ALGORITHMSTournament selection: choose n candidates. Thebest becomes a parent.

import random

fits = [65, 2, 0, 30] #list of fitnessestournamentsize = 2 # candidates in tournament

def Select(fits, tournamentsize):’’’Choose an individual to reproduce by having themrandomly compete in a given size tournament.’’’solutions = len(fits)competitors = random.sample(range(solutions),

tournamentsize)compFits = [fits[i] for i in competitors]# get the index of the best competitorwinner = competitors[compFits.index(max(compFits))]return winner

Page 55: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

EVOLUTIONARY ALGORITHMS

1.Selection

2.Mutation/Recombination

Page 56: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

EVOLUTIONARY ALGORITHMS

1.Selection

2.Mutation/Recombination

3.Repopulation

Page 57: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

EVOLUTIONARY ALGORITHMS

1.Selection

2.Mutation/Recombination

3.Repopulation

Page 58: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

EVOLUTIONARY ALGORITHMS

• Pros:• Unlikely to get stuck in a single local optimum• Can explore lots of areas at once• Biology connection is pretty cool!

• Cons:• Can lose variation quickly• More tuning: selection,

mutation/recombination, selection strength,population size, mutation size

• Slow• Memory-hungry

Page 59: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

ALGORITHM ROUND-UP

• HC: fast but gets stuck easily

• SA: fast-ish, can explore better• EA: slow, memory-hungry, potentially very

powerful• Metropolis-coupled MCMC (my personal

favorite): several parallel searches atdifferent (constant) temperatures, allowthem to swap every so often

Page 60: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

ALGORITHM ROUND-UP

• HC: fast but gets stuck easily• SA: fast-ish, can explore better

• EA: slow, memory-hungry, potentially verypowerful

• Metropolis-coupled MCMC (my personalfavorite): several parallel searches atdifferent (constant) temperatures, allowthem to swap every so often

Page 61: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

ALGORITHM ROUND-UP

• HC: fast but gets stuck easily• SA: fast-ish, can explore better• EA: slow, memory-hungry, potentially very

powerful

• Metropolis-coupled MCMC (my personalfavorite): several parallel searches atdifferent (constant) temperatures, allowthem to swap every so often

Page 62: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

ALGORITHM ROUND-UP

• HC: fast but gets stuck easily• SA: fast-ish, can explore better• EA: slow, memory-hungry, potentially very

powerful• Metropolis-coupled MCMC (my personal

favorite): several parallel searches atdifferent (constant) temperatures, allowthem to swap every so often

Page 63: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

WHAT NEXT?

• papers/books on optimization• for discrete problems, “combinatorial

optimization”• other EAs:

• differential evolution• evolutionary strategies• genetic programming• ALPS

Page 64: Evolutionary Algorithms: Perfecting the Art of "Good Enough"

WHAT NEXT?

• How have I used these?• Generating stable food webs• Identifying similar species (parasites, top

predators) in an ecological system

• code: github.com/esander91/GoodEnoughAlgs

• blog: lizsander.com