Migration from SVN

This commit is contained in:
quemy 2012-08-30 11:30:11 +02:00
commit 8cd56f37db
44 changed files with 0 additions and 4096888 deletions

261
eo/tutorial/html/BitEA.html Normal file
View file

@ -0,0 +1,261 @@
<!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>BitEA.cpp</title>
</head>
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<a href="eoLesson4.html">Back to Lesson 4</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">BitEA.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">
<b>#include &lt;iostream></b><br>
<b>#include &lt;ga/make_ga.h></b><br>
<b>#include &lt;apply.h></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>#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">
<b>using namespace std;</b><br>
<b>int main(int argc, char* argv[])</b><br>
<b>{</b><br>
<b> &nbsp;try</b><br>
<b> &nbsp;{</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 genotype and fitness types<br>
<b> &nbsp;typedef eoBit&lt;double> EOT;</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;eoParser parser(argc, argv); &nbsp;</b>// for user-parameter reading<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;eoState state; &nbsp; &nbsp; &nbsp;</b>// keeps all things allocated<br>
<b> &nbsp;</b>///// FIRST, problem or representation dependent stuff<br>
<b> &nbsp;</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>// The evaluation fn - encapsulated into an eval counter for output <br>
<b> &nbsp;eoEvalFuncPtr&lt;EOT, float> mainEval( binary_value&lt;EOT> );</b><br>
<b> &nbsp;eoEvalFuncCounter&lt;EOT> eval(mainEval);</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">
<b> &nbsp;</b>// the genotype - through a genotype initializer<br>
<b> &nbsp;eoInit&lt;EOT>& init = make_genotype(parser, state, EOT());</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>// Build the variation operator (any seq/prop construct)<br>
<b> &nbsp;eoGenOp&lt;EOT>& op = make_op(parser, state, init);</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> &nbsp;</b>//// Now the representation-independent things<br>
<b> &nbsp;</b>//////////////////////////////////////////////<br>
<b> &nbsp;</b>// initialize the population - and evaluate<br>
<b> &nbsp;</b>// yes, this is representation indepedent once you have an eoInit<br>
<b> &nbsp;eoPop&lt;EOT>& pop &nbsp; &nbsp;= make_pop(parser, state, init);</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">
<b> &nbsp;</b>// stopping criteria<br>
<b> &nbsp;eoContinue&lt;EOT> & term = make_continue(parser, state, eval);</b><br>
<b> &nbsp;</b>// output<br>
<b> &nbsp;eoCheckPoint&lt;EOT> & checkpoint = make_checkpoint(parser, state, eval, term);</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>// algorithm (need the operator!)<br>
<b> &nbsp;eoAlgo&lt;EOT>& ga = make_algo_scalar(parser, state, eval, checkpoint, op);</b><br>
<b> &nbsp;</b>///// End of construction of the algorith<br>
<b> &nbsp;</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>// to be called AFTER all parameters have been read!!!<br>
<b> &nbsp;make_help(parser);</b><br>
<b> &nbsp;</b>//// GO<br>
<b> &nbsp;</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>// evaluate intial population AFTER help and status in case it takes time<br>
<b> &nbsp;apply(eval, pop);</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">
<b> &nbsp;</b>// print it out (sort witout modifying) <br>
<b> &nbsp;cout &lt;&lt; "Initial Population\n";</b><br>
<b> &nbsp;pop.sortedPrintOn(cout);</b><br>
<b> &nbsp;cout &lt;&lt; endl;</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;run_ea(ga, pop); </b>// run the ga<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 it out (sort witout modifying) <br>
<b> &nbsp;cout &lt;&lt; "Final Population\n";</b><br>
<b> &nbsp;pop.sortedPrintOn(cout);</b><br>
<b> &nbsp;cout &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> &nbsp;}</b><br>
<b> &nbsp;catch(exception& e)</b><br>
<b> &nbsp;{</b><br>
<b> &nbsp; &nbsp; &nbsp;cout &lt;&lt; e.what() &lt;&lt; endl;</b><br>
<b> &nbsp;}</b><br>
<b>}</b><br>
</font></font></font></td>
</tr>
</table>
<hr WIDTH="100%"><a href="eoLesson4.html">Back to Lesson 4</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@inria.fr">Marc Schoenauer</a></address>
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last
modified: Wed Mar 6 05:40:27 2002
<!-- hhmts end -->
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 KiB

View file

@ -0,0 +1,369 @@
<!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">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based
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">
<b>#include &lt;ga.h></b><br>
//-----------------------------------------------------------------------------<br>
// define your individuals<br>
<b>typedef eoBit&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 eoUniformGenerator class (see utils/eoRndGenerators.h)<br>
<b> &nbsp;eoUniformGenerator&lt;bool> uGen;</b><br>
<b> &nbsp;eoInitFixedLength&lt;Indi> random(VEC_SIZE, uGen);</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 line<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;eoDetTournamentSelect&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;eo1PtBitXover&lt;Indi> xover1;</b><br>
<b> &nbsp;</b>// uniform crossover for bitstring<br>
<b> &nbsp;eoUBitXover&lt;Indi> xoverU;</b><br>
<b> &nbsp;</b>// 2-pots xover<br>
<b> &nbsp;eoNPtsBitXover&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;eoBitMutation&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">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based
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@inria.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,312 @@
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Mozilla/4.76 [en] (X11; U; Linux 2.2.17-21mdksmp i686) [Netscape]">
<title>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">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based
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">Code for FirstBitGA</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><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">// FirstBitGA.cpp</font></tt>
<br><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
<br><tt><font color="#993300">//*</font></tt>
<br><tt><font color="#993300">// An instance of a VERY simple Bitstring
Genetic Algorithm</font></tt>
<br><tt><font color="#993300">//</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;&nbsp;
</b>// cout</font></tt>
<br><tt><font color="#993300"><b>#include &lt;strstream>&nbsp; </b>// ostrstream,
istrstream</font></tt>
<br><tt><font color="#993300">// the general include for eo</font></tt>
<br><b><tt><font color="#993300">#include &lt;eo></font></tt></b></td>
</tr>
</table>
<a NAME="representation"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
<tr>
<td><b><tt><font color="#999900">#include &lt;ga.h></font></tt></b><br>
<tt><font color="#999900">//-----------------------------------------------------------------------------</font></tt>
<br><tt><font color="#999900">// define your individuals</font></tt>
<br><tt><font color="#999900"><b>typedef eoBit&lt;double> Indi;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</b>// A bitstring with fitness double</font></tt></td>
</tr>
</table>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
<tr>
<td><tt><font color="#CC0000">//-----------------------------------------------------------------------------</font></tt>
<br><tt><font color="#CC0000">// a simple fitness function that computes
the number of ones of a bitstring</font></tt>
<br><tt><font color="#CC0000">//&nbsp; @param _indi A biststring individual</font></tt>
<br><a NAME="evalfunc"></a><b><tt><font color="#CC0000">double binary_value(const
Indi &amp; _indi)</font></tt></b>
<br><b><tt><font color="#CC0000">{</font></tt></b>
<br><b><tt><font color="#CC0000">&nbsp;double sum = 0;</font></tt></b>
<br><b><tt><font color="#CC0000">&nbsp;for (unsigned i = 0; i &lt; _indi.size();
i++)</font></tt></b>
<br><b><tt><font color="#CC0000">&nbsp;&nbsp;&nbsp;&nbsp; sum += _indi[i];</font></tt></b>
<br><b><tt><font color="#CC0000">&nbsp;return sum;</font></tt></b>
<br><b><tt><font color="#CC0000">}</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">//-----------------------------------------------------------------------------</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><tt><font color="#3366FF"><b>&nbsp;</b>// all parameters are hard-coded!</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const unsigned int SEED = 42;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</b>// seed for random number generator</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const unsigned int T_SIZE = 3;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</b>// size for tournament selection</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const unsigned int VEC_SIZE = 8;&nbsp;&nbsp;&nbsp;
</b>// Number of bits in genotypes</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const unsigned int POP_SIZE = 20;&nbsp;
</b>// Size of population</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const unsigned int MAX_GEN = 100;&nbsp;
</b>// Maximum number of generation before STOP</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const float CROSS_RATE = 0.8;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</b>// Crossover rate</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const double P_MUT_PER_BIT = 0.01;
</b>// probability of bit-flip mutation</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const float MUT_RATE = 1.0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</b>// mutation rate</font></tt></td>
</tr>
</table>
<a NAME="general"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
<tr>
<td><a NAME="random"></a><tt><font color="#993300"><b> </b>//////////////////////////</font></tt>
<br><tt><font color="#993300"><b>&nbsp;</b>//&nbsp; Random seed</font></tt>
<br><tt><font color="#993300"><b>&nbsp;</b>//////////////////////////</font></tt>
<br><tt><font color="#993300"><b>&nbsp;</b>//reproducible random seed:
if you don't change SEED above,&nbsp;</font></tt>
<br><tt><font color="#993300"><b>&nbsp;</b>// you'll aways get the same
result, NOT a random run</font></tt>
<br><b><tt><font color="#993300">&nbsp;rng.reseed(SEED);</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> eval(&nbsp;
binary_value );</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>// declare the population</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoPop&lt;Indi> pop;</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;</b>// fill it!</font></tt>
<br><b><tt><font color="#993399">&nbsp;for (unsigned int igeno=0; igeno&lt;POP_SIZE;
igeno++)</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp; {</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Indi v;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</b>// void individual, to be filled</font></tt>
<br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
for (unsigned ivar=0; ivar&lt;VEC_SIZE; ivar++)</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
bool r = rng.flip(); </b>// new value, random in {0,1}</font></tt>
<br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
v.push_back(r);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b>//
append that random value to v</font></tt>
<br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
eval(v);&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;
</b>// evaluate it</font></tt>
<br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
pop.push_back(v);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</font></tt></b><tt><font color="#993399">// and put it in the population</font></tt>
<br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp; }</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>// sort pop before printing
it!</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;</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;cout &lt;&lt; pop;</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;eoDetTournamentSelect&lt;Indi> select(T_SIZE);&nbsp;
</b>// T_SIZE in [2,POP_SIZE]</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</font></tt>
<br><tt><font color="#009900"><b>&nbsp;</b>// so no replacement procedure
is needed</font></tt></td>
</tr>
</table>
<a NAME="stop"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td></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;eo1PtBitXover&lt;Indi> xover;</font></tt></b></td>
</tr>
</table>
<a NAME="mutation"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
<tr>
<td>
<br><tt><font color="#993399"><b>&nbsp;</b>// standard bit-flip mutation
for bitstring</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoBitMutation&lt;Indi>&nbsp; mutation(P_MUT_PER_BIT);</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</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;</b>/////////////////////////////////////</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;</b>// stop after MAX_GEN generations</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;eoGenContinue&lt;Indi> continuator(MAX_GEN);</font></tt></b>
<br>&nbsp;</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>// standard Generational GA
requires as parameters</font></tt>
<br><tt><font color="#FF6666"><b>&nbsp;</b>// selection, evaluation, crossover
and mutation, stopping criterion</font></tt>
<p><b><tt><font color="#FF6666">&nbsp;eoSGA&lt;Indi> gga(select, xover,
CROSS_RATE, mutation, MUT_RATE,&nbsp;</font></tt></b>
<br><b><tt><font color="#FF6666">&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;
eval, continuator);</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>
<br>&nbsp;</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</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="main"></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><tt><font color="#993300"><b>&nbsp;</b>//&nbsp; rng.reseed(42);</font></tt>
<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="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
main page </a>- <a href="eoTopDown.html">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based
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@inria.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,354 @@
<!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>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">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based
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><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">// FirstRealEA.cpp</font></tt>
<br><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
<br><tt><font color="#993300">//*</font></tt>
<br><tt><font color="#993300">// Still an instance of a VERY simple Real-coded&nbsp;
Genetic Algorithm&nbsp;</font></tt>
<br><tt><font color="#993300">// (see FirstBitGA.cpp) but now with&nbsp;
Breeder - and Combined Ops</font></tt>
<br><tt><font color="#993300">//</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;&nbsp;
</b>// cout</font></tt>
<br><tt><font color="#993300"><b>#include &lt;strstream>&nbsp; </b>// ostrstream,
istrstream</font></tt>
<br><tt><font color="#993300">// the general include for eo</font></tt>
<br><b><tt><font color="#993300">#include &lt;eo></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"><b>#include &lt;es.h></b></font></tt><br>
<tt><font color="#999900">//-----------------------------------------------------------------------------</font></tt>
<br><tt><font color="#999900">// define your individuals</font></tt>
<br><b><tt><font color="#999900">typedef eoReal&lt;double> Indi;&nbsp;</font></tt></b></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">//-----------------------------------------------------------------------------</font></tt>
<br><tt><font color="#CC0000">// a simple fitness function that computes
the euclidian norm of a real vector</font></tt>
<br><tt><font color="#CC0000">// Now in a separate file, and declared as
binary_value(const vector&lt;bool> &amp;)</font></tt>
<br><b><tt><font color="#CC0000">#include "real_value.h"</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">//-----------------------------------------------------------------------------</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><tt><font color="#3366FF"><b>&nbsp;const unsigned int SEED = 42; </b>//
seed for random number generator</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const unsigned int T_SIZE = 3; </b>//
size for tournament selection</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const unsigned int VEC_SIZE = 8;
</b>// Number of object variables in genotypes</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const unsigned int POP_SIZE = 20;
</b>// Size of population</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const unsigned int MAX_GEN = 500;
</b>// Maximum number of generation before STOP</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const unsigned int MIN_GEN = 10;&nbsp;
</b>// Minimum number of generation before ...</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const unsigned int STEADY_GEN =
50; </b>// stop after STEADY_GEN gen. without improvelent</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const float P_CROSS = 0.8; </b>//
Crossover probability</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const float P_MUT = 0.5; </b>//
mutation probability</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const double EPSILON = 0.01; </b>//
range for real uniform mutation</font></tt>
<br><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>double
SIGMA = 0.3;&nbsp;</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // std dev.
for normal mutation</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;</b>// some parameters for chosing
among different operators</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const double hypercubeRate = 0.5;&nbsp;&nbsp;&nbsp;
</b>// relative weight for hypercube Xover</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const double segmentRate = 0.5;
</b>// relative weight for segment Xover</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const double uniformMutRate = 0.5;
</b>// relative weight for uniform mutation</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;const double detMutRate = 0.5;&nbsp;&nbsp;&nbsp;&nbsp;
</b>// relative weight for det-uniform mutation</font></tt>
<br><tt><font color="#3366FF">&nbsp;<b>const double normalMutRate = 0.5;</b>&nbsp;
// relative weight for normal mutation</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>//////////////////////////</font></tt>
<br><tt><font color="#993300"><b>&nbsp;</b>//&nbsp; Random seed</font></tt>
<br><tt><font color="#993300"><b>&nbsp;</b>//////////////////////////</font></tt>
<br><tt><font color="#993300"><b>&nbsp;</b>//reproducible random seed:
if you don't change SEED above,&nbsp;</font></tt>
<br><tt><font color="#993300"><b>&nbsp;</b>// you'll aways get the same
result, NOT a random run</font></tt>
<br><b><tt><font color="#993300">&nbsp;rng.reseed(SEED);</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><tt><font color="#CC0000"><b>&nbsp;</b>// you need to give the full
description of the function</font></tt>
<br><b><tt><font color="#CC0000">&nbsp;eoEvalFuncPtr&lt;Indi, double, const
vector&lt;double>&amp; > eval(&nbsp; real_value );</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>// based on a uniform generator</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoInitFixedLength&lt;Indi, uniform_generator&lt;double>
></font></tt></b>
<br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
random(VEC_SIZE, uniform_generator&lt;double>(-1.0, 1.0));</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;&nbsp; </b>// Initialization of
the population</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoPop&lt;Indi> pop(POP_SIZE, random);</font></tt></b>
<p><tt><font color="#993399"><b>&nbsp;</b>// and evaluate it in one loop</font></tt>
<br><tt><font color="#993399"><b>&nbsp;apply&lt;Indi>(eval, pop); </b>//
STL syntax</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!</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;</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;cout &lt;&lt; pop;</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><b><tt><font color="#009900">&nbsp;eoDetTournamentSelect&lt;Indi> selectOne(T_SIZE);</font></tt></b>
<br><a NAME="select_encapsulate"></a><tt><font color="#009900"><b> </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>// uniform chooce on segment
made by the parents</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoSegmentCrossover&lt;Indi> xoverS;</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;</b>// uniform choice in hypercube
built by the parents</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoHypercubeCrossover&lt;Indi> xoverA;</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;</b>// Combine them with relative
weights</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoPropCombinedQuadOp&lt;Indi> xover(xoverS,
segmentRate);</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;xover.add(xoverA, hypercubeRate,
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>
<br><tt><font color="#993399"><b>&nbsp;</b>// offspring(i) uniformly chosen
in [parent(i)-epsilon, parent(i)+epsilon]</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoUniformMutation&lt;Indi>&nbsp;
mutationU(EPSILON);&nbsp;</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;</b>// k (=1) coordinates of parents
are uniformly modified</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoDetUniformMutation&lt;Indi>&nbsp;
mutationD(EPSILON);&nbsp;</font></tt></b>
<br><tt><font color="#993399">&nbsp;// all coordinates of parents are normally
modified (stDev SIGMA)</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoNormalMutation&lt;Indi>&nbsp;
mutationN(SIGMA);&nbsp;</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;</b>// Combine them with relative
weights</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoPropCombinedMonOp&lt;Indi> mutation(mutationU,
uniformMutRate);</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;mutation.add(mutationD, detMutRate);</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;mutation.add(mutationN, normalMutRate,
true);</font></tt></b>
<p><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,
P_CROSS, mutation, P_MUT);</font></tt></b></td>
</tr>
</table>
<a NAME="stop"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
<tr>
<td></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>//////////////////////////////////////</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;</b>// termination conditions: use
more than one</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;</b>/////////////////////////////////////</font></tt>
<br><tt><font color="#3366FF"><b>&nbsp;</b>// stop after MAX_GEN generations</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;eoGenContinue&lt;Indi> genCont(MAX_GEN);</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;</b>// do MIN_GEN gen., then stop
after STEADY_GEN gen. without improvement</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;eoSteadyFitContinue&lt;Indi> steadyCont(MIN_GEN,
STEADY_GEN);</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;</b>// stop when fitness reaches
a target (here VEC_SIZE)</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;eoFitContinue&lt;Indi> fitCont(0);</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;</b>// do stop when one of the above
says so</font></tt>
<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="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(continuator,
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;cout &lt;&lt; "\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Here we go\n\n";</font></tt></b>
<br><b><tt><font color="#FF6666">&nbsp;gga(pop);</font></tt></b>
<br>&nbsp;</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</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><tt><font color="#993300"><b>&nbsp;</b>//&nbsp; rng.reseed(42);</font></tt>
<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="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
main page </a>- <a href="eoTopDown.html">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based
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@inria.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,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>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">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based
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">Code for FirstRealGA</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">
<b>#include &lt;es.h></b><br>
//-----------------------------------------------------------------------------<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;eoDetTournamentSelect&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;eoSegmentCrossover&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">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based
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@inria.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,286 @@
<!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">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based
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">#include &lt;es.h><br>
typedef eoReal&lt;double> Indi;</font></font></td>
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#FFFF00">#include &lt;ga.h><br>
typedef eoBit&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">eoSegmentCrossover&lt;Indi>
xover;</font></font></td>
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC33CC">eoBitMutation&lt;Indi>&nbsp;
mutation(P_MUT_PER_BIT);</font></font>
<br><font face="Courier New,Courier"><font color="#CC33CC">eo1PtBitXover&lt;Indi>
xover;</font></font></td>
</tr>
<tr>
<td BGCOLOR="#C3C2B4">
<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">
<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>
</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">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based
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.Schoenauer@inria.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,77 @@
<!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>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">Algorithm-Based</a>
- <a href="eoBottomUp.html">Component-Based</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%"><!-- -------------- 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!) and are named <b><tt><font color="#990000">exerciseN.p.cpp</font></tt></b>
where N is the lesson number and p the exercise number. 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.p</font></tt></b>
<br>which will compile file <b><tt><font color="#660000">exerciseN.p.cpp</font></tt></b>
into executable <b><tt><font color="#660000">exerciseN.p</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.p.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">Algorithm-Based</a>
- <a href="eoBottomUp.html">Component-Based</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>
<address>
<a href="mailto:Marc.Schoenauer@inria.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,360 @@
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Mozilla/4.78 [en] (X11; U; Linux 2.4.7-10 i686) [Netscape]">
<title>Templates/OneMaxEA.cpp</title>
</head>
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<a href="eoLesson5.html">Back to Lesson 5</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">Templates/OneMaxEA.cpp</font></h1></center>
The places where you have to put some code are on <b><font color="#FF6666">pink
background</font></b>.. Only the the <a href="eoTutorial.html#colors">character
colors have the usual meaning</a>.
<br><a NAME="start"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr NOSAVE>
<td NOSAVE><tt><font color="#993300">/** -*- mode: c++; c-indent-level:
4; c++-member-init-indent: 8; comment-column: 35; -*-</font></tt>
<br><tt><font color="#993300">The above line is usefulin Emacs-like editors</font></tt>
<br><tt><font color="#993300">*/</font></tt>
<br><tt><font color="#993300">/*</font></tt>
<br><tt><font color="#993300">Template for creating a new representation
in EO</font></tt>
<br><tt><font color="#993300">================================================</font></tt>
<br><tt><font color="#993300">This is the template main file.</font></tt>
<br><tt><font color="#993300">It includes all other files that have been
generated by the script create.sh</font></tt>
<br><tt><font color="#993300">so it is the only file to compile.</font></tt>
<br><tt><font color="#993300">In case you want to build up a separate library
for your new Evolving Object,</font></tt>
<br><tt><font color="#993300">you'll need some work - follow what's done
in the src/ga dir, used in the</font></tt>
<br><tt><font color="#993300">main file BitEA in tutorial/Lesson4 dir.</font></tt>
<br><tt><font color="#993300">Or you can wait until we do it :-)</font></tt>
<br><tt><font color="#993300">*/</font></tt>
<br><tt><font color="#993300">// Miscilaneous include and declaration&nbsp;</font></tt>
<br><b><tt><font color="#993300">#include &lt;iostream></font></tt></b>
<br><b><tt><font color="#993300">using namespace std;</font></tt></b>
<br><tt><font color="#993300">// eo general include</font></tt>
<br><b><tt><font color="#993300">#include "eo"</font></tt></b>
<br><tt><font color="#993300">// the real bounds (not yet in general eo
include)</font></tt>
<br><b><tt><font color="#993300">#include "utils/eoRealVectorBounds.h"</font></tt></b>
<br><tt><font color="#993300">// include here whatever specific files for
your representation</font></tt>
<br><tt><font color="#993300">// Basically, this should include at least
the following</font></tt>
<br><tt><font color="#993300">/** definition of representation:&nbsp;</font></tt>
<br><tt><font color="#993300">* class eoOneMax MUST derive from EO&lt;FitT>
for some fitness</font></tt>
<br><tt><font color="#993300">*/</font></tt></td>
</tr>
</table>
<a NAME="representation"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><b><tt><font color="#999900">#include "eoOneMax.h"</font></tt></b>
<br><tt><font color="#999900">/** definition of initilizqtion:&nbsp;</font></tt>
<br><tt><font color="#999900">* class eoOneMaxInit MUST derive from eoInit&lt;eoOneMax></font></tt>
<br><tt><font color="#999900">*/</font></tt></td>
</tr>
</table>
<a NAME="init"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><b><tt><font color="#993399">#include "eoOneMaxInit.h"</font></tt></b>
<br><tt><font color="#993399">/** definition of evaluation:&nbsp;</font></tt>
<br><tt><font color="#993399">* class eoOneMaxEvalFunc MUST derive from
eoEvalFunc&lt;eoOneMax></font></tt>
<br><tt><font color="#993399">* and should test for validity before doing
any computation</font></tt>
<br><tt><font color="#993399">* see tutorial/Templates/evalFunc.tmpl</font></tt>
<br><tt><font color="#993399">*/</font></tt></td>
</tr>
</table>
<a NAME="fitness"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><b><tt><font color="#CC0000">#include "eoOneMaxEvalFunc.h"</font></tt></b>
<br><tt><font color="#CC0000">// GENOTYPE&nbsp;&nbsp;&nbsp; eoOneMax ***MUST***
be templatized over the fitness</font></tt></td>
</tr>
</table>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
<tr>
<td><tt><font color="#CC0000">// START fitness type: double or eoMaximizingFitness
if you are maximizing</font></tt>
<br><tt><font color="#CC0000">//&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;
eoMinimizingFitness if you are minimizing</font></tt>
<br><tt><font color="#CC0000"><b>typedef eoMinimizingFitness MyFitT ; </b>//
type of fitness&nbsp;</font></tt>
<br><tt><font color="#CC0000">// END fitness type</font></tt></td>
</tr>
</table>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td></td>
</tr>
</table>
<a NAME="representation"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><tt><font color="#999900">// Then define your EO objects using that
fitness type</font></tt>
<br><tt><font color="#999900"><b>typedef eoOneMax&lt;MyFitT> Indi;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b>
// ***MUST*** derive from EO&nbsp;</font></tt></td>
</tr>
</table>
<a NAME="init"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><tt><font color="#993399">// create an initializer</font></tt>
<br><b><tt><font color="#993399">#include "make_genotype_OneMax.h"</font></tt></b>
<br><b><tt><font color="#993399">eoInit&lt;Indi> &amp; make_genotype(eoParser&amp;
_parser, eoState&amp;_state, Indi _eo)</font></tt></b>
<br><b><tt><font color="#993399">{</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;return do_make_genotype(_parser,
_state, _eo);</font></tt></b>
<br><b><tt><font color="#993399">}&nbsp;</font></tt></b></td>
</tr>
</table>
<a NAME="operators"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><tt><font color="#993399">// and the variation operaotrs</font></tt>
<br><b><tt><font color="#993399">#include "make_op_OneMax.h"</font></tt></b>
<br><b><tt><font color="#993399">eoGenOp&lt;Indi>&amp;&nbsp; make_op(eoParser&amp;
_parser, eoState&amp; _state, eoInit&lt;Indi>&amp; _init)</font></tt></b>
<br><b><tt><font color="#993399">{</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;return do_make_op(_parser, _state,
_init);</font></tt></b>
<br><b><tt><font color="#993399">}</font></tt></b></td>
</tr>
</table>
<a NAME="init"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><tt><font color="#993399">// Use existing modules to define representation
independent routines</font></tt>
<br><tt><font color="#993399">// These are parser-based definitions of
objects</font></tt>
<br><tt><font color="#993399">// how to initialize the population&nbsp;</font></tt>
<br><tt><font color="#993399">// it IS representation independent if an
eoInit is given</font></tt>
<br><b><tt><font color="#993399">#include &lt;do/make_pop.h></font></tt></b>
<br><b><tt><font color="#993399">eoPop&lt;Indi >&amp;&nbsp; make_pop(eoParser&amp;
_parser, eoState&amp; _state, eoInit&lt;Indi> &amp; _init)</font></tt></b>
<br><b><tt><font color="#993399">{</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;return do_make_pop(_parser, _state,
_init);</font></tt></b>
<br><b><tt><font color="#993399">}</font></tt></b></td>
</tr>
</table>
<a NAME="stop"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><tt><font color="#3366FF">// the stopping criterion</font></tt>
<br><b><tt><font color="#3366FF">#include &lt;do/make_continue.h></font></tt></b>
<br><b><tt><font color="#3366FF">eoContinue&lt;Indi>&amp; make_continue(eoParser&amp;
_parser, eoState&amp; _state, eoEvalFuncCounter&lt;Indi> &amp; _eval)</font></tt></b>
<br><b><tt><font color="#3366FF">{</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;return do_make_continue(_parser,
_state, _eval);</font></tt></b>
<br><b><tt><font color="#3366FF">}</font></tt></b></td>
</tr>
</table>
<a NAME="stat"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><tt><font color="#3366FF">// outputs (stats, population dumps, ...)</font></tt>
<br><b><tt><font color="#3366FF">#include &lt;do/make_checkpoint.h></font></tt></b>
<br><b><tt><font color="#3366FF">eoCheckPoint&lt;Indi>&amp; make_checkpoint(eoParser&amp;
_parser, eoState&amp; _state, eoEvalFuncCounter&lt;Indi>&amp; _eval, eoContinue&lt;Indi>&amp;
_continue)&nbsp;</font></tt></b>
<br><b><tt><font color="#3366FF">{</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;return do_make_checkpoint(_parser,
_state, _eval, _continue);</font></tt></b>
<br><b><tt><font color="#3366FF">}</font></tt></b></td>
</tr>
</table>
<a NAME="engine"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><tt><font color="#009900">// evolution engine (selection and replacement)</font></tt>
<br><b><tt><font color="#009900">#include &lt;do/make_algo_scalar.h></font></tt></b>
<br><b><tt><font color="#009900">eoAlgo&lt;Indi>&amp;&nbsp; make_algo_scalar(eoParser&amp;
_parser, eoState&amp; _state, eoEvalFunc&lt;Indi>&amp; _eval, eoContinue&lt;Indi>&amp;
_continue, eoGenOp&lt;Indi>&amp; _op)</font></tt></b>
<br><b><tt><font color="#009900">{</font></tt></b>
<br><b><tt><font color="#009900">&nbsp;return do_make_algo_scalar(_parser,
_state, _eval, _continue, _op);</font></tt></b>
<br><b><tt><font color="#009900">}</font></tt></b></td>
</tr>
</table>
<a NAME="general"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><tt><font color="#993300">// simple call to the algo. stays there for
consistency reasons&nbsp;</font></tt>
<br><tt><font color="#993300">// no template for that one</font></tt>
<br><b><tt><font color="#993300">#include &lt;do/make_run.h></font></tt></b>
<br><tt><font color="#993300">// the instanciating fitnesses</font></tt>
<br><b><tt><font color="#993300">#include &lt;eoScalarFitness.h></font></tt></b>
<br><b><tt><font color="#993300">void run_ea(eoAlgo&lt;Indi>&amp; _ga,
eoPop&lt;Indi>&amp; _pop)</font></tt></b>
<br><b><tt><font color="#993300">{</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;do_run(_ga, _pop);</font></tt></b>
<br><b><tt><font color="#993300">}</font></tt></b>
<br><tt><font color="#993300">// checks for help demand, and writes the
status file</font></tt>
<br><tt><font color="#993300">// and make_help; in libutils</font></tt>
<br><b><tt><font color="#993300">void make_help(eoParser &amp; _parser);</font></tt></b>
<br><tt><font color="#993300">// now use all of the above, + representation
dependent things</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">&nbsp;try</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;{</font></tt></b>
<br><tt><font color="#993300"><b>&nbsp;eoParser parser(argc, argv);&nbsp;&nbsp;</b>
// for user-parameter reading</font></tt>
<br><tt><font color="#993300"><b>&nbsp;eoState state;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b>
// keeps all things allocated</font></tt></td>
</tr>
</table>
<a NAME="eval"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><tt><font color="#CC0000"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b> //
The fitness</font></tt>
<br><tt><font color="#CC0000"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b> //////////////</font></tt>
<br><tt><font color="#CC0000"><b>&nbsp;&nbsp; eoOneMaxEvalFunc&lt;Indi>
plainEval</b> /* (varType&nbsp; _anyVariable) */;<b>;</b></font></tt>
<br><tt><font color="#CC0000"><b>&nbsp;&nbsp;&nbsp;</b> // turn that object
into an evaluation counter</font></tt>
<br><b><tt><font color="#CC0000">&nbsp;&nbsp; eoEvalFuncCounter&lt;Indi>
eval(plainEval);</font></tt></b></td>
</tr>
</table>
<a NAME="representation"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><tt><font color="#999900"><b>&nbsp;</b> // the genotype - through a
genotype initializer</font></tt>
<br><b><tt><font color="#999900">&nbsp;eoInit&lt;Indi>&amp; init = make_genotype(parser,
state, Indi());</font></tt></b></td>
</tr>
</table>
<a NAME="operators"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><tt><font color="#993399"><b>&nbsp;</b> // Build the variation operator
(any seq/prop construct)</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoGenOp&lt;Indi>&amp; op = make_op(parser,
state, init);</font></tt></b>
<br><tt><font color="#993399"><b>&nbsp;</b> //// Now the representation-independent
things&nbsp;</font></tt>
<br><tt><font color="#993399"><b>&nbsp;</b> //</font></tt>
<br><tt><font color="#993399"><b>&nbsp;</b> // YOU SHOULD NOT NEED TO MODIFY
ANYTHING BEYOND THIS POINT</font></tt>
<br><tt><font color="#993399"><b>&nbsp;</b> // unless you want to add specific
statistics to the checkpoint</font></tt>
<br><tt><font color="#993399"><b>&nbsp;</b> //////////////////////////////////////////////</font></tt></td>
</tr>
</table>
<a NAME="init"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><tt><font color="#993399"><b>&nbsp;</b> // initialize the population</font></tt>
<br><tt><font color="#993399"><b>&nbsp;</b> // yes, this is representation
indepedent once you have an eoInit</font></tt>
<br><b><tt><font color="#993399">&nbsp;eoPop&lt;Indi>&amp; pop&nbsp;&nbsp;&nbsp;
= make_pop(parser, state, init);</font></tt></b></td>
</tr>
</table>
<a NAME="stop"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><tt><font color="#3366FF"><b>&nbsp;</b> // stopping criteria</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;eoContinue&lt;Indi> &amp; term =
make_continue(parser, state, eval);</font></tt></b></td>
</tr>
</table>
<a NAME="output"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><tt><font color="#3366FF"><b>&nbsp;</b> // output</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;eoCheckPoint&lt;Indi> &amp; checkpoint
= make_checkpoint(parser, state, eval, term);</font></tt></b></td>
</tr>
</table>
<a NAME="general"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><tt><font color="#993300"><b>&nbsp;</b> // algorithm (need the operator!)</font></tt>
<br><b><tt><font color="#993300">&nbsp;eoAlgo&lt;Indi>&amp; ga = make_algo_scalar(parser,
state, eval, checkpoint, op);</font></tt></b>
<br><tt><font color="#993300"><b>&nbsp;</b> ///// End of construction of
the algorithm</font></tt>
<br><tt><font color="#993300"><b>&nbsp;</b> /////////////////////////////////////////</font></tt>
<br><tt><font color="#993300"><b>&nbsp;</b> // to be called AFTER all parameters
have been read!!!</font></tt>
<br><b><tt><font color="#993300">&nbsp;make_help(parser);</font></tt></b>
<br><tt><font color="#993300"><b>&nbsp;</b> //// GO</font></tt>
<br><tt><font color="#993300"><b>&nbsp;</b> ///////</font></tt></td>
</tr>
</table>
<a NAME="eval"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><tt><font color="#CC0000"><b>&nbsp;</b> // evaluate intial population
AFTER help and status in case it takes time</font></tt>
<br><b><tt><font color="#CC0000">&nbsp;apply&lt;Indi>(eval, pop);</font></tt></b>
<br><tt><font color="#CC0000"><b>&nbsp;</b> // if you want to print it
out</font></tt>
<br><tt><font color="#CC0000">//&nbsp;&nbsp;&nbsp; cout &lt;&lt; "Initial
Population\n";</font></tt>
<br><tt><font color="#CC0000">//&nbsp;&nbsp;&nbsp; pop.sortedPrintOn(cout);</font></tt>
<br><tt><font color="#CC0000">//&nbsp;&nbsp;&nbsp; cout &lt;&lt; endl;</font></tt></td>
</tr>
</table>
<a NAME="general"></a>
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
<tr>
<td><tt><font color="#993300"><b>&nbsp;run_ea(ga, pop); </b>// run the
ga</font></tt>
<br><b><tt><font color="#993300">&nbsp;cout &lt;&lt; "Final Population\n";</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;pop.sortedPrintOn(cout);</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;cout &lt;&lt; endl;</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;}</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;catch(exception&amp; e)</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;{</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt;
e.what() &lt;&lt; endl;</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;}</font></tt></b>
<br><b><tt><font color="#993300">&nbsp;return 0;</font></tt></b>
<br><b><tt><font color="#993300">}</font></tt></b></td>
</tr>
</table>
<hr WIDTH="100%"><a href="eoLesson5.html">Back to Lesson 5</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@inria.fr">Marc Schoenauer</a></address>
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last modified: Sat May
4 07:37:41 2002<!-- hhmts end -->
</body>
</html>

View file

@ -0,0 +1,569 @@
<!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">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based
</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></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">// a simple fitness function that computes
the number of ones of a bitstring</font></tt>
<br><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 eoBit&lt;double> Indi;</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">// the main_function: nothing changed(!),
except variable initialization</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><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;
# = 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>
<p><tt><font color="#3366FF">// First define a parser from the command-line
arguments</font></tt>
<br><a NAME="parser_declare"></a><b><tt><font color="#3366FF">eoParser
parser(argc, argv);</font></tt></b>
<p><tt><font color="#3366FF">// For each parameter, define Parameter, read
it through the parser,</font></tt>
<br><tt><font color="#3366FF">// and assign the value to the variable</font></tt>
<br><a NAME="random"></a>
<br><b><tt><font color="#990000">eoValueParam&lt;uint32> seedParam(time(0),
"seed", "Random number seed", 'S');</font></tt></b>
<br><a NAME="seed_process"></a><b><tt><font color="#990000">parser.processParam(
seedParam );</font></tt></b>
<br><a NAME="seed_assign"></a><b><tt><font color="#990000">unsigned seed
= seedParam.value();</font></tt></b>
<p><a NAME="_seed_declare"></a><tt><font color="#999900">// decription
of genotype</font></tt>
<br><b><tt><font color="#999900">eoValueParam&lt;unsigned int>&amp; vecSizeParam(8,
"vecSize", "Genotype size",'V');</font></tt></b>
<br><b><tt><font color="#999900">parser.processParam( vecSizeParam, "Representation"
);</font></tt></b>
<br><b><tt><font color="#999900">unsigned vecSize = vecSizeParam.value();</font></tt></b>
<p><tt><font color="#009900">// parameters for evolution engine</font></tt>
<br><b><tt><font color="#009900">eoValueParam&lt;unsigned int>&amp; popSizeParam(10,
"popSize", "Population size",'P');</font></tt></b>
<br><b><tt><font color="#009900">parser.processParam( popSizeParam, "Evolution
engine" );</font></tt></b>
<br><b><tt><font color="#009900">unsigned popSize = popSizeParam.value();</font></tt></b>
<p><b><tt><font color="#009900">eoValueParam&lt;unsigned int>&amp; tSizeParam(10,
"tSize", "Tournament size",'T');</font></tt></b>
<br><b><tt><font color="#009900">parser.processParam( seedParam );</font></tt></b>
<br><b><tt><font color="#009900">unsigned tSize = tSizeParam.value();</font></tt></b>
<br><a NAME="_load_name"></a>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp; </b>// init and stop</font></tt>
<br><b><tt><font color="#3366FF">eoValueParam&lt;string> loadNameParam("",
"Load","A save file to restart from",'L');</font></tt></b>
<br><b><tt><font color="#3366FF">parser.processParam( loadNameParam, "Persistence"
);</font></tt></b>
<br><b><tt><font color="#3366FF">string loadName = loadNameParam.value();</font></tt></b>
<p><b><tt><font color="#3366FF">eoValueParam&lt;unsigned int> maxGenParam(100,
"maxGen", "Maximum number of generations",'G');</font></tt></b>
<br><b><tt><font color="#3366FF">parser.processParam( maxGenParam, "Stopping
criterion" );</font></tt></b>
<br><b><tt><font color="#3366FF">unsigned maxGen = maxGenParam.value();</font></tt></b>
<p><b><tt><font color="#3366FF">eoValueParam&lt;unsigned int> minGenParam(100,
"minGen", "Minimum number of generations",'g');</font></tt></b>
<br><b><tt><font color="#3366FF">parser.processParam( minGenParam, "Stopping
criterion" );</font></tt></b>
<br><b><tt><font color="#3366FF">unsigned minGen = minGenParam.value();</font></tt></b>
<p><b><tt><font color="#3366FF">eoValueParam&lt;unsigned int> steadyGenParam(100,
"steadyGen", "Number of generations with no improvement",'s');</font></tt></b>
<br><b><tt><font color="#3366FF">parser.processParam( steadyGenParam, "Stopping
criterion" );</font></tt></b>
<br><b><tt><font color="#3366FF">unsigned steadyGen = steadyGenParam.value();</font></tt></b>
<p><tt><font color="#CC33CC">// operators probabilities at the algorithm
level</font></tt>
<br><b><tt><font color="#CC33CC">eoValueParam&lt;double> pCrossParam(0.6,
"pCross", "Probability of Crossover", 'C');&nbsp;</font></tt></b>
<br><b><tt><font color="#CC33CC">parser.processParam( pCrossParam, "Genetic
Operators" );</font></tt></b>
<br><b><tt><font color="#CC33CC">double pCross = pCrossParam.value();</font></tt></b>
<p><b><tt><font color="#CC33CC">eoValueParam&lt;double> pMutParam(0.1,
"pMut", "Probability of Mutation", 'M');</font></tt></b>
<br><b><tt><font color="#CC33CC">parser.processParam( pMutParam, "Genetic
Operators" );</font></tt></b>
<br><b><tt><font color="#CC33CC">double pMut = pMutParam.value();</font></tt></b>
<p><tt><font color="#CC33CC">// relative rates for crossovers</font></tt>
<br><b><tt><font color="#CC33CC">eoValueParam&lt;double> onePointRateParam(1,
"onePointRate", "Relative rate for one point crossover", '1');</font></tt></b>
<br><b><tt><font color="#CC33CC">parser.processParam( onePointRateParam,
"Genetic Operators" );</font></tt></b>
<br><b><tt><font color="#CC33CC">double onePointRate = onePointRateParam.value();</font></tt></b>
<p><b><tt><font color="#CC33CC">eoValueParam&lt;double> twoPointsRateParam(1,
"twoPointRate", "Relative rate for two point crossover", '2');</font></tt></b>
<br><b><tt><font color="#CC33CC">parser.processParam( twoPointsRateParam,
"Genetic Operators" );</font></tt></b>
<br><b><tt><font color="#CC33CC">double twoPointsRate = twoPointsRateParam.value();</font></tt></b>
<p><b><tt><font color="#CC33CC">eoValueParam&lt;double> uRateParam(2, "uRate",
"Relative rate for uniform crossover", 'U');</font></tt></b>
<br><b><tt><font color="#CC33CC">parser.processParam( uRateParam, "Genetic
Operators" );</font></tt></b>
<br><b><tt><font color="#CC33CC">double URate =&nbsp; uRateParam.value();</font></tt></b>
<p><tt><font color="#CC33CC">// relative rates and private parameters for
mutations;</font></tt>
<br><b><tt><font color="#CC33CC">eoValueParam&lt;double> pMutPerBitParam(0.01,
"pMutPerBit", "Probability of flipping 1 bit in bit-flip mutation", 'b');</font></tt></b>
<br><b><tt><font color="#CC33CC">parser.processParam( pMutPerBitParam,
"Genetic Operators" );</font></tt></b>
<br><b><tt><font color="#CC33CC">double pMutPerBit = pMutPerBitParam.value();</font></tt></b>
<p><b><tt><font color="#CC33CC">eoValueParam&lt;double> bitFlipRateParam(0.01,
"bitFlipRate", "Relative rate for bit-flip mutation", 'B');</font></tt></b>
<br><b><tt><font color="#CC33CC">parser.processParam( bitFlipRateParam,
"Genetic Operators" );</font></tt></b>
<br><b><tt><font color="#CC33CC">double bitFlipRate =&nbsp; bitFlipRateParam.value();</font></tt></b>
<p><b><tt><font color="#CC33CC">eoValueParam&lt;double> oneBitRateParam(0.01,
"oneBitRate", "Relative rate for deterministic bit-flip mutation", 'D');</font></tt></b>
<br><b><tt><font color="#CC33CC">parser.processParam( oneBitRateParam,
"Genetic Operators" );</font></tt></b>
<br><b><tt><font color="#CC33CC">double oneBitRate = oneBitRateParam.value();</font></tt></b>
<p><tt><font color="#3366FF">// the name of the "status" file where all
actual parameter values will be saved</font></tt>
<br><b><tt><font color="#3366FF">string str_status = parser.ProgramName()
+ ".status"; // default value</font></tt></b>
<br><b><tt><font color="#3366FF">eoValueParam&lt;string> statusParam(str_status.c_str(),
"status","Status file",'S');</font></tt></b>
<br><b><tt><font color="#3366FF">parser.processParam( statusParam, "Persistence"
);</font></tt></b>
<p><tt><font color="#3366FF">// do the following AFTER ALL PARAMETERS HAVE
BEEN PROCESSED</font></tt>
<br><tt><font color="#3366FF">// i.e. in case you need parameters somewhere
else, postpone these</font></tt>
<br><b><tt><font color="#3366FF">if (parser.userNeedsHelp())</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp; {</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; parser.printHelp(cout);</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp; }</font></tt></b>
<br><b><tt><font color="#3366FF">if (statusParam.value() != "")</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp; {</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ofstream
os(statusParam.value().c_str());</font></tt></b>
<br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; os &lt;&lt;
parser; </b>// and you can use that file as parameter file</font></tt>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp; }</font></tt></b></td>
</tr>
<tr>
<td></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; </b>//
the fitness is read in the file:&nbsp;</font></tt>
<br><tt><font color="#993399"><b>&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; </b>//
a Indi random initializer</font></tt>
<br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b>//
based on eoUniformGenerator class (see utils/eoRndGenerators.h)</font></tt>
<br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eoUniformGenerator&lt;bool>
uGen;</font></tt></b>
<br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eoInitFixedLength&lt;Indi>
random(vecSize, uGen);</font></tt></b>
<br><tt><font color="#993399"><b>&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; </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 initialization 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;eoDetTournamentSelect&lt;Indi> selectOne(tSize);&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><b><tt><font color="#009900">&nbsp;eoSelectPerc&lt;Indi> select(selectOne);</font></tt></b>
<br><tt><font color="#009900">&nbsp;// or eoSelectPerc&lt;Indi> select(selectOne,
rate);&nbsp;</font></tt>
<br><tt><font color="#009900">&nbsp;// but 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>// the same generational replacement
at the moment :-)</font></tt>
<br><b><tt><font color="#009900">&nbsp;eoGenerationalReplacement&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;eo1PtBitXover&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;eoUBitXover&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;eoNPtsBitXover&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;eoBitMutation&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; </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; </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; </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; </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; </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</font></tt>
<br><a NAME="outstate_register"></a><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;
outState.registerObject(parser);</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; outState.registerObject(pop);</font></tt></b>
<br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp; outState.registerObject(rng);</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">// 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; </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">&nbsp;// stopping criterion, eval, selection,
transformation, replacement</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">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based
</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@inria.fr">Marc Schoenauer</a></address>
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last modified: Sun Nov
26 09:31:04 2000<!-- hhmts end -->
</body>
</html>

View file

@ -0,0 +1,468 @@
<!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>SecondRealEA</title>
</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">SecondRealEA</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>
// SecondRealEA.cpp<br>
//-----------------------------------------------------------------------------<br>
//*<br>
// Same code than FirstBitEA as far as Evolutionary Computation is concerned<br>
// but now you learn to enter the parameters in a more flexible way<br>
// (also slightly different than in SecondBitEA.cpp)<br>
// and to twidle the output to your preferences (as in SecondBitEA.cpp)<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>
<b>#include &lt;es.h></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;eoMinimizingFitness> 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">
//-----------------------------------------------------------------------------<br>
// instead of having all values of useful parameters as constants, read them:<br>
// either on the command line (--option=value or -o=value)<br>
// &nbsp; &nbsp; &nbsp; &nbsp;or in a parameter file (same syntax, order independent, <br>
// &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 <br>
// &nbsp; &nbsp; &nbsp; &nbsp;or in the environment (TODO)<br>
<b> &nbsp;</b>// First define a parser from the command-line arguments<br>
<b> &nbsp;eoParser parser(argc, argv);</b><br>
<b> &nbsp;</b><br>
<b> &nbsp;</b>// For each parameter, you can in on single line<br>
<b> &nbsp;</b>// define the parameter, read it through the parser, and assign it<br>
<b> &nbsp;</b><br>
<a NAME="random"></a>
<b> &nbsp;unsigned seed = parser.createParam(unsigned(time(0)), "seed", "Random number seed", 'S').value(); </b>// will be in default section General<br>
<b> &nbsp;</b><br>
<b> &nbsp;</b>// description of genotype<br>
<b> &nbsp;unsigned vecSize = parser.createParam(unsigned(8), "vecSize", "Genotype size",'V', "Representation" ).value();</b><br>
<b> &nbsp; &nbsp;</b>// parameters for evolution engine<br>
<b> &nbsp; &nbsp; &nbsp;unsigned popSize = parser.createParam(unsigned(10), "popSize", "Population size",'P', "Evolution engine" ).value();</b><br>
<b> &nbsp; &nbsp; &nbsp;unsigned tSize = parser.createParam(unsigned(2), "tSize", "Tournament size",'T', "Evolution Engine" ).value();</b><br>
<b> &nbsp; &nbsp;</b>// init and stop<br>
<b> &nbsp; &nbsp; &nbsp;string loadName = parser.createParam(string(""), "Load","A save file to restart from",'L', "Persistence" ).value();</b><br>
<b> </b><br>
<b> &nbsp; &nbsp; &nbsp;unsigned maxGen = parser.createParam(unsigned(100), "maxGen", "Maximum number of generations",'G', "Stopping criterion" ).value();</b><br>
<b> &nbsp; &nbsp; &nbsp;unsigned minGen = parser.createParam(unsigned(100), "minGen", "Minimum number of generations",'g', "Stopping criterion" ).value();</b><br>
<b> &nbsp; &nbsp; &nbsp;unsigned steadyGen = parser.createParam(unsigned(100), "steadyGen", "Number of generations with no improvement",'s', "Stopping criterion" ).value();</b><br>
<b> &nbsp; &nbsp;</b>// operators probabilities at the algorithm level<br>
<b> &nbsp; &nbsp; &nbsp;double pCross = parser.createParam(double(0.6), "pCross", "Probability of Crossover", 'C', "Genetic Operators" ).value();</b><br>
<b> &nbsp; &nbsp; &nbsp;double pMut = parser.createParam(double(0.1), "pMut", "Probability of Mutation", 'M', "Genetic Operators" ).value();</b><br>
<b> &nbsp; &nbsp;</b>// relative rates for crossovers<br>
<b> &nbsp; &nbsp; &nbsp;double hypercubeRate = parser.createParam(double(1), "hypercubeRate", "Relative rate for hypercube crossover", '\0', "Genetic Operators" ).value();</b><br>
<b> &nbsp; &nbsp; &nbsp;double segmentRate = parser.createParam(double(1), "segmentRate", "Relative rate for segment crossover", '\0', "Genetic Operators" ).value();</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// internal parameters for the mutations<br>
<b> &nbsp; &nbsp; &nbsp;double EPSILON = parser.createParam(double(0.01), "EPSILON", "Width for uniform mutation", '\0', "Genetic Operators" ).value();</b><br>
<b> &nbsp; &nbsp; &nbsp;</b><br>
<b> &nbsp; &nbsp; &nbsp;double SIGMA = parser.createParam(double(0.3), "SIGMA", "Sigma for normal mutation", '\0', "Genetic Operators" ).value();</b><br>
<b> &nbsp; &nbsp;</b>// relative rates for mutations<br>
<b> &nbsp; &nbsp; &nbsp;double uniformMutRate = parser.createParam(double(1), "uniformMutRate", "Relative rate for uniform mutation", '\0', "Genetic Operators" ).value();</b><br>
<b> &nbsp; &nbsp; &nbsp;double detMutRate = parser.createParam(double(1), "detMutRate", "Relative rate for det-uniform mutation", '\0', "Genetic Operators" ).value();</b><br>
<b> &nbsp; &nbsp; &nbsp;double normalMutRate = parser.createParam(double(1), "normalMutRate", "Relative rate for normal mutation", '\0', "Genetic Operators" ).value();</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// the name of the "status" file where all actual parameter values will be saved <br>
<b> &nbsp; &nbsp; &nbsp;string str_status = parser.ProgramName() + ".status"; </b>// default value<br>
<b> &nbsp; &nbsp; &nbsp;string statusName = parser.createParam(str_status, "status","Status file",'S', "Persistence" ).value();</b><br>
<b> &nbsp; &nbsp;</b>// do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED<br>
<b> &nbsp; &nbsp;</b>// i.e. in case you need parameters somewhere else, postpone these<br>
<b> &nbsp; &nbsp; &nbsp;if (parser.userNeedsHelp())</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;parser.printHelp(cout);</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit(1);</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</b><br>
<b> &nbsp; &nbsp; &nbsp;if (statusName != "")</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</b><br>
<b>ofstream os(statusName.c_str());</b><br>
<b>os &lt;&lt; parser; </b>// and you can use that file as parameter file<br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</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>& > plainEval( &nbsp;real_value );</b><br>
<b> &nbsp;</b>// ... to an object that counts the nb of actual evaluations<br>
<b> &nbsp;eoEvalFuncCounter&lt;Indi> eval(plainEval);</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>// Either load or initialize<br>
<b> &nbsp;</b>// create an empty pop<br>
<b> &nbsp;eoPop&lt;Indi> pop;</b><br>
<b> &nbsp;</b>// create a state for reading<br>
<b> &nbsp;eoState inState; </b>// a state for loading - WITHOUT the parser<br>
<b> &nbsp;</b>// register the rng and the pop in the state, so they can be loaded,<br>
<b> &nbsp;</b>// and the present run will be the exact conitnuation of the saved run<br>
<b> &nbsp;</b>// eventually with different parameters<br>
<b> &nbsp;inState.registerObject(rng);</b><br>
<b> &nbsp;inState.registerObject(pop);</b><br>
<b> &nbsp; &nbsp; &nbsp;</b><br>
<b> &nbsp;if (loadName != "")</b><br>
<b> &nbsp; &nbsp; &nbsp;{</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inState.load(loadName); </b>// &nbsp;load the pop and the rng<br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// the fitness is read in the file: <br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// do only evaluate the pop if the fitness has changed<br>
<b> &nbsp; &nbsp; &nbsp;}</b><br>
<b> &nbsp;else</b><br>
<b> &nbsp; &nbsp; &nbsp;{</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rng.reseed(seed);</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// a Indi random initializer<br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// based on boolean_generator class (see utils/rnd_generator.h)<br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eoUniformGenerator&lt;double> uGen(-1.0, 1.0);</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eoInitFixedLength&lt;Indi> random(vecSize, uGen);</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// Init pop from the randomizer: need to use the append function<br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pop.append(popSize, random); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// and evaluate pop (STL syntax) &nbsp; &nbsp; &nbsp;<br>
<b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;apply&lt;Indi>(eval, pop);</b><br>
<b> &nbsp; &nbsp; &nbsp;} </b>// end of initializatio of the population<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">
<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;eoDetTournamentSelect&lt;Indi> selectOne(tSize);</b><br>
<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;eoGenerationalReplacement&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;eoHypercubeCrossover&lt;Indi> xoverA;</b><br>
<b> &nbsp;</b>// Combine them with relative weights<br>
<b> &nbsp;eoPropCombinedQuadOp&lt;Indi> xover(xoverS, segmentRate);</b><br>
<b> &nbsp;xover.add(xoverA, hypercubeRate, 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>// 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>// all coordinates of parents are normally modified (stDev SIGMA)<br>
<b> &nbsp;eoNormalMutation&lt;Indi> &nbsp;mutationN(SIGMA); </b><br>
<b> &nbsp;</b>// Combine them with relative weights<br>
<b> &nbsp;eoPropCombinedMonOp&lt;Indi> mutation(mutationU, uniformMutRate);</b><br>
<b> &nbsp;mutation.add(mutationD, detMutRate);</b><br>
<b> &nbsp;mutation.add(mutationN, normalMutRate, true);</b><br>
<b> &nbsp;</b>// The operators are &nbsp;encapsulated into an eoTRansform object<br>
<b> &nbsp;eoSGATransform&lt;Indi> transform(xover, pCross, mutation, pMut);</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">
<b> &nbsp;</b>//////////////////////////////////////<br>
<b> &nbsp;</b>// termination condition see FirstBitEA.cpp<br>
<b> &nbsp;</b>/////////////////////////////////////<br>
<b> &nbsp;eoGenContinue&lt;Indi> genCont(maxGen);</b><br>
<b> &nbsp;eoSteadyFitContinue&lt;Indi> steadyCont(minGen, steadyGen);</b><br>
<b> &nbsp;eoFitContinue&lt;Indi> fitCont(0);</b><br>
<b> &nbsp;eoCombinedContinue&lt;Indi> continuator(genCont);</b><br>
<b> &nbsp;continuator.add(steadyCont);</b><br>
<b> &nbsp;continuator.add(fitCont);</b><br>
<b> &nbsp;</b><br>
<b> &nbsp;</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>// but now you want to make many different things every generation <br>
<b> &nbsp;</b>// (e.g. statistics, plots, ...).<br>
<b> &nbsp;</b>// the class eoCheckPoint is dedicated to just that:<br>
<b> &nbsp;</b>// Declare a checkpoint (from a continuator: an eoCheckPoint <br>
<b> &nbsp;</b>// IS AN eoContinue and will be called in the loop of all algorithms)<br>
<b> &nbsp;eoCheckPoint&lt;Indi> checkpoint(continuator);</b><br>
<b> &nbsp;</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// Create a counter parameter<br>
<b> &nbsp; &nbsp; &nbsp;eoValueParam&lt;unsigned> generationCounter(0, "Gen.");</b><br>
<b> &nbsp; &nbsp; &nbsp;</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// Create an incrementor (sub-class of eoUpdater). Note that the <br>
<b> &nbsp; &nbsp; &nbsp;</b>// parameter's value is passed by reference, <br>
<b> &nbsp; &nbsp; &nbsp;</b>// so every time the incrementer is updated (every generation),<br>
<b> &nbsp; &nbsp; &nbsp;</b>// the data in generationCounter will change.<br>
<b> &nbsp; &nbsp; &nbsp;eoIncrementor&lt;unsigned> increment(generationCounter.value());</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// Add it to the checkpoint, <br>
<b> &nbsp; &nbsp; &nbsp;</b>// so the counter is updated (here, incremented) every generation<br>
<b> &nbsp; &nbsp; &nbsp;checkpoint.add(increment);</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// now some statistics on the population:<br>
<b> &nbsp; &nbsp; &nbsp;</b>// Best fitness in population<br>
<b> &nbsp; &nbsp; &nbsp;eoBestFitnessStat&lt;Indi> bestStat;</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// Second moment stats: average and stdev<br>
<b> &nbsp; &nbsp; &nbsp;eoSecondMomentStats&lt;Indi> SecondStat;</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// Add them to the checkpoint to get them called at the appropriate time<br>
<b> &nbsp; &nbsp; &nbsp;checkpoint.add(bestStat);</b><br>
<b> &nbsp; &nbsp; &nbsp;checkpoint.add(SecondStat);</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// The Stdout monitor will print parameters to the screen ...<br>
<b> &nbsp; &nbsp; &nbsp;eoStdoutMonitor monitor(false);</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp;</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// when called by the checkpoint (i.e. at every generation)<br>
<b> &nbsp; &nbsp; &nbsp;checkpoint.add(monitor);</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// the monitor will output a series of parameters: add them <br>
<b> &nbsp; &nbsp; &nbsp;monitor.add(generationCounter);</b><br>
<b> &nbsp; &nbsp; &nbsp;monitor.add(eval); </b>// because now eval is an eoEvalFuncCounter!<br>
<b> &nbsp; &nbsp; &nbsp;monitor.add(bestStat);</b><br>
<b> &nbsp; &nbsp; &nbsp;monitor.add(SecondStat);</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// A file monitor: will print parameters to ... a File, yes, you got it!<br>
<b> &nbsp; &nbsp; &nbsp;eoFileMonitor fileMonitor("stats.xg", " ");</b><br>
<b> &nbsp; &nbsp; &nbsp; &nbsp;</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// the checkpoint mechanism can handle multiple monitors<br>
<b> &nbsp; &nbsp; &nbsp;checkpoint.add(fileMonitor);</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// the fileMonitor can monitor parameters, too, but you must tell it!<br>
<b> &nbsp; &nbsp; &nbsp;fileMonitor.add(generationCounter);</b><br>
<b> &nbsp; &nbsp; &nbsp;fileMonitor.add(bestStat);</b><br>
<b> &nbsp; &nbsp; &nbsp;fileMonitor.add(SecondStat);</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// Last type of item the eoCheckpoint can handle: state savers:<br>
<b> &nbsp; &nbsp; &nbsp;eoState outState;</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// Register the algorithm into the state (so it has something to save!!)<br>
<b> &nbsp; &nbsp; &nbsp;outState.registerObject(parser);</b><br>
<b> &nbsp; &nbsp; &nbsp;outState.registerObject(pop);</b><br>
<b> &nbsp; &nbsp; &nbsp;outState.registerObject(rng);</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// and feed the state to state savers<br>
<b> &nbsp; &nbsp; &nbsp;</b>// save state every 100th &nbsp;generation<br>
<b> &nbsp; &nbsp; &nbsp;eoCountedStateSaver stateSaver1(20, outState, "generation"); </b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// save state every 1 seconds <br>
<b> &nbsp; &nbsp; &nbsp;eoTimedStateSaver &nbsp; &nbsp;stateSaver2(1, outState, "time"); </b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// Don't forget to add the two savers to the checkpoint<br>
<b> &nbsp; &nbsp; &nbsp;checkpoint.add(stateSaver1);</b><br>
<b> &nbsp; &nbsp; &nbsp;checkpoint.add(stateSaver2);</b><br>
<b> &nbsp; &nbsp; &nbsp;</b>// and that's it for the (control and) output<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>// stopping criterion, eval, selection, transformation, replacement<br>
<b> &nbsp;eoEasyEA&lt;Indi> gga(checkpoint, 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="stop"></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> &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="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>
<hr>
<address>
<a href="mailto:marc.schoenauer@inria.fr">Marc Schoenauer</a></address>
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last
modified: Sun Apr 28 06:42:44 2002
<!-- 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">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based
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">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based
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@inria.fr">Marc Schoenauer</a></address>
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last modified: Wed Nov
29 09:03:09 2000<!-- hhmts end -->
</body>
</html>

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">Algorithm-Based
page</a> - <a href="eoBottomUp.html">Component-Based</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,77 @@
<!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 Component-Based 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">Algorithm-Based
</a> - <a href="eoBottomUp.html">Component-Based</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 - Component-Based approach</font></h1></center>
<p><br>Congratulations - You have chosen the component-based 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="340,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">Algorithm-Based
</a> - <a href="eoBottomUp.html">Component-Based</a> - <a href="eoProgramming.html">Programming
hints</a> - <a href="../../doc/html/index.html">EO documentation</a>
<br>
<hr>
<address>
<a href="mailto:Marc.Schoenauer@inria.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,150 @@
<!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.73 [en] (X11; I; Linux 2.2.15-4mdk i686) [Netscape]">
<title>Variation Operators</title>
</head>
<body text="#000000" link="#0000EF" vlink="#51188E" alink="#FF0000" background="beige009.jpg">
<b><font color="#FF0000">General</font></b>: <a href="eoTopDown.html">Algorithm-Based</a>
- <a href="eoBottomUp.html">Component-Based</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%">
<br><b><font color="#FF0000">Local</font></b>: <a href="#introduction">Introduction</a>
- <a href="#continuator">Continuators</a> - <a href="#combined_continue">Combined
continuators</a> - <a href="#checkpoint">Checkpoints</a> - <a href="#statistic">Statistics</a>
- <a href="#monitor">Monitors</a> - <a href="#updater">Updaters</a>
<br>
<hr WIDTH="100%">
<center>
<h1>
<b><font color="#CC0000">CheckPointing</font></b></h1></center>
<a NAME="introduction"></a><b><font color="#000099"><font size=+2>What
is Checkpointing about?</font></font></b>
<br>Evolutionary Algorithms are almost all timely ticked, the basic time
unit being what is called a generation. EO checkpointing mechanism allow
you to program things that you want to be done at the end of every generation.
This includes deciding to stop, outputing some statistics on the current
state of the algorithm,&nbsp; updating some dynamical variables of your
algorithm, saving the population to disk, ...
<p><b><font color="#FF0000">EO classes described in this page</font></b>:
<ul>
<li>
<b><font color="#FF0000">Base classes</font></b>: eoCheckPoint, <a href="#continuator">eoContinue</a>,
eoStat, eoSortedStat, eoMonitor, eoUpdater</li>
<li>
<b><font color="#FF0000">Derived classes</font></b>: eoCombinedContinue,</li>
<li>
<b><font color="#FF0000">Related classes</font></b>: eoGnuPlot1DMonitor,
eoGnuPlot1DSnapshotMonitor,</li>
</ul>
<br>&nbsp;
<p>
<hr WIDTH="100%"><a NAME="continuator"></a><b><font color="#000099"><font size=+1>Continuators:</font></font></b>
<p>Continuators are functors that compute stopping critera. They receive
a population and return a boolean value which is set to false only when
some stopping vriterion is met. All algorithms in EO have a loop that goes
<b><font color="#993300">do{...}while(continuator(pop)</font></b>which
means that the algorithm stops only when the continuator returns <b><font color="#993300">false</font></b>.
<p><b><font color="#FF0000">Interface</font></b>:&nbsp; The abstract class
for computing stopping conditions is <b><font color="#3366FF">eoContinue</font></b>,
and the interface for its operator() is
<p>&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;
<b><tt><font color="#993300"><font size=+1>bool operator()(const eoPop&lt;EOT>&amp;
)</font></font></tt></b>
<p>which you could have guessed from the <a href="../../doc/html/classeo_continue.html">inheritance
diagram</a> for class <b><font color="#3366FF">eoContinue</font></b>, as
you see there that <b><font color="#3366FF">eoContinue</font></b> derives
from class <b><tt><font color="#993300"><font size=+1>eoUF&lt;const eoPop&lt;EOT>&amp;,
bool></font></font></tt></b>.
<p><b><font color="#FF0000">Using a continuator</font></b>:
<br>You can find an first example of using a continuator in the code for
<a href="FirstBitGA.html#stop">FirstBitEA</a>
ior more sophisticated continue conditions in&nbsp; <a href="eoLesson2.html#combinedContinue">Lesson2</a>.
<br>If you want to find out how it is used inside an algorithm, go and
see for instance in eoSGA, the simplest EA within EO.
<p><a NAME="writing_continuator"></a><b><font color="#FF0000">Writing a
continuator:</font></b>
<br>There are only two things to modify in the <a href="../Templates/continue.tmpl">template
class definitions</a> provided (apart from the name of the class you are
creating!)
<ul>
<li>
The <font color="#FF6600">constructor</font>, where you pass to the object
any useful parameter (see the private data at end of class definition).</li>
<li>
The <font color="#FF6600">operator()</font> method, which performs the
computation of the actual test using the population plus any other parameter
passed at construct time. Don't forget to <b><font color="#FF6600">returnfalse</font></b>
when the stopping criterion <b><font color="#FF6600">is met!</font></b></li>
</ul>
<a NAME="existingContinue"></a><b><font color="#FF0000">Existing continuators:
</font></b>Of
course you can find out all existing (non-virtual!) subclasses of eoContinue
by looking at its&nbsp; <a href="../../doc/html/classeo_continue.html">inheritance
diagram</a>. But you might find it more convenient to have them listed
here:
<ul>
<li>
</li>
</ul>
<p><br>
<hr WIDTH="100%"><a NAME="combined_continue"></a><b><font color="#000099"><font size=+1>Combining
continuators:</font></font></b>
<p>
<hr WIDTH="100%"><a NAME="checkpoint"></a><b><font color="#000099"><font size=+1>CheckPoints:</font></font></b>
<p><b><font color="#FF0000">Interface</font></b>:
<p><b><font color="#FF0000">Using a checkpoint</font></b>: An eoCheckPoint
being an eoContinue, its usage is exactly the same. However, an eoCheckPoint
will actually do many more things than an eoContinue before returning its
boolean result as an eoContinue.
<p><a NAME="writing_checkpoint"></a><b><font color="#FF0000">Writing a
checkpoint:</font></b>
<br>This is something you should never have to do. However, should you
feel you have to do it, please do - and send us both the reasons that lead
you to that (what is it you couldn't do with existing eoCheckPoint), and
the resulting code, of course.
<br>
<hr WIDTH="100%"><a NAME="statistic"></a><b><font color="#000099"><font size=+1>Statistics:</font></font></b>
<p><b><font color="#FF0000">Interface</font></b>:
<p><b><font color="#FF0000">Using statistics</font></b>:
<p><a NAME="writing_statistic"></a><b><font color="#FF0000">Writing a statitic</font></b>:
<br>
<hr WIDTH="100%"><a NAME="monitor"></a><b><font color="#000099"><font size=+1>Monitors:</font></font></b>
<p><b><font color="#FF0000">Interface</font></b>:
<p><b><font color="#FF0000">Using monitors</font></b>:
<p><a NAME="writing_monitor"></a><b><font color="#FF0000">Writing a monitor</font></b>:
<p>
<hr WIDTH="100%"><a NAME="updater"></a><b><font color="#000099"><font size=+1>Updater:</font></font></b>
<p><b><font color="#FF0000">Interface</font></b>:
<p><b><font color="#FF0000">Using updaters</font></b>:
<p><a NAME="writing_updater"></a><b><font color="#FF0000">Writing an updater</font></b>:
<br>&nbsp;
<p>
<hr WIDTH="100%"><b><font color="#FF0000">Local</font></b>: <a href="#introduction">Introduction</a>
- <a href="#continuator">Continuators</a> - <a href="#combined_continue">Combined
continuators</a> - <a href="#checkpoint">Checkpoints</a> - <a href="#statistic">Statistics</a>
- <a href="#monitor">Monitors</a> - <a href="#updater">Updaters</a>
<br>
<hr WIDTH="100%"><b><font color="#FF0000">General</font></b>: <a href="eoTopDown.html">Algorithm-Based</a>
- <a href="eoBottomUp.html">Component-Based</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>
<address>
<a href="mailto:Marc.Schoenauer@inria.fr">Marc Schoenauer</a></address>
<br><!-- Created: Mon Oct 30 07:27:13 CET 2000 --><!-- hhmts start -->Last
modified: Fri Dec. 8 2000&nbsp;<!-- hhmts end -->
<br>&nbsp;
</body>
</html>

View file

@ -0,0 +1,778 @@
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Mozilla/4.78 [en] (X11; U; Linux 2.4.7-10 i686) [Netscape]">
<title>Genetic Engine</title>
</head>
<body text="#000000" link="#0000EF" vlink="#51188E" alink="#FF0000" background="beige009.jpg">
<b><font color="#CC0000">General: </font></b><a href="eoTopDown.html">Algorithm-Based</a>
- <a href="eoBottomUp.html">Component-Based</a> - <a href="eoProgramming.html">Programming
hints</a> - <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/index.html">EO
documentation
<hr WIDTH="100%"></a></font></font><font color="#CC0000">Local: </font></b><a href="#introduction">Introduction</a>&nbsp;
- <a href="#selection">Selection</a> - <a href="#replacement">Replacement</a>
- <a href="#general">General Replacement</a> - <a href="#popular">Popular
evolution engines</a> - <a href="#tournament">Tournaments</a> - <a href="#merge">Merge</a>
- <a href="#reduce">Reduce</a> - <a href="#howmany">HowMany</a> - <a href="#SAD">SurviveAndDie</a>
<hr WIDTH="100%">
<center>
<h1>
<b><font color="#CC0000">Evolution Engine</font></b></h1></center>
<hr WIDTH="100%">
<br><a NAME="introduction"></a><b><font color="#000099"><font size=+2>Evolution
Engines</font></font></b>
<p>The term <b><font color="#FF6600">evolution engine</font></b> denotes
the different parts of an Evolutionary Algorithm that simulate the Darwinism:
<center>
<p><i><font color="#009900">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 <b><font color="#009900">reproduce</font></b><font color="#CC33CC">.</font>
<br><a href="#replacement">Replacement</a> takes place after reproduction,
and is the Darwinistic choice of those individuals that will <b><font color="#009900">survive</font></b>,
i.e. become the parents of the next generation.
<p>Both selection and replacement will be discussed in turn, before some
helper classes that are used within selection and replacement procedures
are presented.
<p>
<hr WIDTH="100%"><a NAME="selection"></a><b><font color="#000099"><font size=+2>Selection</font></font></b>
<p>The very beginning of the generation loop is the selection phase, where
some individuals from the population are chosen to become the parents,
to be later modified by the variation operators and become the offspring.
This is the first step of the <font color="#009900">artificial Darwinism</font>,
where the <i><font color="#FF6600">fittest individuals are allowed to reproduce</font></i>.
<br>Conceptually, there are two distinct ways to choose the lucky ones:
one by one from the very same population (i.e. with replacement), which
means that at the extreme the same individual can be chosen every time;
or as a whole, in some sort of batch procedure. Of course, repeated selection
of one individual results in a batch selection!
<p>There are hence two basic EO classes for selection: <font color="#009900">eoSelectOne</font>
and <font color="#009900">eoSelect</font>, with different interfaces.
<br>
<hr WIDTH="50%">
<br><b><font color="#000099">eoSelectOne: </font><font color="#FF0000">The
interface</font></b>
<p>The abstract class for selection of a single individual from a population
is <font color="#009900">eoSelectOne</font>, and the interface for its
<tt><font color="#993300">operator()</font></tt>
is
<center>
<p><b><tt><font color="#993300">const EOT &amp; operator()(const eoPop&lt;EOT>&amp;
_parents)</font></tt></b></center>
<p>which you could have guessed from the inheritance tree for class <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_select_one.html">eoSelectOne</a></font></font></b>.,
as you see there that <font color="#009900">eoSelectOne</font> derives
from <tt><font color="#993300">class eoUF&lt;const eoPop&lt;EOT>&amp;,
const EOT&amp;></font></tt>.
<br>This means that it takes <font color="#FF6600">1 population</font>
(without the right to modify it - see the <b><tt><font color="#993300">const</font></tt></b>
keyword in argument) and returns a const reference to an individual (again,
the <b><tt><font color="#993300">const</font></tt></b> keyword ensures
that nothing will happen to the individual in the population - remember
it returns a reference).
<br>
<hr WIDTH="50%">
<br><b><font color="#000099">eoSelectOne: </font><font color="#FF0000">Instances</font></b>
<ul>
<li>
<b><tt><font color="#009900">eoDetTournamentSelect</font></tt></b> uses
the <a href="#detTournament">(deterministic) tournament</a> to choose one
individual. Its constructor has one parameter, the tournament size (integer
>= 2).</li>
<li>
<b><tt><font color="#009900">eoStochTournamentSelect</font></tt></b> uses
the <a href="#stochTournament">binary stochastic tournament</a> to choose
one individual. Its constructor has one parameter, the tournament rate
(real in [0.5,1]).</li>
<li>
&nbsp;<b><tt><font color="#009900">eoProportionalSelect</font></tt></b>
is the original <font color="#FF6600">roulette wheel</font> selection:
each parent is selected with a probability proportional to its fitness.</li>
<li>
<b><tt><font color="#009900">eoRandomSelect</font></tt></b> is the <font color="#FF6600">random</font>
selection and should give bad results! At the moment, it selects one individual
uniformly, but it would be easy to use any probability distribution.</li>
</ul>
<hr WIDTH="50%">
<br><b><font color="#000099">eoSelect: </font><font color="#FF0000">The
interface</font></b>
<p>The abstract class for batch selection of a&nbsp; whole set of individuals
from a population is <font color="#009900">eoSelect</font>, and the interface
for its
<tt><font color="#993300">operator()</font></tt> is
<center>
<p><b><tt><font color="#993300">void operator()(const eoPop&lt;EOT>&amp;
_source, eoPop&lt;EOT>&amp; _dest)</font></tt></b></center>
<p>which you could have guessed from the inheritance tree for class <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_select.html">eoSelect</a></font></font></b>.,
as you see there that <font color="#009900">eoSelect</font> derives from
<tt><font color="#993300">class
eoBF&lt;const eoPop&lt;EOT>&amp;, eoPop&lt;EOT>&amp;, void></font></tt>.
<br>This means that it takes <font color="#FF6600">2 populations</font>,
and fills the second one with individuals from the first one without modifying
it (see the <b><tt><font color="#993300">const</font></tt></b> keyword).
This raises two questions:
<ul>
<li>
How does it know how many individuals to select?</li>
<li>
How to use repeated selection of one individual (see above the <font color="#009900">eoSelectOne</font>
class)?</li>
</ul>
<hr WIDTH="50%">
<br><b><font color="#000099">eoSelect: </font><font color="#FF0000">HowMany</font></b>
<p>There are two ways an&nbsp; can derive the number of individuals it
has to select: either it is a fixed number, or it is some percentage of
the source population size (any positive real number). In both case, this
must be passed to the constructor. In most instances, however, the constructor
will accept a real number (double) and a boolean indicating whether this
real number should be used as an absolute integer or as a rate, thanks
to the <a href="#howmany">eoHowMany</a> class.
<p><b><font color="#FF0000">Note</font></b>: an <font color="#009900">eoSelect</font>
can select more individuals than there are in the original population.
It is the job of the <font color="#009900">replacement</font> to ensure
that the population size does not grow along the generations.
<p>
<hr WIDTH="50%"><b><font color="#000099">eoSelectMany: </font><font color="#FF0000">Encapsulating
eoSelectOne</font></b>
<p>It is clear that repeated selection of a single individual is a way
to do batch selection. This is why it is possible to encapsulate an object
of class <font color="#009900">eoSelectOne</font> into an object of class
<font color="#009900">eoSelect</font>
using the class <font color="#009900">eoSelectMany</font>. Class <font color="#009900">eoSelectMany</font>
is derived from class <font color="#009900">eoSelect</font> and takes in
its constructor an <font color="#009900">eoSelectOne</font> (plus the number
of individuals it should select, according to the <a href="#howmany">eoHowMany</a>
paradigm).
<p><b><font color="#FF0000">Note</font></b>: some procedures for selecting
a single individual require some pre-processing of the whole population
that takes place before any selection, and will be repeated identically
for every individual. The encapsulation of an&nbsp; into an&nbsp; allows
to call such technical processing only once through the use of method setup
of class . This method does nothing by default, but is mandatory
<br>
<hr WIDTH="50%">
<br><b><font color="#000099">eoSelect: </font><font color="#FF0000">Other
instances</font></b>
<ul>
<li>
<b><tt><font color="#009900">eoDetSelect</font></tt></b> selects individuals
<b><font color="#FF6600">deterministically</font></b>,
i.e. starting from the best ones down to the worse ones. If the total number
to select is less than the size of the source populations, the best individuals
are selected once. If more individuals are needed after reaching the bottom
of the population, then the selection starts again at top. It the total
number required is N times that of the source size, all individuals are
selected exactly N times.</li>
</ul>
No other instances of <font color="#009900">eoSelect</font> that are not
encapsualtions of <font color="#009900">eoSelectOne</font> procedures are
avaiable as of today (Jan. 4 2001).
<br>
<hr WIDTH="100%">
<br><a NAME="replacement"></a><b><font color="#000099"><font size=+2>Replacement</font></font></b>
<p>The replacement phase takes plac<font color="#000000">e after the birth
of all offspring through </font>variation operators. This is the second
step of the <font color="#009900">artificial Darwinism</font>, where the
<i><font color="#FF6600">fittest
individuals are allowed to survive</font></i>.
<br>It can also be viewed on the algorithmic side as closing the generation
loop, i.e. building the population that will be the initial population
of next generation. That population will be <font color="#FF6600">built
upon the old parents and the new-born offspring</font>. In all algorithms
that come up with EO, the <font color="#FF6600">population size</font>
is supposed to be <font color="#FF6600">constant</font> from one generation
to the next one - though nothing stops you from writing an algorithm with
varying population size.
<br>
<hr WIDTH="50%">
<br><b><font color="#000099">Replacement: </font><font color="#FF0000">The
interface</font></b>
<p>The abstract class for replacement procedures is the functor class
<font color="#009900">eoReplacement</font>,
and the interface for its <tt><font color="#993300">operator()</font></tt>
is
<center>
<p><b><tt><font color="#993300">void operator()(eoPop&lt;EOT>&amp; _parents,
eoPop&lt;EOT>&amp; _offspring)</font></tt></b></center>
<p>which you could have guessed from the inheritance tree for class <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_replacement.html">eoReplacement</a></font></font></b>.,
as you see there that <font color="#009900">eoReplacement</font> derives
from <tt><font color="#993300">class eoBF&lt;eoPop&lt;EOT>&amp;, eoPop&lt;EOT>&amp;,
void></font></tt>.
<br>This means that it takes <font color="#FF6600">2 populations</font>
(called, for obvious anthropomorphic reasons, _parents and _offspring :-)
and is free to modify both, but the resulting population should be placed
in the first argument (usually called_parents) to close the loop and go
to next generation.
<br>
<hr WIDTH="50%">
<br><b><font color="#000099">Replacement: </font><font color="#FF0000">Instances</font></b>
<ul>
<li>
<b><tt><font color="#009900">eoGenerationalReplacement </font></tt></b>This
is the most straightforward replacement, called <font color="#FF6600">generational
replacement</font>: all offspring replace all parents (used in Holland's
and Goldberg's traditional GAs).&nbsp; It takes no argument, and supposes
that offspring and parents are of the same size (but does not check!).</li>
<br>&nbsp;
<li>
<a NAME="SSGA"></a><b><tt><font color="#009900">eoMergeReduce</font></tt></b>
This is one the basic types of replacement in EO. It has two major steps,
<font color="#FF6600">merging</font>
both populations of parents and offspring, and <font color="#FF6600">reducing</font>
this big population to the right size. It <font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_merge_reduce.html">contains
two objects</a></font></font> of respective types <b><font color="#009900"><a href="#merge">eoMerge</a></font></b>
and <b><font color="#009900"><a href="#reduce">eoReduce</a></font></b>
and you can probably guess what each of them actually does :-)</li>
<br>&nbsp;
<p>&nbsp;
<br>&nbsp;
<br>&nbsp;
<p>Available <font color="#FF6600">instances of eoMergeReduce</font> replacement
include
<ul>
<li>
<b><tt><font color="#009900">eoCommaReplacement</font></tt></b>, one of
the two standard strategies in <font color="#000000">Evolution Strategies</font>,
<b><font color="#FF6600">selects
the best offspring</font></b>. It is an
<b><tt><font color="#993300">eoMergeReduce(eoNoElitism,
eoTruncate)</font></tt></b>.</li>
<li>
<b><tt><font color="#009900">eoPlusReplacement</font></tt></b>, the other
standar<font color="#000000">d Evolution Startegies repla</font>cement,
where <b><font color="#FF6600">the best from offspring+parents</font></b>
become the next generation. It is an <b><tt><font color="#993300">eoMergeReduce(eoPlus,
eoTruncate)</font></tt></b>.</li>
<li>
<b><tt><font color="#009900">eoEPReplacement</font></tt></b>, used in th<font color="#000000">e
Evolutionary Programming historical </font>algorithm, does an EP stochastic
tournament among parents + offspring. It is an <b><tt><font color="#993300">eoMergeReduce(eoPlus,
eoEPReduce)</font></tt></b> and its constructor requires as argument T,
the size of the tournament (unsigned int).</li>
</ul>
<li>
<b><tt><font color="#009900">eoReduceMerge</font></tt></b> is another important
type of eoReplacement: the parents are first reduced, and then merged with
the offspring. Note that the parent population is reduced of the exact
number of offspring.</li>
<br>Though not mandatory, it is implicitely assumed that few offspring
have been generated. Hence, all derived replacement procedures of class
<b><tt><font color="#009900">eoReduceMerge</font></tt></b>
are termed <b><tt><font color="#009900">eoSSGAxxx</font></tt></b>, as they
are the ones to use in SteadyState Genetic Algorithm engine. This gives
the following <font color="#FF6600">instances of eoReduceMerge</font>:
<ul>
<li>
&nbsp;<b><tt><font color="#009900">eoSSGAWorseReplacement</font></tt></b>
in which the worse parents are killed and replaced by all offsprings (no
additional argument needed);</li>
<li>
<b><tt><font color="#009900">eoSSGADetTournamentReplacement</font></tt></b>
in which parents to be killed are chosen by a (reverse) determinitic tournament.
Additional parameter (in the constructor) is the tournament size, an <b><tt><font color="#993300">unsigned
int</font></tt></b>.</li>
<li>
<b><tt><font color="#009900">eoSSGAStochTournamentReplacement</font></tt></b>
in which parents to be killed are chosen by a (reverse) stochastic tournament.
Additional parameter (in the constructor) is the tournament rate, a <b><tt><font color="#993300">double</font></tt></b>.</li>
</ul>
<li>
<a NAME="SADreplacement"></a><b><tt><font color="#009900">eoSurviveAndDie</font></tt></b>
replacement strategies are a generalization of both the above that allows
strong elitist and eugenism in both the parent population and the offspring
population. The <b><tt><font color="#009900"><a href="#SAD">eoSurviveAndDie</a></font></tt></b>
building block takes one population, kills the worse and moves the best
to some safe place.&nbsp; The corresponding replacements apply an <b><tt><font color="#009900">eoSurviveAndDie</font></tt></b>
to the parents, another one to the offspring, and finally merges the remaining
parents and offspring before reducing the resulting population to the right
size. Available instances of <b><tt><font color="#009900">eoSurviveAndDieReplacement</font></tt></b>
are limited todayto the <b><tt><font color="#009900">eoDeterministicSaDReplacement</font></tt></b>,
the&nbsp; that uses a deterministic MergeReduce.</li>
<br>&nbsp;
<p>&nbsp;
<br>&nbsp;
<br>&nbsp;
<p><b><font color="#FF0000">Note</font></b>: The basic use (and initial
motivation) for <b><tt><font color="#009900">eoSurviveAndDie</font></tt></b>
takes 2 arguments, an eoMergeReduce and a number of surviving parents.
It starts by copying the best parents to the new populations, then merges
the remaining parents with the offspring before reducing to the number
of remaining seats in the new population.</ul>
<hr WIDTH="50%">
<br><a NAME="weakelitism"></a><b><font color="#000099">Replacement: </font><font color="#FF0000">Adding
(weak) elitism</font></b>
<p>You can add what is called <font color="#FF6600">weak elitism</font>
to any replacement by encapsulating it into an <b><tt><font color="#009900">eoWeakElitismReplacement</font></tt></b>
object. Weak elitism ensures that the overall <font color="#FF6600">best
fitness</font> in the population <font color="#FF6600">will never decrease</font>:
if the best fitness in the new population is less than the best fitness
of the parent population, then the best parent is added back to the new
population, replacing the worse.
<p>Within EO, this is very easy to add:
<p>First, declare your replacement functor (here, generational, but it
can be any replacement object):
<br><b><tt><font color="#009900">eoGenerationalReplacement&lt;Indi> genReplace;</font></tt></b>
<br>Then wrap the weak elitism around it:
<br><b><tt><font color="#009900">eoWeakElitismReplacement&lt;Indi> replace(genReplace);</font></tt></b>
<br>and use now replace as your replacement procedure within your algorithm.
<p><font color="#FF0000">Note</font>: of course, adding weak elitism to
an elitist replacement makes no sense - but will not harm either :-)
<br>
<hr WIDTH="50%">
<br><b><font color="#000099">Replacement: </font><font color="#FF0000">Test
file</font></b>
<p>The file <b><tt><font color="#993300">t-eoReplacement</font></tt></b>
in the <b><font color="#FF6600">test directory</font></b> implements all
above replacement procedures within a very simple and easy-to-monitor Dummy
EO class.
<p>
<hr WIDTH="100%">
<br><a NAME="general"></a><b><font color="#000099"><font size=+2>General
Replacement: eoReduceMergeReduce</font></font></b>
<p>In an attempt to unify all the well-known replacements, plus most of
the less-known-though-often-used, the <b><tt><font color="#009900">eoReduceMergeReduce</font></tt></b>
replacement has been designed, and supersedes all of the above instances.
<br>It allows to implement <b><font color="#FF0000">strong elistism</font></b>
(i.e. some parents survive, whatever their fitness and that of the offspring),
as well as multiple weak elistism (the best parents are put back in the
next population if they outperform the best offspring).
<p>Basically, an <b><tt><font color="#009900">eoReduceMergeReduce</font></tt></b>
starts by eventually preserving the (strong) elite parents, proceeds by
reducing both the parent and offspring populations, merges those populations,
and eventually reduces the resulting populations (augmented with the elite
parents) to the right size. Last, the weak elitism is taken care of if
necessary.
<br>The following image, taken from the graphical interface of EASEA, somehow
demonstrates the different parameters of an <b><tt><font color="#009900">eoReduceMergeReduce.</font></tt></b>
<br>
<hr WIDTH="50%">
<br><b><font color="#000099">eoReduceMergeReduce: </font><font color="#FF0000">The&nbsp;
interface</font></b>
<br>Of course the interface is that of eoReplacement. Let's concentrate
on the constructor.
<p>The constructor takes 6 arguments:
<ol>
<li>
<b><tt><font color="#993300">eoHowMany elite</font></tt></b> determines
the number of parents to be treated as elite (actual behavior determined
by next parameter) using the <a href="#howmany">eoHowMany</a> behavior.</li>
<li>
<b><tt><font color="#993300">bool strongElitism</font></tt></b> tells whether
the elite above corresponds to strong (true) or false(weak) elitism.</li>
<br><b><font color="#FF0000">Note</font></b>: the case of no elitism is
obtained by setting elite to 0
<li>
<b><tt><font color="#993300">eoHowMany reducedParents </font></tt></b>gives
the number of parents remaining after reducing them</li>
<br><b><font color="#FF0000">Note</font></b>: 0 means that no parent survive
(except the possible elite), as in <b><tt><font color="#009900">eoCommaReplacement</font></tt></b>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-1 is used inside <b><tt><font color="#009900">eoSSGAReplacement</font></tt></b>s
(one parent is killed to make room for the newborn)
<li>
<b><tt><font color="#993300">eoReduce&lt;EOT> &amp; reduceParents</font></tt></b>
indicates how the parents will be reduced (see <a href="#reduce">available
instances</a>).</li>
<li>
<b><tt><font color="#993300">eoHowMany reducedOffspring </font></tt></b>gives
the number of offspring remaining after reducing them</li>
<br><b><font color="#FF0000">Note</font></b>: 0 is impossible (no evolution!!!)
<li>
<b><tt><font color="#993300">eoReduce&lt;EOT> &amp; reduceOffspring</font></tt></b>
indicates how the offspring will be reduced (see <a href="#reduce">available
instances</a>).</li>
<li>
<b><tt><font color="#993300">eoReduce&lt;EOT> &amp; reduceFinal</font></tt></b>
indicates how the merged reduced-parents/reduced-offspring will be reduced
(see <a href="#reduce">available instances</a>).</li>
<br><b><font color="#FF0000">Note</font></b>: the number of individuals
remaining after that reduction is of course the original size of the population.</ol>
All popular evolution engines use some replacement procedure that can be
viewed as an instance of an <b><tt><font color="#009900">eoReduceMergeReduce</font></tt></b>.
<br>Moreover, a <b><font color="#FF0000">parser-based</font></b> input
of a general&nbsp; is proposed in file do/make_checkpoint.h.
<br>
<hr WIDTH="100%">
<br><a NAME="popular"></a><b><font color="#000099"><font size=+2>Popular
evolution engines</font></font></b>
<p>The most popular evolution engines are listed below, together with the
way to use them in EO. If you don't find your particuler algorithm, please
send it to us, and we might include it here! In the following, P will denote
the number of individuals in the initial population.
<ul>
<li>
<a NAME="GGA"></a><b><font color="#000099">Generational Genetic Algorihtm</font></b><font color="#000000">:
popularized by Holland (75) and Goldberg (89), it uses</font></li>
<br><font color="#FF0000">Number of offspring:&nbsp;</font><font color="#000000">
P</font>
<br><font color="#FF0000">Selection:</font><font color="#000000"> Proportional
(the historical roulette wheel) </font><b><font color="#FF6600">when maximizing
a positive scalar fitness</font></b><font color="#000000">, ranking or
tournament (stochatic or deterministic) in all cases.</font>
<br><font color="#FF0000">Replacement:</font><font color="#000000"> Generational.</font>
<br><font color="#FF0000">Remark:</font><font color="#000000"> You could
use also the </font><b><tt><font color="#009900">eoCommaReplacement</font></tt></b><font color="#000000">,
with exactly the same result as there are as many offspring as we need
indiviudals in the next population. And using the </font><b><tt><font color="#009900">eoSSGAWorseReplacement</font></tt></b><font color="#000000">
would also give the same result, but would be very inefficient!</font>
<br><font color="#000000">You can also add <a href="#weakelitism">weak
elitism</a> to preserve the best individual.</font>
<li>
<a NAME="SSGA"></a><b><font color="#000099">Steady-State Genetic Algorithm</font></b><font color="#000000">:
widely used in GA/GP community</font></li>
<br><font color="#FF0000">Number of offspring:&nbsp;</font><font color="#000000">
small (historically, 1)</font>
<br><font color="#FF0000">Selection:</font><font color="#000000"> tournament
(you can use ranking or proportional, but it will be rather inefficient).</font>
<br><font color="#FF0000">Replacement:</font><font color="#000000"> An
eoSSGAxxxReplacement.</font>
<br><font color="#FF0000">Remark:</font><font color="#000000"> You can
also use the eoPlusReplacement, but you divert from the original SSGA</font>
<li>
<a NAME="ESPlus"></a><b><font color="#000099">(MU+Lambda)-Evolution Strategy</font></b><font color="#000000">:
The elitist ES strategy (Rechenberg 71 and Schwefel 81)</font></li>
<br><font color="#FF0000">Number of offspring:&nbsp;</font><font color="#000000">
Any</font>
<br><font color="#FF0000">Selection:</font><font color="#000000"> eoDetSelect
(batch deterministic).</font>
<br><font color="#FF0000">Replacement:</font><font color="#000000"> eoPlusReplacement</font>
<br><font color="#FF0000">Remark:</font><font color="#000000"> You could
also use eoEPReplacement, to smoothen the selective pressure during replacement,
thus getting close to EP evolution engine</font>
<li>
<a NAME="ESComma"></a><b><font color="#000099">(MU,Lambda)-Evolution Strategy</font></b><font color="#000000">:
The non-elitist ES strategy</font></li>
<br><font color="#FF0000">Number of offspring:&nbsp;</font><font color="#000000">
> P</font>
<br><font color="#FF0000">Selection:</font><font color="#000000"> eoDetSelect
(batch deterministic).</font>
<br><font color="#FF0000">Replacement:</font><font color="#000000"> eoCommaReplacement</font>
<br><font color="#FF0000">Remark:</font><font color="#000000"> You can
also add <a href="#weakelitism">weak elitism</a> to preserve the best individual
- though you'd probably use the plus strategy if you want (strong) elitism.</font>
<li>
<a NAME="EP"></a><b><font color="#000099">Evolutionary Programming</font></b><font color="#000000">:
The historical method of L. Fogel (65)</font></li>
<br><font color="#FF0000">Number of offspring:&nbsp;</font><font color="#000000">
P</font>
<br><font color="#FF0000">Selection:</font><font color="#000000"> eoDetSelect
(batch deterministic). Every individual reproduces exactly once.</font>
<br><font color="#FF0000">Replacement:</font><font color="#000000"> eoEPReplacement,
though one historical replacement was the determnistic replacement - i.e.
in EO the eoPlusReplacement).</font>
<br><font color="#FF0000">Remark:</font><font color="#000000"> Close to
an (P+P)-ES</font>
<li>
<a NAME="General"></a><font color="#FF0000">You name it :-)</font><font color="#000000">:
you can of course choose whatever combination you like - respecting a few
constraints and common-sense remarks. For instance, eoProportionalSelect
should be used only when maximizing a positive fitness, eoCommaReplacement
requires more offspring than parents, and, over all, existing EO algorithms
wirk with fixed size population - and it is your responsability to use
a cmbinatino of selection/replacement that fulfills this requirement (or
to create your own eoAlgo that handles varying size populations).</font></li>
</ul>
<hr WIDTH="100%">
<br><a NAME="tournament"></a><b><font color="#000099"><font size=+2>Tournaments</font></font></b>
<p>Tournaments are an easy and quick way to <b><font color="#FF6600">select</font></b>
individuals within a population based on simple comparisons. Though usually
based on fitness comparisons, they can use any comparison operator.
<br>In EO, there are two variants of tournaments used to select one single
individual, namely <b><tt><font color="#009900">Deterministic Tournament</font></tt></b>
and <b><tt><font color="#009900">Stochastic Tournament</font></tt></b>,
that are used in selection and in replacement procedures, and a global
tournament-based selection of a whole bunch of individuals, the <b><tt><font color="#009900">EP
Tournament</font></tt></b>. Though the single-selection tournaments can
be repeated to select more than one individual, and the batch tournament
selection can be used to select a single individual, both uses are probably
a waste of CPU time.
<ul>
<li>
<a NAME="detTournament"></a><b><tt><font color="#009900">Deterministic
Tournament</font></tt></b> of size T returns the best of T uniformly chosen
individuals in the population. Its size T should be an integer >= 2. It
is implemented in the <b><tt><font color="#009900">eoDetTournamentSelect</font></tt></b>
class, a sub-class of eoSelectOne, as well as in the <b><tt><font color="#009900">eoDetTournamentTruncate</font></tt></b>
class that repeatidly removes from the population the "winner" of the inverse
tournament.&nbsp; These objects use the C++ function determinitic_tournament
in&nbsp; <a href="../../doc/html/selectors_h-source.html">selectors.h</a>.</li>
<li>
<a NAME="stochTournament"></a><b><tt><font color="#009900">Stochastic Tournament</font></tt></b>
of rate R first choses two individuals from the population, and selects
the best one with probability R (the worse one with probability 1-R). Real
parameter R should be in [0.5,1]. It is implemented in the <b><tt><font color="#009900">eoStochTournamentSelect</font></tt></b>
class, a sub-class of eoSelectOne, as well as in the <b><tt><font color="#009900">eoStochTournamentTruncate</font></tt></b>
class that repeatidly removes from the population the "winner" of the inverse
tournament.&nbsp; These objects use the C++ function determinitic_tournament
in&nbsp; <a href="../../doc/html/selectors_h-source.html">selectors.h</a>.</li>
<br><b><font color="#FF0000">Note</font></b>: A stochastic tournament with
rate 1.0 is strictly identical to a deterministic tournament of size 2.
<li>
<a NAME="EPTournament"></a><b><tt><font color="#009900">EP Tournament</font></tt></b>
of size T is a global tournament: it works by assigning a score to all
individuals in the population the following way: starting with a score
of 0, each individual I is "opposed" T times to a uniformly chosen individual.
Everytime I wins, its score in incremented by 1 (and by 0.5 for every draw).
The individuals are then selected deterministically based on their scores
from that procedure. The <b><tt><font color="#009900">EP Tournament</font></tt></b>
is implemented in the&nbsp; <b><tt><font color="#009900">eoEPReduce</font></tt></b>
truncation method used in the <b><tt><font color="#009900">eoEPReplacement</font></tt></b>
procedure.</li>
<br><b><font color="#FF0000">Note</font></b>: whereas both the determinitic
and the stochastic tournament select one individual, the EP tournament
is designed for batch selection. Of course it could be used to select a
single individual, but at a rather high computational cost.</ul>
<p><br>
<hr WIDTH="100%">
<br><a NAME="merge"></a><b><font color="#000099"><font size=+2>Merging
populations</font></font></b>
<p>In replacement procedures, one frequently needs to merge two populations
(computed form old parents and new-born offspring). Classes derived from
the abstract class eoMerge are written for that purpose.
<br>
<hr WIDTH="50%">
<br><b><font color="#000099">eoMerge</font></b>: <b><font color="#FF0000">interface</font></b>
<br>The abstract class for merging procedures is the functor class
<font color="#009900">eoMerge</font>,
and the interface for its <tt><font color="#993300">operator()</font></tt>
is
<center>
<p><b><tt><font color="#993300">void operator()(const eoPop&lt;EOT>&amp;
_parents, eoPop&lt;EOT>&amp; _offspring)</font></tt></b></center>
<p>which you could have guessed from the inheritance tree for class <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_merge.html">eoMerge</a></font></font></b>,
as you see there that <font color="#009900">eoMerge</font> derives from
<br><tt><font color="#993300">class eoBF&lt;const eoPop&lt;EOT>&amp;, eoPop&lt;EOT>&amp;,
void></font></tt>.
<br>This means that it takes <font color="#FF6600">2 populations</font>
and modifies the seond one by adding some individuals from the first one
(which is supposed to remain <b><tt><font color="#993300">const</font></tt></b>ant).
<br>
<hr WIDTH="50%">
<br><b><font color="#000099">eoMerge</font></b>: <b><font color="#FF0000">instances</font></b>
<br>Available <font color="#FF6600">instances of eoMerge</font> objects
are <b><tt><font color="#009900">eoPlus</font></tt></b>, that simply adds
the parents to the offspring, or <b><tt><font color="#009900">eoElitism</font></tt></b>,
that adds only some of the (best) parents to the offspring. A special case
of eoElistism is <b><tt><font color="#009900">eoNoElitism</font></tt></b>,
an eoMerge that does nothing.
<p>
<hr WIDTH="100%">
<br><a NAME="reduce"></a><b><font color="#000099"><font size=+2>Reducing
populations</font></font></b>
<p>The other useful component of replacement procedures, <font color="#009900">eoReduce</font>,
<font color="#FF6600">kills
some individuals</font> from a given population.
<br>
<hr WIDTH="50%">
<br><b><font color="#000099">eoReduce</font></b>: <b><font color="#FF0000">interface</font></b>
<br>The abstract class for reducing procedures is the functor class
<font color="#009900">eoReduce</font>,
and the interface for its <tt><font color="#993300">operator()</font></tt>
is
<center>
<p><b><tt><font color="#993300">void operator()(eoPop&lt;EOT>&amp; _parents,
unsigned int new_size)</font></tt></b></center>
<p>which you could have guessed from the inheritance tree for class <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_reduce.html">eoReduce</a></font></font></b>,
as you see there that <font color="#009900">eoReduce</font> derives from
<br><tt><font color="#993300">class eoBF&lt;eoPop&lt;EOT>&amp;, unsigned
int, void></font></tt>.
<br>An <font color="#009900">eoReduce</font> shoud take a<font color="#FF6600">
population</font> and shrink it to the required size.
<br>
<hr WIDTH="50%">
<br><b><font color="#000099">eoReduce</font></b>: <b><font color="#FF0000">instances</font></b>
<br>Available <font color="#FF6600">instances of eoReduce</font> are
<ul>
<li>
<b><tt><font color="#009900">eoTruncate</font></tt></b>, deterministically
kills the worse individuals, keeping only the required number. It starts
by sorting teh populations, and hence does <font color="#FF6600">modify
its order</font>.</li>
<li>
<b><tt><font color="#009900">eoLinearTruncate</font></tt></b>, deterministically
kills the worse individuals, keeping only the required number. It does
so by repeatedly removing the worsr individual. Hence does <font color="#FF6600">not
modify its order</font>, but takes longer time than <b><tt><font color="#009900">eoTruncate</font></tt></b>
in case of many offspring.</li>
<li>
<b><tt><font color="#009900">eoEPReduce</font></tt></b>, uses the <a href="#EPtournament">EP
stochastic tournament</a> to reduce the population. It requires an additinal
argument, the tournament size.</li>
<li>
<b><tt><font color="#009900">eoDetTournamentTruncate</font></tt></b> uses
inverse deterministic tournament to repeatidly kill one individual until
the propoer size is reached. As <b><tt><font color="#009900">eoLinearTruncate</font></tt></b>,
it might take some time in the case of many offspring. It requires the
size of the tournament (<b><tt><font color="#993300">unsigned int</font></tt></b>)
as parameter in the constructor (default is 2).</li>
<li>
<b><tt><font color="#009900">eoStochTournamentruncate</font></tt></b>&nbsp;
uses inverse stochastic tournament to repeatidly kill individuals from
the population. It requires the rate of the tournament (<b><tt><font color="#993300">double</font></tt></b>)
as parameter in the constructor (default is 0.75).</li>
</ul>
<hr WIDTH="100%">
<br><a NAME="howmany"></a><b><font color="#000099"><font size=+2>eoHowMany:
Choosing a number of individuals</font></font></b>
<p>Many classes in selection/replacement procedures will handle a number
of individuals that may either be fixed or be related to some argument-population
size.
<br>Of course, it is possible to write different classes that will only
differ by the way they compute the number of individuals they have to treat,
as it is done for selectors with the two classes <font color="#009900">eoSelectPerc</font>
and <font color="#009900">eoSelectNumber</font> (it could also have been
possible to have some pure abstrat class and implement the computation
of the number of individuals to treat in some derived classes).
<br>However, the class <b><tt><font color="#993300">eoHowMany</font></tt></b>
allows one to handle in a single class three different behaviors when given
a poopulatio size as argument:
<ul>
<li>
return a given rate of the argument population size</li>
<li>
return an absolute (unsigned) integer, whatever the argument population
size</li>
<li>
return the argument population size minus a given number</li>
</ul>
<hr WIDTH="50%">
<br><b><font color="#000099">eoHowMany</font></b>: <b><font color="#FF0000">interface</font></b>
<br>The class interface for its <tt><font color="#993300">operator()</font></tt>
is
<center>
<p><b><tt><font color="#993300">unsigned int operator()(unsigned int _pop_size)</font></tt></b></center>
<p>which you could have guessed from the inheritance tree for class <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_how_many.html">eoHowMany</a></font></font></b>,
as you see there that <b><tt><font color="#993300">eoHowMany</font></tt></b>
derives from
<br><tt><font color="#993300">class eoUF&lt;unsigned int, unsigned int></font></tt>.
<p>It has 3 possible constructors, that determine its behavior:
<ul>
<li>
<b><tt><font color="#993300">eoHowMany(double _rate, bool _interpret_as_rate
= true)</font></tt></b> where <b><tt><font color="#993300">_rate</font></tt></b>
is by default the fixed rate of behavior 1 above. However, if the boolean
second argument is false, the rate is transformed into a positive integer,
and is used for behavior 2 above</li>
<li>
<b><tt><font color="#993300">eoHowMany(int _combien)</font></tt></b>: if
<b><tt><font color="#993300">_combien</font></tt></b>
is positive, it is the absolute number of behavior 2 above, and if <b><tt><font color="#993300">_combien</font></tt></b>
is negative, its absolute value is used to decrease the argument population
in behavior 3 above</li>
<li>
<b><tt><font color="#993300">eoHowMany(unsigned int _combien)</font></tt></b>:
<b><tt><font color="#993300">_combien</font></tt></b>
(positive!)&nbsp; is the absolute number of behavior 2 above. <b><font color="#FF6600">Note</font></b>
that this constructor is mandatory to avoid ambiguity, as an unsigned int
can be casted to either an int or a double.</li>
</ul>
It is used in <font color="#009900">eoSelectMany</font> (which supersedes
<font color="#009900">eoSelectPerc</font>
and <font color="#009900">eoSelectNumber</font>, but they are left there
for tutorial reasons!) as well as in many <a href="#reduce">truncation</a>
methods, and it is used a lot in the eoGeneralReplacement construct.
<p>
<hr WIDTH="100%">
<br><a NAME="SAD"></a><b><font color="#000099"><font size=+2>Survive and
Die</font></font></b>
<br>This class is highly politically incorrect: it implements strong elitism
and eugenism :-)
<br>It starts by killing the worse individuals from the source argument,
then appends the best ones to the destination argument and removes them
from the source argument. It is used in <b><tt><a href="#SADreplacement">eoSurviveAndDieReplacement</a></tt></b>,
where the same dest is used successively for the parents and the offspring.
<p><b><font color="#000099">eoSurviveAndDie</font></b>: <b><font color="#FF0000">interface</font></b>
<br>The class interface for its <tt><font color="#993300">operator()</font></tt>
is
<center>
<p><b><tt><font color="#993300">void operator()(eoPop&lt;EOT>&amp; _source,
eoPop&lt;EOT>&amp; _dest)</font></tt></b></center>
<p>which you could have guessed from the inheritance tree for class <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_survive_and_die.html">eoSurviveAndDie</a></font></font></b>,
as you see there that <b><tt><font color="#993300">eoSurviveAndDie</font></tt></b>
derives from <tt><font color="#993300">class eoBF&lt;eoPop&lt;EOT>&amp;,
eoPop&lt;EOT>&amp;, void></font></tt>.
<p>Its constructor takes 3 argumenrts:
<center>
<p><b><tt><font color="#993300">eoHowMany(double _survive, double _die,
bool _interpret_as_rate = true)</font></tt></b></center>
<p>to indicate how many (or what proportion, according to <b><tt><font color="#993300">_interpret_as_rate</font></tt></b>)
of the source should be copied to the dest population, and how many (or
what proportion, according to <b><tt><font color="#993300">_interpret_as_rate</font></tt></b>)&nbsp;
should be erased from the source.
<p>
<hr WIDTH="100%"><b><font color="#CC0000">Local: </font></b><a href="#introduction">Introduction</a>&nbsp;
- <a href="#selection">Selection</a> - <a href="#replacement">Replacement</a>
- <a href="#general">General Replacement</a> - <a href="#popular">Popular
evolution engines</a> - <a href="#tournament">Tournaments</a> - <a href="#merge">Merge</a>
- <a href="#reduce">Reduce</a> - <a href="#howmany">HowMany</a> - <a href="#SAD">SurviveAndDie</a>
<br>
<hr WIDTH="100%">
<br><b><font color="#CC0000">General: </font></b><a href="eoTopDown.html">Algorithm-Based</a>
- <a href="eoBottomUp.html">Component-Based</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 WIDTH="100%">
<address>
<a href="mailto:Marc.Schoenauer@inria.fr">Marc Schoenauer</a></address>
<br><!-- Created: Mon Oct 30 07:27:13 CET 2000 --><!-- hhmts start -->Last
modified: Tue. Jan. 9 2001&nbsp;<!-- 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">Algorithm-Based
page</a> - <a href="eoBottomUp.html">Component-Based</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">Algorithm-Based
page</a> - <a href="eoBottomUp.html">Component-Based</a> - <a href="eoProgramming.html">Programming
hints</a> - <a href="../../doc/html/index.html">EO documentation</a>
<hr>
<address><a href="mailto:Marc.Schoenauer@inria.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.Schoenauer@inria.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">Algorithm-Based
page</a> - <a href="eoBottomUp.html">Component-Based</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">Algorithm-Based
page</a> - <a href="eoBottomUp.html">Component-Based</a> - <a href="eoProgramming.html">Programming
hints</a> - <a href="../../doc/html/index.html">EO documentation</a>
<hr>
<address>
<a href="mailto:Marc.Schoenauer@inria.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>

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