没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
ABOUTTHISBOOK
Copyright©2016byClintonSheppard<fluentcoder@gmail.com>.
Allrightsreserved.Thisbookoranyportionthereofmaynotbereproduced,transmitted
inanyformorbyanyelectronicormechanicalmeansincludinginformationstorageand
retrieval systems, or used in any manner whatsoever without the express written
permissionofthepublisher.
FirstPrinting:2016
ClintonSheppard<fluentcoder@gmail.com>
Austin,Texas,USA
www.cs.unm.edu/~sheppard
The final code from each chapter is available at
https://github.com/handcraftsman/GeneticAlgorithmsWithPython, licensed under the
ApacheLicense,Version2.0.
The text of this book was written in AsciiDoc using Atom and AsciiDoc-Preview for
editingandconvertedtousingAsciiDoctor1.5.5.Thecodewaswrittenandtestedusing
JetBrains’ PyCharm IDE for Python. Some images were produced using GraphViz and
Paint.Net.
PREFACE
Genetic Algorithms with Python distills more than 5 years of experience using genetic
algorithms and helping others learn how to apply genetic algorithms, into a graduated
seriesoflessonsthatwillimparttoyouapowerfullife-longskill.Togetthemostoutof
thisbook youneed todo morethan simplyread along, you need totrainyour mind by
experimentingwiththecode.Thatmeanstypinginorcopyingthecodechangesasthey
areintroducedsoyoucantrydifferentthingsateachcheckpointbeforemovingontothe
next.Enjoy!
ClintonSheppard
ABRIEFINTRODUCTIONTO
GENETICALGORITHMS
Geneticalgorithmsareoneofthetoolswecanusetoapplymachinelearningtofinding
good, sometimes even optimal, solutions to problems that have billions of potential
solutions.Theyusebiologicalprocessesinsoftwaretofindanswerstoproblemsthathave
reallylargesearchspacesbycontinuouslygeneratingcandidatesolutions,evaluatinghow
wellthesolutionsfitthedesiredoutcome,andrefiningthebestsolutions.
Whensolvingaproblemwithageneticalgorithm,insteadofaskingforaspecificsolution,
youprovidecharacteristicsthatthesolutionmusthaveorrulesitssolutionmustpasstobe
accepted.Forexample,whenfillingamovingtruckyouprovideasetofruleslike:load
bigthingsfirst,distributetheweighttobothsides,putlightthingsontopbutnotloose,
andinterlockthingswithoddshapessotheydon’tmovearound.Themoreconstraintsyou
addthemorepotentialsolutionsareblocked.Supposeyousay:puttherefrigerator,washer
and dryer along the front left wall, load boxes of books along the front right wall,
mattresses down the middle, and clothes on top of the mattresses. These more specific
rulesdonotworkifyouareloadingpackages,orChristmastrees,butthepreviousgoal
orientedonesstilldo.
Goalorientedproblemsolving
Imagineyou are given10 chances toguessa number between1 and 1000andthe only
feedbackyouget is whether your guess is rightorwrong.Could you reliably guess the
number?Withonlyrightorwrongasfeedback,youhavenowaytoimproveyourguesses
soyouhaveatbesta1 in100chanceofguessingthenumber.Afundamentalaspectof
solvingproblemsusinggeneticalgorithms isthattheymustprovidefeedbackthat helps
the engine select the better of two guesses. That feedback is called the fitness, for how
closelytheguessfitsthedesiredresult.Moreimportantlyitimpliesageneralprogression.
Ifinsteadofrightorwrong as feedback you receive higherorlower indicating that the
numberishigherorlowerthanyourguess,youcanalwaysfindthenumberbecause10
guessesaresufficienttobinarysearchyourwaytoanynumberinthe1to1000range.
Now imagine multiplying the size of this problem so that instead of trying to find 1
numberyouaresimultaneouslytryingtofindasetof100numbers,allintherange1to
1000,your only receive backafitness value indicatinghow closely that setof numbers
matchesthedesiredoutcome.Yourgoalwouldbetomaximizeorminimizethatfitness.
Couldyoufindtherightsetof100numbers?Youmightbeabletodobetterthanrandom
guessingifyouhaveproblem-specificknowledgethathelpsyoueliminatecertainnumber
combinations.Usingproblem-specificknowledgetoguidethegeneticalgorithm’screation
andmodificationofpotentialsolutionscanhelpthemfindasolutionordersofmagnitude
faster.
Geneticalgorithmsandgeneticprogrammingarevery good at finding solutions to very
largeproblems.Theydoitbytakingmillionsofsamplesfromthesearchspace,making
smallchanges,possiblyrecombiningparts ofthe bestsolutions, comparingtheresultant
fitness against that of the current best solution, and keeping the better of the two. This
processrepeatsuntilastopconditionlikeoneofthefollowingoccurs:theknownsolution
isfound,asolutionmeetingallrequirementsisfound,acertainnumberofgenerationshas
passed,aspecificamountoftimehaspassed,etc.
Firstproject
Imagine you are asked to guess a 3-letter password; what kind of feedback would you
want? If the password is ‘aaa‘ and you guess ‘abc‘ what should the fitness value be?
Would something simple like how many of the letters in your guess are correct be
sufficient?Should‘bab‘,whichhasonecorrectletter,getabetterfitnessvaluethan‘zap‘,
alsoonecorrectletterbutthewronglettersarealphabeticallyfartheraway,orshouldthe
fitness be the same? These are some of the first decisions you have to make when
planning to implement a genetic algorithm to find a solution to your problem. Genetic
algorithms are good at finding good solutions to problems with large search spaces
becausetheycanquicklyfindthepartsoftheguessesthatimprovefitnessvaluesorlead
tobettersolutions.
Intheprojectabove,let’ssaythefitnessfunctionreturnsacountofthenumberofletters
thatmatchthepassword.Thatmeans‘abc‘,‘bab‘and‘zba‘allgetafitnessvalueofone,
becausetheyeachhaveonelettercorrect.Thegeneticalgorithmmightthencombinethe
firsttwolettersof‘abc‘withthelastletterof‘zba‘throughcrossover,tocreatetheguess
‘aba‘.Thefitnessfor that guess would be two because two letters matchthepassword.
Thealgorithmmightalsomutatethelastletterof‘zba‘toget‘zbc‘andafitnessvalueof
zero.Dependingonhowtheengineisconfigureditmightthrowout‘zbc‘immediately,it
mightkeepitinordertomaintaingeneticdiversity,orperhapsitwouldonlykeepitifitis
betterthansomecutofffitnessvaluewhencomparedwithalltheotherguessestriedbythe
engine.
We will look more at the password project in the first chapter, and go on to explore a
variety of projects to learn different ways of solving problems with genetic algorithms.
However,thisbookisnotaboutshowingyouahand-pickedsetofproblemsyoucansolve
withgeneticalgorithms.Itisaboutgivingyouexperiencemakinggeneticalgorithmswork
foryouusingsampleprojectsthatyouunderstandandcanfallbackuponwhenlearningto
useothermachinelearningtools andtechniques, orapplying geneticalgorithmsin your
ownfieldofexpertise.
GeneticprogrammingwithPython
This book uses the Python programming language to explore genetic algorithms. Why
Python?BecausePython is a low ceremony, powerfulandeasy-to-readlanguage whose
code can be understood by entry-level programmers. I explain the occasional Python
featurebutshouldyouencounteraprogrammingconstructyou’veneverseenbeforeand
can’t intuit, Python.organd StackOverflow.com are great places to find explanations. If
you have experience with another programming language then you should have no
difficultylearningPythonbyinductionwhilealsoexploringgeneticalgorithms.
剩余281页未读,继续阅读
资源评论
weixin_38290023
- 粉丝: 4
- 资源: 224
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功