SlideShare a Scribd company logo
A Brief Introduction to Genetic Algorithms
Geoff Harcourt
Hi
I’m Geoff
Developer at thoughtbot
Maintainer of thoughtbot/dotfiles
and parity (Heroku app shortcuts)
The Knapsack Problem
Given a set of items, each
with its own weight, size,
price, determine the
combination of items under
the weight and size budget
that has the most value CC BY-SA 2.5, https://commons.wikimedia.org/w/index.php?curid=985491
• Positive points for putting guests who like each other
together
• Negative points for putting guests who don’t get
along together
• families must be together



what arrangement produces the most happiness?
Seating Chart for a Wedding
Delivery Truck Route
Given a set of packages
that must be delivered in
one trip, what’s the ideal
order of stops to do the
delivery in the shortest
time (and/or least distance
travelled?)
What Do These Problems
Have In Common?
• Potentially massive/infinite set of possible solutions
(“large solution space”)

• Optimized solutions are better, but “great” is
almost as good as “perfect”

• Cheap to test any one solution’s value (“fitness”)
When Force Isn’t Enough
First potential approach is brute force: test every
possible solution
For some problems this technique can find the
solution, but if the solution space is too large
and/or infinite, may not be feasible.
Genetic Algorithms
Genetic Algorithms (GA) are a type of search
algorithm that mimics the mechanic of natural
selection to traverse a space of possible solutions
and generate high-quality solutions.
What does that mean?
A Genetic Algorithm combines and re-combines
elements of solutions to “evolve” toward more
optimal solutions in manner similar to that by
which a biological population evolves over time
Current Genetic
Research
How does GA work?
Representation: represent parts of the problem
as “genes”
Fitness: a function that can be run against the
expressed genes to measure the quality of the
solution
Evolution: breeding and/or mutation and
selection
Representation
A DNA-based organism represents its genetic
code with a base-4 system [A, C, G, T]. 



A chromosome’s gene sequence might read as
AACTGACTGA
Many problems can be expressed as base-2:
0110101000
Representation: 

The Knapsack Problem
Generate a random set of 100 items, each with its
own weight, size, and value. Put the items in an array
for reference.



Our organism will have one “chromosome” with 100
“genes”. Each gene is set to either 0 (not in the
knapsack) or 1 (in the knapsack).
The gene’s position in the chromosome matches that
of the item’s state that it represents.
Fitness
The Fitness Function is how we test any solution’s
fitness, or how effective the solution is.
For some problems the best fitness will be the
highest number possible or lowest number possible,
or it might be the number closest to an ideal value.
Fitness: The Knapsack Problem
The fitness function for the Knapsack Problem would
be the sum of the value of the items that are in the
Knapsack.
Our 9-gene chromosome: [0, 1, 1, 0, 0, 0, 1, 0, 1]
Our knapsack has items 1 ($11), 2 ($5), 6 ($3), and 8
($21). Our knapsack’s value is 40, the sum of the
dollar values of the items inside.
But wait, there’s more 

(to fitness)!
Some solutions are invalid. In the Knapsack Problem,
solutions whose summed weights or volumes are
greater than the knapsack can hold aren’t valid even
if they contain the highest dollar value.
These problems need to return a fitness that
disqualifies them. In our case, we’ll return 0 for any
knapsack that exceeds the weight or size limit.
Seeding a Population
To determine a solution to our problem, let’s start with
a population.
We’ll randomly generate 200 organisms by building
200 chromosomes, randomly flipping the bits in our
chromosome either to put the item into the knapsack
or hold it out.
Seeding (continued)
If we think we know some parts of the solution
already (such as an item that’s worth a lot and is
small and lightweight), we can use non-random or
partially random seed data to nudge the population
closer to the solution.
This is called “warm starting”. It should be used
carefully, as it may preclude unexpectedly fit
solutions.
Now what?
We are going to iterate through a number of
generations. In each generation, we’ll use the
following mechanisms to move toward the best
fitness:

• Crossover

• Mutation

• Selection
Sweet, sweet love
Our algorithm takes two organisms* from the
population and has them mate. Mating the organisms
combines their respective genes and produces two
new organisms, each containing some elements of
their parents’ gene expressions.
* Complicated algorithms can mate more than two
organisms at once, we won’t do that here.
Crossover
Crossover is how we combine genes from two
organisms to produce new solutions.
Crossover takes the chromosomes from two
organisms and has them trade pieces with each
other. The result of crossover is a group of organisms
with new combinations of gene expressions that
might not have existed in prior generations.
Crossover
Cr0ssover
Crossover mimics the process of gene recombination
(both between organisms and between
chromosomes themselves) that occurs in biological
organisms.
Crossover
Crossover will do much of the work for creating
genetic diversity (different solutions through different
combinations), from one generation to another, but
what if our seed population was missing some gene
expressions that would produce better solutions?
Mutation
Mutation is a mechanism to maintain genetic
diversity.
Mutation is applied by flipping a gene’s expression
according to a probability defined by the algorithm.
These flipped states introduce new values into the
population and contribute to a wider search space.
Mutation
Mutation probabilities need to be kept low or else
they result in a loss of progress from generation to
generation, and the genetic algorithm becomes more
of random search than an evolution toward an ideal
solution.
Mutation
It’s often helpful to tweak mutation settings
(probability of mutation) over several tests to see how
it affects the search.
A New Generation
By mating our initial population through crossover
and randomly applying mutations to a small
percentage of genes, we’ve produces a new
generation of solutions.
We’ll test each organism’s fitness to see which
organisms are most fit.
Selection
Here’s where it gets interesting (and where we have
some decisions to make).
In each generation, we want to promote the most fit
solutions and demote the least fit. There’s a number
of factors to consider.
Selection
The fittest solutions will be selected more often to
breed into the next generation. The frequency can be
determined by various techniques including:

• weigh by organism’s % of generation’s total fitness
• randomly, weighted by fitness (“roulette wheel”)
• “tournament selection” (taking a subset and
picking the best of the subset)
Elitism
In order to ensure we never lose the best solution
taken, we can ensure that the best organism(s) found
is/are always included in the next generation.
This mechanism is termed elitism.
If the threshold for elitism is too harsh, the solution
may prematurely converge on a solution, sometimes
called “hill-climbing”.
Hill-climbing
A “hill-climbing” algorithm takes a solution and checks adjacent
solutions to see if neighboring options are better.
Vulnerable to local maxima/minima
Why Did I Do This?
Fantasy Baseball!
I play in a fantasy baseball league where over 600
baseball players are controlled by the teams.
Our league was expanding from 14 to 16 teams, and
I wanted to see the effect that the change would
have on positional scarcity.
Why Did I Do This?
Some positions (first base, outfield) have lots of great
hitters, while some (shortstop, catcher) have fewer
good hitters.
Some players are eligible to play at multiple
positions.
I wanted a way to simulate how our league would
draft and allocate players in the upcoming season.
Why Did I Do This?
My first attempt was to build a program that
performed a draft. Each turn it found the weakest
position and then selected the best player who could
play that position.
I noticed that this program frequently turned out
solutions that looked incorrect (positions looked
oddly ranked relatively to one another).
Why Did I Do This?
It turns out my solution prematurely optimized, so I
was allocating players inefficiently and failing to
accurately simulate what would happen in a real
draft where people could observe the scarcity of
each position in real-time.
Why Did I Do This?
I wasn’t actually interested in getting a perfect
solution, but was concerned with getting something
that was a reasonable representation of what would
happen in an auction.
I used the Darwinning gem, which provides a GA
framework, to simulate the allocation of players.
Further Reading
• Daniel Sellergren - Solving the 0-1 Knapsack
Problem with a Genetic Algorithm in Ruby

http://www.danielsellergren.com/posts/solving-
the-0-1-knapsack-problem-with-a-genetic-
algorithm-in-ruby

• MIT Course Lecture (very high-level, great
introduction!) - Genetic Algorithms

https://www.youtube.com/watch?v=kHyNqSnzP8Y

• Darwinning - Ruby gem for GA

https://github.com/dorkrawk/darwinning
Keep in Touch!
GitHub - @geoffharcourt
Twitter - @geoffharcourt
Email - geoff@thoughtbot.com
DC Tech Slack - @geoffharcourt

More Related Content

Similar to Info to Genetic Algorithms - DC Ruby Users Group 11.10.2016

4.Genetic-Algorithms.ppt
4.Genetic-Algorithms.ppt4.Genetic-Algorithms.ppt
4.Genetic-Algorithms.ppt
RamjiChaurasiya
 
Genetic Algorithms in Artificial Intelligence
Genetic Algorithms in Artificial IntelligenceGenetic Algorithms in Artificial Intelligence
Genetic Algorithms in Artificial Intelligence
ritwijkp2
 
Explanation and example of genetic algorithm
Explanation and example of genetic algorithmExplanation and example of genetic algorithm
Explanation and example of genetic algorithm
MarkKhan23
 
Genetic-Algorithms.ppt
Genetic-Algorithms.pptGenetic-Algorithms.ppt
Genetic-Algorithms.ppt
Nipun85
 
AI_PPT_Genetic-Algorithms.ppt
AI_PPT_Genetic-Algorithms.pptAI_PPT_Genetic-Algorithms.ppt
AI_PPT_Genetic-Algorithms.ppt
HotTea
 
Genetic-Algorithms-computersciencepptnew.ppt
Genetic-Algorithms-computersciencepptnew.pptGenetic-Algorithms-computersciencepptnew.ppt
Genetic-Algorithms-computersciencepptnew.ppt
Fitnessfreaksfam
 
Genetic-Algorithms forv artificial .ppt
Genetic-Algorithms forv artificial  .pptGenetic-Algorithms forv artificial  .ppt
Genetic-Algorithms forv artificial .ppt
neelamsanjeevkumar
 
Genetic-Algorithms for machine learning and ai.ppt
Genetic-Algorithms for machine learning and ai.pptGenetic-Algorithms for machine learning and ai.ppt
Genetic-Algorithms for machine learning and ai.ppt
neelamsanjeevkumar
 
Genetic-Algorithms.ppt
Genetic-Algorithms.pptGenetic-Algorithms.ppt
Genetic-Algorithms.ppt
ssuser2e437f
 
Data Science - Part XIV - Genetic Algorithms
Data Science - Part XIV - Genetic AlgorithmsData Science - Part XIV - Genetic Algorithms
Data Science - Part XIV - Genetic Algorithms
Derek Kane
 
RM 701 Genetic Algorithm and Fuzzy Logic lecture
RM 701 Genetic Algorithm and Fuzzy Logic lectureRM 701 Genetic Algorithm and Fuzzy Logic lecture
RM 701 Genetic Algorithm and Fuzzy Logic lecture
VIT University (Chennai Campus)
 
Genetic algorithms
Genetic algorithmsGenetic algorithms
Genetic algorithms
RimpleDhamija
 
Genetic Algorithm 2 -.pptx
Genetic Algorithm 2 -.pptxGenetic Algorithm 2 -.pptx
Genetic Algorithm 2 -.pptx
TAHANMKH
 
Introduction to Optimization with Genetic Algorithm (GA)
Introduction to Optimization with Genetic Algorithm (GA)Introduction to Optimization with Genetic Algorithm (GA)
Introduction to Optimization with Genetic Algorithm (GA)
Ahmed Gad
 
Genetic algorithms
Genetic algorithmsGenetic algorithms
Genetic algorithms
Amna Saeed
 
Genetic algorithms full lecture
Genetic algorithms full lectureGenetic algorithms full lecture
Genetic algorithms full lecture
sadiacs
 
A genetic algorithm approach to static job shop scheduling
A genetic algorithm approach to static job shop schedulingA genetic algorithm approach to static job shop scheduling
A genetic algorithm approach to static job shop schedulingNagendra Bvs
 
Genetic Algorithm
Genetic AlgorithmGenetic Algorithm
Genetic Algorithm
rabidityfactor
 
Genetic Algorithm
Genetic Algorithm Genetic Algorithm
Genetic Algorithm
Thushan Ganegedara
 
Genetic Algorithms : A class of Evolutionary Algorithms
Genetic Algorithms : A class of Evolutionary AlgorithmsGenetic Algorithms : A class of Evolutionary Algorithms
Genetic Algorithms : A class of Evolutionary Algorithms
Kavya Barnadhya Hazarika
 

Similar to Info to Genetic Algorithms - DC Ruby Users Group 11.10.2016 (20)

4.Genetic-Algorithms.ppt
4.Genetic-Algorithms.ppt4.Genetic-Algorithms.ppt
4.Genetic-Algorithms.ppt
 
Genetic Algorithms in Artificial Intelligence
Genetic Algorithms in Artificial IntelligenceGenetic Algorithms in Artificial Intelligence
Genetic Algorithms in Artificial Intelligence
 
Explanation and example of genetic algorithm
Explanation and example of genetic algorithmExplanation and example of genetic algorithm
Explanation and example of genetic algorithm
 
Genetic-Algorithms.ppt
Genetic-Algorithms.pptGenetic-Algorithms.ppt
Genetic-Algorithms.ppt
 
AI_PPT_Genetic-Algorithms.ppt
AI_PPT_Genetic-Algorithms.pptAI_PPT_Genetic-Algorithms.ppt
AI_PPT_Genetic-Algorithms.ppt
 
Genetic-Algorithms-computersciencepptnew.ppt
Genetic-Algorithms-computersciencepptnew.pptGenetic-Algorithms-computersciencepptnew.ppt
Genetic-Algorithms-computersciencepptnew.ppt
 
Genetic-Algorithms forv artificial .ppt
Genetic-Algorithms forv artificial  .pptGenetic-Algorithms forv artificial  .ppt
Genetic-Algorithms forv artificial .ppt
 
Genetic-Algorithms for machine learning and ai.ppt
Genetic-Algorithms for machine learning and ai.pptGenetic-Algorithms for machine learning and ai.ppt
Genetic-Algorithms for machine learning and ai.ppt
 
Genetic-Algorithms.ppt
Genetic-Algorithms.pptGenetic-Algorithms.ppt
Genetic-Algorithms.ppt
 
Data Science - Part XIV - Genetic Algorithms
Data Science - Part XIV - Genetic AlgorithmsData Science - Part XIV - Genetic Algorithms
Data Science - Part XIV - Genetic Algorithms
 
RM 701 Genetic Algorithm and Fuzzy Logic lecture
RM 701 Genetic Algorithm and Fuzzy Logic lectureRM 701 Genetic Algorithm and Fuzzy Logic lecture
RM 701 Genetic Algorithm and Fuzzy Logic lecture
 
Genetic algorithms
Genetic algorithmsGenetic algorithms
Genetic algorithms
 
Genetic Algorithm 2 -.pptx
Genetic Algorithm 2 -.pptxGenetic Algorithm 2 -.pptx
Genetic Algorithm 2 -.pptx
 
Introduction to Optimization with Genetic Algorithm (GA)
Introduction to Optimization with Genetic Algorithm (GA)Introduction to Optimization with Genetic Algorithm (GA)
Introduction to Optimization with Genetic Algorithm (GA)
 
Genetic algorithms
Genetic algorithmsGenetic algorithms
Genetic algorithms
 
Genetic algorithms full lecture
Genetic algorithms full lectureGenetic algorithms full lecture
Genetic algorithms full lecture
 
A genetic algorithm approach to static job shop scheduling
A genetic algorithm approach to static job shop schedulingA genetic algorithm approach to static job shop scheduling
A genetic algorithm approach to static job shop scheduling
 
Genetic Algorithm
Genetic AlgorithmGenetic Algorithm
Genetic Algorithm
 
Genetic Algorithm
Genetic Algorithm Genetic Algorithm
Genetic Algorithm
 
Genetic Algorithms : A class of Evolutionary Algorithms
Genetic Algorithms : A class of Evolutionary AlgorithmsGenetic Algorithms : A class of Evolutionary Algorithms
Genetic Algorithms : A class of Evolutionary Algorithms
 

Recently uploaded

Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 

Recently uploaded (20)

Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 

Info to Genetic Algorithms - DC Ruby Users Group 11.10.2016

  • 1. A Brief Introduction to Genetic Algorithms Geoff Harcourt
  • 2. Hi I’m Geoff Developer at thoughtbot Maintainer of thoughtbot/dotfiles and parity (Heroku app shortcuts)
  • 3. The Knapsack Problem Given a set of items, each with its own weight, size, price, determine the combination of items under the weight and size budget that has the most value CC BY-SA 2.5, https://commons.wikimedia.org/w/index.php?curid=985491
  • 4. • Positive points for putting guests who like each other together • Negative points for putting guests who don’t get along together • families must be together
 
 what arrangement produces the most happiness? Seating Chart for a Wedding
  • 5. Delivery Truck Route Given a set of packages that must be delivered in one trip, what’s the ideal order of stops to do the delivery in the shortest time (and/or least distance travelled?)
  • 6. What Do These Problems Have In Common? • Potentially massive/infinite set of possible solutions (“large solution space”)
 • Optimized solutions are better, but “great” is almost as good as “perfect”
 • Cheap to test any one solution’s value (“fitness”)
  • 7. When Force Isn’t Enough First potential approach is brute force: test every possible solution For some problems this technique can find the solution, but if the solution space is too large and/or infinite, may not be feasible.
  • 8. Genetic Algorithms Genetic Algorithms (GA) are a type of search algorithm that mimics the mechanic of natural selection to traverse a space of possible solutions and generate high-quality solutions.
  • 9. What does that mean? A Genetic Algorithm combines and re-combines elements of solutions to “evolve” toward more optimal solutions in manner similar to that by which a biological population evolves over time
  • 11. How does GA work? Representation: represent parts of the problem as “genes” Fitness: a function that can be run against the expressed genes to measure the quality of the solution Evolution: breeding and/or mutation and selection
  • 12. Representation A DNA-based organism represents its genetic code with a base-4 system [A, C, G, T]. 
 
 A chromosome’s gene sequence might read as AACTGACTGA Many problems can be expressed as base-2: 0110101000
  • 13. Representation: 
 The Knapsack Problem Generate a random set of 100 items, each with its own weight, size, and value. Put the items in an array for reference.
 
 Our organism will have one “chromosome” with 100 “genes”. Each gene is set to either 0 (not in the knapsack) or 1 (in the knapsack). The gene’s position in the chromosome matches that of the item’s state that it represents.
  • 14. Fitness The Fitness Function is how we test any solution’s fitness, or how effective the solution is. For some problems the best fitness will be the highest number possible or lowest number possible, or it might be the number closest to an ideal value.
  • 15. Fitness: The Knapsack Problem The fitness function for the Knapsack Problem would be the sum of the value of the items that are in the Knapsack. Our 9-gene chromosome: [0, 1, 1, 0, 0, 0, 1, 0, 1] Our knapsack has items 1 ($11), 2 ($5), 6 ($3), and 8 ($21). Our knapsack’s value is 40, the sum of the dollar values of the items inside.
  • 16. But wait, there’s more 
 (to fitness)! Some solutions are invalid. In the Knapsack Problem, solutions whose summed weights or volumes are greater than the knapsack can hold aren’t valid even if they contain the highest dollar value. These problems need to return a fitness that disqualifies them. In our case, we’ll return 0 for any knapsack that exceeds the weight or size limit.
  • 17. Seeding a Population To determine a solution to our problem, let’s start with a population. We’ll randomly generate 200 organisms by building 200 chromosomes, randomly flipping the bits in our chromosome either to put the item into the knapsack or hold it out.
  • 18. Seeding (continued) If we think we know some parts of the solution already (such as an item that’s worth a lot and is small and lightweight), we can use non-random or partially random seed data to nudge the population closer to the solution. This is called “warm starting”. It should be used carefully, as it may preclude unexpectedly fit solutions.
  • 19. Now what? We are going to iterate through a number of generations. In each generation, we’ll use the following mechanisms to move toward the best fitness:
 • Crossover
 • Mutation
 • Selection
  • 20. Sweet, sweet love Our algorithm takes two organisms* from the population and has them mate. Mating the organisms combines their respective genes and produces two new organisms, each containing some elements of their parents’ gene expressions. * Complicated algorithms can mate more than two organisms at once, we won’t do that here.
  • 21. Crossover Crossover is how we combine genes from two organisms to produce new solutions. Crossover takes the chromosomes from two organisms and has them trade pieces with each other. The result of crossover is a group of organisms with new combinations of gene expressions that might not have existed in prior generations.
  • 23. Cr0ssover Crossover mimics the process of gene recombination (both between organisms and between chromosomes themselves) that occurs in biological organisms.
  • 24. Crossover Crossover will do much of the work for creating genetic diversity (different solutions through different combinations), from one generation to another, but what if our seed population was missing some gene expressions that would produce better solutions?
  • 25. Mutation Mutation is a mechanism to maintain genetic diversity. Mutation is applied by flipping a gene’s expression according to a probability defined by the algorithm. These flipped states introduce new values into the population and contribute to a wider search space.
  • 26. Mutation Mutation probabilities need to be kept low or else they result in a loss of progress from generation to generation, and the genetic algorithm becomes more of random search than an evolution toward an ideal solution.
  • 27. Mutation It’s often helpful to tweak mutation settings (probability of mutation) over several tests to see how it affects the search.
  • 28. A New Generation By mating our initial population through crossover and randomly applying mutations to a small percentage of genes, we’ve produces a new generation of solutions. We’ll test each organism’s fitness to see which organisms are most fit.
  • 29. Selection Here’s where it gets interesting (and where we have some decisions to make). In each generation, we want to promote the most fit solutions and demote the least fit. There’s a number of factors to consider.
  • 30. Selection The fittest solutions will be selected more often to breed into the next generation. The frequency can be determined by various techniques including:
 • weigh by organism’s % of generation’s total fitness • randomly, weighted by fitness (“roulette wheel”) • “tournament selection” (taking a subset and picking the best of the subset)
  • 31. Elitism In order to ensure we never lose the best solution taken, we can ensure that the best organism(s) found is/are always included in the next generation. This mechanism is termed elitism. If the threshold for elitism is too harsh, the solution may prematurely converge on a solution, sometimes called “hill-climbing”.
  • 32. Hill-climbing A “hill-climbing” algorithm takes a solution and checks adjacent solutions to see if neighboring options are better. Vulnerable to local maxima/minima
  • 33. Why Did I Do This? Fantasy Baseball! I play in a fantasy baseball league where over 600 baseball players are controlled by the teams. Our league was expanding from 14 to 16 teams, and I wanted to see the effect that the change would have on positional scarcity.
  • 34. Why Did I Do This? Some positions (first base, outfield) have lots of great hitters, while some (shortstop, catcher) have fewer good hitters. Some players are eligible to play at multiple positions. I wanted a way to simulate how our league would draft and allocate players in the upcoming season.
  • 35. Why Did I Do This? My first attempt was to build a program that performed a draft. Each turn it found the weakest position and then selected the best player who could play that position. I noticed that this program frequently turned out solutions that looked incorrect (positions looked oddly ranked relatively to one another).
  • 36. Why Did I Do This? It turns out my solution prematurely optimized, so I was allocating players inefficiently and failing to accurately simulate what would happen in a real draft where people could observe the scarcity of each position in real-time.
  • 37. Why Did I Do This? I wasn’t actually interested in getting a perfect solution, but was concerned with getting something that was a reasonable representation of what would happen in an auction. I used the Darwinning gem, which provides a GA framework, to simulate the allocation of players.
  • 38. Further Reading • Daniel Sellergren - Solving the 0-1 Knapsack Problem with a Genetic Algorithm in Ruby
 http://www.danielsellergren.com/posts/solving- the-0-1-knapsack-problem-with-a-genetic- algorithm-in-ruby
 • MIT Course Lecture (very high-level, great introduction!) - Genetic Algorithms
 https://www.youtube.com/watch?v=kHyNqSnzP8Y
 • Darwinning - Ruby gem for GA
 https://github.com/dorkrawk/darwinning
  • 39. Keep in Touch! GitHub - @geoffharcourt Twitter - @geoffharcourt Email - geoff@thoughtbot.com DC Tech Slack - @geoffharcourt