paradiseo/eo/tutorial/html/FirstRealGA.html

350 lines
14 KiB
HTML

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="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>