SlideShare a Scribd company logo
Introduction to
Genetic Algorithms
with Python
Hello World!
The number game
Reach back in your
memory to a game we
played as kids.
Pick a number
between 1 and 10.
(pick 3)
Is your number 2?
No
Is it 7?
No
Is it 3?
Yes
That works
reasonably well for
1..10 but...
it becomes boring as
we increase the range
to 1..100.
Why?
Because we have
no way to
improve our guesses.
The guess is either
right or wrong
so it becomes a
mechanical process.
Is it 1? No
Is it 2? No
Is it 3? No
Is it 4? No
So, to make it
more interesting
instead of no,
let's say
higher or lower.
(pick 3 again)
Is your number 1?
Higher
7?
Lower
3?
Correct
At this point, the
person who evolves
the most efficient
guessing strategy wins.
Domain knowledge
When playing this
game we make use of
domain knowledge.
For example,
after this sequence:
1? Higher
7? Lower
Why wouldn't we guess
8, 9, or 10?
Because we know
that those numbers
are not lower than 7.
Why wouldn't we guess
1?
Because we already
tried it.
We use our memory
of what we've tried,
our successes
and failures,
and our
knowledge of numbers
to make better guesses.
A genetic algorithm
does not know
what lower means.
It has no intelligence.
It does not learn.
It will make the same
mistakes every time.
It will only be as good
at solving a problem
as the person
who writes the code.
And yet, it can be used
to find solutions to
problems that
humans would
struggle to solve or
could not solve at all.
How is that possible?
Genetic algorithms use
random exploration
of the problem space
combined with
evolutionary processes
like mutation and
crossover - exchange
of genetic information -
to improve guesses.
But also, because they
have no experience in
the problem domain
they try things
a human would never
think to try.
Thus, a person using
a genetic algorithm
may learn more about
the problem space and
potential solutions.
This helps them to
improve the algorithm,
in a virtuous cycle.
What can we learn
from this?
Genetic algorithms
should make
informed guesses.
Guess the password
Let's see how
this applies to
guessing a password.
We'll start by randomly
generating an initial
sequence of letters
then mutate/change
one random letter
in that sequence
at a time until the
sequence of letters is
"Hello World!"
Psuedo code
_letters = [a..zA..Z !]
target = "Hello World!"
guess = get 12 random letters from _letters
while guess != target:
index = get random value from [0..length of target]
guess[index] = get 1 random value from _letters
If you try this in your
favorite programming
language...
you'll find that it
performs worse than
guessing numbers
using yes and no
because it cannot tell
when one guess is
better than another.
So, let's help it
make an
informed guess
by telling it how many
guessed letters are
in the correct spots.
For example
"World.Hello?"
would get 2
because only the 4th
letter of each word
is correct.
The number 2
indicates how close the
answer is to correct.
This is called
the fitness value.
"hello_world?"
would get
a fitness value of 9
because 9 letters are
correct. Only the
h, w, and ? are wrong.
First Program
Now we're ready to
write some Python.
Start with a
set of letters for genes
and a target password
Genes and target
geneSet = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!."
target = "Hello World!"
Note: You can run the code in your browser at
https://repl.it/CZL1/1
Generate a guess
Next we need a way to
generate a random
string of letters.
Generate parent
import random
…
def generate_parent(length):
genes = []
while len(genes) < length:
sampleSize = min(length - len(genes), len(geneSet))
genes.extend(random.sample(geneSet, sampleSize))
return ''.join(genes)
Fitness
The fitness value
the genetic algorithm
provides
is the only feedback
the engine gets
to guide it
toward a solution.
Our fitness value is
the total number
of correct letters.
Get fitness
def get_fitness(guess):
return sum(1 for expected, actual in zip(target, guess)
if expected == actual)
Mutation
We also need
a way to produce
a new guess
by mutating
the current guess.
We start by
converting the parent
string to an array.
Mutate
def mutate(parent):
childGenes = list(parent)
...
Then replace
1 letter in the array
with a randomly
selected one
Mutate (cont'd)
...
index = random.randint(0, len(parent) - 1)
newGene, alternate = random.sample(geneSet, 2)
childGenes[index] = alternate 
if newGene == childGenes[index] 
else newGene
...
and then
recombine the result
into a string.
Mutate (cont'd)
...
return ''.join(childGenes)
Display
Next, it is important
to monitor
what is happening
so that we can
stop the engine
if it gets stuck.
It also allows us to
learn what works
and what does not
so we can
improve the algorithm.
To do that we display a
visual representation
of the gene sequence,
which may not be
the literal
gene sequence,
its fitness value and
the total run time.
Display
import datetime
...
def display(guess):
timeDiff = datetime.datetime.now() - startTime
fitness = get_fitness(guess)
print("{0}t{1}t{2}".format(guess, fitness, str(timeDiff)))
Sample output
ftljCDPvhasn 1 0:00:00
Main
Now we're ready to
write the main
program.
We start with a
random guess.
Main
random.seed()
startTime = datetime.datetime.now()
bestParent = generate_parent(len(target))
bestFitness = get_fitness(bestParent)
display(bestParent)
...
Then we add
the heart of
the genetic engine.
It is a loop
that creates a guess,
Main loop
while True:
child = mutate(bestParent)
...
requests the fitness
for that guess
Main loop (cont'd)
...
childFitness = get_fitness(child)
...
then compares it to
the fitness of the
previous best guess
Main loop (cont'd)
…
if bestFitness >= childFitness:
continue
display(child)
if childFitness >= len(bestParent):
break
...
and keeps
the one with
the best fitness.
Main loop (cont't)
...
bestFitness, bestParent = childFitness, child
This cycle repeats
until a stop condition
occurs.
Here, we stop
when all the letters
match the target.
Run
Sample output
ftljCDPvhasn 1 0:00:00
ftljC Pvhasn 2 0:00:00
ftljC Pohasn 3 0:00:00.001000
HtljC Pohasn 4 0:00:00.002000
HtljC Wohasn 5 0:00:00.004000
Htljo Wohasn 6 0:00:00.005000
Htljo Wohas! 7 0:00:00.008000
Htljo Wohls! 8 0:00:00.010000
Heljo Wohls! 9 0:00:00.013000
Hello Wohls! 10 0:00:00.013000
Hello Wohld! 11 0:00:00.013000
Hello World! 12 0:00:00.015000
Congratulations!
You've written
a genetic algorithm
in Python!
Next step
The next step is to
extract the genetic
engine code
from that specific to
the password problem
so it can be reused.
Ready for more?
Continue the lesson on CodeProject
or learn about my book.

More Related Content

PPTX
G@S: Adaptivity in Educational Games
PDF
Dodo does-math
PDF
Python32 pyhackathon-201011
PPT
Python advanced 3.the python std lib by example – algorithm
PDF
Definizione e sviluppo di un algoritmo genetico multiobiettivo per problemi d...
PPS
Vocabulary Test For The Dirty Minded
PPS
XX - Vocabulary Test for the dirty minded
PDF
Genetic Algorithms Made Easy
G@S: Adaptivity in Educational Games
Dodo does-math
Python32 pyhackathon-201011
Python advanced 3.the python std lib by example – algorithm
Definizione e sviluppo di un algoritmo genetico multiobiettivo per problemi d...
Vocabulary Test For The Dirty Minded
XX - Vocabulary Test for the dirty minded
Genetic Algorithms Made Easy

Similar to Introduction to Genetic Algorithms with Python - Hello World! (20)

PPT
Genetic Algorithms-1.ppt
PDF
Genetic Algorithm
PDF
Lec 7 genetic algorithms
PDF
Data Science - Part XIV - Genetic Algorithms
PDF
Da35573574
PPTX
Genetic algorithm
PDF
3_GO_Olesya_Genetic_AlgorithmsOPTIMZTION.p.pdf
PPT
Genetic algorithm
PDF
04 2 machine evolution
PPTX
Genetic algorithms
PDF
Info to Genetic Algorithms - DC Ruby Users Group 11.10.2016
PDF
generic optimization techniques lecture slides
PDF
A Review On Genetic Algorithm And Its Applications
ODP
Genetic Programming in Python
PDF
Genetic algorithm
PPT
Introduction to Genetic algorithms
PDF
04 1 evolution
PDF
How to easily find the optimal solution without exhaustive search using Genet...
PDF
Artificial intelligence cs607 handouts lecture 11 - 45
PPT
Demonstration1 G As
Genetic Algorithms-1.ppt
Genetic Algorithm
Lec 7 genetic algorithms
Data Science - Part XIV - Genetic Algorithms
Da35573574
Genetic algorithm
3_GO_Olesya_Genetic_AlgorithmsOPTIMZTION.p.pdf
Genetic algorithm
04 2 machine evolution
Genetic algorithms
Info to Genetic Algorithms - DC Ruby Users Group 11.10.2016
generic optimization techniques lecture slides
A Review On Genetic Algorithm And Its Applications
Genetic Programming in Python
Genetic algorithm
Introduction to Genetic algorithms
04 1 evolution
How to easily find the optimal solution without exhaustive search using Genet...
Artificial intelligence cs607 handouts lecture 11 - 45
Demonstration1 G As
Ad

Recently uploaded (20)

PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
ai tools demonstartion for schools and inter college
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
assetexplorer- product-overview - presentation
PDF
System and Network Administration Chapter 2
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Transform Your Business with a Software ERP System
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
top salesforce developer skills in 2025.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
Odoo Companies in India – Driving Business Transformation.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
How to Choose the Right IT Partner for Your Business in Malaysia
Softaken Excel to vCard Converter Software.pdf
ai tools demonstartion for schools and inter college
Digital Systems & Binary Numbers (comprehensive )
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
assetexplorer- product-overview - presentation
System and Network Administration Chapter 2
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Reimagine Home Health with the Power of Agentic AI​
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
VVF-Customer-Presentation2025-Ver1.9.pptx
Transform Your Business with a Software ERP System
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Computer Software and OS of computer science of grade 11.pptx
top salesforce developer skills in 2025.pdf
Design an Analysis of Algorithms II-SECS-1021-03
Ad

Introduction to Genetic Algorithms with Python - Hello World!