Initial version of the tutorial.

Warning: all Makefile's are hand-made, and will only work in Linux
This commit is contained in:
evomarc 2000-11-29 18:19:57 +00:00
commit b8d4e7faef
34 changed files with 9174 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

View file

@ -0,0 +1,368 @@
<!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="cpp2html Marc Schoenauer">
<title>FirstBitEA.cpp</title>
<!-- Changed by: Marc Schoenauer, 28-Nov-2000 -->
</head>
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
documentation</a></font>
<br>
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
<center>
<h1>
<font color="#FF0000">FirstBitEA.cpp</font></h1></center>
Click on the figure to see the corresponding code.<br>
In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a><br>
The actual code is in boldface and the comment in normal face.
<br><img SRC="EA_tutorial.jpg" USEMAP="#Map" >
<map NAME="Map">
<!-- Init -->
<area SHAPE="rect" HREF="#init" COORDS="14,31,135,70">
<area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150">
<!-- main loop -->
<area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150">
<area SHAPE="rect" HREF="#output" COORDS="280,45,480,70">
<area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150">
<area SHAPE="rect" HREF="#select" COORDS="495,110,615,150">
<area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230">
<area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287">
<area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305">
<area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310">
<area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310">
<area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230">
<!-- Center of loop -->
<area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260">
<!-- 4 bottom lines -->
<area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370">
<area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370">
<area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397">
<area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423">
<area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450">
<area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450">
<area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450">
<area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378">
</map>
<br>&nbsp;
<A NAME="start"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr NOSAVE>
<td NOSAVE><tt><font color="#993300">
//-----------------------------------------------------------------------------<br>
// FirstBitEA.cpp<br>
//-----------------------------------------------------------------------------<br>
//*<br>
// Still an instance of a VERY simple Bitstring Genetic Algorithm <br>
// (see FirstBitGA.cpp) but now with &nbsp;Breeder - and Combined Ops<br>
//<br>
//-----------------------------------------------------------------------------<br>
// standard includes<br>
<b>#include &lt;stdexcept> &nbsp;</b>// runtime_error <br>
<b>#include &lt;iostream> &nbsp; &nbsp;</b>// cout<br>
<b>#include &lt;strstream> &nbsp;</b>// ostrstream, istrstream<br>
// the general include for eo<br>
<b>#include &lt;eo></b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="representation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
<tr>
<td>
<tt><font color="#999900">
//-----------------------------------------------------------------------------<br>
// define your individuals<br>
<b>typedef eoBin&lt;double> Indi; </b>// A bitstring with fitness double<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="evalfunc"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
<tr>
<td>
<tt><font color="#CC0000">
//-----------------------------------------------------------------------------<br>
// a simple fitness function that computes the number of ones of a bitstring<br>
// Now in a separate file, and declared as binary_value(const vector&lt;bool> &)<br>
<b>#include "binary_value.h"</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td>
<tt><font color="#993300">
//-----------------------------------------------------------------------------<br>
<b>void main_function(int argc, char **argv)</b><br>
<b>{</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="parametres"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
<b> &nbsp;const unsigned int SEED = 42; </b>// seed for random number generator<br>
<b> &nbsp;const unsigned int T_SIZE = 3; </b>// size for tournament selection<br>
<b> &nbsp;const unsigned int VEC_SIZE = 8; </b>// Number of bits in genotypes<br>
<b> &nbsp;const unsigned int POP_SIZE = 20; </b>// Size of population<br>
<b> &nbsp;const unsigned int MAX_GEN = 500; </b>// Maximum number of generation before STOP<br>
<b> &nbsp;const float CROSS_RATE = 0.8; </b>// Crossover rate<br>
<b> &nbsp;const double P_MUT_PER_BIT = 0.01; </b>// probability of bit-flip mutation<br>
<b> &nbsp;const float MUT_RATE = 1.0; </b>// mutation rate<br>
<b> &nbsp;</b>// some parameters for chosing among different operators<br>
<b> &nbsp;const double onePointRate = 0.5; &nbsp; &nbsp; &nbsp; &nbsp;</b>// rate for 1-pt Xover<br>
<b> &nbsp;const double twoPointsRate = 0.5; &nbsp; &nbsp; &nbsp; &nbsp;</b>// rate for 2-pt Xover<br>
<b> &nbsp;const double URate = 0.5; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// rate for Uniform Xover<br>
<b> &nbsp;const double bitFlipRate = 0.5; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// rate for bit-flip mutation<br>
<b> &nbsp;const double oneBitRate = 0.5; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// rate for one-bit mutation<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td>
<tt><font color="#993300">
<b> &nbsp;</b>//////////////////////////<br>
<b> &nbsp;</b>// &nbsp;Random seed<br>
<b> &nbsp;</b>//////////////////////////<br>
<b> &nbsp;</b>//reproducible random seed: if you don't change SEED above, <br>
<b> &nbsp;</b>// you'll aways get the same result, NOT a random run<br>
<b> &nbsp;rng.reseed(SEED);</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="eval"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
<tr>
<td>
<tt><font color="#CC0000">
<b> &nbsp;</b>/////////////////////////////<br>
<b> &nbsp;</b>// Fitness function<br>
<b> &nbsp;</b>////////////////////////////<br>
<b> &nbsp;</b>// Evaluation: from a plain C++ fn to an EvalFunc Object<br>
<b> &nbsp;</b>// you need to give the full description of the function<br>
<b> &nbsp;eoEvalFuncPtr&lt;Indi, double, const vector&lt;bool>& > eval( &nbsp;binary_value );</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="init"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td>
<tt><font color="#993399">
<b> &nbsp;</b>////////////////////////////////<br>
<b> &nbsp;</b>// Initilisation of population<br>
<b> &nbsp;</b>////////////////////////////////<br>
<b> &nbsp;</b>// based on boolean_generator class (see utils/rnd_generator.h)<br>
<b> &nbsp;eoInitFixedLength&lt;Indi, boolean_generator> </b><br>
<b> &nbsp; &nbsp; &nbsp;random(VEC_SIZE, boolean_generator());</b><br>
<b> &nbsp;</b>// Initialization of the population<br>
<b> &nbsp;eoPop&lt;Indi> pop(POP_SIZE, random);</b><br>
<b> &nbsp;</b>// and evaluate it in one loop<br>
<b> &nbsp;apply&lt;Indi>(eval, pop); </b>// STL syntax<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
<b> &nbsp;</b>// sort pop before printing it!<br>
<b> &nbsp;pop.sort();</b><br>
<b> &nbsp;</b>// Print (sorted) intial population (raw printout)<br>
<b> &nbsp;cout &lt;&lt; "Initial Population" &lt;&lt; endl;</b><br>
<b> &nbsp;cout &lt;&lt; pop;</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="engine"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
<tr>
<td>
<tt><font color="#009900">
<b> &nbsp;</b>/////////////////////////////////////<br>
<b> &nbsp;</b>// selection and replacement<br>
<b> &nbsp;</b>////////////////////////////////////<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="select"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
<tr>
<td>
<tt><font color="#009900">
<b> &nbsp;</b>// The robust tournament selection<br>
<b> &nbsp;eoDetTournament&lt;Indi> selectOne(T_SIZE); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// T_SIZE in [2,POP_SIZE]<br>
<a NAME="select_encapsulate"></a>
<b> &nbsp;</b>// is now encapsulated in a eoSelectPerc (entage)<br>
<b> &nbsp;eoSelectPerc&lt;Indi> select(selectOne);</b>// by default rate==1<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="replace"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
<tr>
<td>
<tt><font color="#009900">
<b> &nbsp;</b>// And we now have the full slection/replacement - though with <br>
<b> &nbsp;</b>// no replacement (== generational replacement) at the moment :-)<br>
<b> &nbsp;eoNoReplacement&lt;Indi> replace; </b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="operators"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td>
<tt><font color="#993399">
<b> &nbsp;</b>//////////////////////////////////////<br>
<b> &nbsp;</b>// The variation operators<br>
<b> &nbsp;</b>//////////////////////////////////////<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="crossover"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td>
<tt><font color="#993399">
<b> &nbsp;</b>// 1-point crossover for bitstring<br>
<b> &nbsp;eoBinCrossover&lt;Indi> xover1;</b><br>
<b> &nbsp;</b>// uniform crossover for bitstring<br>
<b> &nbsp;eoBinUxOver&lt;Indi> xoverU;</b><br>
<b> &nbsp;</b>// 2-pots xover<br>
<b> &nbsp;eoBinNxOver&lt;Indi> xover2(2);</b><br>
<b> &nbsp;</b>// Combine them with relative rates<br>
<b> &nbsp;eoPropCombinedQuadOp&lt;Indi> xover(xover1, onePointRate);</b><br>
<b> &nbsp;xover.add(xoverU, URate);</b><br>
<b> &nbsp;xover.add(xover2, twoPointsRate, true);</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="mutation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td>
<tt><font color="#993399">
<b> &nbsp;</b><br>
<b> &nbsp;</b>// standard bit-flip mutation for bitstring<br>
<b> &nbsp;eoBinMutation&lt;Indi> &nbsp;mutationBitFlip(P_MUT_PER_BIT);</b><br>
<b> &nbsp;</b>// mutate exactly 1 bit per individual<br>
<b> &nbsp;eoDetBitFlip&lt;Indi> mutationOneBit; </b><br>
<b> &nbsp;</b>// Combine them with relative rates<br>
<b> &nbsp;eoPropCombinedMonOp&lt;Indi> mutation(mutationBitFlip, bitFlipRate);</b><br>
<b> &nbsp;mutation.add(mutationOneBit, oneBitRate, true);</b><br>
<b> &nbsp;</b><br>
<a NAME="transform"></a>
<b> &nbsp;</b>// The operators are &nbsp;encapsulated into an eoTRansform object<br>
<b> &nbsp;eoSGATransform&lt;Indi> transform(xover, CROSS_RATE, mutation, MUT_RATE);</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="stop"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
</font></tt>
</td>
</tr>
</table>
<a NAME="checkpoint"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
<b> &nbsp;</b>//////////////////////////////////////<br>
<b> &nbsp;</b>// termination conditions: use more than one<br>
<b> &nbsp;</b>/////////////////////////////////////<br>
<b> &nbsp;</b>// stop after MAX_GEN generations<br>
<b> &nbsp;eoGenContinue&lt;Indi> genCont(MAX_GEN);</b><br>
<b> &nbsp;</b>// do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement<br>
<b> &nbsp;eoSteadyFitContinue&lt;Indi> steadyCont(MIN_GEN, STEADY_GEN);</b><br>
<b> &nbsp;</b>// stop when fitness reaches a target (here VEC_SIZE)<br>
<b> &nbsp;eoFitContinue&lt;Indi> fitCont(0);</b><br>
<b> &nbsp;</b>// do stop when one of the above says so<br>
<b> &nbsp;eoCombinedContinue&lt;Indi> continuator(genCont);</b><br>
<b> &nbsp;continuator.add(steadyCont);</b><br>
<b> &nbsp;continuator.add(fitCont);</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="generation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td>
<tt><font color="#FF6666">
<b> &nbsp;</b>/////////////////////////////////////////<br>
<b> &nbsp;</b>// the algorithm<br>
<b> &nbsp;</b>////////////////////////////////////////<br>
<b> &nbsp;</b>// Easy EA requires <br>
<b> &nbsp;</b>// selection, transformation, eval, replacement, and stopping criterion<br>
<b> &nbsp;eoEasyEA&lt;Indi> gga(continuator, eval, select, transform, replace);</b><br>
<b> &nbsp;</b>// Apply algo to pop - that's it!<br>
<b> &nbsp;gga(pop);</b><br>
<b> &nbsp;</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
<b> &nbsp;</b>// Print (sorted) intial population<br>
<b> &nbsp;pop.sort();</b><br>
<b> &nbsp;cout &lt;&lt; "FINAL Population\n" &lt;&lt; pop &lt;&lt; endl;</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td>
<tt><font color="#993300">
<b>}</b><br>
// A main that catches the exceptions<br>
<b>int main(int argc, char **argv)</b><br>
<b>{</b><br>
<b>#ifdef _MSC_VER</b><br>
<b> &nbsp;</b>// &nbsp;rng.reseed(42);<br>
<b> &nbsp; &nbsp; &nbsp;int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp;flag |= _CRTDBG_LEAK_CHECK_DF;</b><br>
<b> &nbsp; &nbsp; &nbsp;_CrtSetDbgFlag(flag);</b><br>
// &nbsp; &nbsp;_CrtSetBreakAlloc(100);<br>
<b>#endif</b><br>
<b> &nbsp; &nbsp; &nbsp;try</b><br>
<b> &nbsp; &nbsp; &nbsp;{</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;main_function(argc, argv);</b><br>
<b> &nbsp; &nbsp; &nbsp;}</b><br>
<b> &nbsp; &nbsp; &nbsp;catch(exception& e)</b><br>
<b> &nbsp; &nbsp; &nbsp;{</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &lt;&lt; "Exception: " &lt;&lt; e.what() &lt;&lt; '\n';</b><br>
<b> &nbsp; &nbsp; &nbsp;}</b><br>
<b> &nbsp; &nbsp; &nbsp;return 1;</b><br>
<b>}</b><br>
</font></font></font></td>
</tr>
</table>
<hr WIDTH="100%"><a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
documentation</a></font>
<hr>
<address>
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last
modified: Sun Nov 19 22:26:27 2000
<!-- hhmts end -->
</body>
</html>

View file

@ -0,0 +1,350 @@
<!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="cpp2html Marc Schoenauer">
<title>FirstBitGA.html</title>
<!-- Changed by: Marc Schoenauer, 29-Nov-2000 -->
</head>
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
documentation</a></font>
<br>
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
<center>
<h1>
<font color="#FF0000">FirstBitGA.html</font></h1></center>
Click on the figure to see the corresponding code.<br>
In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a><br>
The actual code is in boldface and the comment in normal face.
<br><img SRC="EA_tutorial.jpg" USEMAP="#Map" >
<map NAME="Map">
<!-- Init -->
<area SHAPE="rect" HREF="#init" COORDS="14,31,135,70">
<area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150">
<!-- main loop -->
<area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150">
<area SHAPE="rect" HREF="#output" COORDS="280,45,480,70">
<area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150">
<area SHAPE="rect" HREF="#select" COORDS="495,110,615,150">
<area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230">
<area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287">
<area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305">
<area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310">
<area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310">
<area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230">
<!-- Center of loop -->
<area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260">
<!-- 4 bottom lines -->
<area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370">
<area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370">
<area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397">
<area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423">
<area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450">
<area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450">
<area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450">
<area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378">
</map>
<br>&nbsp;
<A NAME="start"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr NOSAVE>
<td NOSAVE><tt><font color="#993300">
//-----------------------------------------------------------------------------<br>
// FirstBitGA.cpp<br>
//-----------------------------------------------------------------------------<br>
//*<br>
// An instance of a VERY simple Bitstring Genetic Algorithm<br>
//<br>
//-----------------------------------------------------------------------------<br>
// standard includes<br>
<b>#include &lt;stdexcept> &nbsp;</b>// runtime_error <br>
<b>#include &lt;iostream> &nbsp; &nbsp;</b>// cout<br>
<b>#include &lt;strstream> &nbsp;</b>// ostrstream, istrstream<br>
// the general include for eo<br>
<b>#include &lt;eo></b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="representation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
<tr>
<td>
<tt><font color="#999900">
//-----------------------------------------------------------------------------<br>
// define your individuals<br>
<b>typedef eoBin&lt;double> Indi; &nbsp; &nbsp; &nbsp; &nbsp;</b>// A bitstring with fitness double<br>
</font></tt>
</td>
</tr>
</table>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
<tr>
<td>
<tt><font color="#CC0000">
//-----------------------------------------------------------------------------<br>
// a simple fitness function that computes the number of ones of a bitstring<br>
// &nbsp;@param _indi A biststring individual<br>
<a name="evalfunc"></a>
<b>double binary_value(const Indi & _indi)</b><br>
<b>{</b><br>
<b> &nbsp;double sum = 0;</b><br>
<b> &nbsp;for (unsigned i = 0; i &lt; _indi.size(); i++)</b><br>
<b> &nbsp; &nbsp; &nbsp;sum += _indi[i];</b><br>
<b> &nbsp;return sum;</b><br>
<b>}</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td>
<tt><font color="#993300">
//-----------------------------------------------------------------------------<br>
<b>void main_function(int argc, char **argv)</b><br>
<b>{</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="parametres"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
<b> &nbsp;</b>// all parameters are hard-coded!<br>
<b> &nbsp;const unsigned int SEED = 42; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// seed for random number generator<br>
<b> &nbsp;const unsigned int T_SIZE = 3; &nbsp; &nbsp; &nbsp; &nbsp;</b>// size for tournament selection<br>
<b> &nbsp;const unsigned int VEC_SIZE = 8; &nbsp; &nbsp;</b>// Number of bits in genotypes<br>
<b> &nbsp;const unsigned int POP_SIZE = 20; &nbsp;</b>// Size of population<br>
<b> &nbsp;const unsigned int MAX_GEN = 100; &nbsp;</b>// Maximum number of generation before STOP<br>
<b> &nbsp;const float CROSS_RATE = 0.8; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// Crossover rate<br>
<b> &nbsp;const double P_MUT_PER_BIT = 0.01; </b>// probability of bit-flip mutation<br>
<b> &nbsp;const float MUT_RATE = 1.0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// mutation rate<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td>
<tt><font color="#993300">
<a NAME="random"></a>
<b> &nbsp;</b>//////////////////////////<br>
<b> &nbsp;</b>// &nbsp;Random seed<br>
<b> &nbsp;</b>//////////////////////////<br>
<b> &nbsp;</b>//reproducible random seed: if you don't change SEED above, <br>
<b> &nbsp;</b>// you'll aways get the same result, NOT a random run<br>
<b> &nbsp;rng.reseed(SEED);</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="eval"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
<tr>
<td>
<tt><font color="#CC0000">
<b> &nbsp;</b>/////////////////////////////<br>
<b> &nbsp;</b>// Fitness function<br>
<b> &nbsp;</b>////////////////////////////<br>
<b> &nbsp;</b>// Evaluation: from a plain C++ fn to an EvalFunc Object<br>
<b> &nbsp;eoEvalFuncPtr&lt;Indi> eval( &nbsp;binary_value );</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="init"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td>
<tt><font color="#993399">
<b> &nbsp;</b>////////////////////////////////<br>
<b> &nbsp;</b>// Initilisation of population<br>
<b> &nbsp;</b>////////////////////////////////<br>
<b> &nbsp;</b>// declare the population<br>
<b> &nbsp;eoPop&lt;Indi> pop;</b><br>
<b> &nbsp;</b>// fill it!<br>
<b> &nbsp;for (unsigned int igeno=0; igeno&lt;POP_SIZE; igeno++)</b><br>
<b> &nbsp; &nbsp; &nbsp;{</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Indi v; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// void individual, to be filled<br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (unsigned ivar=0; ivar&lt;VEC_SIZE; ivar++)</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bool r = rng.flip(); </b>// new value, random in {0,1}<br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;v.push_back(r); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// append that random value to v<br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eval(v); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// evaluate it<br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pop.push_back(v); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// and put it in the population<br>
<b> &nbsp; &nbsp; &nbsp;}</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
<b> &nbsp;</b>// sort pop before printing it!<br>
<b> &nbsp;pop.sort();</b><br>
<b> &nbsp;</b>// Print (sorted) intial population (raw printout)<br>
<b> &nbsp;cout &lt;&lt; "Initial Population" &lt;&lt; endl;</b><br>
<b> &nbsp;cout &lt;&lt; pop;</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="engine"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
<tr>
<td>
<tt><font color="#009900">
<b> &nbsp;</b>/////////////////////////////////////<br>
<b> &nbsp;</b>// selection and replacement<br>
<b> &nbsp;</b>////////////////////////////////////<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="select"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
<tr>
<td>
<tt><font color="#009900">
<b> &nbsp;</b>// The robust tournament selection<br>
<b> &nbsp;eoDetTournament&lt;Indi> select(T_SIZE); &nbsp;</b>// T_SIZE in [2,POP_SIZE]<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="replace"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
<tr>
<td>
<tt><font color="#009900">
<b> &nbsp;</b>// The simple GA evolution engine uses generational replacement<br>
<b> &nbsp;</b>// so no replacement procedure is needed<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="stop"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
</font></tt>
</td>
</tr>
</table>
<a NAME="operators"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td>
<tt><font color="#993399">
<b> &nbsp;</b>//////////////////////////////////////<br>
<b> &nbsp;</b>// The variation operators<br>
<b> &nbsp;</b>//////////////////////////////////////<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="crossover"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td>
<tt><font color="#993399">
<b> &nbsp;</b>// 1-point mutation for bitstring<br>
<b> &nbsp;eoBinCrossover&lt;Indi> xover;</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="mutation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td>
<tt><font color="#993399">
<b> &nbsp;</b><br>
<b> &nbsp;</b>// standard bit-flip mutation for bitstring<br>
<b> &nbsp;eoBinMutation&lt;Indi> &nbsp;mutation(P_MUT_PER_BIT);</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="checkpoint"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
<b> &nbsp;</b>//////////////////////////////////////<br>
<b> &nbsp;</b>// termination condition<br>
<b> &nbsp;</b>/////////////////////////////////////<br>
<b> &nbsp;</b>// stop after MAX_GEN generations<br>
<b> &nbsp;eoGenContinue&lt;Indi> continuator(MAX_GEN);</b><br>
<b> &nbsp;</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="generation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td>
<tt><font color="#FF6666">
<b> &nbsp;</b>/////////////////////////////////////////<br>
<b> &nbsp;</b>// the algorithm<br>
<b> &nbsp;</b>////////////////////////////////////////<br>
<b> &nbsp;</b>// standard Generational GA requires as parameters<br>
<b> &nbsp;</b>// selection, evaluation, crossover and mutation, stopping criterion<br>
<b> </b><br>
<b> &nbsp;eoSGA&lt;Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE, </b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eval, continuator);</b><br>
<b> &nbsp;</b>// Apply algo to pop - that's it!<br>
<b> &nbsp;gga(pop);</b><br>
<b> &nbsp;</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="final_output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
<b> &nbsp;</b>// Print (sorted) intial population<br>
<b> &nbsp;pop.sort();</b><br>
<b> &nbsp;cout &lt;&lt; "FINAL Population\n" &lt;&lt; pop &lt;&lt; endl;</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="main"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td>
<tt><font color="#993300">
<b>}</b><br>
<b> </b>// A main that catches the exceptions<br>
<b>int main(int argc, char **argv)</b><br>
<b>{</b><br>
<b>#ifdef _MSC_VER</b><br>
<b> &nbsp;</b>// &nbsp;rng.reseed(42);<br>
<b> &nbsp; &nbsp; &nbsp;int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp;flag |= _CRTDBG_LEAK_CHECK_DF;</b><br>
<b> &nbsp; &nbsp; &nbsp;_CrtSetDbgFlag(flag);</b><br>
// &nbsp; &nbsp;_CrtSetBreakAlloc(100);<br>
<b>#endif</b><br>
<b> &nbsp; &nbsp; &nbsp;try</b><br>
<b> &nbsp; &nbsp; &nbsp;{</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;main_function(argc, argv);</b><br>
<b> &nbsp; &nbsp; &nbsp;}</b><br>
<b> &nbsp; &nbsp; &nbsp;catch(exception& e)</b><br>
<b> &nbsp; &nbsp; &nbsp;{</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &lt;&lt; "Exception: " &lt;&lt; e.what() &lt;&lt; '\n';</b><br>
<b> &nbsp; &nbsp; &nbsp;}</b><br>
<b> &nbsp; &nbsp; &nbsp;return 1;</b><br>
<b>}</b><br>
</font></font></font></td>
</tr>
</table>
<hr WIDTH="100%"><a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
documentation</a></font>
<hr>
<address>
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last
modified: Sun Nov 19 08:31:26 2000
<!-- hhmts end -->
</body>
</html>

View file

@ -0,0 +1,366 @@
<!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="cpp2html Marc Schoenauer">
<title>FirstRealEA.cpp</title>
</head>
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
documentation</a></font>
<br>
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
<center>
<h1>
<font color="#FF0000">FirstRealEA.cpp</font></h1></center>
Click on the figure to see the corresponding code.<br>
In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a><br>
The actual code is in boldface and the comment in normal face.
<br><img SRC="EA_tutorial.jpg" USEMAP="#Map" >
<map NAME="Map">
<!-- Init -->
<area SHAPE="rect" HREF="#init" COORDS="14,31,135,70">
<area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150">
<!-- main loop -->
<area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150">
<area SHAPE="rect" HREF="#output" COORDS="280,45,480,70">
<area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150">
<area SHAPE="rect" HREF="#select" COORDS="495,110,615,150">
<area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230">
<area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287">
<area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305">
<area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310">
<area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310">
<area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230">
<!-- Center of loop -->
<area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260">
<!-- 4 bottom lines -->
<area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370">
<area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370">
<area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397">
<area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423">
<area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450">
<area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450">
<area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450">
<area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378">
</map>
<br>&nbsp;
<A NAME="start"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr NOSAVE>
<td NOSAVE><tt><font color="#993300">
//-----------------------------------------------------------------------------<br>
// FirstRealEA.cpp<br>
//-----------------------------------------------------------------------------<br>
//*<br>
// Still an instance of a VERY simple Real-coded &nbsp;Genetic Algorithm <br>
// (see FirstBitGA.cpp) but now with &nbsp;Breeder - and Combined Ops<br>
//<br>
//-----------------------------------------------------------------------------<br>
// standard includes<br>
<b>#include &lt;stdexcept> &nbsp;</b>// runtime_error <br>
<b>#include &lt;iostream> &nbsp; &nbsp;</b>// cout<br>
<b>#include &lt;strstream> &nbsp;</b>// ostrstream, istrstream<br>
// the general include for eo<br>
<b>#include &lt;eo></b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="representation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
<tr>
<td>
<tt><font color="#999900">
//-----------------------------------------------------------------------------<br>
// define your individuals<br>
<b>typedef eoReal&lt;double> Indi; </b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="evalfunc"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
<tr>
<td>
<tt><font color="#CC0000">
//-----------------------------------------------------------------------------<br>
// a simple fitness function that computes the euclidian norm of a real vector<br>
// Now in a separate file, and declared as binary_value(const vector&lt;bool> &)<br>
<b>#include "real_value.h"</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td>
<tt><font color="#993300">
//-----------------------------------------------------------------------------<br>
<b>void main_function(int argc, char **argv)</b><br>
<b>{</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="parametres"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
<b> &nbsp;const unsigned int SEED = 42; </b>// seed for random number generator<br>
<b> &nbsp;const unsigned int T_SIZE = 3; </b>// size for tournament selection<br>
<b> &nbsp;const unsigned int VEC_SIZE = 8; </b>// Number of object variables in genotypes<br>
<b> &nbsp;const unsigned int POP_SIZE = 20; </b>// Size of population<br>
<b> &nbsp;const unsigned int MAX_GEN = 500; </b>// Maximum number of generation before STOP<br>
<b> &nbsp;const unsigned int MIN_GEN = 10; &nbsp;</b>// Minimum number of generation before ...<br>
<b> &nbsp;const unsigned int STEADY_GEN = 50; </b>// stop after STEADY_GEN gen. without improvelent<br>
<b> &nbsp;const float P_CROSS = 0.8; </b>// Crossover probability<br>
<b> &nbsp;const float P_MUT = 0.5; </b>// mutation probability<br>
<b> &nbsp;const double EPSILON = 0.01; </b>// range for real uniform mutation<br>
<b> &nbsp;</b>// some parameters for chosing among different operators<br>
<b> &nbsp;const double segmentRate = 0.5; &nbsp; &nbsp; &nbsp; &nbsp;</b>// rate for 1-pt Xover<br>
<b> &nbsp;const double arithmeticRate = 0.5; &nbsp; &nbsp; &nbsp; &nbsp;</b>// rate for 2-pt Xover<br>
<b> &nbsp;const double uniformMutRate = 0.5; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// rate for bit-flip mutation<br>
<b> &nbsp;const double detMutRate = 0.5; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// rate for one-bit mutation<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td>
<tt><font color="#993300">
<b> &nbsp;</b>//////////////////////////<br>
<b> &nbsp;</b>// &nbsp;Random seed<br>
<b> &nbsp;</b>//////////////////////////<br>
<b> &nbsp;</b>//reproducible random seed: if you don't change SEED above, <br>
<b> &nbsp;</b>// you'll aways get the same result, NOT a random run<br>
<b> &nbsp;rng.reseed(SEED);</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="eval"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
<tr>
<td>
<tt><font color="#CC0000">
<b> &nbsp;</b>/////////////////////////////<br>
<b> &nbsp;</b>// Fitness function<br>
<b> &nbsp;</b>////////////////////////////<br>
<b> &nbsp;</b>// Evaluation: from a plain C++ fn to an EvalFunc Object<br>
<b> &nbsp;</b>// you need to give the full description of the function<br>
<b> &nbsp;eoEvalFuncPtr&lt;Indi, double, const vector&lt;double>& > eval( &nbsp;real_value );</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="init"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td>
<tt><font color="#993399">
<b> &nbsp;</b>////////////////////////////////<br>
<b> &nbsp;</b>// Initilisation of population<br>
<b> &nbsp;</b>////////////////////////////////<br>
<b> &nbsp;</b>// based on a uniform generator<br>
<b> &nbsp;eoInitFixedLength&lt;Indi, uniform_generator&lt;double> ></b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;random(VEC_SIZE, uniform_generator&lt;double>(-1.0, 1.0));</b><br>
<b> &nbsp; &nbsp;</b>// Initialization of the population<br>
<b> &nbsp;eoPop&lt;Indi> pop(POP_SIZE, random);</b><br>
<b> </b><br>
<b> &nbsp;</b>// and evaluate it in one loop<br>
<b> &nbsp;apply&lt;Indi>(eval, pop); </b>// STL syntax<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
<b> &nbsp;</b>// sort pop before printing it!<br>
<b> &nbsp;pop.sort();</b><br>
<b> &nbsp;</b>// Print (sorted) intial population (raw printout)<br>
<b> &nbsp;cout &lt;&lt; "Initial Population" &lt;&lt; endl;</b><br>
<b> &nbsp;cout &lt;&lt; pop;</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="engine"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
<tr>
<td>
<tt><font color="#009900">
<b> &nbsp;</b>/////////////////////////////////////<br>
<b> &nbsp;</b>// selection and replacement<br>
<b> &nbsp;</b>////////////////////////////////////<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="select"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
<tr>
<td>
<tt><font color="#009900">
<b> &nbsp;</b>// The robust tournament selection<br>
<b> &nbsp;eoDetTournament&lt;Indi> selectOne(T_SIZE);</b><br>
<a NAME="select_encapsulate"></a>
<b> &nbsp;</b>// is now encapsulated in a eoSelectPerc (entage)<br>
<b> &nbsp;eoSelectPerc&lt;Indi> select(selectOne);</b>// by default rate==1<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="replace"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
<tr>
<td>
<tt><font color="#009900">
<b> &nbsp;</b>// And we now have the full slection/replacement - though with <br>
<b> &nbsp;</b>// no replacement (== generational replacement) at the moment :-)<br>
<b> &nbsp;eoNoReplacement&lt;Indi> replace; </b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="operators"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td>
<tt><font color="#993399">
<b> &nbsp;</b>//////////////////////////////////////<br>
<b> &nbsp;</b>// The variation operators<br>
<b> &nbsp;</b>//////////////////////////////////////<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="crossover"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td>
<tt><font color="#993399">
<b> &nbsp;</b>// uniform chooce on segment made by the parents<br>
<b> &nbsp;eoSegmentCrossover&lt;Indi> xoverS;</b><br>
<b> &nbsp;</b>// uniform choice in hypercube built by the parents<br>
<b> &nbsp;eoArithmeticCrossover&lt;Indi> xoverA;</b><br>
<b> &nbsp;</b>// Combine them with relative rates<br>
<b> &nbsp;eoPropCombinedQuadOp&lt;Indi> xover(xoverS, segmentRate);</b><br>
<b> &nbsp;xover.add(xoverA, arithmeticRate, true);</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="mutation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td>
<tt><font color="#993399">
<b> &nbsp;</b><br>
<b> &nbsp;</b>// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]<br>
<b> &nbsp;eoUniformMutation&lt;Indi> &nbsp;mutationU(EPSILON); </b><br>
<b> &nbsp;</b>// k (=1) coordinates of parents are uniformly modified<br>
<b> &nbsp;eoDetUniformMutation&lt;Indi> &nbsp;mutationD(EPSILON); </b><br>
<b> &nbsp;</b>// Combine them with relative rates<br>
<b> &nbsp;eoPropCombinedMonOp&lt;Indi> mutation(mutationU, uniformMutRate);</b><br>
<b> &nbsp;mutation.add(mutationD, detMutRate, true);</b><br>
<b> &nbsp;</b><br>
<b> &nbsp;</b>// The operators are &nbsp;encapsulated into an eoTRansform object<br>
<b> &nbsp;eoSGATransform&lt;Indi> transform(xover, P_CROSS, mutation, P_MUT);</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="stop"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
</font></tt>
</td>
</tr>
</table>
<a NAME="checkpoint"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
<b> &nbsp;</b>//////////////////////////////////////<br>
<b> &nbsp;</b>// termination conditions: use more than one<br>
<b> &nbsp;</b>/////////////////////////////////////<br>
<b> &nbsp;</b>// stop after MAX_GEN generations<br>
<b> &nbsp;eoGenContinue&lt;Indi> genCont(MAX_GEN);</b><br>
<b> &nbsp;</b>// do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement<br>
<b> &nbsp;eoSteadyFitContinue&lt;Indi> steadyCont(MIN_GEN, STEADY_GEN);</b><br>
<b> &nbsp;</b>// stop when fitness reaches a target (here VEC_SIZE)<br>
<b> &nbsp;eoFitContinue&lt;Indi> fitCont(0);</b><br>
<b> &nbsp;</b>// do stop when one of the above says so<br>
<b> &nbsp;eoCombinedContinue&lt;Indi> continuator(genCont);</b><br>
<b> &nbsp;continuator.add(steadyCont);</b><br>
<b> &nbsp;continuator.add(fitCont);</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="generation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td>
<tt><font color="#FF6666">
<b> &nbsp;</b>/////////////////////////////////////////<br>
<b> &nbsp;</b>// the algorithm<br>
<b> &nbsp;</b>////////////////////////////////////////<br>
<b> &nbsp;</b>// Easy EA requires <br>
<b> &nbsp;</b>// selection, transformation, eval, replacement, and stopping criterion<br>
<b> &nbsp;eoEasyEA&lt;Indi> gga(continuator, eval, select, transform, replace);</b><br>
<b> &nbsp;</b>// Apply algo to pop - that's it!<br>
<b> &nbsp;cout &lt;&lt; "\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Here we go\n\n";</b><br>
<b> &nbsp;gga(pop);</b><br>
<b> &nbsp;</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
<b> &nbsp;</b>// Print (sorted) intial population<br>
<b> &nbsp;pop.sort();</b><br>
<b> &nbsp;cout &lt;&lt; "FINAL Population\n" &lt;&lt; pop &lt;&lt; endl;</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td>
<tt><font color="#993300">
<b>}</b><br>
// A main that catches the exceptions<br>
<b>int main(int argc, char **argv)</b><br>
<b>{</b><br>
<b>#ifdef _MSC_VER</b><br>
<b> &nbsp;</b>// &nbsp;rng.reseed(42);<br>
<b> &nbsp; &nbsp; &nbsp;int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp;flag |= _CRTDBG_LEAK_CHECK_DF;</b><br>
<b> &nbsp; &nbsp; &nbsp;_CrtSetDbgFlag(flag);</b><br>
// &nbsp; &nbsp;_CrtSetBreakAlloc(100);<br>
<b>#endif</b><br>
<b> &nbsp; &nbsp; &nbsp;try</b><br>
<b> &nbsp; &nbsp; &nbsp;{</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;main_function(argc, argv);</b><br>
<b> &nbsp; &nbsp; &nbsp;}</b><br>
<b> &nbsp; &nbsp; &nbsp;catch(exception& e)</b><br>
<b> &nbsp; &nbsp; &nbsp;{</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &lt;&lt; "Exception: " &lt;&lt; e.what() &lt;&lt; '\n';</b><br>
<b> &nbsp; &nbsp; &nbsp;}</b><br>
<b> &nbsp; &nbsp; &nbsp;return 1;</b><br>
<b>}</b><br>
</font></font></font></td>
</tr>
</table>
<hr WIDTH="100%"><a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
documentation</a></font>
<hr>
<address>
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last
modified: Wed Nov 29 07:38:36 2000
<!-- hhmts end -->
</body>
</html>

View file

@ -0,0 +1,349 @@
<!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="cpp2html Marc Schoenauer">
<title>../FirstRealGA.html</title>
<!-- Changed by: Marc Schoenauer, 29-Nov-2000 -->
</head>
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
documentation</a></font>
<br>
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
<center>
<h1>
<font color="#FF0000">../FirstRealGA.html</font></h1></center>
Click on the figure to see the corresponding code.<br>
In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a><br>
The actual code is in boldface and the comment in normal face.
<br><img SRC="EA_tutorial.jpg" USEMAP="#Map" >
<map NAME="Map">
<!-- Init -->
<area SHAPE="rect" HREF="#init" COORDS="14,31,135,70">
<area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150">
<!-- main loop -->
<area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150">
<area SHAPE="rect" HREF="#output" COORDS="280,45,480,70">
<area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150">
<area SHAPE="rect" HREF="#select" COORDS="495,110,615,150">
<area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230">
<area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287">
<area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305">
<area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310">
<area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310">
<area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230">
<!-- Center of loop -->
<area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260">
<!-- 4 bottom lines -->
<area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370">
<area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370">
<area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397">
<area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423">
<area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450">
<area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450">
<area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450">
<area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378">
</map>
<br>&nbsp;
<A NAME="start"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr NOSAVE>
<td NOSAVE><tt><font color="#993300">
//-----------------------------------------------------------------------------<br>
// FirstRealGA.cpp<br>
//-----------------------------------------------------------------------------<br>
//*<br>
// An instance of a VERY simple Real-coded Genetic Algorithm<br>
//<br>
//-----------------------------------------------------------------------------<br>
// standard includes<br>
<b>#include &lt;stdexcept> &nbsp;</b>// runtime_error <br>
<b>#include &lt;iostream> &nbsp; &nbsp;</b>// cout<br>
<b>#include &lt;strstream> &nbsp;</b>// ostrstream, istrstream<br>
// the general include for eo<br>
<b>#include &lt;eo></b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="representation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
<tr>
<td>
<tt><font color="#999900">
//-----------------------------------------------------------------------------<br>
// define your individuals<br>
<b> typedef eoReal&lt;double> Indi;</b><br>
</font></tt>
</td>
</tr>
</table>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
<tr>
<td>
<tt><font color="#CC0000">
//-----------------------------------------------------------------------------<br>
// a simple fitness function that computes the euclidian norm of a real vector<br>
// &nbsp; &nbsp; &nbsp;@param _indi A real-valued individual <br>
<a name="evalfunc"></a>
<b>double real_value(const Indi & _indi)</b><br>
<b>{</b><br>
<b> &nbsp;double sum = 0;</b><br>
<b> &nbsp;for (unsigned i = 0; i &lt; _indi.size(); i++)</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sum += _indi[i]*_indi[i];</b><br>
<b> &nbsp;return (-sum); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// maximizing only<br>
<b>}</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td>
<tt><font color="#993300">
//-----------------------------------------------------------------------------<br>
<b>void main_function(int argc, char **argv)</b><br>
<b>{</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="parametres"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
<b> &nbsp;</b>// all parameters are hard-coded!<br>
<b> &nbsp;const unsigned int SEED = 42; </b>// seed for random number generator<br>
<b> &nbsp;const unsigned int VEC_SIZE = 8; </b>// Number of object variables in genotypes<br>
<b> &nbsp;const unsigned int POP_SIZE = 20; </b>// Size of population<br>
<b> &nbsp;const unsigned int T_SIZE = 3; </b>// size for tournament selection<br>
<b> &nbsp;const unsigned int MAX_GEN = 500; </b>// Maximum number of generation before STOP<br>
<b> &nbsp;const float CROSS_RATE = 0.8; </b>// Crossover rate<br>
<b> &nbsp;const double EPSILON = 0.01; &nbsp;</b>// range for real uniform mutation<br>
<b> &nbsp;const float MUT_RATE = 0.5; &nbsp; &nbsp;</b>// mutation rate<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td>
<tt><font color="#993300">
<a NAME="random"></a>
<b> &nbsp;</b>//////////////////////////<br>
<b> &nbsp;</b>// &nbsp;Random seed<br>
<b> &nbsp;</b>//////////////////////////<br>
<b> &nbsp;</b>//reproducible random seed: if you don't change SEED above, <br>
<b> &nbsp;</b>// you'll aways get the same result, NOT a random run<br>
<b> &nbsp;rng.reseed(SEED);</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="eval"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
<tr>
<td>
<tt><font color="#CC0000">
<b> &nbsp;</b>/////////////////////////////<br>
<b> &nbsp;</b>// Fitness function<br>
<b> &nbsp;</b>////////////////////////////<br>
<b> &nbsp;</b>// Evaluation: from a plain C++ fn to an EvalFunc Object<br>
<b> &nbsp;eoEvalFuncPtr&lt;Indi> eval( &nbsp;real_value );</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="init"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td>
<tt><font color="#993399">
<b> &nbsp;</b>////////////////////////////////<br>
<b> &nbsp;</b>// Initilisation of population<br>
<b> &nbsp;</b>////////////////////////////////<br>
<b> &nbsp;</b>// declare the population<br>
<b> &nbsp;eoPop&lt;Indi> pop;</b><br>
<b> &nbsp;</b>// fill it!<br>
<b> &nbsp;for (unsigned int igeno=0; igeno&lt;POP_SIZE; igeno++)</b><br>
<b> &nbsp; &nbsp; &nbsp;{</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Indi v; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// void individual, to be filled<br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (unsigned ivar=0; ivar&lt;VEC_SIZE; ivar++)</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;double r = 2*rng.uniform() - 1; </b>// new value, random in [-1,1)<br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;v.push_back(r); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// append that random value to v<br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eval(v); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// evaluate it<br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pop.push_back(v); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// and put it in the population<br>
<b> &nbsp; &nbsp; &nbsp;}</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
<b> &nbsp;</b>// sort pop before printing it!<br>
<b> &nbsp;pop.sort();</b><br>
<b> &nbsp;</b>// Print (sorted) intial population (raw printout)<br>
<b> &nbsp;cout &lt;&lt; "Initial Population" &lt;&lt; endl;</b><br>
<b> &nbsp;cout &lt;&lt; pop;</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="engine"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
<tr>
<td>
<tt><font color="#009900">
<b> &nbsp;</b>/////////////////////////////////////<br>
<b> &nbsp;</b>// selection and replacement<br>
<b> &nbsp;</b>////////////////////////////////////<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="select"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
<tr>
<td>
<tt><font color="#009900">
<b> &nbsp;</b>// The robust tournament selection<br>
<b> &nbsp;eoDetTournament&lt;Indi> select(T_SIZE); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// T_SIZE in [2,POP_SIZE]<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="replace"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
<tr>
<td>
<tt><font color="#009900">
<b> &nbsp;</b>// eoSGA uses generational replacement by default<br>
<b> &nbsp;</b>// so no replacement procedure has to be given<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="stop"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
</font></tt>
</td>
</tr>
</table>
<a NAME="checkpoint"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
<b> &nbsp;</b>//////////////////////////////////////<br>
<b> &nbsp;</b>// termination condition<br>
<b> &nbsp;</b>/////////////////////////////////////<br>
<b> &nbsp;</b>// stop after MAX_GEN generations<br>
<b> &nbsp;eoGenContinue&lt;Indi> continuator(MAX_GEN);</b><br>
<b> &nbsp;</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="operators"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td>
<tt><font color="#993399">
<b> &nbsp;</b>//////////////////////////////////////<br>
<b> &nbsp;</b>// The variation operators<br>
<b> &nbsp;</b>//////////////////////////////////////<br>
</font></tt>
</td>
</tr>
</table>
<a NAME="mutation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td>
<tt><font color="#993399">
<b> &nbsp;</b>// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]<br>
<b> &nbsp;eoUniformMutation&lt;Indi> &nbsp;mutation(EPSILON); </b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="crossover"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td>
<tt><font color="#993399">
<b> &nbsp;</b>// offspring(i) is a linear combination of parent(i)<br>
<b> &nbsp;eoArithmeticCrossover&lt;Indi> xover;</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="generation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td>
<tt><font color="#FF6666">
<b> &nbsp;</b>/////////////////////////////////////////<br>
<b> &nbsp;</b>// the algorithm<br>
<b> &nbsp;</b>////////////////////////////////////////<br>
<b> &nbsp;</b>// standard Generational GA requires<br>
<b> &nbsp;</b>// selection, evaluation, crossover and mutation, stopping criterion<br>
<b> </b><br>
<b> &nbsp;eoSGA&lt;Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE, </b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eval, continuator);</b><br>
<b> &nbsp;</b>// Apply algo to pop - that's it!<br>
<b> &nbsp;gga(pop);</b><br>
<b> &nbsp;</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="final_output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td>
<tt><font color="#3366FF">
<b> &nbsp;</b>// Print (sorted) intial population<br>
<b> &nbsp;pop.sort();</b><br>
<b> &nbsp;cout &lt;&lt; "FINAL Population\n" &lt;&lt; pop &lt;&lt; endl;</b><br>
</font></tt>
</td>
</tr>
</table>
<a NAME="main"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td>
<tt><font color="#993300">
<b>}</b><br>
// A main that catches the exceptions<br>
<b>int main(int argc, char **argv)</b><br>
<b>{</b><br>
<b>#ifdef _MSC_VER</b><br>
<b> &nbsp;</b>// &nbsp;rng.reseed(42);<br>
<b> &nbsp; &nbsp; &nbsp;int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp;flag |= _CRTDBG_LEAK_CHECK_DF;</b><br>
<b> &nbsp; &nbsp; &nbsp;_CrtSetDbgFlag(flag);</b><br>
// &nbsp; &nbsp;_CrtSetBreakAlloc(100);<br>
<b>#endif</b><br>
<b> &nbsp; &nbsp; &nbsp;try</b><br>
<b> &nbsp; &nbsp; &nbsp;{</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;main_function(argc, argv);</b><br>
<b> &nbsp; &nbsp; &nbsp;}</b><br>
<b> &nbsp; &nbsp; &nbsp;catch(exception& e)</b><br>
<b> &nbsp; &nbsp; &nbsp;{</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &lt;&lt; "Exception: " &lt;&lt; e.what() &lt;&lt; '\n';</b><br>
<b> &nbsp; &nbsp; &nbsp;}</b><br>
<b> &nbsp; &nbsp; &nbsp;return 1;</b><br>
<b>}</b><br>
</font></font></font></td>
</tr>
</table>
<hr WIDTH="100%"><a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
documentation</a></font>
<hr>
<address>
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last
modified: Sun Nov 19 08:31:29 2000
<!-- hhmts end -->
</body>
</html>

View file

@ -0,0 +1,278 @@
<!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.51 [en] (X11; I; Linux 2.2.5-15 i686) [Netscape]">
<title>First Real GA</title>
</head>
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
documentation</a></font>
<br>
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
<center>
<h1>
<font color="#FF0000">A First Real GA</font></h1></center>
Click on the figure to see the corresponding code. Tutorial comments are
in variable length fonts, after the code.
<center>
<p><img SRC="EA_tutorial.jpg" USEMAP="#Map" ></center>
<map NAME="Map"><area SHAPE="rect" HREF="#init" COORDS="0,0,112,40"><area SHAPE="rect" HREF="#eval" COORDS="0,78,112,112"><area SHAPE="rect" HREF="#select" COORDS="395,70,507,107"><area SHAPE="rect" HREF="#replace" COORDS="145,144,253,182"><area SHAPE="rect" HREF="#crossover" COORDS="433,145,543,164"><area SHAPE="rect" HREF="#mutation" COORDS="433,164,543,182"><area SHAPE="rect" HREF="#eval" COORDS="218,217,325,252"><area SHAPE="rect" HREF="#output" COORDS="235,14,415,38"><area SHAPE="rect" HREF="#representation" COORDS="143,68,253,114"><area SHAPE="rect" HREF="#representation" COORDS="394,214,515,255"><area SHAPE="rect" HREF="#stop" COORDS="278,74,370,109"><area SHAPE="rect" HREF="#generation" COORDS="257,114,431,213"><!-- 4 lignes en bas --><area SHAPE="rect" HREF="#eval" COORDS="0,334,207,352"><area SHAPE="rect" HREF="#engine" COORDS="0,310,356,334"><area SHAPE="rect" HREF="#operators" COORDS="0,288,230,310"><area SHAPE="rect" HREF="#representation" COORDS="230,288,450,310"><area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378"></map>
<p><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
<p><font face="Courier New,Courier"><font color="#993300"><font size=+1>#include
&lt;stdexcept > // runtime_error</font></font></font>
<p><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
<br><font face="Courier New,Courier"><font size=+1>// FirstRealGA.cpp</font></font>
<br><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
<br><font face="Courier New,Courier"><font size=+1>//*</font></font>
<br><font face="Courier New,Courier"><font size=+1>// An instance of a
VERY simple Real-coded Genetic Algorithm</font></font>
<br><font face="Courier New,Courier"><font size=+1>//</font></font>
<br><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
<br><font face="Courier New,Courier"><font size=+1>// standard includes</font></font>
<p><font face="Courier New,Courier"><font color="#993300"><font size=+1>#include
&lt;iostream>// cout</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>#include
&lt;strstream>// ostrstream, istrstream</font></font></font>
<p><font face="Courier New,Courier"><font size=+1>// the general include
for eo</font></font>
<p><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>#include
&lt;eo></font></font></font>
<p><font face="Courier New,Courier"><font size=+1>// specific incluse,
as Real is not (yet) in the standard eo source dir</font></font>
<br><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>#include
"eoeal.h"</font></font></font>
<br><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>#include
"eoRealOp.h"</font></font></font>
<p><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
<br><font face="Courier New,Courier"><font size=+1>// define your individual:</font></font>
<p><a NAME="representation"></a><font face="Courier New,Courier"><font color="#FFFF00"><font size=+1>typedef
eoReal&lt;double> Indi;</font></font></font>
<p>You say here that you will be handling <font face="Arial,Helvetica">arrays
of doubles</font>, whose fitness is a double
<br>Note that this makes Indi derive from <a href="eoProgramming.html#STL">STL
class</a> <font color="#993300">vector&lt;double></font>
<br><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
<br><font face="Courier New,Courier"><font size=+1>/** a simple fitness
function that computes the euclidian norm of a real vector</font></font>
<br><font face="Courier New,Courier"><font size=+1>&nbsp;&nbsp;&nbsp; @param
_indi A real-valued individual</font></font>
<br><font face="Courier New,Courier"><font size=+1>*/</font></font>
<p><a NAME="evalfunc"></a><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>double
real_value(const Indi &amp; _indi)</font></font></font>
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>{</font></font></font>
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>&nbsp;
double sum = 0;</font></font></font>
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>&nbsp;
for (unsigned i = 0; i &lt; _indi.size(); i++)</font></font></font>
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
sum += _indi[i]*_indi[i];</font></font></font>
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>&nbsp;
return (-sum);&nbsp;&nbsp; // maximizing</font></font></font>
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>}</font></font></font>
<p>This simple function computes the sum of the squares of the variables
(remember Indi is here a vector of doubles)
<br><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
<p><font face="Courier New,Courier"><font color="#993300"><font size=+1>void
main_function(int argc, char **argv)</font></font></font>
<br><font face="Courier New,Courier"><font size=+1>{</font></font>
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
unsigned int SEED = 42;// seed for random number generator</font></font></font>
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
unsigned int VEC_SIZE = 8; // Number of object variables in genotypes</font></font></font>
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
unsigned int POP_SIZE = 20; // Size of population</font></font></font>
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
unsigned int T_SIZE = 3; // size for tournament selection</font></font></font>
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
unsigned int MAX_GEN = 500; // Maximum number of generation before STOP</font></font></font>
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
float CROSS_RATE = 0.8; // Crossover rate</font></font></font>
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
double EPSILON = 0.01; // range for real uniform mutation</font></font></font>
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
float MUT_RATE = 0.5; // mutation rate</font></font></font>
<br>&nbsp;
<p><font face="Courier New,Courier"><font size=+1>//////////////////////////</font></font>
<br><font face="Courier New,Courier"><font size=+1>// Random seed</font></font>
<br><font face="Courier New,Courier"><font size=+1>//////////////////////////</font></font>
<br><font face="Courier New,Courier"><font size=+1>//reproducible random
seed: if you don't change SEED above,</font></font>
<br><font face="Courier New,Courier"><font size=+1>// you'll aways get
the same result, NOT a random run</font></font>
<br><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>rng.reseed(SEED);</font></font></font>
<p><font face="Courier New,Courier"><font size=+1>/////////////////////////////</font></font>
<br><font face="Courier New,Courier"><font size=+1>// Fitness function</font></font>
<br><font face="Courier New,Courier"><font size=+1>////////////////////////////</font></font>
<br><font face="Courier New,Courier"><font size=+1>// Evaluation: from
a plain C++ fn to an EvalFunc Object</font></font>
<br><a NAME="eval"></a><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>eoEvalFuncPtr&lt;Indi>
eval( <a href="#evalfunc">real_value</a> );</font></font></font>
<p>This encapsulate the C++ function real_value into a <a href="eoProgramming.html#functors">functor
object</a>
<p><font face="Courier New,Courier"><font size=+1>////////////////////////////////</font></font>
<br><font face="Courier New,Courier"><font size=+1>// Initilisation of
population</font></font>
<br><font face="Courier New,Courier"><font size=+1>////////////////////////////////</font></font>
<p><font face="Courier New,Courier"><font size=+1>// declare the population</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>eoPop&lt;Indi>
pop;</font></font></font>
<p>Declares a <font face="Arial,Helvetica"><a href="doc/html/class_eopop.html">population
object</a>, </font>which is basically an <a href="eoProgramming.html#STL">STL</a>
<tt><font color="#993300"><font size=+1>vector&lt;Indi></font></font></tt>
<p><font face="Courier New,Courier"><font size=+1>// fill it!</font></font>
<br><a NAME="init"></a><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>for
(unsigned int igeno=0; igeno&lt;POP_SIZE; igeno++)</font></font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>&nbsp;
{</font></font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>&nbsp;&nbsp;&nbsp;
Indi v; // generate a random individual</font></font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>&nbsp;&nbsp;&nbsp;
for (unsigned ivar=0; ivar&lt;VEC_SIZE; ivar++)</font></font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{</font></font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
double r = 2*rng.uniform() - 1; // new value, random in [-1,1)</font></font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
v.push_back(r); //</font></font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</font></font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>&nbsp;&nbsp;&nbsp;
eval(v); // evaluate it</font></font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>&nbsp;&nbsp;&nbsp;
pop.push_back(v); // and put it in the population</font></font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>&nbsp;
}</font></font></font>
<p>This initialization of the population is straightforward
<br><font color="#CC33CC">rng.uniform()</font> generates a double <font face="Arial,Helvetica"><a href="doc/html/class_eorng.html">uniformly
in [0,1)</a>,</font>
<br><font color="#CC33CC">v.push_back</font> simply appends its argument
at the end of <a href="eoProgramming.html#STL">STL</a> vector <font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>v</font></font></font>
<br><font color="#CC33CC">eval(v)</font>evaluates individal <font color="#CC33CC">v</font>
the <a href="eoProgramming.html#functors">functor way</a>, calling <font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>eval.operator()(v)</font></font></font>
<p><font face="Courier New,Courier"><font size=+1>// sort pop before printing
it!</font></font>
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>pop.sort();</font></font></font>
<p><font face="Courier New,Courier"><font size=+1>// Print (sorted) intial
population (raw printout)</font></font>
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>cout
&lt;&lt; "Initial Population" &lt;&lt; endl;</font></font></font>
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>cout
&lt;&lt; pop;</font></font></font>
<p>If you looked at <font face="Arial,Helvetica"><a href="doc/html/class_eopop.html">eoPop</a></font>
inheritance diagram, you noticed that it derives from <font face="Arial,Helvetica"><a href="doc/html/class_eoprintable.html">eoPrintable</a>:</font>
hence you can stream them using the &lt;&lt; operator. Of course, Indis
are <font face="Arial,Helvetica"><a href="doc/html/class_eo.html">EO objects</a>
</font>, which also are eoPrintable, and the &lt;&lt; operator for eoPop
uses the &lt;&lt; operator for Indi.
<p><font face="Courier New,Courier"><font size=+1>/////////////////////////////////////</font></font>
<br><a NAME="engine"></a><font face="Courier New,Courier"><font size=+1>//
selection and replacement</font></font>
<br><font face="Courier New,Courier"><font size=+1>////////////////////////////////////</font></font>
<br><font face="Courier New,Courier"><font size=+1>// The well-known roulette
selection</font></font>
<br><a NAME="select"></a><font face="Courier New,Courier"><font size=+1>//
The robust tournament selection</font></font>
<br><font face="Courier New,Courier"><font color="#009900"><font size=+1>eoDetTournament&lt;Indi>
select(T_SIZE); // SIZE in [2,POP_SIZE]</font></font></font>
<p><a NAME="replace"></a><font face="Courier New,Courier"><font size=+1>//
eoSGA uses <font color="#009900">generational replacement</font> by default</font></font>
<br><font face="Courier New,Courier"><font size=+1>// so no replacement
procedure has to be given</font></font>
<p><font face="Courier New,Courier"><font size=+1>//////////////////////////////////////</font></font>
<br><font face="Courier New,Courier"><font size=+1>// termination condition</font></font>
<br><font face="Courier New,Courier"><font size=+1>/////////////////////////////////////</font></font>
<br><font face="Courier New,Courier"><font size=+1>// stop after MAX_GEN
generations</font></font>
<br><a NAME="stop"></a><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>eoGenContinue&lt;Indi>
continuator(MAX_GEN);</font></font></font>
<p>This class is called <font face="Arial,Helvetica"><a href="doc/html/class_eogencontinue.html">eoGenContinue</a>
</font>because the main loop of all <font face="Arial,Helvetica"><a href="doc/html/class_eoalgo.html">eoAlgo</a>
</font>says ... while continuator(pop)
<p><font face="Courier New,Courier"><font size=+1>//////////////////////////////////////</font></font>
<br><a NAME="operators"></a><font face="Courier New,Courier"><font size=+1>//
The variation operators</font></font>
<br><font face="Courier New,Courier"><font size=+1>//////////////////////////////////////</font></font>
<p><a NAME="mutation"></a><font face="Courier New,Courier"><font size=+1>//
offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>eoUniformMutation&lt;Indi>&nbsp;
mutation(EPSILON);</font></font></font>
<br><a NAME="crossover"></a><font face="Courier New,Courier"><font size=+1>//
offspring(i) is a linear combination of parent(i)</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>eoArithmeticCrossover&lt;Indi>
xover;</font></font></font>
<p>The <a href="eoOperators.html">variation operators</a> are respectively
an <font face="Arial,Helvetica"><a href="doc/html/class_eomonop.html">eoMonOp</a></font>
(unary operator) and an <font face="Arial,Helvetica"><a href="doc/html/class_eoquadraticop.html">eoQuadOp</a></font>
(binary operator that modifies both its arguments).
<p><font face="Courier New,Courier"><font size=+1>/////////////////////////////////////////</font></font>
<br><font face="Courier New,Courier"><font size=+1>// the algorithm</font></font>
<br><font face="Courier New,Courier"><font size=+1>////////////////////////////////////////</font></font>
<br><font face="Courier New,Courier"><font size=+1>// standard Generational
GA requires</font></font>
<br><font face="Courier New,Courier"><font size=+1>// selection, evaluation,
crossover and mutation, stopping criterion</font></font>
<br>&nbsp;
<p><a NAME="generation"></a><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>eoSGA&lt;Indi>
gga(select, xover, CROSS_RATE, mutation, MUT_RATE,</font></font></font>
<br><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>eval,
continuator);</font></font></font>
<p><font face="Courier New,Courier"><font size=+1>// Apply algo to pop
- that's it!</font></font>
<br><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>gga(pop);</font></font></font>
<p>This simple algorithm of class eoSGA is a <a href="eoProgramming.html#functors">functor
object</a>, and running the algorithm amounts to call the <font color="#993300">operator()</font>
on the initial population!
<p><font face="Courier New,Courier"><font size=+1>// Print (sorted) intial
population (raw printout)</font></font>
<br><a NAME="output"></a><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>pop.sort();</font></font></font>
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>cout
&lt;&lt; "FINAL Population\n" &lt;&lt; pop &lt;&lt; endl;</font></font></font>
<p>that's it - just print the final population and you're done!!!
<p><font face="Courier New,Courier"><font size=+1>}</font></font>
<p><font face="Courier New,Courier"><font color="#000000"><font size=+1>//
A main that catches the exceptions - do not modify!</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>int
main(int argc, char **argv)</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>{</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>#ifdef
_MSC_VER</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>//
rng.reseed(42);</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>int
flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>flag
|= _CRTDBG_LEAK_CHECK_DF;</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>_CrtSetDbgFlag(flag);</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>//
_CrtSetBreakAlloc(100);</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>#endif</font></font></font>
<p><font face="Courier New,Courier"><font color="#993300"><font size=+1>try</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>{</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>main_function(argc,
argv);</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>}</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>catch(exception&amp;
e)</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>{</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>cout
&lt;&lt; "Exception: " &lt;&lt; e.what() &lt;&lt; '\n';</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>}</font></font></font>
<p><font face="Courier New,Courier"><font color="#993300"><font size=+1>return
1;</font></font></font>
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>}</font></font></font>
<p>
<hr WIDTH="100%"><a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
documentation</a></font>
<hr>
<address>
<a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
<br><!-- Created: Wed Nov 1 07:18:21 CET 2000 --><!-- hhmts start -->Last
modified: Wed Nov 6 17:22:43 CET 2000<!-- hhmts end -->
</body>
</html>

View file

@ -0,0 +1,287 @@
<!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.75 [en] (X11; U; Linux 2.2.17-21mdksmp i686) [Netscape]">
<title>Differences</title>
</head>
<body text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000" background="beige009.jpg">
<a href="eoLesson1.html">Back to Lesson 1</a> -
<a href="eoTutorial.html">Tutorial
main page</a> -
<a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> - <a href="doc/html/index.html">EO
documentation</a>
<br>
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
<center>
<h1>
<font color="#FF0000">FirstBitGA and FirstRealGA: differences</font></h1></center>
Below is a comparison of the codes for both algorithms (comments have been
removed).
<br><b><font color="#FF6600">Warning</font></b>: the <font color="#FF6666">pink
background</font> here denotes the differences, not the section&nbsp; of
the algorithm, which is only recalled by the color of the text!
<br>These differences are limited to
<ul>
<li>
the declaration of <font color="#666600">the type of the genotypes,</font></li>
<li>
the <font color="#990000">fitness function</font> (what did you expect
:-),</li>
<li>
the <font color="#CC33CC">initialization</font> (and if you look carefully,
you'll find out that only a small part of the initialization is different,
as both genotypes are eoFixedLength objects) and of course</li>
<li>
the choice of <font color="#CC33CC">variation operators </font>(including
the parameter for the mutation).</li>
</ul>
<table BORDER=0 CELLPADDING=5 COLS=2 WIDTH="100%" BGCOLOR="#C3C2B4" >
<tr>
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#993300">#include
&lt;stdexcept ></font></font>
<br><font face="Courier New,Courier"><font color="#993300">#include &lt;iostream></font></font>
<br><font face="Courier New,Courier"><font color="#993300">#include &lt;strstream></font></font>
<br><font face="Courier New,Courier"><font color="#FF6666">#include &lt;eo></font></font></td>
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#993300">#include
&lt;stdexcept ></font></font>
<br><font face="Courier New,Courier"><font color="#993300">#include &lt;iostream></font></font>
<br><font face="Courier New,Courier"><font color="#993300">#include &lt;strstream></font></font>
<br><font face="Courier New,Courier"><font color="#FF6666">#include &lt;eo></font></font></td>
</tr>
<tr>
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#FFFF00">typedef
eoReal&lt;double> Indi;</font></font></td>
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#FFFF00">typedef
eoBin&lt;double> Indi;</font></font></td>
</tr>
<tr>
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC0000">double
real_value(const Indi &amp; _indi)</font></font>
<br><font face="Courier New,Courier"><font color="#CC0000">{</font></font>
<br><font face="Courier New,Courier"><font color="#CC0000">&nbsp; double
sum = 0;</font></font>
<br><font face="Courier New,Courier"><font color="#CC0000">&nbsp; for (unsigned
i = 0; i &lt; _indi.size(); i++)</font></font>
<br><font face="Courier New,Courier"><font color="#CC0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
sum += _indi[i]*_indi[i];</font></font>
<br><font face="Courier New,Courier"><font color="#CC0000">&nbsp; return
(-sum);&nbsp;&nbsp; // maximizing</font></font>
<br><font face="Courier New,Courier"><font color="#CC0000">}</font></font></td>
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC0000">double
binary_value(const Indi &amp; _indi)</font></font>
<br><font face="Courier New,Courier"><font color="#CC0000">{</font></font>
<br><font face="Courier New,Courier"><font color="#CC0000">&nbsp; double
sum = 0;</font></font>
<br><font face="Courier New,Courier"><font color="#CC0000">&nbsp; for (unsigned
i = 0; i &lt; _indi.size(); i++)</font></font>
<br><font face="Courier New,Courier"><font color="#CC0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
sum += _indi[i];</font></font>
<br><font face="Courier New,Courier"><font color="#CC0000">&nbsp; return
(sum);</font></font>
<br><font face="Courier New,Courier"><font color="#CC0000">}</font></font></td>
</tr>
<tr>
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#993300">void
main_function(int argc, char **argv)</font></font>
<br><font face="Courier New,Courier">{</font>
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
int SEED = 42;</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
int VEC_SIZE = 8;</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
int POP_SIZE = 20;</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
int T_SIZE = 3;</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
int MAX_GEN = 500;</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">const float
CROSS_RATE = 0.8;</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">const float
MUT_RATE = 0.5;</font></font></td>
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#993300">void
main_function(int argc, char **argv)</font></font>
<br><font face="Courier New,Courier">{</font>
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
int SEED = 42;</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
int VEC_SIZE = 8;</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
int POP_SIZE = 20;</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
int T_SIZE = 3;</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
int MAX_GEN = 500;</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">const float
CROSS_RATE = 0.8;</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">const float
MUT_RATE = 0.5;</font></font></td>
</tr>
<tr>
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#3366FF">const
double EPSILON = 0.01;</font></font></td>
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#3366FF">const
double P_MUT_PER_BIT = 0.01;</font></font></td>
</tr>
<tr>
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC0000">eoEvalFuncPtr&lt;Indi>
eval(real_value);</font></font></td>
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC0000">eoEvalFuncPtr&lt;Indi>
eval(binary_value);</font></font></td>
</tr>
<tr>
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#CC33CC">eoPop&lt;Indi>
pop;</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">for (unsigned
int igeno=0; igeno&lt;POP_SIZE; igeno++)</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">&nbsp; {</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">&nbsp;&nbsp;&nbsp;
Indi v;</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">&nbsp;&nbsp;&nbsp;
for (unsigned ivar=0; ivar&lt;VEC_SIZE; ivar++)</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{</font></font></td>
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#CC33CC">eoPop&lt;Indi>
pop;</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">for (unsigned
int igeno=0; igeno&lt;POP_SIZE; igeno++)</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">&nbsp; {</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">&nbsp;&nbsp;&nbsp;
Indi v;</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">&nbsp;&nbsp;&nbsp;
for (unsigned ivar=0; ivar&lt;VEC_SIZE; ivar++)</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{</font></font></td>
</tr>
<tr>
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
double r = 2*rng.uniform() - 1;</font></font></td>
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
bool r = rng.flip();&nbsp;</font></font></td>
</tr>
<tr>
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
v.push_back(r); //</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">&nbsp;&nbsp;&nbsp;
eval(v);</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">&nbsp;&nbsp;&nbsp;
pop.push_back(v);</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">&nbsp; }</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">pop.sort();</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">cout &lt;&lt;
"Initial Population" &lt;&lt; endl;</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">cout &lt;&lt;
pop;</font></font>
<br><font face="Courier New,Courier"><font color="#009900">eoDetTournament&lt;Indi>
select(T_SIZE);</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">eoGenContinue&lt;Indi>
continuator(MAX_GEN);</font></font></td>
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
v.push_back(r); //</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">&nbsp;&nbsp;&nbsp;
eval(v);</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">&nbsp;&nbsp;&nbsp;
pop.push_back(v);</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">&nbsp; }</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">pop.sort();</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">cout &lt;&lt;
"Initial Population" &lt;&lt; endl;</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">cout &lt;&lt;
pop;</font></font>
<br><font face="Courier New,Courier"><font color="#009900">eoDetTournament&lt;Indi>
select(T_SIZE);</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">eoGenContinue&lt;Indi>
continuator(MAX_GEN);</font></font></td>
</tr>
<tr>
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC33CC">eoUniformMutation&lt;Indi>&nbsp;
mutation(EPSILON);</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">eoArithmeticCrossover&lt;Indi>
xover;</font></font></td>
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC33CC">eoBinMutation&lt;Indi>&nbsp;
mutation(P_MUT_PER_BIT);</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">eoBinCrossover&lt;Indi>
xover;</font></font></td>
</tr>
<tr>
<td BGCOLOR="#C3C2B4">
<br><font face="Courier New,Courier"><font color="#FF6666">eoSGA&lt;Indi>
gga(select, xover, CROSS_RATE,</font></font>
<br><font face="Courier New,Courier"><font color="#FF6666">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
mutation, MUT_RATE, eval, continuator);</font></font>
<br><font face="Courier New,Courier"><font color="#FF6666">gga(pop);</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">pop.sort();</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">cout &lt;&lt;
"FINAL Population\n" &lt;&lt; pop &lt;&lt; endl;</font></font>
<br><font face="Courier New,Courier">}</font>
<br><font face="Courier New,Courier"><font color="#993300">int main(int
argc, char **argv)</font></font>
<br><font face="Courier New,Courier"><font color="#993300">{</font></font>
<br><font face="Courier New,Courier"><font color="#993300">... [technical
code removed]</font></font>
<br><font face="Courier New,Courier"><font color="#993300">}</font></font></td>
<td BGCOLOR="#C3C2B4">
<br><font face="Courier New,Courier"><font color="#FF6666">eoSGA&lt;Indi>
gga(select, xover, CROSS_RATE,</font></font>
<br><font face="Courier New,Courier"><font color="#FF6666">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
mutation, MUT_RATE,</font></font>
<br><font face="Courier New,Courier"><font color="#FF6666">eval, continuator);</font></font>
<br><font face="Courier New,Courier"><font color="#FF6666">gga(pop);</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">pop.sort();</font></font>
<br><font face="Courier New,Courier"><font color="#3366FF">cout &lt;&lt;
"FINAL Population\n" &lt;&lt; pop &lt;&lt; endl;</font></font>
<br><font face="Courier New,Courier">}</font>
<br><font face="Courier New,Courier"><font color="#993300">int main(int
argc, char **argv)</font></font>
<br><font face="Courier New,Courier"><font color="#993300">{</font></font>
<br><font face="Courier New,Courier"><font color="#993300">[... technical
code removed ]</font></font>
<br><font face="Courier New,Courier"><font color="#993300">}</font></font></td>
</tr>
</table>
<hr WIDTH="100%"><a href="eoLesson1.html">Back to Lesson 1</a> -
<a href="eoTutorial.html">Tutorial
main page</a> -
<a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> - <a href="doc/html/index.html">EO
documentation</a>
<br>
<hr>
<address>
<a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
<br><!-- Created: Tue Nov 7 07:04:15 CET 2000 --><!-- hhmts start -->Last
modified: Tue Nov 7 07:49:47 CET 2000<!-- hhmts end -->
</body>
</html>

View file

@ -0,0 +1,74 @@
<!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.75 [en] (X11; U; Linux 2.2.17-21mdksmp i686) [Netscape]">
<title>Tutorial: Solutions</title>
</head>
<body text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000" background="beige009.jpg">
<a href="eoTutorial.html">Tutorial main page</a> -
<a href="eoTopDown.html">Top-Down
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
hints</a> -<font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
documentation</a></font></font>
<br>
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
<center>
<h1>
<font color="#FF0000">Tutorial:</font></h1></center>
<center><b><font color="#FF0000"><font size=+2>Solutions of exercises</font></font></b></center>
<p>No, <b><font color="#33CC00">w</font><font color="#FF9900">e </font><font color="#CC66CC">w</font><font color="#33CCFF">o</font><font color="#FFFF00">n</font><font color="#FF0000">'t</font><font color="#999999">
p</font><font color="#999900">r</font><font color="#CC6600">o</font><font color="#993399">v</font><font color="#006600">i</font><font color="#990000">d</font><font color="#867CE9">e
</font></b><font color="#000000">any
hypertext link directly to the correct code, so</font> you get a chance
to find out the solution by yourself without immediately going there&nbsp;
:-)))
<p><b><font color="#000099"><font size=+2>What you should do:</font></font></b>
<ul>
<li>
copy the code of an example onto another name (e.g. <b><tt><font color="#660000">mytest.cpp</font></tt></b>)</li>
<li>
edit and modify <b><tt><font color="#660000">mytest.cpp</font></tt></b>
according to the corresponding instructions of the exercise</li>
<li>
compile <b><tt><font color="#660000">mytest.cpp</font></tt></b> by typing
<b><tt><font color="#FF6666">make mytest</font></tt></b> at system prompt,
and you will hopefully get an executable file named <b><tt><font color="#660000">mytest</font></tt></b>
(for Unix systems: if eventually someone tells me how to do that in Windows,
apart that you'll probably end up with an executable file named <b><tt><font color="#660000">mytest.exe...</font></tt></b>).</li>
</ul>
<hr WIDTH="100%">
<br><b><font color="#000099"><font size=+2>What you may do later:</font></font></b>
<p>All solutions to exercises are in the same<b><font color="#FF6600">
sub-dir</font></b> of the Tutorial directory than the example files (i.e.
Lesson1 for Lesson1, yes!). Hence you may browse through the code, eventually
modifying it, and then simply type, in the corresponding directory,
<br><b><tt><font color="#FF6666">... % make exerciseN</font></tt></b>
<br>which will compile file <b><tt><font color="#660000">exerciseN.cpp</font></tt></b>
into executable <b><tt><font color="#660000">exerciseN</font></tt></b>
(for Unix systems: if eventually someone tells me how to do that in Windows,
you'll probably end up with an executable file named <b><tt><font color="#660000">exerciseN.exe</font></tt></b>).
<p>
<hr WIDTH="100%">
<br><b><font color="#000099"><font size=+2>What you may not do:</font></font></b>
<center>Complain that it does not work under Windows :-)</center>
<hr WIDTH="100%"><a href="eoTutorial.html">Tutorial main page</a> -
<a href="eoTopDown.html">Top-Down
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
hints</a> - <font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
documentation</a></font></font>
<br>
<hr>
<address>
<a href="mailto:Marc.Schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
<br><!-- Created: Fri Nov 3 18:49:12 CET 2000 --><!-- hhmts start -->Last
modified: Fri Nov 3 18:49:12 CET 2000<!-- hhmts end -->
</body>
</html>

View file

@ -0,0 +1,612 @@
<!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.75 [en] (X11; U; Linux 2.2.17-21mdk i686) [Netscape]">
<title>SecondBitEA.cpp</title>
<!-- Changed by: Marc Schoenauer, 29-Nov-2000 -->
</head>
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<a href="eoLesson3.html">Back to Lesson 3</a> - <a href="eoTutorial.html">Tutorial
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
documentation</a></font>
<br>
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
<center>
<h1>
<font color="#FF0000">SecondBitEA.cpp</font></h1></center>
Click on the figure to see the corresponding code.
<br>In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a>
<br>The actual code is in boldface and the comment in normal face.
<p>For this particular program, as all new lines are concerned with program
parameter input and information output, the <font color="#FF6600">font
color</font> for program-parameter-related sections will <font color="#FF6600">refer
to the section where the parameters are used</font> whereas the background
color will remain blue.
<p><img SRC="EA_tutorial.jpg" USEMAP="#Map" ><map NAME="Map"><!-- Init --><area SHAPE="rect" HREF="#init" COORDS="14,31,135,70"><area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150"><!-- main loop --><area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150"><area SHAPE="rect" HREF="#output" COORDS="280,45,480,70"><area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150"><area SHAPE="rect" HREF="#select" COORDS="495,110,615,150"><area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230"><area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287"><area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305"><area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310"><area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310"><area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230"><!-- Center of loop --><area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260"><!-- 4 bottom lines --><area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370"><area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370"><area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397"><area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423"><area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450"><area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450"><area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450"><area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378"></map>
<br><a NAME="start"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr NOSAVE>
<td NOSAVE><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
<br><tt><font color="#993300">// SecondGA.cpp</font></tt>
<br><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
<br><tt><font color="#993300">//*</font></tt>
<br><tt><font color="#993300">// Same code than FirstBitEA as far as Evolutionary
Computation is concerned</font></tt>
<br><tt><font color="#993300">// but now you learn to enter the parameters
in a more flexible way</font></tt>
<br><tt><font color="#993300">// and to twidle the output to your preferences!</font></tt>
<br><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
<br><tt><font color="#993300">// standard includes</font></tt>
<br><tt><font color="#993300"><b>#include &lt;stdexcept>&nbsp; </b>// runtime_error&nbsp;</font></tt>
<br><tt><font color="#993300"><b>#include &lt;iostream>&nbsp;&nbsp; </b>//
cout</font></tt>
<br><tt><font color="#993300"><b>#include &lt;strstream>&nbsp; </b>// ostrstream,
istrstream</font></tt>
<br><b><tt><font color="#993300">#include &lt;fstream></font></tt></b>
<br><tt><font color="#993300">// the general include for eo</font></tt>
<br><b><tt><font color="#993300">#include &lt;eo></font></tt></b>
<br><tt><font color="#993300">// specific includes</font></tt>
<br><b><tt><font color="#993300">#include "eoSGATransform.h"</font></tt></b>
<br><b><tt><font color="#993300">#include "eoPropCombinedOp.h"</font></tt></b></td>
</tr>
</table>
<a NAME="evalfunc"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
<tr>
<td><b><tt><font color="#CC0000">#include "binary_value.h"</font></tt></b></td>
</tr>
</table>
<a NAME="representation"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
<tr>
<td><tt><font color="#999900">//-----------------------------------------------------------------------------</font></tt>
<br><tt><font color="#999900">// define your genotype and fitness types</font></tt>
<br><b><tt><font color="#999900">typedef eoBin&lt;double> Indi;</font></tt></b></td>
</tr>
</table>
<a NAME="parametres"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td><tt><font color="#3366FF">//-----------------------------------------------------------------------------</font></tt>
<br><tt><font color="#3366FF">// instead of having all values of useful
parameters as constants, read them:</font></tt>
<br><tt><font color="#3366FF">// either on the command line (--option=value
or -o=value)</font></tt>
<br><tt><font color="#3366FF">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
or in a parameter file (same syntax, order independent,&nbsp;</font></tt>
<br><tt><font color="#3366FF">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# = usual comment character&nbsp;</font></tt>
<br><tt><font color="#3366FF">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
or in the environment (TODO)</font></tt>
<br><tt><font color="#3366FF">// note that the parameters are passed by
reference so they can be updated</font></tt>
<br><b><tt><font color="#3366FF">void read_param(int argc, char *argv[],&nbsp;</font></tt></b>
<br><a NAME="seed_passing"></a><b><tt><font color="#660000">uint32 &amp;
_seed</font><font color="#3366FF">,</font></tt></b>
<br><b><tt><font color="#999900">unsigned int &amp; _vecSize</font><font color="#3366FF">,</font></tt></b>
<br><b><tt><font color="#009900">unsigned int &amp; _popSize,</font></tt></b>
<br><b><tt><font color="#009900">unsigned int &amp; _tSize</font><font color="#3366FF">,</font></tt></b>
<br><b><tt><font color="#CC33CC">double &amp; _pCross,</font></tt></b>
<br><b><tt><font color="#CC33CC">double &amp; _pMut</font><font color="#3366FF">,</font></tt></b>
<br><b><tt><font color="#3366FF">string &amp; _load_name,</font></tt></b>
<br><b><tt><font color="#3366FF">unsigned int &amp; _maxGen,</font></tt></b>
<br><b><tt><font color="#3366FF">unsigned int &amp; _minGen,</font></tt></b>
<br><b><tt><font color="#3366FF">unsigned int &amp; _steadyGen,</font></tt></b>
<br><b><tt><font color="#CC33CC">double &amp; _onePointRate,&nbsp;</font></tt></b>
<br><b><tt><font color="#CC33CC">double &amp; _twoPointsRate,&nbsp;</font></tt></b>
<br><b><tt><font color="#CC33CC">double &amp; _uRate,&nbsp;</font></tt></b>
<br><b><tt><font color="#CC33CC">double &amp; _pMutPerBit,&nbsp;</font></tt></b>
<br><b><tt><font color="#CC33CC">double &amp; _bitFlipRate,&nbsp;</font></tt></b>
<br><b><tt><font color="#CC33CC">double &amp; _oneBitRate</font></tt></b>
<br><b><tt><font color="#3366FF">)</font></tt></b>
<br><b><tt><font color="#3366FF">{&nbsp;</font></tt></b>
<br><a NAME="parser_declare"></a><tt><font color="#3366FF">// define a
parser from the command-line arguments</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; eoParser parser(argc,
argv);</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// For each
parameter, define Parameters directly in the parser,&nbsp;</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// and assign
the value to the variable</font></tt>
<br><a NAME="random"></a><a NAME="_seed_declare"></a><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;
</font><font color="#660000">eoValueParam&lt;uint32>&amp; seedParam = parser.createParam&lt;uint32>(time(0),
"seed", "Random number seed", 'S');</font></tt></b>
<br><a NAME="seed_assign"></a><b><tt><font color="#660000">&nbsp;&nbsp;&nbsp;&nbsp;
_seed = seedParam.value();</font></tt></b>
<p><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#999900">eoValueParam&lt;unsigned
int>&amp; vecSizeParam = parser.createParam&lt;unsigned int>(8, "vecSize",
"Genotype size",'V', "Representation");</font></tt></b>
<br><b><tt><font color="#999900">&nbsp;&nbsp;&nbsp;&nbsp; _vecSize = vecSizeParam.value();</font></tt></b>
<p><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#009900">eoValueParam&lt;unsigned
int>&amp; popSizeParam = parser.createParam&lt;unsigned int>(10, "popSize",
"Population size",'P', "Evolution");</font></tt></b>
<br><b><tt><font color="#009900">&nbsp;&nbsp;&nbsp;&nbsp; _popSize = popSizeParam.value();</font></tt></b>
<br><b><tt><font color="#009900">&nbsp;&nbsp;&nbsp;&nbsp; eoValueParam&lt;unsigned
int>&amp; tSizeParam = parser.createParam&lt;unsigned int>(10, "tSize",
"Tournament size",'T', "Evolution");</font></tt></b>
<br><b><tt><font color="#009900">&nbsp;&nbsp;&nbsp;&nbsp; _tSize = tSizeParam.value();</font></tt></b>
<br><a NAME="_load_name"></a>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp; eoValueParam&lt;string>&amp;
load_nameParam = parser.createParam&lt;string>("", "Load","A save file
to restart from",'L', "Persistence");</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; _load_name =
load_nameParam.value();</font></tt></b>
<p><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; eoValueParam&lt;unsigned
int>&amp; maxGenParam = parser.createParam&lt;unsigned int>(100, "maxGen",
"Maximum number of generations",'G', "Stopping criterion");</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; _maxGen = maxGenParam.value();</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; eoValueParam&lt;unsigned
int>&amp; minGenParam = parser.createParam&lt;unsigned int>(100, "minGen",
"Minimum number of generations",'g', "Stopping criterion");</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; _minGen = minGenParam.value();</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; eoValueParam&lt;unsigned
int>&amp; steadyGenParam = parser.createParam&lt;unsigned int>(100, "steadyGen",
"Number of generations with no improvement",'s', "Stopping criterion");</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; _steadyGen =
steadyGenParam.value();</font></tt></b>
<p><b><tt><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp; eoValueParam&lt;double>&amp;
pCrossParam = parser.createParam&lt;double>(0.6, "pCross", "Probability
of Crossover", 'C', "Genetic Operators");&nbsp;</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp; _pCross = pCrossParam.value();</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp; eoValueParam&lt;double>&amp;
pMutParam = parser.createParam&lt;double>(0.1, "pMut", "Probability of
Mutation", 'M', "Genetic Operators");</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp; _pMut = pMutParam.value();</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp; eoValueParam&lt;double>&amp;
onePointRateParam = parser.createParam&lt;double>(1, "onePointRate", "Relative
rate for one point crossover", '1', "Genetic Operators");</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp; _onePointRate
= onePointRateParam.value();</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp; eoValueParam&lt;double>&amp;
twoPointsRateParam = parser.createParam&lt;double>(1, "twoPointRate", "Relative
rate for two point crossover", '2', "Genetic Operators");</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp; _twoPointsRate
= twoPointsRateParam.value();</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp; eoValueParam&lt;double>&amp;
uRateParam = parser.createParam&lt;double>(2, "uRate", "Relative rate for
uniform crossover", 'U', "Genetic Operators");</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp; _uRate =&nbsp;
uRateParam.value();</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp; eoValueParam&lt;double>&amp;
pMutPerBitParam = parser.createParam&lt;double>(0.01, "pMutPerBit", "Probability
of flipping 1 bit in bit-flip mutation", 'b', "Genetic Operators");</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp; _pMutPerBit =
pMutPerBitParam.value();</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp; eoValueParam&lt;double>&amp;
bitFlipRateParam = parser.createParam&lt;double>(0.01, "bitFlipRate", "Relative
rate for bit-flip mutation", 'B', "Genetic Operators");</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp; _bitFlipRate
=&nbsp; bitFlipRateParam.value();</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp; eoValueParam&lt;double>&amp;
oneBitRateParam = parser.createParam&lt;double>(0.01, "oneBitRate", "Relative
rate for deterministic bit-flip mutation", 'D', "Genetic Operators");</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
_oneBitRate = oneBitRateParam.value();</font></tt></b>
<p><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// the name
of the "status" file where all actual parameter values will be saved</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; string str_status
= parser.ProgramName() + ".status";</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; eoValueParam&lt;string>&amp;
status_nameParam = parser.createParam&lt;string>(str_status.c_str(), "status","Status
file",'S', "Persistence");</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp; </b>// do the following AFTER
ALL PARAMETERS HAVE BEEN PROCESSED</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp; </b>// i.e. in case you need
parameters somewhere else, postpone these</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; if (parser.userNeedsHelp())</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
parser.printHelp(cout);</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
exit(1);</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; if (status_nameParam.value()
!= "")</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{</font></tt></b>
<br><b><tt><font color="#3366FF">ofstream os(status_nameParam.value().c_str());</font></tt></b>
<br><tt><font color="#3366FF"><b>os &lt;&lt; parser; </b>// and you can
use that file as parameter file</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</font></tt></b>
<br><b><tt><font color="#3366FF">}</font></tt></b></td>
</tr>
</table>
<a NAME="general"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td><tt><font color="#993300">// now the main_function: nothing changed,
except input/output</font></tt>
<br><b><tt><font color="#993300">void main_function(int argc, char **argv)</font></tt></b>
<br><b><tt><font color="#993300">{</font></tt></b></td>
</tr>
</table>
<a NAME="parametres"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td><a NAME="seed_declare"></a><b><tt><font color="#660000">&nbsp;&nbsp;
uint32 seed;</font></tt></b>
<br><tt><font color="#999900"><b>&nbsp;&nbsp; </b>// decription of genotype</font></tt>
<br><b><tt><font color="#999900">&nbsp;&nbsp; unsigned int vecSize;</font></tt></b>
<br><tt><font color="#33CC00"><b>&nbsp;&nbsp; </b>// parameters for evolution
engine</font></tt>
<br><b><tt><font color="#33CC00">&nbsp;&nbsp; unsigned int popSize;</font></tt></b>
<br><b><tt><font color="#33CC00">&nbsp;&nbsp; unsigned int tSize;</font></tt></b>
<br><tt><font color="#CC33CC"><b>&nbsp;&nbsp; </b>// operators probabilities
at the algorithm level</font></tt>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp; double pCross;</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp; double pMut;</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp; </b>// init and stop</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp; string load_name;</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp; unsigned int maxGen;</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp; unsigned int minGen;</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp; unsigned int steadyGen;</font></tt></b>
<br><tt><font color="#CC33CC"><b>&nbsp;&nbsp; </b>// rates for crossovers</font></tt>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp; double onePointRate;</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp; double twoPointsRate;</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp; double URate;</font></tt></b>
<br><tt><font color="#CC33CC"><b>&nbsp;&nbsp; </b>// rates and private
parameters for mutations;</font></tt>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp; double pMutPerBit;</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp; double bitFlipRate;</font></tt></b>
<br><b><tt><font color="#CC33CC">&nbsp;&nbsp; double oneBitRate;</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp; </b>// Now read the parameters
of the program</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; read_param(argc,
argv, seed, vecSize, popSize, tSize,</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
pCross, pMut, load_name, maxGen, minGen, steadyGen,</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
onePointRate, twoPointsRate, URate,&nbsp;</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
pMutPerBit, bitFlipRate, oneBitRate );</font></tt></b></td>
</tr>
</table>
<a NAME="eval"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
<tr>
<td><tt><font color="#CC0000"><b>&nbsp;</b>/////////////////////////////</font></tt>
<br><tt><font color="#CC0000"><b>&nbsp;</b>// Fitness function</font></tt>
<br><tt><font color="#CC0000"><b>&nbsp;</b>////////////////////////////</font></tt>
<br><tt><font color="#CC0000"><b>&nbsp;</b>// Evaluation: from a plain
C++ fn to an EvalFunc Object ...</font></tt>
<br><b><tt><font color="#CC0000">&nbsp;eoEvalFuncPtr&lt;Indi, double, const
vector&lt;bool>&amp; > plainEval( binary_value );</font></tt></b>
<br><a NAME="evalcounter"></a><tt><font color="#CC0000">// ... to an object
that counts the nb of actual evaluations</font></tt>
<br><b><tt><font color="#CC0000">&nbsp;eoEvalFuncCounter&lt;Indi> eval(plainEval);</font></tt></b></td>
</tr>
</table>
<a NAME="init"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td><tt><font color="#993399"><b>&nbsp;</b>////////////////////////////////</font></tt>
<br><tt><font color="#993399"><b>&nbsp;</b>// Initilisation of population</font></tt>
<br><tt><font color="#993399"><b>&nbsp;</b>////////////////////////////////</font></tt>
<br><tt><font color="#993399"><b>&nbsp;</b>// Either load or initialize</font></tt>
<br><tt><font color="#993399"><b>&nbsp;</b>// create an empty pop</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoPop&lt;Indi> pop;</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;</b>// create a state for reading</font></tt>
<br><tt><font color="#993399"><b>&nbsp;eoState inState; </b>// a state
for loading - WITHOUT the parser</font></tt>
<br><a NAME="register"></a><tt><font color="#993399">// register the rng
and the pop in the state, so they can be loaded,</font></tt>
<br><tt><font color="#993399"><b>&nbsp;</b>// and the present run will
be the exact conitnuation of the saved run</font></tt>
<br><tt><font color="#993399"><b>&nbsp;</b>// eventually with different
parameters</font></tt>
<br><b><tt><font color="#993399">&nbsp;inState.registerObject(rng);</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;inState.registerObject(pop);</font></tt></b>
<p><a NAME="instate.load"></a><b><tt><font color="#993399">if (load_name
!= "")</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp; {</font></tt></b>
<br><a NAME="loadstate"></a><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
inState.load(load_name); </b>//&nbsp; load the pop and the rng</font></tt>
<br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</b>// the fitness is read in the file:&nbsp;</font></tt>
<br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</b>// do only evaluate the pop if the fitness has changed</font></tt>
<br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp; }</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;else</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp; {</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
rng.reseed(seed);</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</b>// a Indi random initializer</font></tt>
<br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</b>// based on boolean_generator class (see utils/rnd_generator.h)</font></tt>
<br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
eoInitFixedLength&lt;Indi, boolean_generator>&nbsp;</font></tt></b>
<br><b><tt><font color="#993399">random(vecSize, boolean_generator());</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</b>// Init pop from the randomizer: need to use the append function</font></tt>
<br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
pop.append(popSize, random);&nbsp;</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</b>// and evaluate pop (STL syntax)&nbsp;</font></tt>
<br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
apply&lt;Indi>(eval, pop);</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp; } </b>// end
of initializatio of the population</font></tt></td>
</tr>
</table>
<a NAME="output"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td><tt><font color="#3366FF"><b>&nbsp;</b>// sort pop for pretty printout</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;pop.sort();</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;</b>// Print (sorted) intial population
(raw printout)</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;cout &lt;&lt; "Initial Population"
&lt;&lt; endl &lt;&lt; pop &lt;&lt; endl;</font></tt></b></td>
</tr>
</table>
<a NAME="engine"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
<tr>
<td><tt><font color="#009900"><b>&nbsp;</b>/////////////////////////////////////</font></tt>
<br><tt><font color="#009900"><b>&nbsp;</b>// selection and replacement</font></tt>
<br><tt><font color="#009900"><b>&nbsp;</b>////////////////////////////////////</font></tt></td>
</tr>
</table>
<a NAME="select"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
<tr>
<td><tt><font color="#009900"><b>&nbsp;</b>// The robust tournament selection</font></tt>
<br><tt><font color="#009900"><b>&nbsp;eoDetTournament&lt;Indi> selectOne(tSize);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</b>// tSize in [2,POPSIZE]</font></tt>
<br><tt><font color="#009900"><b>&nbsp;</b>// is now encapsulated in a
eoSelectPerc (entage)</font></tt>
<br><tt><font color="#009900"><b>&nbsp;eoSelectPerc&lt;Indi> select(selectOne);</b>//
by default rate==1</font></tt></td>
</tr>
</table>
<a NAME="replace"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
<tr>
<td><tt><font color="#009900"><b>&nbsp;</b>// And we now have the full
slection/replacement - though with&nbsp;</font></tt>
<br><tt><font color="#009900"><b>&nbsp;</b>// no replacement (== generational
replacement) at the moment :-)</font></tt>
<br><b><tt><font color="#009900">&nbsp;eoNoReplacement&lt;Indi> replace;&nbsp;</font></tt></b></td>
</tr>
</table>
<a NAME="operators"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td><tt><font color="#993399"><b>&nbsp;</b>//////////////////////////////////////</font></tt>
<br><tt><font color="#993399"><b>&nbsp;</b>// The variation operators</font></tt>
<br><tt><font color="#993399"><b>&nbsp;</b>//////////////////////////////////////</font></tt></td>
</tr>
</table>
<a NAME="crossover"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td><tt><font color="#993399"><b>&nbsp;</b>// 1-point crossover for bitstring</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoBinCrossover&lt;Indi> xover1;</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;</b>// uniform crossover for bitstring</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoBinUxOver&lt;Indi> xoverU;</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;</b>// 2-pots xover</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoBinNxOver&lt;Indi> xover2(2);</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;</b>// Combine them with relative
rates</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoPropCombinedQuadOp&lt;Indi> xover(xover1,
onePointRate);</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;xover.add(xoverU, URate);</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;xover.add(xover2, twoPointsRate,
true);</font></tt></b></td>
</tr>
</table>
<a NAME="mutation"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td><tt><font color="#993399"><b>&nbsp;</b>// standard bit-flip mutation
for bitstring</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoBinMutation&lt;Indi>&nbsp; mutationBitFlip(pMutPerBit);</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;</b>// mutate exactly 1 bit per
individual</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoDetBitFlip&lt;Indi> mutationOneBit;&nbsp;</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;</b>// Combine them with relative
rates</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoPropCombinedMonOp&lt;Indi> mutation(mutationBitFlip,
bitFlipRate);</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;mutation.add(mutationOneBit, oneBitRate,
true);</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;</b>// The operators are&nbsp; encapsulated
into an eoTRansform object</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoSGATransform&lt;Indi> transform(xover,
pCross, mutation, pMut);</font></tt></b></td>
</tr>
</table>
<a NAME="stop"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td><tt><font color="#3366FF"><b>&nbsp;</b>//////////////////////////////////////</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;</b>// termination condition see
FirstBitEA.cpp</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;</b>/////////////////////////////////////</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;eoGenContinue&lt;Indi> genCont(maxGen);</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;eoSteadyFitContinue&lt;Indi> steadyCont(minGen,
steadyGen);</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;eoFitContinue&lt;Indi> fitCont(vecSize);</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;eoCombinedContinue&lt;Indi> continuator(genCont);</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;continuator.add(steadyCont);</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;continuator.add(fitCont);</font></tt></b></td>
</tr>
</table>
<a NAME="checkpoint"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td><tt><font color="#3366FF"><b>&nbsp;</b>// but now you want to make
many different things every generation&nbsp;</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;</b>// (e.g. statistics, plots,
...).</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;</b>// the class eoCheckPoint is
dedicated to just that:</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;</b>// Declare a checkpoint (from
a continuator: an eoCheckPoint&nbsp;</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;</b>// IS AN eoContinue and will
be called in the loop of all algorithms)</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;eoCheckPoint&lt;Indi> checkpoint(continuator);</font></tt></b>
<p><a NAME="param_declare"></a><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;
</b>// Create a counter parameter</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; eoValueParam&lt;unsigned>
generationCounter(0, "Gen.");</font></tt></b>
<p><a NAME="param_pass"></a><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;
</b>// Create an incrementor (sub-class of eoUpdater). Note that the&nbsp;</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// parameter's
value is passed by reference,&nbsp;</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// so every
time the incrementer is updated (every generation),</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// the data
in generationCounter will change.</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; eoIncrementor&lt;unsigned>
increment(generationCounter.value());</font></tt></b>
<br><a NAME="updater_pass"></a><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;
</b>// Add it to the checkpoint,&nbsp;</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// so the
counter is updated (here, incremented) every generation</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; checkpoint.add(increment);</font></tt></b>
<br><a NAME="stat_declare"></a><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;
</b>// now some statistics on the population:</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// Best fitness
in population</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; eoBestFitnessStat&lt;Indi>
bestStat;</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// Second
moment stats: average and stdev</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; eoSecondMomentStats&lt;Indi>
SecondStat;</font></tt></b>
<br><a NAME="stat_pass"></a><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;
</b>// Add them to the checkpoint to get them called at the appropriate
time</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; checkpoint.add(bestStat);</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; checkpoint.add(SecondStat);</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// The Stdout
monitor will print parameters to the screen ...</font></tt>
<br><a NAME="monitor_declare"></a><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;
eoStdoutMonitor monitor(false);</font></tt></b>
<p><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// when called
by the checkpoint (i.e. at every generation)</font></tt>
<br><a NAME="monitor_pass"></a><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;
checkpoint.add(monitor);</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// the monitor
will output a series of parameters: add them&nbsp;</font></tt>
<br><a NAME="monitor_add"></a><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;
monitor.add(generationCounter);</font></tt></b>
<a name="eval_monitor"></a>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; monitor.add(eval);
</b>//
because now eval is an eoEvalFuncCounter!</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; monitor.add(bestStat);</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; monitor.add(SecondStat);</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// A file
monitor: will print parameters to ... a File, yes, you got it!</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; eoFileMonitor
fileMonitor("stats.xg", " ");</font></tt></b>
<p><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// the checkpoint
mechanism can handle multiple monitors</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; checkpoint.add(fileMonitor);</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// the fileMonitor
can monitor parameters, too, but you must tell it!</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; fileMonitor.add(generationCounter);</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; fileMonitor.add(bestStat);</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; fileMonitor.add(SecondStat);</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// Last type
of item the eoCheckpoint can handle: state savers:</font></tt>
<br><a NAME="outstate_declare"></a><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;
eoState outState;</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// Register
the algorithm into the state (so it has something to save!!)</font></tt>
<br><a NAME="outstate_register"></a><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;
outState.registerObject(rng);</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; outState.registerObject(pop);</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// and feed
the state to state savers</font></tt>
<br><a NAME="statesaver_declare"></a><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;
</b>// save state every 100th&nbsp; generation</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; eoCountedStateSaver
stateSaver1(100, outState, "generation");&nbsp;</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// save state
every 1 seconds&nbsp;</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; eoTimedStateSaver&nbsp;&nbsp;&nbsp;
stateSaver2(1, outState, "time");&nbsp;</font></tt></b>
<br><a NAME="statesaver_pass"></a><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;
</b>// Don't forget to add the two savers to the checkpoint</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; checkpoint.add(stateSaver1);</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; checkpoint.add(stateSaver2);</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp; </b>// and that's
it for the (control and) output</font></tt></td>
</tr>
</table>
<a NAME="generation"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td><tt><font color="#FF6666"><b>&nbsp;</b>/////////////////////////////////////////</font></tt>
<br><tt><font color="#FF6666"><b>&nbsp;</b>// the algorithm</font></tt>
<br><tt><font color="#FF6666"><b>&nbsp;</b>////////////////////////////////////////</font></tt>
<br><tt><font color="#FF6666"><b>&nbsp;</b>// Easy EA requires&nbsp;</font></tt>
<br><tt><font color="#FF6666"><b>&nbsp;</b>// selection, transformation,
eval, replacement, and stopping criterion</font></tt>
<br><b><tt><font color="#FF6666">&nbsp;eoEasyEA&lt;Indi> gga(checkpoint,
eval, select, transform, replace);</font></tt></b>
<br><tt><font color="#FF6666"><b>&nbsp;</b>// Apply algo to pop - that's
it!</font></tt>
<br><b><tt><font color="#FF6666">&nbsp;gga(pop);</font></tt></b></td>
</tr>
</table>
<a NAME="output"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td><tt><font color="#3366FF"><b>&nbsp;</b>// Print (sorted) final population</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;pop.sort();</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;cout &lt;&lt; "FINAL Population\n"
&lt;&lt; pop &lt;&lt; endl;</font></tt></b></td>
</tr>
</table>
<a NAME="general"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td><b><tt><font color="#993300">}</font></tt></b>
<br><tt><font color="#993300">// A main that catches the exceptions</font></tt>
<br><b><tt><font color="#993300">int main(int argc, char **argv)</font></tt></b>
<br><b><tt><font color="#993300">{</font></tt></b>
<br><b><tt><font color="#993300">#ifdef _MSC_VER</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;&nbsp;&nbsp;&nbsp; int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; flag
|= _CRTDBG_LEAK_CHECK_DF;</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;&nbsp;&nbsp;&nbsp; _CrtSetDbgFlag(flag);</font></tt></b>
<br><tt><font color="#993300">//&nbsp;&nbsp;&nbsp; _CrtSetBreakAlloc(100);</font></tt>
<br><b><tt><font color="#993300">#endif</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;&nbsp;&nbsp;&nbsp; try</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;&nbsp;&nbsp;&nbsp; {</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
main_function(argc, argv);</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;&nbsp;&nbsp;&nbsp; }</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;&nbsp;&nbsp;&nbsp; catch(exception&amp;
e)</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;&nbsp;&nbsp;&nbsp; {</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
cout &lt;&lt; "Exception: " &lt;&lt; e.what() &lt;&lt; '\n';</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;&nbsp;&nbsp;&nbsp; }</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;&nbsp;&nbsp;&nbsp; return 1;</font></tt></b>
<br><b><tt><font color="#993300">}</font></tt></b></td>
</tr>
</table>
<hr WIDTH="100%"><a href="eoLesson3.html">Back to Lesson 3</a> - <a href="eoTutorial.html">Tutorial
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> - <b><font face="Arial,Helvetica"><a href="doc/html/index.html">EO
documentation</a></font></b>
<hr>
<address>
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last modified: Sun Nov
26 09:31:04 2000<!-- hhmts end -->
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

View file

@ -0,0 +1,57 @@
<!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.75 [en] (X11; U; Linux 2.2.17-21mdk i686) [Netscape]">
<title>binary_value.h</title>
</head>
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> - <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
documentation</a></font></font></b>
<br>
<hr WIDTH="100%">
<center>
<h1>
<font color="#FF0000">binary_value.h</font></h1></center>
<a NAME="start"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr NOSAVE>
<td NOSAVE><b><tt><font color="#993300">#include &lt;eo></font></tt></b>
<br><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
<br><tt><font color="#993300">/** Just a simple function that takes binary
value of a chromosome and sets</font></tt>
<br><tt><font color="#993300">&nbsp;&nbsp;&nbsp;&nbsp; the fitnes.</font></tt>
<br><tt><font color="#993300">&nbsp;&nbsp;&nbsp;&nbsp; @param _chrom A
binary chromosome&nbsp;</font></tt>
<br><tt><font color="#993300">*/</font></tt></td>
</tr>
</table>
<a NAME="init"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td><b><tt><font color="#993399">double binary_value(const vector&lt;bool>&amp;
_chrom)</font></tt></b>
<br><b><tt><font color="#993399">{</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;double sum = 0;</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;for (unsigned i = 0; i &lt; _chrom.size();
i++)</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp; sum += _chrom[i];</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;return sum;</font></tt></b>
<br><b><tt><font color="#993399">}</font></tt></b></td>
</tr>
</table>
<hr WIDTH="100%"><a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> - <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
documentation</a></font></font></b>
<hr>
<address>
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last modified: Wed Nov
29 09:03:09 2000<!-- hhmts end -->
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 209 KiB

View file

@ -0,0 +1,21 @@
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>TITRE</title>
</head>
<body text="#000000" background="beige009.jpg" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<a href="eoTutorial.html">Tutorial main page </a>-
<a href="eoTopDown.html">Top-Down
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO documentation</a></font>
<br>
<hr WIDTH="100%">
<!-- -------------- End of header ------------------ -->
<!-- ----------------------------------------------- -->
<center>
<h1><font color="#FF0000">TITRE</font></h1>
</center>
<font face="Courier New,Courier"><font size=+1>
<p>

View file

@ -0,0 +1,74 @@
<!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.72 [en] (X11; U; Linux 2.2.16 i686) [Netscape]">
<title>EO - The Bottom-Up approach</title>
</head>
<body text="#000000" bgcolor="#CCCCCC" link="#0000EF" vlink="#51188E" alink="#FF0000">
<a href="eoTutorial.html">Tutorial main page </a>-
<a href="eoTopDown.html">Top-Down
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
hints</a> - <a href="doc/html/index.html">EO documentation</a>
<br>
<hr WIDTH="100%">
<center>
<h1>
<font color="#FF0000">EO - Bottom-Up approach</font></h1></center>
<p><br>Congratualtions - You have chosen the bottom-up approach!&nbsp;
From here you will be allowed to browse into the different components of
an Evolutionary Algorithm, and to see how to program your favorite using
the EO library.
<h3>
<font color="#000099">Table of Content</font></h3>
<center>
<p><img SRC="EA_tutorial.jpg" USEMAP="#Map" ></center>
<p>Yes, this is the table of content of this part
of the EO tutorial. If you don't already know what this symbolic
representation of an EA means, you should try <a
href="eoIntroEA.html">here</a>. Otherwise, <font color="#FF6600">click on the figure</font>
to go directly to the corresponding section of the tutorial.
<br>
<map NAME="Map">
<area SHAPE="rect" HREF="eoInit.html" COORDS="14,31,135,70">
<area SHAPE="rect" HREF="eoEval.html" COORDS="14,110,135,150">
<area SHAPE="rect" HREF="eoRepresentation.html" COORDS="240,270,465,310">
<area SHAPE="rect" HREF="eoIo.html#output" COORDS="280,45,480,70">
<area SHAPE="rect" HREF="eoIo.html#stop" COORDS="348,110,430,150">
<area SHAPE="rect" HREF="eoEngine.html#selection" COORDS="495,110,615,150">
<area SHAPE="rect" HREF="eoRepresentation.html" COORDS="495,190,615,230">
<area SHAPE="rect" HREF="eoOperators.html#crossover" COORDS="495,265,625,287">
<area SHAPE="rect" HREF="eoOperators.html#mutation" COORDS="495,287,625,305">
<area SHAPE="rect" HREF="eoRepresentation.html" COORDS="170,110,295,150">
<area SHAPE="rect" HREF="eoEval.html" COORDS="170,270,295,310">
<area SHAPE="rect" HREF="eoEngine.html#replacement" COORDS="170,190,295,230">
<area SHAPE="rect" HREF="eoGeneration.html" COORDS="310,160,485,260">
<area SHAPE="rect" HREF="eoOperators.html" COORDS="15,350,260,370">
<area SHAPE="rect" HREF="eoRepresentation.html" COORDS="270,350,460,370">
<area SHAPE="rect" HREF="eoEngine.html" COORDS="15,377,400,397">
<area SHAPE="rect" HREF="eoEval.html" COORDS="15,403,230,423">
<area SHAPE="rect" HREF="eoCheckpoint.html" COORDS="15,430,221,450">
<area SHAPE="rect" HREF="eoCheckpoint.html#stop" COORDS="221,430,345,450">
<area SHAPE="rect" HREF="eoCheckpoint.html#stat" COORDS="375,430,445,450">
</map>
<br>
<hr WIDTH="100%">
<br><a href="eoTutorial.html">Tutorial main page </a>- <a href="eoTopDown.html">Top-Down
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
hints</a> - <a href="doc/html/index.html">EO documentation</a>
<br>
<hr>
<address>
<a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
<br><!-- Created: Mon Oct 30 07:27:13 CET 2000 --><!-- hhmts start -->Last
modified: Mon Oct 30 07:28:36 CET 2000<!-- hhmts end -->
</body>
</html>

View file

@ -0,0 +1,59 @@
<!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.72 [en] (X11; U; Linux 2.2.16 i686) [Netscape]">
<title>Genetic Engine</title>
</head>
<body>
<body text="#000000" link="#0000EF" vlink="#51188E" alink="#FF0000" background="beige009.jpg">
<center>
<h1>
<b><font color="#CC0000">Evolution Engine</font></b></h1></center>
<p><br><b><font color="#000099"><font size=+2>Contents</font></font></b>
<ul>
<li>
Introduction</li>
<li>
Selection</li>
<li>
Replacement</li>
<li>
Popular evolution engines</li>
</ul>
<p><br>The term evolution engine denotes the different parts that simulate
the Darwinism in Evolutionary Algorithms.<font color="#33CC00"></font>
<center>
<p><i><font color="#33CC00">The fittest individuals are more likely to
reproduce and survive.</font></i></center>
<p>Darwinism takes place in two different phases of an EA, though in many
<a href="#popular">popular variants</a>, only one phase is activated.
<p><a href="#selection">Selection</a> is the Darwinistic choice of parents
that will be allowed to <font color="#33CC00">reproduce</font><font color="#CC33CC">.</font>
<br><a href="#replacement">Replacement</a> takes place after reproduction,
and is the Darwinistic choice of those individuals that will <font color="#33CC00">survive</font>,
i.e. become the parents of the next generation.
<p><a NAME="selection"></a><b><font color="#000099"><font size=+2>Selection</font></font></b>
<br>&nbsp;
<p><a NAME="replacement"></a><b><font color="#000099"><font size=+2>Replacement</font></font></b>
<br>&nbsp;
<p><a NAME="popular"></a><b><font color="#000099"><font size=+2>Popular
evolution engines</font></font></b>
<br>&nbsp;
<br>&nbsp;
<p>
<hr>
<address>
<a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
<br><!-- Created: Mon Oct 30 18:15:16 CET 2000 --><!-- hhmts start -->Last
modified: Mon Oct 30 18:15:17 CET 2000<!-- hhmts end -->
</body>
</html>

View file

@ -0,0 +1,34 @@
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>Evaluation</title>
</head>
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<a href="eoTutorial.html">Tutorial main page </a>-
<a href="eoTopDown.html">Top-Down
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
hints</a> - <a href="doc/html/index.html">EO documentation</a>
<br>
<hr WIDTH="100%">
<center>
<h1><font color="#FF0000">Evaluation</font></h1>
</center>
<br>
<hr WIDTH="100%">
<a href="eoTutorial.html">Tutorial main page </a>-
<a href="eoTopDown.html">Top-Down
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
hints</a> - <a href="doc/html/index.html">EO documentation</a>
<hr>
<address><a href="mailto:Marc.Schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
<!-- Created: Mon Oct 30 17:51:43 CET 2000 -->
<!-- hhmts start -->
Last modified: Mon Nov 6 10:54:33 CET 2000
<!-- hhmts end -->
</body>
</html>

View file

@ -0,0 +1,19 @@
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>Generation</title>
</head>
<body>
<h1>Generation</h1>
<hr>
<address><a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
<!-- Created: Mon Oct 30 19:29:28 CET 2000 -->
<!-- hhmts start -->
Last modified: Mon Oct 30 19:29:29 CET 2000
<!-- hhmts end -->
</body>
</html>

View file

@ -0,0 +1,36 @@
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>Initialization</title>
</head>
<body text="#000000" background="beige009.jpg" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<a href="eoTutorial.html">Tutorial main page </a>-
<a href="eoTopDown.html">Top-Down
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
hints</a> - <a href="doc/html/index.html">EO documentation</a>
<br>
<hr WIDTH="100%">
<!-- -------------- End of header ------------------ -->
<!-- ----------------------------------------------- -->
<center>
<h1>
<b><font color="#CC0000">Initialization</font></b></h1></center>
<!-- Bottom of file: display some links and address -->
<!-- ----------------------------------------------- -->
<br>
<hr WIDTH="100%">
<a href="eoTutorial.html">Tutorial main page </a>- <a href="eoTopDown.html">Top-Down
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
hints</a> - <a href="doc/html/index.html">EO documentation</a>
<hr>
<address>
<a href="mailto:Marc.Schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
<!-- Created: Mon Oct 30 17:51:29 CET 2000 -->
<!-- hhmts start -->
Last modified: Mon Nov 6 11:25:18 CET 2000
<!-- hhmts end -->
</body>
</html>

View file

@ -0,0 +1,25 @@
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>Input / Output</title>
</head>
<body>
<h1>Input / Output</h1>
<p>
<A NAME=stop>
<h3>Stopping criteria</h3>
<p>
<A NAME=output>
<h3>Displaying statistics</h3>
<hr>
<address><a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
<!-- Created: Mon Oct 30 19:29:46 CET 2000 -->
<!-- hhmts start -->
Last modified: Tue Oct 31 18:32:22 CET 2000
<!-- hhmts end -->
</body>
</html>

View file

@ -0,0 +1,413 @@
<!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.75 [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">Tutorial
main page</a> -
<a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> -<font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
documentation</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 within
EO 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>
write your own <font color="#990000">fitness function</font>,</li>
<li>
use different kinds of <font color="#009900">selection procedures</font>
in the framework of a generational GA <font color="#009900">evolution engine</font>,</li>
<li>
check that EO let you separate the <font color="#999900">representation</font>
from the <font color="#009900">evolution engine</font>.</li>
</ul>
<h3>
<a NAME="run"></a><font color="#000099">I want to run an Evolutionary Algorithm
<b>now</b></font></h3>
You can choose to run a standard <a href="FirstBitGA.html">bitstring Genetic
Algorithm</a> (as defined in Goldberg's book) or a standard <a href="FirstRealGA.html">real-valued
genetic 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">Tutorial
main page</a> , do it <b><font color="#FF6600">NOW</font></b>. Then go
to the Lesson1 sub-dir of the tutorial dir, and simply type at the system
prompt
<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 the
number 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 default
action for EO is to maximize) the inverse of the sum (i.e. minimizing the
sum) of the square of its variables (also called the <font color="#FF6600">sphere
function</font>, whose solution is ... <font color="#FF6600">all zeroes</font>).
<p>And what you see on the screen when running one of these two programs
is, in each case, the initial and final population of an Evolutionary run,
one individual per line, its fitness first, then the number of items (bits
or real numbers) of the genotype, and the genotype itself. The final population
hopefully contains the solution in the discrete case, and is close to it
in the continuous case.
<p><a NAME="browse"></a>You need now to take a look at either programs
by browsing alone through the&nbsp; sources for <a href="FirstBitGA.html">FirstBitGA</a>
and <a href="FirstRealGA.html">FirstRealGA</a>, or follow the guided tour
below, or go directly to the <a href="#exercise1">exercises</a>.
<h3>
<a NAME="tour"></a><font color="#000099">Browsing the code:</font></h3>
<ul>
<li>
<b><font color="#993300">General includes:</font></b><font color="#000000">Like
all 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 spedific 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 most important EO files.</font></li>
<br>&nbsp;
<li>
<b><font color="#999900">Representation</font></b><font color="#000000">:
you then have to delclare the type of individuals you will be handling.
All evolution-related objects you will need are templatized w.r.t. the
type of individuals.</font></li>
<ul>
<li>
<a href="FirstBitGA.html#representation">Bit</a> You say here that you
will be handling <b><font face="Arial,Helvetica"><a href="doc/html/class_eobin.html">Bitstring
genotypes</a></font></b>, whose fitness is a double. This makes Indi derive
from the <a href="eoProgramming.html#STL">STL class</a> <font color="#993300">vector&lt;bool></font></li>
<li>
<a href="FirstRealGA.html#representation">Real</a> You say here that you
will be handling <b><font face="Arial,Helvetica"><a href="doc/html/class_eoreal.html">Real-valued
genotypes</a></font></b>, whose fitness is a double. This makes Indi derive
from the <a href="eoProgramming.html#STL">STL class</a> <font color="#993300">vector&lt;double></font></li>
<br>&nbsp;</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 take
as argument a reference to an individual (at the moment).</font></li>
<ul>
<li>
<a href="FirstBitGA.html#evalfunc">Bit</a> This function simply computes
the 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 computes
the inverse of the sum of the squares of all variables (also called the
sphere function). The optimum is of course the all-zeroes vector.</li>
<br>&nbsp;</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 and
assigned. Of course, this means that you will need to recompile to change
these values - see Lesson 3 to get rid of that heavy requirement.</font></li>
<br>&nbsp;
<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">EO
programming 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">global
Random 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 variable
of 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>):
everytime you run the algorithm with the </font><font color="#FF6600">same
seed</font><font color="#000000">, you will get the </font><font color="#FF6600">same
result</font><font color="#000000">. Hence, to test the robustness of your
algorithm, you should run it with different seeds. This is rather time
consuming in the present programs, so we suggest that you wait until Lesson
3 to do so.</font></li>
<br>&nbsp;
<li>
<b><font color="#CC0000">Fitness function encapsulation: </font></b><font color="#000000">EO
is based on the notion of <a href="eoProgramming.html#functors">functors</a>
- hence you now need to encapsulate your fitness function into a functor
object. This is what is done here (<a href="FirstBitGA.html#eval">Bit</a>
- <a href="FirstRealGA.html#eval">Real</a>).</font></li>
<br>&nbsp;
<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&lt;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&lt;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/class_eorng.html">random
boolean</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/class_eorng.html">real
value uniformly drawn in [0,1].</a></font></font></li>
<br>&nbsp;</ul>
<li>
<b><font color="#3366FF">Output</font></b><font color="#000000">: take
a snapshot at the initial population (<a href="FirstBitGA.html#output">Bit</a>
- <a href="FirstRealGA.html#output">Real</a>). Sort it first, so the best
individuals are first, and display it. Note that an eoPop has a </font><b><tt><font color="#990000">&lt;&lt;</font></tt></b><font color="#000000">
method, which means that a simple </font><b><tt><font color="#990000">os
&lt;&lt; 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/class_eoprintable.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">&lt;&lt;</font></tt></b><font color="#000000">
operator).</font></li>
<br>&nbsp;
<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
GA here: a simple selector, and a generational replacement. The <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eodettournament.html">eoDetTournament</a></font></font></b>
has been chosen as a robust selection, and the generational replacement
(all parents are replaced by the offspring) is hard-coded in the eoSGA
<a href="#algorithm">algorithm</a>.</font></li>
<br>&nbsp;
<li>
<font color="#CC33CC"><b>Variation operators</b>:</font><font color="#000000">
in the simple algorithm considered here, individuals undergo </font><font color="#CC33CC">crossover</font><font color="#000000">
and </font><font color="#CC33CC">mutation</font><font color="#000000">.
In EO, these operators are (<a href="eoProgramming.html#functors">functor</a>)
objects of class</font> <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eoquadraticop.html">eoQuadOp</a></font></font></b>
(binary operator that modifies both its arguments) and <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eomonop.html">eoMonOp</a></font></font></b>
(unary operator).&nbsp; These operators are applied in turn to all selected
parents, according to user-defined probabilities.&nbsp; These probabilities
are defined with all other <a href="#parametres">parameters</a>, and will
be passed to the <b><tt><font color="#FF6666"><font size=+1>eoSGA</font></font></tt></b><a href="#parametres">algorithm</a><font color="#000000">.</font></li>
<ul>
<li>
<a href="FirstBitGA.html#operators">Bit</a> The crossover <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eobincrossover.html">eoBinCrossover</a></font></font></b>
is the standard <font color="#CC33CC">1-point crossover</font>, and <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eobinmutation.html">eoBinMutation</a></font></font></b>
is the standard <font color="#FF6600">bit-flip mutation</font> that randomly
flips all bits with a given probability <b><tt>P_MUT_PER_BIT.</tt></b></li>
<br><b><font color="#FF0000">Warning</font></b>: the <b><tt>P_MUT_PER_BIT</tt></b>
probability is an <font color="#FF6600">internal parameter</font> of the
<b><tt><font color="#CC33CC"><font size=+1>eoBinMutation</font></font></tt></b>,
it is <b><font color="#FF6600">NOT</font></b> the probability of mutation
at the individual level. EO corrects what can be viewed as an inconsistency
in Holland's original work, further used in Goldberg's book by separating
the probability of mutation for each individual (independent of the type
of mutation that will be applied) from the probability of flipping each
bit, which is specific of the bit-flip mutation.&nbsp; Hence, to run the
same algorithm as Goldberg's SGA, the mutation probability (at individual
level) is 1, and the probability of flipping each bit is <b><tt>P_MUT_PER_BIT.</tt></b>
<li>
<a href="FirstRealGA.html#operators">Real</a> The crossover <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eoarithmeticcrossover.html">eoArithmeticCrossover</a></font></font></b>
is the standard <font color="#CC33CC">arithmetic crossover</font> for real-valued
vectors, that chooses a point randomly on the segment between both parents
(also termed <font color="#CC33CC">BLX-0</font>). <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eouniformmutation.html">eoUniformMutation</a></font></font></b>
is the <font color="#CC33CC">uniform mutation</font> for real-valued vectors
that choses a new value for each variable uniformly on an interval centered
on the parent value. The width of the interval is an <font color="#FF6600">internal
parameter</font> of the object, here called <b><tt>EPSILON</tt></b>.</li>
<br>&nbsp;</ul>
<li>
<b><font color="#3366FF">Stopping criterion:</font></b><font color="#000000">
Specify a </font><font color="#3366FF">maximum number of generations</font><font color="#000000">
to run (<a href="FirstBitGA.html#stop">Bit</a> - <a href="FirstRealGA.html#stop">Real</a>):
the simplest of all stopping criteria at the moment, using an object of
a sub-class of class </font><b><font face="Arial,Helvetica"><font color="#3366FF"><font size=+1><a href="doc/html/class_eocontinue.html">eoContinue</a></font></font></font></b><font color="#000000">.</font></li>
<br>&nbsp;
<li>
<a NAME="algorithm"></a><b><font color="#FF6666">The algorithm: </font></b><font color="#000000">the
simple algorithm that is used here, called&nbsp; </font><b><tt><font color="#FF6666"><font size=+1>eoSGA
</font></font></tt></b><font color="#000000">requires
as parameters a </font><font color="#FF6600">selector</font><font color="#000000">,
a </font><font color="#FF6600">crossover</font><font color="#000000"> and
the associated </font><font color="#FF6600">crossover rate</font><font color="#000000">,
a </font><font color="#FF6600">mutation</font><font color="#000000"> and
the associated </font><font color="#FF6600">mutation rate,</font><font color="#000000">
and a </font><font color="#FF6600">stopping criterion</font><font color="#000000">.
Take a look at the corresponding
<a href="eoSGA.html#constructor">constructor</a>
of the class </font><b><tt><font color="#FF6666"><font size=+1>eoSGA</font></font></tt></b><font color="#000000">:
it only initializes its <a href="eoSGA.html#parametres">private data</a>
with the parameters. Now look at the <a href="eoSGA.html#generation">operator()</a>
method - the one that is called in the code for <a href="FirstBitGA.html#generation">FirstBitGA</a>
or <a href="FirstRealGA.html#generation">FirstRealGA</a> - and you'll find
out that is is as simple as it sounds.</font></li>
<br>&nbsp;
<li>
<b><font color="#3366FF">Output</font></b><font color="#000000">: After
running the algorithm, output the sorted final population (<a href="FirstBitGA.html#final_output">Bit</a>
- <a href="FirstRealGA.html#final_output">Real</a>) - and look at the best
individual: this is the result of the algorithm.</font></li>
<br>&nbsp;
<li>
<b><font color="#990000">Main body:</font></b><font color="#000000"> for
technical reasons (intercepting the exceptions), we need a main like this
one (<a href="FirstBitGA.html#main">Bit</a> - <a href="FirstRealGA.html#main">Real</a>).,
and you should not touch it unless you know what you are doing. Simply
note that this main calls the function main_function, which we have been
discussing up to now!</font></li>
</ul>
<h3>
<a NAME="exercise1"></a><font color="#000099">Exercise 1: maximize your
own function</font></h3>
This is very easy - if your search space is that of bitstring or of unbounded
real numbers.
<ul>
<li>
Go to the tutorial directory, and <font color="#FF6600">copy</font> the
program you want to modify onto <b><tt><font color="#663300">mytest.cpp.</font></tt></b></li>
<li>
<font color="#FF6600">Edit</font> <b><tt><font color="#990000">mytest.cpp</font></tt></b>
with any text editor:</li>
<li>
<font color="#FF6600">Modify</font> the fitness function itself (<a href="FirstBitGA.html#evalfunc">binary_value</a>
in <font color="#FF6666"><b><tt>FirstBitGA</tt></b><font face="Courier New,Courier">,</font></font><a href="FirstRealGA.html#evalfunc">real_value</a>
in&nbsp; <font color="#FF6666"><b><tt>FirstRealGA</tt></b><font face="Courier New,Courier">)</font></font></li>
<li>
<font color="#FF6600">Compile</font> the program by typing <b><tt><font color="#990000">make
mytest</font></tt></b> at system prompt</li>
<li>
<font color="#FF6600">Run</font> the new program by entering the command
<b><tt><font color="#990000">mytest</font></tt></b>
at system prompt.</li>
</ul>
<h3>
<font color="#000099">Exercise 2: check the differences between both programs</font></h3>
Go and take a look at the code for these programs <font color="#000000">(<a href="FirstBitGA.html">Bit</a>
- <a href="FirstRealGA.html">Real</a>)</font>. Use the symbolic representation
of an Evolutionary Algorithm (you should understand that figure now, otherwise
go <a href="eoIntro.html">there</a> and come back) to understand how each
part of the EA is coded. Try to spot the differences between both codes:
there are not so many!
<br>After you've tried that alone, take a look at the <a href="Firstmerge.html">solution</a>
:-)
<br>&nbsp;
<h3>
<font color="#000099">Exercise 3: change the selection procedure</font></h3>
This is rather straightforward ... if you know what other types of selection
are available!
<br>At the moment, let's only consider only the following simple ones:
<ul>
<li>
You already know the <font color="#FF6600">tournament selection</font></li>
<br><font color="#FF0000">Syntax:&nbsp; </font><tt><font color="#009900"><b>eoDetTournament&lt;Indi>
select(T_SIZE);&nbsp;&nbsp; </b>// T_SIZE in [2,POP_SIZE)</font></tt>
<li>
Try the well-known <font color="#FF6600">roulette wheel</font></li>
<br>&nbsp;<font color="#FF0000">Syntax:&nbsp;</font>&nbsp;&nbsp; <b><tt><font color="#009900">eoProportional&lt;Indi>
select;</font></tt></b>
<li>
Or the <font color="#FF6600">stochastic binary tournament</font></li>
<br><font color="#FF0000">Syntax:&nbsp; </font><tt><font color="#009900"><b>eoStochTournament&lt;Indi>
select(RATE);&nbsp;</b>&nbsp;&nbsp;&nbsp; // RATE in ]0.5,1]</font></tt>
<li>
and of course the <font color="#FF6600">random</font> selection should
give bad results!</li>
<br><font color="#FF0000">Syntax:&nbsp; </font><b><tt><font color="#009900">eoSelectRandom&lt;Indi>
select;</font></tt></b></ul>
Note that all these classes of eoObjects are derived from the abstract
class
<b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eoselectone.html">eoSelectOne.</a></font></font></b>
<p><b><font color="#000099"><font size=+2>Lessons:</font></font></b>
<ul>
<li>
in EO, all actions are performed by <a href="eoProgramming.html#functors">functor
objects</a> (this section is the last time in this tutorial that there
is a direct link to the <a href="eoProgramming.html">EO Programming hints</a>
page - though the link at top and bottom of all pages will remain there).</li>
<li>
in EO, all object you will usually need to manipulate are <a href="eoProgramming.html#templates">templatized</a><b><font color="#FF6600">
w.r.t. the type of the individual</font></b> you are handling.</li>
<li>
The type of the individual is itself <a href="eoProgramming.html#templates">templatized</a><b><font color="#FF6600">
w.r.t. the type of fitness</font></b> (double by default).</li>
<li>
In EO (actually, in EC!) <font color="#CC33CC">initialization and variation</font>
operators are <font color="#999900">representation</font>-dependent, while
the <font color="#009900">evolution engine</font> is <font color="#999900">representation</font>-independent
(well, like any rule, this one does have some exceptions).</li>
<li>
Changing the <font color="#990000">fitness function</font>, or the <font color="#009900">selection
procedure</font> inside the generational GA evolution engine is straightforward.</li>
<li>
remember, all <font color="#FF6600">solutions</font> to exercises are in
the same sub-dir of dir Tutorial than the lesson itself (see <a href="NoWay.html">here</a>).</li>
</ul>
<hr WIDTH="100%"><a href="eoLesson2.html">Lesson 2</a> -
<a href="eoTutorial.html">Tutorial
main page</a> -
<a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
documentation</a></font></font>
<br>
<hr>
<address>
<a href="mailto:Marc.Schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
<br><!-- Created: Fri Nov 3 18:49:12 CET 2000 --><!-- hhmts start -->Last
modified: Fri Nov 3 18:49:12 CET 2000<!-- hhmts end -->
</body>
</html>

Some files were not shown because too many files have changed in this diff Show more