99,9% of all species(!) ever existed died out. The other 0,1% is about to do the same. But image if all those guys would still exist. Would be a fairly uncomfortable crowd, wouldn‘t it?
99,9% of all species(!) ever existed died out. The other 0,1% is about to do the same. But image if all those guys would still exist. Would be a fairly uncomfortable crowd, wouldn‘t it?
99,9% of all species(!) ever existed died out. The other 0,1% is about to do the same. But image if all those guys would still exist. Would be a fairly uncomfortable crowd, wouldn‘t it?
acceptable = certain fitness, boundary = n generations, best individual didnt improve for n generations steady improvement of the fittest individuals
acceptable = certain fitness, boundary = n generations, best individual didnt improve for n generations steady improvement of the fittest individuals
acceptable = certain fitness, boundary = n generations, best individual didnt improve for n generations steady improvement of the fittest individuals
Evolutionary Algorithms In Ruby - Presentation Transcript
Evolutionary
Algorithms
in Ruby
Julian Fischer
fischer@enterprise-rails.de
http://www.enterprise-rails.de
Introduction
Introduction
About me
About me
Julian Fischer
‣Twitter: http://www.twitter.com/railshoster
‣E-Mail: fischer@enterprise-rails.de
About me
Julian Fischer
‣Twitter: http://www.twitter.com/railshoster
‣E-Mail: fischer@enterprise-rails.de
‣ CEO of Avarteq GmbH
About me
Julian Fischer
‣Twitter: http://www.twitter.com/railshoster
‣E-Mail: fischer@enterprise-rails.de
‣ CEO of Avarteq GmbH
‣ Lecturer „Ruby on Rails“ @ HTWdS
About me
Julian Fischer
‣Twitter: http://www.twitter.com/railshoster
‣E-Mail: fischer@enterprise-rails.de
‣ CEO of Avarteq GmbH
‣ Lecturer „Ruby on Rails“ @ HTWdS
‣ Ruby und Ruby on Rails programmer
About me
Julian Fischer
‣Twitter: http://www.twitter.com/railshoster
‣E-Mail: fischer@enterprise-rails.de
‣ CEO of Avarteq GmbH
‣ Lecturer „Ruby on Rails“ @ HTWdS
‣ Ruby und Ruby on Rails programmer
‣ Entperise-Rails.de - Head of Hosting
Introduction
About Avarteq GmbH
About Avarteq GmbH
About Avarteq GmbH
‣ Founded in Nov. 2008
from two existing companies.
About Avarteq GmbH
‣ Founded in Nov. 2008
from two existing companies.
‣ Involvment of Key-Systems GmbH
manages ~2,5 * 10^6 domains for customers of 200+ countries.
About Avarteq GmbH
‣ Founded in Nov. 2008
from two existing companies.
‣ Involvment of Key-Systems GmbH
manages ~2,5 * 10^6 domains for customers of 200+ countries.
‣ Team size: 14 people
8 full-time, 6 part-time/freelancer
Introduction
Portfolio
About Avarteq GmbH
About Avarteq GmbH
‣ Covers all stages of web development
About Avarteq GmbH
‣ Covers all stages of web development
‣ Consulting
About Avarteq GmbH
‣ Covers all stages of web development
‣ Consulting
‣ Conceptual ~ and screen design
About Avarteq GmbH
‣ Covers all stages of web development
‣ Consulting
‣ Conceptual ~ and screen design
‣ Ruby&onplace. development.
In house in
Rails
About Avarteq GmbH
‣ Covers all stages of web development
‣ Consulting
‣ Conceptual ~ and screen design
‣ Ruby&onplace. development.
In house in
Rails
‣ Ruby on Rails
hosting/servers/clusters
RailsHoster.de - Enterprise-Rails.de
Evolution
Evolution
Evolution
Evolution
‣ Population of individuals (organisms)
Evolution
‣ Population of individuals (organisms)
‣ Genes are passed from generation to
generation.
Evolution
‣ Population of individuals (organisms)
‣ Genes are passed from generation to
generation.
‣ Slightgeneration to thein genetic material
from one
changes
next.
Evolution
‣ Population of individuals (organisms)
‣ Genes are passed from generation to
generation.
‣ Slightgeneration to thein genetic material
from one
changes
next.
‣ Differences accumulate over time.
Evolution
‣ Population of individuals (organisms)
‣ Genes are passed from generation to
generation.
‣ Slightgeneration to thein genetic material
from one
changes
next.
‣ Differences accumulate over time.
‣ New properties or even species emerge.
Evolution
Genetic Operations
Evolution
Evolution
‣ Basic genetic operations
Evolution
‣ Basic genetic operations
‣ Mutation of genetic material.
Random changes
Evolution
‣ Basic genetic operations
‣ Mutation of genetic material.
Random changes
‣ Recombination of two individuals.
Recombine genetic material
Evolution
‣ Basic genetic operations
‣ Mutation of genetic material.
Random changes
‣ Recombination of two individuals.
Recombine genetic material
‣ also known as sexual reproduction
Evolution
Evolution
‣ Selection
Evolution
‣ Selection
‣ Who will survive and thus
Evolution
‣ Selection
‣ Who will survive and thus
‣ is able to create the most offsprings?
Evolution Challenges
Evolution
Evolution
‣ Changing environment and a lot of other changing
temperature, energy resources, competitors
and moving stuff, ...
Evolution
‣ Changing environment and a lot of other changing
temperature, energy resources, competitors
and moving stuff, ...
‣ Huge search space countless possible individuals.
vast amount of dna combinations,
Evolution
‣ Changing environment and a lot of other changing
temperature, energy resources, competitors
and moving stuff, ...
‣ Huge search space countless possible individuals.
vast amount of dna combinations,
‣ Restricted resources
Death = release of allocated resources.
Evolution
Evolution
‣ Which combinations to create as an
individual?
Evolution
‣ Which combinations to create as an
individual?
‣ Only validthey usually don‘t breed a banana plant but another
If two dogs breed
individuals
dog, instead!
Evolution
‣ Which combinations to create as an
individual?
‣ Only validthey usually don‘t breed a banana plant but another
If two dogs breed
individuals
dog, instead!
‣ Preserve good gene materialetc. have a lot of
Maybe that‘s why rockstars, football champions,
girlfriends?! Hey, I am a Ruby programmer, that‘s also awesome!
Evolution
‣ Which combinations to create as an
individual?
‣ Only validthey usually don‘t breed a banana plant but another
If two dogs breed
individuals
dog, instead!
‣ Preserve good gene materialetc. have a lot of
Maybe that‘s why rockstars, football champions,
girlfriends?! Hey, I am a Ruby programmer, that‘s also awesome!
‣ Improve! but even improve what‘s good and make it better!
Not only preserve
How does that relate to
computer science and
Ruby?
Evolution and Ruby?
Evolution and Ruby?
Given:
Search or optimization problem with a
huge search space
Evolution and Ruby?
Given:
Search or optimization problem with a
huge search space
Wanted:
A (heuristic) solution.
Evolutionary
Programming
(EP) ...
...with
Genetic Algorithms (GA)
Genetic Algorithms
Basic Idea
Imitate the
evolutionary process.
Genetic Algorithms
What we need
Genetic Algorithms
Genetic Algorithms
‣ Generation = Nr. of individuals
Genetic Algorithms
‣ Generation = Nr. of individuals
‣ Individualbut a valid problem solution
maybe a bad one
=
valid.
Genetic Algorithms
‣ Generation = Nr. of individuals
‣ Individualbut a valid problem solution
maybe a bad one
=
valid.
‣ Individuals constist of genes
Genetic Algorithms
‣ Generation = Nr. of individuals
‣ Individualbut a valid problem solution
maybe a bad one
=
valid.
‣ Individuals constist of genes
‣ Individuals havethe individual (good .. badfitness
indicating the worthiness of
a well known
solution).
Genetic Algorithms
‣ Start with a first individual (genesis)
Genetic Algorithms
‣ Start with a first individual (genesis)
‣ Create 2nd individual through mutation
Genetic Algorithms
‣ Start with a first individual (genesis)
‣ Create 2nd individual through mutation
‣ Randomly/Statistically choose a genetic
operation to create new individuals
Genetic Algorithms
Genetic Algorithms
‣ If a generation is full create a new one
Genetic Algorithms
‣ If a generation is full create a new one
‣ Select fittest individuals to be passed in to
the next generation.
Genetic Algorithms
‣ If a generation is full create a new one
‣ Select fittest individuals to be passed in to
the next generation.
‣ Repeat the process until we have an
acceptable solution or hit a given
boundary.
Concrete?!
Genetic Algorithms
Example
Travelling Salesman
Problem (TSP)
Traveling Salesman
Traveling Salesman
‣ A salesman needs to travel to n cities
Traveling Salesman
‣ A salesman needs to travel to n cities
‣ Each city needs to be visited once
Traveling Salesman
‣ A salesman needs to travel to n cities
‣ Each city needs to be visited once
‣ The salesman wants to end where he
started.
Traveling Salesman
‣ A salesman needs to travel to n cities
‣ Each city needs to be visited once
‣ The salesman wants to end where he
started.
‣ The route should be minimal.
Why should we care about
salesmen?
Traveling Salesman
Traveling Salesman
‣ TSP can be applied to many scenarios:
Traveling Salesman
‣ TSP can be applied to many scenarios:
‣ (Flight) planning, logistics, ...
Traveling Salesman
‣ TSP can be applied to many scenarios:
‣ (Flight) planning, logistics, ...
‣ Manufactoring of microchips
Traveling Salesman
‣ TSP can be applied to many scenarios:
‣ (Flight) planning, logistics, ...
‣ Manufactoring of microchips
‣ Slightly modified in DNA sequencing
Traveling Salesman
‣ TSP can be applied to many scenarios:
‣ (Flight) planning, logistics, ...
‣ Manufactoring of microchips
‣ Slightly modified in DNA sequencing
‣ ...
Traveling Salesman
Traveling Salesman
‣ What‘s the deal about it?
Traveling Salesman
‣ What‘s the deal about it?
‣ Problem of combinatorical optimization
Traveling Salesman
‣ What‘s the deal about it?
‣ Problem of combinatorical optimization
‣ Not a shortest path problem!
Traveling Salesman
‣ What‘s the deal about it?
‣ Problem of combinatorical optimization
‣ Not a shortest path problem!
‣ Computational complexity:
np complete
nondeterministic polynomial time
Too many possible
combinations!
Traveling Salesman
Traveling Salesman
‣ Most direct solution: Check permutations
Traveling Salesman
‣ Most direct solution: Check permutations
‣ O(n!)
Traveling Salesman
‣ Most direct solution: Check permutations
‣ O(n!)
‣ 5! = 120 combinations
Traveling Salesman
‣ Most direct solution: Check permutations
‣ O(n!)
‣ 5! = 120 combinations
‣ 10! = 3.628.800 combinations
Use a genetic algorithm to find a
heuristic solution
TSP
Individual
TSP Individual
TSP Individual
‣ Valid roundtrip- = cycle of -cities Saarbrücken.
Saarbrücken - Amsterdam Madrid - Poznan Berlin -
TSP Individual
‣ Valid roundtrip- = cycle of -cities Saarbrücken.
Saarbrücken - Amsterdam Madrid - Poznan Berlin -
‣ Individual = valid roundtrip
TSP Individual
‣ Valid roundtrip- = cycle of -cities Saarbrücken.
Saarbrücken - Amsterdam Madrid - Poznan Berlin -
‣ Individual = valid roundtrip
‣ Genes = cities
TSP
Fitness
TSP Fitness
TSP Fitness
‣ Each city knows its geo coords (lat, lon)
TSP Fitness
‣ Each city knows its geo coords (lat, lon)
‣ Calculate distance between two geo
coords
TSP Fitness
‣ Each city knows its geo coords (lat, lon)
‣ Calculate distance between two geo
coords
‣ Fitness = sum of distances between cities
TSP
Genetic Operations
Genetic Operations
Mutation
TSP Fitness
TSP Fitness
‣ Shuffle mutation
TSP Fitness
‣ Shuffle mutation
‣ Completely shuffle the order of cities in
the roundtrip
TSP Fitness
‣ Shuffle mutation
‣ Completely shuffle the order of cities in
the roundtrip
‣ 1-3-5-4-2 → 3-5-1-2-4
TSP Fitness
‣ Shuffle mutation
‣ Completely shuffle the order of cities in
the roundtrip
‣ 1-3-5-4-2 → 3-5-1-2-4
‣ (-) Doesn‘t rely on existing individuals
TSP Fitness
‣ Shuffle mutation
‣ Completely shuffle the order of cities in
the roundtrip
‣ 1-3-5-4-2 → 3-5-1-2-4
‣ (-) Doesn‘t rely on existing individuals
‣ Not a slightly but a massive change
TSP Fitness
‣ Partly shuffle
‣ Exchange two randomly choosen cities
‣ 1-3-5-4-2 → 1-3-4-5-2
‣ (+) Uses existing individuals
‣ Many more mutation operations
imaginable.
Genetic Operations
Recombination
TSP Recombination
TSP Recombination
‣ Recombination challenge:
TSP Recombination
‣ Recombination challenge:
‣ How to combine parts of individuals in
a way that a valid roundtrip will be
produced?
Recombination
Cycle Crossover
Cycle Crossover
Cycle Crossover
‣ Pass randomly choosen parts from the
the parents to the offsprings
Cycle Crossover
‣ Pass randomly choosen parts from the
the parents to the offsprings
‣ Creates two offsprings
Cycle Crossover
‣ Pass randomly choosen parts from the
the parents to the offsprings
‣ Creates two offsprings
‣ Example
Cycle Crossover
‣ Pass randomly choosen parts from the
the parents to the offsprings
‣ Creates two offsprings
‣ Example
‣ mom = [2, 3, 5, 7, 1, 6, 4]
Cycle Crossover
‣ Pass randomly choosen parts from the
the parents to the offsprings
‣ Creates two offsprings
‣ Example
‣ mom = [2, 3, 5, 7, 1, 6, 4]
‣ dad = [4, 5, 6, 3, 2, 7, 1]
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Choose a gene from the first parent
(e.g. gene=3, index=1).
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Choose a gene from the first parent
(e.g. gene=3, index=1).
‣ Add it to the cycle.
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Choose a gene from the first parent
(e.g. gene=3, index=1).
‣ Add it to the cycle.
‣ Cycle: [3]
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Search for gene with the same index (1) in
Dad's genes (gene=5, index=1).
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Search for gene with the same index (1) in
Dad's genes (gene=5, index=1).
‣ Add it to the cycle.
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Search for gene with the same index (1) in
Dad's genes (gene=5, index=1).
‣ Add it to the cycle.
‣ Cycle: [3, 5]
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Search for the corresponding gene
(gene=5, index=2) in Mom's genes.
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Search for the corresponding gene
(gene=5, index=2) in Mom's genes.
‣ Look for the gene at index 2 at Dad's
genes (gene=6, index=2).
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Search for the corresponding gene
(gene=5, index=2) in Mom's genes.
‣ Look for the gene at index 2 at Dad's
genes (gene=6, index=2).
‣ Add it to the cycle.
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Search for the corresponding gene
(gene=5, index=2) in Mom's genes.
‣ Look for the gene at index 2 at Dad's
genes (gene=6, index=2).
‣ Add it to the cycle.
‣ Cycle: [3, 5, 6]
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Search for the corresponding gene in
Dad's genes (gene=6, index=5).
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Search for the corresponding gene in
Dad's genes (gene=6, index=5).
‣ Look for the gene at index 5 (gene=7,
index=5) in Mom‘s genes.
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Search for the corresponding gene in
Dad's genes (gene=6, index=5).
‣ Look for the gene at index 5 (gene=7,
index=5) in Mom‘s genes.
‣ Add it to the cycle.
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Search for the corresponding gene in
Dad's genes (gene=6, index=5).
‣ Look for the gene at index 5 (gene=7,
index=5) in Mom‘s genes.
‣ Add it to the cycle.
‣ Cycle: [3, 5, 6, 7]
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Search for the corresponding gene in
Moms's genes (gene=7, index=3).
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Search for the corresponding gene in
Moms's genes (gene=7, index=3).
‣ Look for the gene at index 3 (gene=3,
index=3).
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Search for the corresponding gene in
Moms's genes (gene=7, index=3).
‣ Look for the gene at index 3 (gene=3,
index=3).
‣ Add it to the cycle.
Cycle Crossover
mom = [2, 3, 5, 7, 1, 6, 4]
dad = [4, 5, 6, 3, 2, 7, 1]
‣ Search for the corresponding gene in
Moms's genes (gene=7, index=3).
‣ Look for the gene at index 3 (gene=3,
index=3).
‣ Add it to the cycle.
‣ Cycle: [3, 5, 6, 7, 3]
Cycle Crossover
cycle = [3, 5, 6, 7, 3]
Cycle Crossover
cycle = [3, 5, 6, 7, 3]
‣ Now the cycle is complete!
Cycle Crossover
cycle = [3, 5, 6, 7, 3]
‣ Now the cycle is complete!
‣ We can create the offsprings.
Cycle Crossover
cycle = [3, 5, 6, 7, 3]
‣ Now the cycle is complete!
‣ We can create the offsprings.
‣ Genes which are in the cycle will be taken
from their parents preserving their original positions
Cycle Crossover
cycle = [3, 5, 6, 7, 3]
‣ Now the cycle is complete!
‣ We can create the offsprings.
‣ Genes which are in the cycle will be taken
from their parents preserving their original positions
moms_child = [x, 3, 5, 7, x, 6, x]
dads_child = [x, 5, 6, 3, x, 7, x]
TSP Evolver
‣ Generation size
‣ Choose genetic operations
‣ Probabilities of genetic operations
‣ e.g. 30% mutation, 70% x-over
TSP Evolver
‣ Generation size
‣ Choose genetic operations
‣ Probabilities of genetic operations
‣ e.g. 30% mutation, 70% x-over
‣ Set exit condition
TSP Evolver
‣ Generation size
‣ Choose genetic operations
‣ Probabilities of genetic operations
‣ e.g. 30% mutation, 70% x-over
‣ Set exit condition
‣ ...
Go!
Code and Demo!
GA Challenges
Genetic Algorithms
Genetic Algorithms
‣ How to represent an individual?
Genetic Algorithms
‣ How to represent an individual?
‣ Find a meaningful fitness function
Genetic Algorithms
‣ How to represent an individual?
‣ Find a meaningful fitness function
‣ Can‘t be applied to boolean fitness
functions (match, no match)
Genetic Algorithms
‣ How to represent an individual?
‣ Find a meaningful fitness function
‣ Can‘t be applied to boolean fitness
functions (match, no match)
‣ Define than blind guessing skip it! operations
If it‘s worse
efficient genetic
Genetic Algorithms
Genetic Algorithms
‣ Don‘t get lost in the metaphor! a problem!
It‘s not about re-modeling the world, it‘s about solving
Genetic Algorithms
‣ Don‘t get lost in the metaphor! a problem!
It‘s not about re-modeling the world, it‘s about solving
‣ Non-determinism
1st run might find a very good solution, 2nd a totally bad one, ...
Genetic Algorithms
‣ Don‘t get lost in the metaphor! a problem!
It‘s not about re-modeling the world, it‘s about solving
‣ Non-determinism
1st run might find a very good solution, 2nd a totally bad one, ...
‣ Store your best solutions and use them
as the origin for the next run!
What is it good
for again?
Genetic Algorithms
Genetic Algorithms
‣ Find good solutions for np hard
combinatorical problems
Genetic Algorithms
‣ Find good solutions for np hard
combinatorical problems
‣ ... even for difficult search spaces!
What else can be done?
Genetic Algorithms
Genetic Algorithms
‣ Find smart break conditions
Genetic Algorithms
‣ Find smart break conditions
‣ e.g. exit if there is no change for n
generations
Genetic Algorithms
‣ Find smart break conditions
‣ e.g. exit if there is no change for n
generations
‣ Implement more genetic operations and
evaluate them.
Genetic Algorithms
Genetic Algorithms
‣ Optimize ratio between
Genetic Algorithms
‣ Optimize ratio between
‣ Generation size
Genetic Algorithms
‣ Optimize ratio between
‣ Generation size
‣ Operation propabilities
Genetic Algorithms
‣ Optimize ratio between
‣ Generation size
‣ Operation propabilities
‣ Fasten your fitness function!
Genetic Algorithms
‣ Optimize ratio between
‣ Generation size
‣ Operation propabilities
‣ Fasten your fitness function!
‣ Use heuristic fitness functions
estimate fitness to be faster
Genetic Algorithms
‣ Optimize ratio between
‣ Generation size
‣ Operation propabilities
‣ Fasten your fitness function!
‣ Use heuristic fitness functions
estimate fitness to be faster
‣ Cache frequently computed values
e.g. distances between cities
FAQ
FAQ
FAQ
‣ Do you use EP/GA for your everyday
work?
FAQ
‣ Do you use EP/GA for your everyday
work?
‣ Not very often ...
FAQ
‣ Do you use EP/GA for your everyday
work?
‣ Not very often ...
‣ ... but how often do you encounter np-
hard problems in everyday‘s work?
FAQ
FAQ
‣ Why talk about it?
FAQ
‣ Why talk about it?
‣ Interesting and generic methodology
FAQ
‣ Why talk about it?
‣ Interesting and generic methodology
‣ Solves NP hard problems heuristically
with minimal thought.
FAQ
‣ Why talk about it?
‣ Interesting and generic methodology
‣ Solves NP hard problems heuristically
with minimal thought.
‣ Gives a good understanding why some
people are what they are:
Just bad guesses :-)
Further questions?
Thank you
for your
attention!
Headquarter: Rails Enterprise Hosting:
http://www.avarteq.de http://www.enterprise-
rails.de
Blog:
http://ww.treibstofff.de Rails Hosting:
http://www.railshoster.de
This talk will introduce into programming with evol more
This talk will introduce into programming with evolutionary algorithms.
We'll build and/or discover a small framework for creating genetic
algorithms.
Genetic algorithms are good to solve certain kind of algorithmic
problems.
The talk will also give a rough picture about situations where
evolutionary programming might be the right solution.
Especially np hard problems like the traveling sales man problem (TSP) or
the rucksack problem are well known combinatoric problems (such as
distributing vservers across a number of vserver-hosts). less
0 comments
Post a comment