Vous êtes sur la page 1sur 110

Genetic Algorithms and Genetic Programming in Python

Evolved Virtual Creatures


http://www.youtube.com/watch?v=JBgGV!"#$%

&hat are Ge'etic (lgorithms a'd
Ge'etic "rograms?

!earch algorithms based o'
the mecha'ics o$ ...

)atural !electio'

)atural Ge'etics

*he +echa'ics ,Circle o$ -i$e.

Joh' /olla'd0 1'iversity o$ +ichiga'

Joh' 2o3a0 1'iversity o$ +ichiga'0 !ta'$ord

*hey seem to do well whe'...

*here is a huge solutio' space

*he solutio' space is dy'amic0 'o'4li'ear0 or otherwise


comple5

*he solutio' space is poorly u'derstood

6omai' 7'owledge is scarce or di$$icult to e'code

)o mathematical a'alysis is available

8it'ess0 payo$$0 or suitability o$ a solutio' ca' be


determi'ed

*raditio'al search methods $ail



('te''a 6esig'
http://ti.arc.'asa.gov/pro9ects/esg/research/a'te''a.htm

-ocomotio'
http://www.7arlsims.com/evolved4virtual4creatures.html

Evolvi'g Cars
http://bo5car:d.com/

(rt
http://rogeralsi'g.com/:;;%/<:/;#/ge'etic4programmi'g4evolutio'4o$4mo'a4lisa/

+i'imi3e co''ectio' le'gth
http://d5.doi.org/<;.<<<=/<.:#>:<<>

!pacecra$t *ragectory 6esig'
http://www.astos.de/solutio's/space/i'terpla'etary

*he ?/umie@ (wards

Evolve patches to C programs

Evolve e$$icie't search algorithm $or mate4i'4)


problem

6iag'osi'g prostate ca'cer usi'g i'$rared


spectroscopi'g imagi'g

E5tracti'g ellipses $rom a' image usi'g G"

(utomated test program ge'eratio' $or


microprocessor test a'd validatio'

PyEvolve

GA/GP Library for Python

Uses multiprocessing

Fast with PyPy!

http://pyevolvesourceforgenet/wor!press/"p#$$%&

'urrently release! version ()

*on+t Use

,etter to use PyEvolve (-.'$

/houl! be release! near the en! of 0arch



&hat are the ?mecha'ics@ o$ 'atural
selectio' a'd 'atural ge'etics?

*he Circle o$ -i$e

Ge'otype0 "he'otype0 a'd Ge'ome
?bluepri't@
?"hysical ma'i$estatio'@
?A'sta'ces@

"yevolve Built4A' Ge'otypes

1ne2*imensional ,inary /tring

3wo2*imensional ,inary /tring

1ne2*imensional List

3wo2*imensional List

3ree

Genetic Program 43ree specific to GP5

6ou can roll your own!



<46 Bi'ary !tri'g
from pyevolve import G1DBinaryString
genotype = G1DBinaryString.G1DBinaryString(16)

:46 Bi'ary !tri'g
from pyevolve import G2DBinaryString
genotype = G1DBinaryString.G2DBinaryString(12, 36)

<46 a'd :46 -ists
from pyevolve import G1DList, G2DList
genotype = G1DList.G1DList(14)
genotype = G2DList.G2DList(!, ")
?(lleles@ are the ob9ect types the list is made o$... by de$ault i'tegers
Bou ca' pass i' a $u'ctio' to co'struct the list with whatever ob9ect you wa't
genotype.set#arams(rangemin=$".12, rangema%=".13)
genotype.initiali&ator.set('nitiali&ators.G1DList'nitiali&ator(eal)

*ree a'd Ge'etic "rogram
from pyevolve import G)ree
genotype = G)ree.G)ree()
gp*genotype = G)ree.G)reeG#()

*ree a'd Ge'etic "rogram
from pyevolve import G)ree
genotype = G)ree.G)ree()
gp*genotype = G)ree.G)reeG#()

)ow that we have our ge'otype0 how is it
e5pressed0 a'd how do we 7'ow how ?good@
i'sta'ces o$ the ge'otype are?

*he Circle o$ -i$e

8it'ess

8i'd the global mi'ima o$ !cha$$er 8C

*he ge'otype is a :6 poi't0 i.e. ,50y.

"he'otype is how the ,50y. poi't ma'i$ests itsel$ i'
its e'viro'me't0 i' this case $,50y.

Ge'ome ,or chromosomes. is a speci$ic i'sta'ce
o$ a' ,50y. poi't0 i.e. ,D0 4:.=.

8it'ess is how well the poi't does i' its
e'viro'me't. A' this case0 how close $,50y. is to
the global mi'imum value ,i' this case0 3ero.

*he 8it'ess 8u'ctio'
+ef s,-affer.6(genome)/
%2y2 = genome01222 3 genome011222
t1 = mat-.sin(mat-.s4rt(%2y2))5
t2 = 1. 3 .12(%2y2)5
s,ore = ." 3 (t1222 $ .")6(t22t2)
ret7rn s,ore
genotype = G1DList.G1DList(2)
genotype.set#arams(rangemin=$1., rangema%=1.,
8estra9s,ore=., ro7n++e,imal=4)
genotype.initiali&ator.set('nitiali&ators.G1DList'nitiali&ator(eal)
genotype.eval7ator.set(s,-affer.6)

*he 8it'ess 8u'ctio'
+ef s,-affer.6(genome)/
%2y2 = genome01222 3 genome011222
t1 = mat-.sin(mat-.s4rt(%2y2))5
t2 = 1. 3 .12(%2y2)5
s,ore = ." 3 (t1222 $ .")6(t22t2)
ret7rn s,ore
genotype = G1DList.G1DList(2)
genotype.set#arams(rangemin=$1., rangema%=1.,
8estra9s,ore=., ro7n++e,imal=4)
genotype.initiali&ator.set('nitiali&ators.G1DList'nitiali&ator(eal)
genotype.eval7ator.set(s,-affer.6)

*he "etri 6ish

*he G( E'gi'e
from pyevolve import GSimpleG:
; ,reate genome ;
ga = GSimpleG:.GSimpleG:(genome, see+=123)
ga.set<inima%(=onsts.minima%)ype0>minimi&e>1)
ga.evolve(fre4*stats=1)
print ga.8est'n+ivi+7al()
?7tp7t/
Gen. (.@)/ <a%6<in6:vg .itness((a9) 0.6(.!2)6.3A(.B)6."(.")1
Gen. 1 (12."@)/ <a%6<in6:vg .itness((a9) 0.3(.BA)6.23(.1)6.2"(.2")1
Gen. 2 (2".@)/ <a%6<in6:vg .itness((a9) 0.21(.BB)6.1A(.1)6.1!(.1!)1
Gen. 3 (3A."@)/ <a%6<in6:vg .itness((a9) 0.26(.BB)6.21(.)6.22(.22)1
Cvol7tion stoppe+ 8y )ermination =riteria f7n,tion D
Gen. 323 (4.4@)/ <a%6<in6:vg .itness((a9) 0.3(.BB)6.23(.)6.2"(.2")1
)otal time elapse+/ 14.3"A se,on+s.
$ GenomeBase
S,ore/ ."
.itness/ .232!!
$ G1DList
List si&e/ 2
List/ 0.2!!13B4"33!42BB, .43"!B6A62B"!46311

*he G( E'gi'e
from pyevolve import GSimpleG:
; ,reate genome ;
ga = GSimpleG:.GSimpleG:(genome, see+=123)
ga.set<inima%(=onsts.minima%)ype0>minimi&e>1)
ga.evolve(fre4*stats=1)
print ga.8est'n+ivi+7al()
?7tp7t/
Gen. (.@)/ <a%6<in6:vg .itness((a9) 0.6(.!2)6.3A(.B)6."(.")1
Gen. 1 (12."@)/ <a%6<in6:vg .itness((a9) 0.3(.BA)6.23(.1)6.2"(.2")1
Gen. 2 (2".@)/ <a%6<in6:vg .itness((a9) 0.21(.BB)6.1A(.1)6.1!(.1!)1
Gen. 3 (3A."@)/ <a%6<in6:vg .itness((a9) 0.26(.BB)6.21(.)6.22(.22)1
Cvol7tion stoppe+ 8y )ermination =riteria f7n,tion D
Gen. 323 (4.4@)/ <a%6<in6:vg .itness((a9) 0.3(.BB)6.23(.)6.2"(.2")1
)otal time elapse+/ 14.3"A se,on+s.
$ GenomeBase
S,ore/ ."
.itness/ .232!!
$ G1DList
List si&e/ 2
List/ 0.2!!13B4"33!42BB, .43"!B6A62B"!46311

*he Circle o$ -i$e

!electio'
E Gary -arso'0 8arside

"yevolve Built4A' !electio' Fperators

.an7 /election

'hoose the best 4!efault5

Uniform /election

'hoose at ran!om

3ournament /election

'hoose best from a ran!om subset of population

.oulette 8heel /election

0ore fit more li7ely to be chosen



!etti'g the selector
from pyevolve import Sele,tors
ga = GSimpleG:.GSimpleG:(genome)
ga.sele,tor.set(Sele,tors.G(o7letteE-eel)
gaselector is a 9Function/lot: ;t can accept any number of functions that will be
use! in or!er gaevaluator is another

*he Circle o$ -i$e

Crossover

*a7e two ,or more. i'dividuals a'd combi'e
them i' some way to create childre'

!i'gle "oi't Crossover

!i'gle "oi't Crossover

"yevolve Built4A' Crossover Fperators

<6 Bi'ary !tri'g


!i'gle "oi't Crossover0 *wo "oi't Crossover0 1'i$orm Crossover

<6 -ist
!i'gle "oi't Crossover0 *wo "oi't Crossover0 1'i$orm Crossover0 FG Crossover0 Edge
Hecombi'atio' Crossover0 Cut a'd Cross$ill Crossover0 Heal !BG Crossover

:6 -ist
1'i$orm Crossover0 !i'gle Vertical "oi't Crossover0 !i'gle /ori3o'tal "oi't Crossover

:6 Bi'ary !tri'g
1'i$orm Crossover0 !i'gle Vertical "oi't Crossover0 !i'gle /ori3o'tal "oi't Crossover

*ree
!i'gle "oi't Crossover0 !trict !i'gle "oi't Crossover

G" *ree

!i'gle "oi't Crossover



+utatio'

Bi'ary +utatio'

*ree +utatio'

"yevolve Built4A' +utatio' Fperators

$* ,inary /tring

/wap 0utator< Flip 0utator

=* ,inary /tring

/wap 0utator< Flip 0utator

$* List

/wap 0utator< ;nteger .ange 0utator< .eal .ange 0utator< ;nteger Gaussian
0utator< .eal Gaussian 0utator< ;nteger ,inary 0utator< Allele 0utator< /imple
;nversion 0utator

=* List

/wap 0utator< ;nteger Gaussian 0utator< .eal Gaussian 0utator< Allele 0utator<
;nteger .ange 0utator

3ree

/wap 0utator< ;nteger .ange 0utator< .eal .ange 0utator< ;nteger Gaussian
0utator< .eal Gaussian 0utator

GP 3ree

1peration 0utator< /ubtree mutator



Ge'etic "rograms are basically 9ust a type o$
Ge'etic (lgorithm

( ge'etic program is 9ust a tree with 'odes
,*hi'7 -A!".

)odes ca' be operators ,$u'ctio's. or termi'als
,accepti'g 'o i'puts.


!imple Fperator )ode E5amples

(dditio'0 !ubtractio'0 +ultiplicatio'0 6ivisio'

*wo i'puts

Cosi'e0 !i'e0 (bsolute Value

F'e i'put

+a50 +i'0 (verage

*wo i'puts ,or more.

A$ Greater *ha'0 A$ -ess *ha'

8our i'puts: i$ ( is greater tha' B the' C else 6



*ermi'al )ode E5amples

Ephemeral Co'sta'ts

Ge'erated at ru'time

!peci$ied Co'sta'ts

6e$i'ed by you

Variables

Value determi'ed whe' evaluated




Putting it all together:
'reating a better uns7ille! forecast

*efining 3he Problem

Columbus )e5t46ay /igh *emperatures

8rom 8ebruary :;;= to Ju'e :;<;

(verage (bsolute Error

*odayIs /igh: =.#J degrees

Climate (verage: #.C# degrees

BesterdayIs /igh: %.;< degrees

-ast BearIs /igh: <;.%# degrees

Ca' we beat that?



Create *he Ge'ome
genotype = G)ree.G)reeG#()
genotype.set#arams(ma%*+ept-=4, met-o+=>rampe+>)
genotype.eval7ator.set(eval*f7n,)

Create *he Ge'ome
genotype = G)ree.G)reeG#()
genotype.set#arams(ma%*+ept-=4, met-o+=>rampe+>)
genotype.eval7ator.set(eval*f7n,)

Create *he Ge'ome
genotype = G)ree.G)reeG#()
genotype.set#arams(ma%*+ept-=4, met-o+=>rampe+>)
genotype.eval7ator.set(eval*f7n,)

6e$i'e *he Fperators
FG)ree.gp+e,(representation=>3>, ,olor=>re+>)
+ef gp*a++(a, 8)/ ret7rn a38
FG)ree.gp+e,(representation=>$>, ,olor=>re+>)
+ef gp*s78(a, 8)/ ret7rn a$8
FG)ree.gp+e,(representation=>avg>, ,olor=>green>)
+ef gp*avg(a, 8)/ ret7rn (a38)62.
FG)ree.gp+e,(representation=>if*gt>, ,olor=>8l7e>)
+ef gp*gt(a, 8, ,, +)/
if aG8/
ret7rn ,
else/
ret7rn +

6e$i'e *he Fperators
FG)ree.gp+e,(representation=>3>, ,olor=>re+>)
+ef gp*a++(a, 8)/ ret7rn a38
FG)ree.gp+e,(representation=>$>, ,olor=>re+>)
+ef gp*s78(a, 8)/ ret7rn a$8
FG)ree.gp+e,(representation=>avg>, ,olor=>green>)
+ef gp*avg(a, 8)/ ret7rn (a38)62.
FG)ree.gp+e,(representation=>if*gt>, ,olor=>8l7e>)
+ef gp*gt(a, 8, ,, +)/
if aG8/
ret7rn ,
else/
ret7rn +

6e$i'e *he 8it'ess 8u'ctio'
+ef eval*f7n,(genome)/
,o+e*,omp = genome.get=ompile+=o+e()
error = .
,o7nt = .
for +ay, ), H, L, =, a,t7al in +ata/
fore,ast = eval(,o+e*,omp)
error 3= a8s(fore,ast$a,t7al)
,o7nt 3= 1
ret7rn error6,o7nt

6e$i'e *he 8it'ess 8u'ctio'
+ef eval*f7n,(genome)/
,o+e*,omp = genome.get=ompile+=o+e()
error = .
,o7nt = .
for +ay, ), H, L, =, a,t7al in +ata/
fore,ast = eval(,o+e*,omp)
error 3= a8s(fore,ast$a,t7al)
,o7nt 3= 1
ret7rn error6,o7nt

6e$i'e *he 8it'ess 8u'ctio'
+ef eval*f7n,(genome)/
,o+e*,omp = genome.get=ompile+=o+e()
error = .
,o7nt = .
for +ay, ), H, L, =, a,t7al in +ata/
fore,ast = eval(,o+e*,omp)
error 3= a8s(fore,ast$a,t7al)
,o7nt 3= 1
ret7rn error6,o7nt

A'itiali3e *he E'gi'e
ga = GSimpleG:.GSimpleG:(genotype)
ga.set#arams(gp*terminals = 0I)I, ILI, I=I1,
gp*f7n,tion*prefi% = >gp>)
ga.set<inima%(=onsts.minima%)ype0>minimi&e>1)
ga.setGenerations(")
ga.set=rossover(ate(1.)
ga.set<7tation(ate(.2")
ga.set#op7lationSi&e(!)

A'itiali3e *he E'gi'e
ga = GSimpleG:.GSimpleG:(genotype)
ga.set#arams(gp*terminals = 0I)I, ILI, I=I1,
gp*f7n,tion*prefi% = >gp>)
ga.set<inima%(=onsts.minima%)ype0>minimi&e>1)
ga.setGenerations(")
ga.set=rossover(ate(1.)
ga.set<7tation(ate(.2")
ga.set#op7lationSi&e(!)

A'itiali3e *he E'gi'e
ga = GSimpleG:.GSimpleG:(genotype)
ga.set#arams(gp*terminals = 0I)I, ILI, I=I1,
gp*f7n,tion*prefi% = >gp>)
ga.set<inima%(=onsts.minima%)ype0>minimi&e>1)
ga.setGenerations(")
ga.set=rossover(ate(1.)
ga.set<7tation(ate(.2")
ga.set#op7lationSi&e(!)

A'itiali3e *he E'gi'e
ga = GSimpleG:.GSimpleG:(genotype)
ga.set#arams(gp*terminals = 0I)I, ILI, I=I1,
gp*f7n,tion*prefi% = >gp>)
ga.set<inima%(=onsts.minima%)ype0>minimi&e>1)
ga.setGenerations(")
ga.set=rossover(ate(1.)
ga.set<7tation(ate(.2")
ga.set#op7lationSi&e(!)

Hu'
ga = GSimpleG:.GSimpleG:(genotype)
ga.set#arams(gp*terminals = 0IperI,IperlastI,I,lmI1,
gp*f7n,tion*prefi% = >gp>)
ga.set<inima%(=onsts.minima%)ype0>minimi&e>1)
ga.setGenerations(")
ga.set=rossover(ate(1.)
ga.set<7tation(ate(.2")
ga.set#op7lationSi&e(!)
ga.evolve(fre4*stats=1)
8est = ga.8est'n+ivi+7al()
print 8est

Hesults
(pyenv)efloe-rF,assini/J6+ev6evolve$9%6py,onK pyt-on step1.py
Gen. (.@)/ <a%6<in6:vg .itness((a9 01!.A4(3A".3)6!".2"(6.3B)6B.61(B.61)1
Gen. 1 (1.@)/ <a%6<in6:vg .itness((a9) 02.13(24B.B!)616.61(".A4)616.AA(16.AA)1
Gen. 2 (2.@)/ <a%6<in6:vg .itness((a9) 043.1(1!A.3!)634.4(".44)63".!4(3".!4)1
Gen. 3 (3.@)/ <a%6<in6:vg .itness((a9) 043.B3(2"."2)63"."4(".44)636.61(36.61)1
Gen. 4 (4.@)/ <a%6<in6:vg .itness((a9) 03"."!(1!A.4)62!.A4(".44)62B.6"(2B.6")1
Gen. " (".@)/ <a%6<in6:vg .itness((a9) 03"."1(1"6.22)62!.46(".44)62B."B(2B."B)1
Gen. 6 (6.@)/ <a%6<in6:vg .itness((a9) 04".4"(21!."3)636."1(".44)63A.!A(3A.!A)1
Gen. A (A.@)/ <a%6<in6:vg .itness((a9) 042.!1(23.4)634.3B(".44)63".6!(3".6!)1
Gen. ! (!.@)/ <a%6<in6:vg .itness((a9) 02A.!6(12".A)622.41(".44)623.22(23.22)1
Gen. B (B.@)/ <a%6<in6:vg .itness((a9) 023."!(1!A."!)61B.32(".44)61B.6"(1B.6")1
Gen. 1 (1.@)/ <a%6<in6:vg .itness((a9) 032.A2(1!!.44)626."3(".44)62A.26(2A.26)1

Hesults
$ G)ree
Leig-t/ 2
Mo+es/ "
G)reeMo+eBase 0=-il+s=21 $ 0gp*avg1
G)reeMo+eBase 0=-il+s=21 $ 0gp*avg1
G)reeMo+eBase 0=-il+s=1 $ 0=1
G)reeMo+eBase 0=-il+s=1 $ 0)1
G)reeMo+eBase 0=-il+s=1 $ 0)1
$ G)reeG#
C%pression/ gp*avg(gp*avg(=, )), ))

*he !hi'y !tu$$

A'teractive +ode
ga.set'ntera,tiveGeneration(")
GGG it.plotList#opS,ore(pop7lation)
GGG it.plot#opS,ore(pop7lation)
GGG popS,ores = it.get#opS,ores(pop7lation)

;nteractive 0o!e

G" *ree Graphs
Gtree.G)reeG#.9rite#op7lationDot(gp, filename,
>png>, , 1)
8est = ga.8est'n+ivi+7al()
8est.9riteDot'mage(>8est.png>)

G" *ree Graphs
Gtree.G)reeG#.9rite#op7lationDot(gp, filename,
>png>, , 1)
8est = ga.8est'n+ivi+7al()
8est.9riteDot'mage(>8est.png>)

'allbac7s
+ef step*,all8a,N(gp*engine)/
gen = gp*engine.get=7rrentGeneration()
if gen @ 1 == /
filename = >8est*OgenP.png>.format(gen=gen)
G)ree.G)reeG#.9rite#op7lationDot(gp*engine,
filename, >png>, , 1)
ga.step=all8a,N.set(step*,all8a,N)

'allbac7s
+ef step*,all8a,N(gp*engine)/
gen = gp*engine.get=7rrentGeneration()
if gen @ 1 == /
filename = >8est*OgenP.png>.format(gen=gen)
G)ree.G)reeG#.9rite#op7lationDot(gp*engine,
filename, >png>, , 1)
ga.step=all8a,N.set(step*,all8a,N)

6atabase (dapters
,sv*a+apter = DB:+apters.DB.ile=SQ(i+entify=>r7n1>,
filename=>stats.,sv>)
ga.setDB:+apter(,sv*a+apter)
ga.evolve(fre4*stats=1)
print ga.getStatisti,s()
$ Statisti,s
<inim7m ra9 s,ore = A.3"
.itness average = 16.43
<inim7m fitness = 16.32
(a9 s,ores varian,e = 13"2.22
Stan+ar+ +eviation of ra9 s,ores = 36.AA
:verage of ra9 s,ores = 16.43
<a%im7m fitness = 1B.A2
<a%im7m ra9 s,ore = 2B"."A

Heal4*ime "lots
a+apter = DB:+apters.DBQ#yt-onGrap-(i+entify=>r7n*1>,
fre47en,y = 1)
ga.setDB:+apter(a+apter)

Heal4*ime "lots

Post2Processing Genomes



CTT
:

<
:
CT

<
:
CT
TJ
:

<
:
T:

<
:
CT
<
:
T:
:

<
J
C
D
J
T<

<
J
C
D
J
T<

<
J
C
D
J
T<

<
J
C
D
J
T<


'omparison

Average absolute error:

1ur GP: )>% !egrees

*odayIs /igh: =.#J degrees

Climate (verage: #.C# degrees

BesterdayIs /igh: %.;< degrees

-ast BearIs /igh: <;.%# degrees

Amproved o' persiste'ce $orecast ,todayIs high will


be tomorrowIs high. by ;.DC degrees.

(verage s7illed $orecast is about D degrees error



*he Challe'ge

=
*here is a supercomputer u'der your des7

*here is a supercomputer u'der your des7
CM-5 Dell T-3500
Year <>>D :;<;
CPUs D: J
MHz D: :>DD
Architecture !"(HC 5%C4CJ
Memory <GB <:GB
MFLOPS J0;>C J#0;;;
Cost K<0J;;0;;; K:0;;;




1se the C"1L

&ith *his )ew "ower Comes Hespo'sibility
http://57cd.com/=DJ/

EvoGuido

A'spired by Evo-isa0 used $irst picture retur'ed by Google o$ B68- ,you ca' ru' with a'y pic.

/ac7ed a little last 'ight0 'eeds wor7

&ill have time !u'day to wor7 o' ,but hac7 away M $ramewor7 thereL.

Amproveme'ts i'clude allowi'g a varyi'g 'umber o$ polygo's0 better mutatio' a'd crossover ops
,i'cludi'g polygo' move0 add0 remove.

www.i'tellovatio's.com/pyevolve
http://is.gd/pyevolve http://bit.ly/g/JhNC
ericOi'tellovatio's.com
O8orecast&atch

Vous aimerez peut-être aussi