📄 eolesson1.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.76 [en] (X11; U; Linux 2.2.17-21mdksmp i686) [Netscape]"> <title>Tutorial: Lesson 1</title></head><body text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000" background="beige009.jpg"><a href="eoLesson2.html">Lesson 2</a> -<a href="eoTutorial.html">Tutorialmain page</a> -<a href="eoTopDown.html">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based</a>- <a href="eoProgramming.html">Programming hints</a> -<font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/index.html">EOdocumentation</a></font></font><br><hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- --><center><h1><font color="#FF0000">Tutorial: Lesson 1</font></h1></center>This lesson will let you<ul><li><a href="#run">run</a> your first Evolutionary Algorithm written withinEO Library, choosing between evolving <font color="#999900">bitstrings</font>,or evolving <font color="#999900">vectors of real numbers</font>,</li><li><a href="#browse">browse</a> through the code of these algorithms, or</li><li>follow the <a href="#tour">guided tour</a>.</li></ul>Later you will be asked to<ul><li><a href="#exercise1">write</a> your own <font color="#990000">fitness function</font>,</li><li><a href="#exercise2">check</a> that EO let you separate the <font color="#999900">representation</font>from the <font color="#009900">evolution engine</font>.</li><li><a href="#exercise3">use different kinds</a> of <font color="#009900">selectionprocedures</font> in the framework of a generational GA <font color="#009900">evolutionengine</font>,</li></ul><h3><hr WIDTH="100%"><a NAME="run"></a><font color="#000099">I want to runan Evolutionary Algorithm<b>now</b></font></h3>You can choose to run a standard <a href="FirstBitGA.html">bitstring GeneticAlgorithm</a> (as defined in Goldberg's book) or a standard <a href="FirstRealGA.html">real-valuedgenetic algorithm</a>, as proposed in Micahlewicz's book.<p>If you have not already done what was recommended in the <a href="eoTutorial.html#install">Tutorialmain page</a> , do it <b><font color="#FF6600">NOW</font></b>. Then goto the Lesson1 sub-dir of the tutorial dir, and simply type at the systemprompt<p><tt>(myname@myhost) EOdir/Tutorial/Lesson1 % <font color="#FF6666">FirstRealGA</font></tt><br>or<br><tt>(myname@myhost) EOdir/Tutorial/Lesson1 % <font color="#FF6666">FirstBitGA</font></tt><p>and something should happen.<h3><font color="#000099">What is happening?</font></h3>At the moment, the <font color="#FF6600">FirstBitGA</font> maximizes thenumber of ones in the bitstring (also calls the <font color="#FF6600">OneMaxfunction</font>,whose solution is, as you can guess,<font color="#FF6600">11111111</font>),and the <font color="#FF6600">FirstRealGA</font> is maximizing (the defaultaction for EO is to maximize) the inverse of the sum (i.e. minimizing thesum) of the square of its variables (also called the <font color="#FF6600">spherefunction</font>, whose solution is ... <font color="#FF6600">all zeroes</font>).<p>And what you see on the screen when running one of these two programsis, in each case, the initial and final population of an Evolutionary run,one individual per line, its fitness first, then the number of items (bitsor real numbers) of the genotype, and the genotype itself. The final populationhopefully contains the solution in the discrete case, and is close to itin the continuous case.<br><hr WIDTH="100%"><a NAME="browse"></a><b><font color="#000099"><font size=+1>Browsingthe code:</font></font></b><p>Now you need to take a look at the program codes, either by browsingalone through the sources for <a href="FirstBitGA.html">FirstBitGA</a>and <a href="FirstRealGA.html">FirstRealGA</a>, or by following the guidedtour below. You might prefer to go directly to the <a href="#exercise1">exercises</a>.<h3><hr WIDTH="100%"><a NAME="tour"></a><font color="#000099">Guided tour:</font></h3><ul><li><b><font color="#993300">General includes:</font></b><font color="#000000">Likeall C-like code, the file starts with include directives (<a href="FirstBitGA.html#start">Bit</a>- <a href="FirstRealGA.html#start">Real</a>). Apart from standard includes,the specific file to include is </font><b><font color="#FF6600">eo</font></b><font color="#000000">:this is a file that contains the list of the all important representation-independentEO files.</font></li><br> <li><b><font color="#999900">Representation</font></b><font color="#000000">:you then have to declare the type of individuals you will be handling.All evolution-related objects you will need are templatized w.r.t. thetype of individuals.</font></li><ul><li><a href="FirstBitGA.html#representation">Bit</a> You first need to includethe representation-dependent file <b><tt><font color="#993300"><font size=+1>ga.h</font></font></tt></b>,containing the bitstring representation and operators. More details about<a href="eoRepresentation.html#bitstring">what is available for bitstringshere</a>.<br>You then say that you will be handling <b><font face="Arial,Helvetica"><a href="../../doc/html/classeo_bit.html">Bitstringgenotypes</a></font></b>, whose fitness is a double. This makes Indi derivefrom the <a href="eoProgramming.html#STL">STL class</a> <font color="#993300">vector<bool></font></li><li><a href="FirstRealGA.html#representation">Real</a> You first need to includethe representation-dependent file <b><tt><font color="#993300"><font size=+1>es.h</font></font></tt></b>,containing the bitstring representation and operators. More details about<a href="eoRepresentation.html#real">what is available for real-valueshere</a>.<br>You then say that you will be handling <b><font face="Arial,Helvetica"><a href="../../doc/html/classeo_real.html">Real-valuedgenotypes</a></font></b>, whose fitness is a double. This makes Indi derivefrom the <a href="eoProgramming.html#STL">STL class</a> <font color="#993300">vector<double></font></li><br> </ul><li><b><font color="#CC0000">Fitness function:</font></b><font color="#000000">the code for the fitness function is included in the file. It must takeas argument a reference to an individual (at the moment).</font></li><ul><li><a href="FirstBitGA.html#evalfunc">Bit</a> This function simply computesthe number of ones of the bitstring (it's called the OneMax function).The optimum is of course the all-ones bitstring.</li><li><a href="FirstRealGA.html#evalfunc">Real</a> This function simply computesthe inverse of the sum of the squares of all variables (also called thesphere function). The optimum is of course the all-zeroes vector.</li><br> </ul><li><a NAME="parametres"></a><b><font color="#3366FF">Parameters</font></b><font color="#000000">:all parameters of the algorithm are declared here (<a href="FirstBitGA.html#parametres">Bit</a>- <a href="FirstRealGA.html#parametres">Real</a>), and their values andassigned. Of course, this means that you will need to recompile to changethese values - see Lesson 3 to get rid of that heavy requirement.</font></li><br> <li><b><font color="#990000">Random seeding:</font></b><font color="#000000">Random numbers play an important role in Evolutionary Algorithms. See in<a href="eoProgramming.html#random">EOprogramming hints</a> more details about how this is simulated in EO -but as far as you are concerned now, remember that the </font><font color="#660000">globalRandom Number Generator</font><font color="#000000"> is called </font><b><tt><font color="#660000"><font size=+1>rng</font></font></tt></b><font color="#000000">and should be used everywhere you need a realization of a random variableof known law. Moreover, this RNG requires a </font><b><tt><font color="#660000"><font size=+1>seed</font></font></tt></b><font color="#000000">,which is set here (<a href="FirstBitGA.html#random">Bit</a> - <a href="FirstRealGA.html#random">Real</a>):every time you run the algorithm with the </font><font color="#FF6600">sameseed</font><font color="#000000">, you will get the </font><font color="#FF6600">sameresult</font><font color="#000000">. Hence, to test the robustness of youralgorithm, you should run it with different seeds. This is rather timeconsuming in the present programs, so we suggest that you wait until Lesson3 to do so.</font></li><br> <li><b><font color="#CC0000">Fitness function encapsulation: </font></b><font color="#000000">EOis based on the notion of <a href="eoProgramming.html#functors">functors</a>- hence you now need to encapsulate your fitness function into a functorobject. This is what is done here (<a href="FirstBitGA.html#eval">Bit</a>- <a href="FirstRealGA.html#eval">Real</a>).</font></li><br> <li><b><font color="#CC33CC">Initialization</font></b><font color="#000000">:to initialize the population, first declare an empty object of class </font><b><tt><font color="#990000">eoPop<Indi></font></tt></b><font color="#000000">,which is basically an <a href="eoProgramming.html#STL">STL</a> </font><b><tt><font color="#990000">vector<Indi></font></tt></b><font color="#000000">,then fill it with Indi's.</font> And remember that<b><tt><font color="#990000">v.push_back</font></tt></b>simply appends its argument at the end of <a href="eoProgramming.html#STL">STL</a>vector <b><tt><font color="#993300">v.</font></tt></b></li><ul><li><a href="FirstBitGA.html#init">Bit</a> <b><tt><font color="#990000">rng.flip()</font></tt></b>return a <font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_rng.html">randomboolean</a></font></font></li><li><a href="FirstRealGA.html#init">Real</a> <b><tt><font color="#990000">rng.uniform()</font></tt></b>returns a <font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_rng.html">realvalue uniformly drawn in [0,1].</a></font></font></li><br> </ul><li><b><font color="#3366FF">Output</font></b><font color="#000000">: takea snapshot at the initial population (<a href="FirstBitGA.html#output">Bit</a>- <a href="FirstRealGA.html#output">Real</a>). Sort it first, so the bestindividuals are first, and display it. Note that an eoPop has a </font><b><tt><font color="#990000"><<</font></tt></b><font color="#000000">method, which means that a simple </font><b><tt><font color="#990000">os<< pop </font></tt></b><font color="#000000">streams the </font><b><tt><font color="#990000">pop</font></tt></b><font color="#000000">onto the ostream </font><b><tt><font color="#990000">os</font></tt></b><font color="#000000">.This is true for </font><font color="#FF6600">all objects of of class </font><font color="#000000"><b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_printable.html">eoPrintable</a></font></font></b>(most EO objects) through the method </font><b><tt><font color="#990000">printOn</font></tt></b><font color="#000000">(which is then called by the </font><b><tt><font color="#990000"><<</font></tt></b><font color="#000000">operator).</font></li><br> <li><b><font color="#009900">Evolution engine:</font></b><font color="#000000">The selection/replacement mechanism (<a href="FirstBitGA.html#engine">Bit</a>- <a href="FirstRealGA.html#engine">Real</a>) is a simple generational
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -