📄 eolesson5.html
字号:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"><html><head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="GENERATOR" content="Mozilla/4.78 [en] (X11; U; Linux 2.4.7-10 i686) [Netscape]"> <title>Tutorial: Lesson 5</title></head><body text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000" background="beige009.jpg"><a href="eoLesson4.html">Lesson 4</a> -<a href="eoLesson6.html">Lesson6</a> -<a href="eoTutorial.html">Main page</a> -<a href="eoTopDown.html">Algorithm-Based</a>- <a href="eoBottomUp.html">Component-Based</a> - <a href="eoProgramming.html">Hints</a>- <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/index.html">EOdocumentation</a></font></font></b><br><hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- --><center><h1><font color="#FF0000">Tutorial Lesson 5: using your own genotype</font></h1></center>In this lesson, you will learn how to design and evolve <b><font color="#FF0000">yourown genotype structure</font></b>. Note that at the moment, only algorithmsinvolving a scalar fitness (double) are implemented (see test dir for Paretooptimization of multiple-objective fitness - or be patient :-)<p>The minimum code you'll have to write is first, of course, the codefor the <a href="#genotype">genotype structure</a>. Then, the representation-dependentcode: <a href="#initialization">intialization procedure(s)</a>,<a href="#variation">variationoperators</a> (quadratic crossover, mutation operator), ... and <a href="#evaluation">evaluationfunction</a> - and that's it : we have prepared some template files andthe script create.sh that will take care of generating a few other filesto make your application complete.<p>In what follows, we will suppose that you want to evolve some data structure,and that you have enough programming skills to be able to write C codefor its random initilialization, its crossover, its mutation and the computationof its fitness.<br>The examples will be described supposing you want to evolve ... bitstingsto solve the OneMax problem (oh no!!!).<p><b><blink><font color="#FF0000">New</font></blink></b> May 2004 : Asecond script, <b><tt><font color="#993300">createSimple</font></tt></b>, was added some time ago, that generates much simplerset of files, and the <font color="#3366FF"><b>stat.tmpl</b></font> file is now used to allow you to <font color="#FF0000"><b>compute</b></font> and <font color="#FF0000"><b>print</b></font> and <font color="#FF0000"><b>save-to-disk</b></font> and <font color="#FF0000"><b>plot-on-line</b></font> your own <font color="#3366FF"><b>statistics</b></font>. But you'll have to find out by yourself how those work, sorry, notime. It should be easy by just looking at the code (in main file, andin <b><tt><font color="#993300">OneMaxEA.cpp</font></tt></b> and the newly created <b><tt><font color="#993300">eoOneMaxStat.h</font></tt></b>.<br><br><b><blink><font color="#FF0000">New</font></blink></b> May 2004 : Inthe same simplified main file (e.g. <b><tt><fontcolor="#993300">OneMaxEA.cpp</font></tt></b> after running <b><tt><font color="#FF6666">./createsimple OneMax</font></tt></b> in dir <b><tt><fontcolor="#FF6666"><b>.../eo/tutorial/Templates</b></font></tt></b>), youwill also be able to use fitness sharing (together with roulette) as apossible selector.<hr WIDTH="100%"><a NAME="using"></a><b><font color="#000099"><font size=+2>Usingtemplate files</font></font></b><br>Follow this very simple procedure:<ul><li>choose a <b><font color="#FF6600">name</font></b> for you application -here <b><font color="#FF6600">OneMax</font></b> will be used</li><li>go to the <b><tt><font color="#993300">tutorial/Templates</font></tt></b>dir</li><br><tt><font color="#FF6666"> <b>cdpathWhereEOisInstalled/tutorial/Templates</b></font></tt><li>run the <b><tt><font color="#993300">create.sh</font></tt></b> script withargument OneMax and second optional argument a directory name (supposeit's called APPLICATION here)</li><br><b><tt><font color="#FF6666"> ./create.sh OneMax APPLICATION</font></tt></b><br>This will create a directory <b><tt><font color="#993300">tutorial/APPLICATION</font></tt></b><li>Go to the APPLICATION directory</li><br><b><tt><font color="#FF6666"> cd ../APPLICATION</font></tt></b><br>You should see the following files:<br> <b><tt><font color="#993300">OneMaxEA.cpp </font></tt></b>the main main file, includes all other, to be compiled<br> <b><tt><font color="#993300">Makefile </font></tt></b>with default target eoOneMaxEA<br> <b><tt><font color="#993300">eoOneMax.h </font></tt></b>class eoOneMax, the genotype<br> <b><tt><font color="#993300">eoOneMaxEvalFunc.h </font></tt></b>class for the computation of fitness<br> <b><tt><font color="#993300">eoOneMaxInit.h </font></tt></b>class for genotype initlialization<br> <b><tt><font color="#993300">eoOneMaxMutation.h </font></tt></b>class for mutation<br> <b><tt><font color="#993300">eoOneMaxQuadCrossover.h</font></tt></b>classfor (quadratic) crossover<br> <b><tt><font color="#993300">make_genotype_OneMax.h</font></tt></b>helper function that create the initializer<br> <b><tt><font color="#993300">make_op_OneMax.h </font></tt></b>helper function that handles the rates of application ofthe variation operators<br> <b><tt><font color="#993300">OneMaxLibEA.cpp </font></tt></b>another main file, for separate compilation of representation-independentstuff<br> <b><tt><font color="#993300">make_OneMaxEA.cpp </font></tt></b>the source for the representation-independent stuff<br> <li>Compile the whole application</li><br><b><tt><font color="#FF6666"> make</font></tt></b><br>and you should have no error there and see a new executable file named<b><tt><font color="#FF6666">OneMaxEA</font></tt></b>.You can run it and ... it will do nothing (what did you expect???).<br><BR><li>Now you should go and edit the files. The minimal changes that youwill need are</li><br> in <b><tt><font color="#993300">eoOneMax.h </font></tt></b>define your genotype<br> in <b><tt><font color="#993300">eoOneMaxInit.h </font></tt></b>define the initialization of one genotype<br> in <b><tt><font color="#993300">eoOneMaxMutation.h </font></tt></b>define the mutation of one genotype<br> in <b><tt><font color="#993300">eoOneMaxQuadCrossover.h </font></tt></b>define the crossover of 2 genotypes</ul><font color="#FF0000"><font size=+1>Smooth application building:</font></font><ul><li>After editing a particular file, <b><font color="#FF6600">compile</font></b>the whole thing immediately (by running make) <b><font color="#FF6600">andrun</font></b> the algorithm, to validate your code</li><li>In each file, start by only adding code between keyword-pairs <b><tt><font color="#993300">START</font></tt></b>and <b><font color="#993300">END</font></b></li></ul>We shall now take a look in turn at the 4 files mentionned above, thendescribe rapidly the other files, especially the main files.<br><hr WIDTH="100%"><a NAME="genotype"></a><b><font color="#000099"><font size=+2>Genotype- and its pre-requisites: </font></font><tt><font color="#993300"><font size=+1>eoOneMax.h</font></font></tt></b><p>First thing is to write the code for <font color="#999900"><b>the structureof the genotype</b>.</font> This is done by filling in the template file<a href="eoOneMax.html">eoOneMax.h</a>.There are 4 places that you should consider filling in:<ul><li>Of course, <a href="eoOneMax.html#data">the data</a> that will build upthe genotype. The convention in EO is to have it at the end of the classdefinition, and as private data.</li><li>The <a href="eoOneMax.html#constructor">default constructor</a> of an instance.Note that <b><font color="#FF6600">you must provide a default constructor</font></b>,and that no other constructor will be called within EO code. you mightthink you need some other constructor to initialize problem-specific data.This should be rather done in the eoInit object used to randomly initializeall individuals, or in the eoEvalFunc object, used to compute the fitness.</li><li>The <a href="eoOneMax.html#print">printOn</a> method, that writes the objectto a stream. You<font color="#000000"> must</font><b><font color="#FF6600">call the EO::printOn</font></b> method that will take care of the fitness,and then write the specific code for your data structure.</li><li>The <a href="eoOneMax.html#read">readFrom</a> method, that will read anobject from a stream. Again, you should <b><font color="#FF6600">call theEO::readFrom</font></b> method first to take care of the fitness.</li><br>Also note that readFrom will be generally called from an object thatwill have been constructed through the default constructor. This is whythe EO objects that we provide always start printing structures with theirlength...</ul>You can of course also add <a href="eoOneMax.html#destructor">a destructor</a>if needed, and any other helper method. For instance, you will probablyconsider adding <b><font color="#FF6600">accessors and setters</font></b>for the private data - unless you prefer to make everything public :-(<br>See now an example of <a href="eoOneMax_complete.html">a comple eoOneMax.h</a>file. Note that this is the only "colored" completed file we will show,you will have to go to the <b><tt><font color="#993300">.../tutorial/OneMax</font></tt></b>dir to <a href="../OneMax">browse all files at once</a>.<br><hr WIDTH="100%"><a NAME="initialization"></a><b><font color="#000099"><font size=+2>Initialization:</font></font></b><p><font size=+1><font color="#FF0000">Initializer: </font><b><tt><font color="#993300">eoOneMaxInit.h</font></tt></b></font><br>You must provide an <b><tt><font color="#CC33CC">eoInit</font></tt></b>object for your genotype, that is an object that will <b><font color="#FF6600">randomize
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -