paradiseo/eo/tutorial/html/BitEA.html
2012-08-30 11:30:11 +02:00

261 lines
9.1 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>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>