Initial version of the tutorial.
Warning: all Makefile's are hand-made, and will only work in Linux
This commit is contained in:
parent
a27dc53ef0
commit
b8d4e7faef
34 changed files with 9174 additions and 0 deletions
BIN
eo/tutorial/html/EA_tutorial.jpg
Normal file
BIN
eo/tutorial/html/EA_tutorial.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 53 KiB |
368
eo/tutorial/html/FirstBitEA.html
Normal file
368
eo/tutorial/html/FirstBitEA.html
Normal file
|
|
@ -0,0 +1,368 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="cpp2html Marc Schoenauer">
|
||||
<title>FirstBitEA.cpp</title>
|
||||
<!-- Changed by: Marc Schoenauer, 28-Nov-2000 -->
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">FirstBitEA.cpp</font></h1></center>
|
||||
Click on the figure to see the corresponding code.<br>
|
||||
In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a><br>
|
||||
The actual code is in boldface and the comment in normal face.
|
||||
<br><img SRC="EA_tutorial.jpg" USEMAP="#Map" >
|
||||
<map NAME="Map">
|
||||
<!-- Init -->
|
||||
<area SHAPE="rect" HREF="#init" COORDS="14,31,135,70">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150">
|
||||
<!-- main loop -->
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150">
|
||||
<area SHAPE="rect" HREF="#output" COORDS="280,45,480,70">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150">
|
||||
<area SHAPE="rect" HREF="#select" COORDS="495,110,615,150">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230">
|
||||
<area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287">
|
||||
<area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310">
|
||||
<area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230">
|
||||
<!-- Center of loop -->
|
||||
<area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260">
|
||||
<!-- 4 bottom lines -->
|
||||
<area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370">
|
||||
<area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423">
|
||||
<area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450">
|
||||
<area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450">
|
||||
<area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378">
|
||||
</map>
|
||||
<br>
|
||||
<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 Breeder - and Combined Ops<br>
|
||||
//<br>
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// standard includes<br>
|
||||
<b>#include <stdexcept> </b>// runtime_error <br>
|
||||
<b>#include <iostream> </b>// cout<br>
|
||||
<b>#include <strstream> </b>// ostrstream, istrstream<br>
|
||||
// the general include for eo<br>
|
||||
<b>#include <eo></b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="representation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#999900">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// define your individuals<br>
|
||||
<b>typedef eoBin<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<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> const unsigned int SEED = 42; </b>// seed for random number generator<br>
|
||||
<b> const unsigned int T_SIZE = 3; </b>// size for tournament selection<br>
|
||||
<b> const unsigned int VEC_SIZE = 8; </b>// Number of bits in genotypes<br>
|
||||
<b> const unsigned int POP_SIZE = 20; </b>// Size of population<br>
|
||||
<b> const unsigned int MAX_GEN = 500; </b>// Maximum number of generation before STOP<br>
|
||||
<b> const float CROSS_RATE = 0.8; </b>// Crossover rate<br>
|
||||
<b> const double P_MUT_PER_BIT = 0.01; </b>// probability of bit-flip mutation<br>
|
||||
<b> const float MUT_RATE = 1.0; </b>// mutation rate<br>
|
||||
<b> </b>// some parameters for chosing among different operators<br>
|
||||
<b> const double onePointRate = 0.5; </b>// rate for 1-pt Xover<br>
|
||||
<b> const double twoPointsRate = 0.5; </b>// rate for 2-pt Xover<br>
|
||||
<b> const double URate = 0.5; </b>// rate for Uniform Xover<br>
|
||||
<b> const double bitFlipRate = 0.5; </b>// rate for bit-flip mutation<br>
|
||||
<b> const double oneBitRate = 0.5; </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> </b>//////////////////////////<br>
|
||||
<b> </b>// Random seed<br>
|
||||
<b> </b>//////////////////////////<br>
|
||||
<b> </b>//reproducible random seed: if you don't change SEED above, <br>
|
||||
<b> </b>// you'll aways get the same result, NOT a random run<br>
|
||||
<b> 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> </b>/////////////////////////////<br>
|
||||
<b> </b>// Fitness function<br>
|
||||
<b> </b>////////////////////////////<br>
|
||||
<b> </b>// Evaluation: from a plain C++ fn to an EvalFunc Object<br>
|
||||
<b> </b>// you need to give the full description of the function<br>
|
||||
<b> eoEvalFuncPtr<Indi, double, const vector<bool>& > eval( 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> </b>////////////////////////////////<br>
|
||||
<b> </b>// Initilisation of population<br>
|
||||
<b> </b>////////////////////////////////<br>
|
||||
<b> </b>// based on boolean_generator class (see utils/rnd_generator.h)<br>
|
||||
<b> eoInitFixedLength<Indi, boolean_generator> </b><br>
|
||||
<b> random(VEC_SIZE, boolean_generator());</b><br>
|
||||
<b> </b>// Initialization of the population<br>
|
||||
<b> eoPop<Indi> pop(POP_SIZE, random);</b><br>
|
||||
<b> </b>// and evaluate it in one loop<br>
|
||||
<b> apply<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> </b>// sort pop before printing it!<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> </b>// Print (sorted) intial population (raw printout)<br>
|
||||
<b> cout << "Initial Population" << endl;</b><br>
|
||||
<b> cout << 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> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// selection and replacement<br>
|
||||
<b> </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> </b>// The robust tournament selection<br>
|
||||
<b> eoDetTournament<Indi> selectOne(T_SIZE); </b>// T_SIZE in [2,POP_SIZE]<br>
|
||||
<a NAME="select_encapsulate"></a>
|
||||
<b> </b>// is now encapsulated in a eoSelectPerc (entage)<br>
|
||||
<b> eoSelectPerc<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> </b>// And we now have the full slection/replacement - though with <br>
|
||||
<b> </b>// no replacement (== generational replacement) at the moment :-)<br>
|
||||
<b> eoNoReplacement<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> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// The variation operators<br>
|
||||
<b> </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> </b>// 1-point crossover for bitstring<br>
|
||||
<b> eoBinCrossover<Indi> xover1;</b><br>
|
||||
<b> </b>// uniform crossover for bitstring<br>
|
||||
<b> eoBinUxOver<Indi> xoverU;</b><br>
|
||||
<b> </b>// 2-pots xover<br>
|
||||
<b> eoBinNxOver<Indi> xover2(2);</b><br>
|
||||
<b> </b>// Combine them with relative rates<br>
|
||||
<b> eoPropCombinedQuadOp<Indi> xover(xover1, onePointRate);</b><br>
|
||||
<b> xover.add(xoverU, URate);</b><br>
|
||||
<b> 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> </b><br>
|
||||
<b> </b>// standard bit-flip mutation for bitstring<br>
|
||||
<b> eoBinMutation<Indi> mutationBitFlip(P_MUT_PER_BIT);</b><br>
|
||||
<b> </b>// mutate exactly 1 bit per individual<br>
|
||||
<b> eoDetBitFlip<Indi> mutationOneBit; </b><br>
|
||||
<b> </b>// Combine them with relative rates<br>
|
||||
<b> eoPropCombinedMonOp<Indi> mutation(mutationBitFlip, bitFlipRate);</b><br>
|
||||
<b> mutation.add(mutationOneBit, oneBitRate, true);</b><br>
|
||||
<b> </b><br>
|
||||
<a NAME="transform"></a>
|
||||
<b> </b>// The operators are encapsulated into an eoTRansform object<br>
|
||||
<b> eoSGATransform<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> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// termination conditions: use more than one<br>
|
||||
<b> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// stop after MAX_GEN generations<br>
|
||||
<b> eoGenContinue<Indi> genCont(MAX_GEN);</b><br>
|
||||
<b> </b>// do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement<br>
|
||||
<b> eoSteadyFitContinue<Indi> steadyCont(MIN_GEN, STEADY_GEN);</b><br>
|
||||
<b> </b>// stop when fitness reaches a target (here VEC_SIZE)<br>
|
||||
<b> eoFitContinue<Indi> fitCont(0);</b><br>
|
||||
<b> </b>// do stop when one of the above says so<br>
|
||||
<b> eoCombinedContinue<Indi> continuator(genCont);</b><br>
|
||||
<b> continuator.add(steadyCont);</b><br>
|
||||
<b> 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> </b>/////////////////////////////////////////<br>
|
||||
<b> </b>// the algorithm<br>
|
||||
<b> </b>////////////////////////////////////////<br>
|
||||
<b> </b>// Easy EA requires <br>
|
||||
<b> </b>// selection, transformation, eval, replacement, and stopping criterion<br>
|
||||
<b> eoEasyEA<Indi> gga(continuator, eval, select, transform, replace);</b><br>
|
||||
<b> </b>// Apply algo to pop - that's it!<br>
|
||||
<b> gga(pop);</b><br>
|
||||
<b> </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> </b>// Print (sorted) intial population<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> cout << "FINAL Population\n" << pop << 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> </b>// rng.reseed(42);<br>
|
||||
<b> int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</b><br>
|
||||
<b> flag |= _CRTDBG_LEAK_CHECK_DF;</b><br>
|
||||
<b> _CrtSetDbgFlag(flag);</b><br>
|
||||
// _CrtSetBreakAlloc(100);<br>
|
||||
<b>#endif</b><br>
|
||||
<b> try</b><br>
|
||||
<b> {</b><br>
|
||||
<b> main_function(argc, argv);</b><br>
|
||||
<b> }</b><br>
|
||||
<b> catch(exception& e)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> cout << "Exception: " << e.what() << '\n';</b><br>
|
||||
<b> }</b><br>
|
||||
<b> return 1;</b><br>
|
||||
<b>}</b><br>
|
||||
</font></font></font></td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr WIDTH="100%"><a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last
|
||||
modified: Sun Nov 19 22:26:27 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
350
eo/tutorial/html/FirstBitGA.html
Normal file
350
eo/tutorial/html/FirstBitGA.html
Normal file
|
|
@ -0,0 +1,350 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="cpp2html Marc Schoenauer">
|
||||
<title>FirstBitGA.html</title>
|
||||
<!-- Changed by: Marc Schoenauer, 29-Nov-2000 -->
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">FirstBitGA.html</font></h1></center>
|
||||
Click on the figure to see the corresponding code.<br>
|
||||
In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a><br>
|
||||
The actual code is in boldface and the comment in normal face.
|
||||
<br><img SRC="EA_tutorial.jpg" USEMAP="#Map" >
|
||||
<map NAME="Map">
|
||||
<!-- Init -->
|
||||
<area SHAPE="rect" HREF="#init" COORDS="14,31,135,70">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150">
|
||||
<!-- main loop -->
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150">
|
||||
<area SHAPE="rect" HREF="#output" COORDS="280,45,480,70">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150">
|
||||
<area SHAPE="rect" HREF="#select" COORDS="495,110,615,150">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230">
|
||||
<area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287">
|
||||
<area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310">
|
||||
<area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230">
|
||||
<!-- Center of loop -->
|
||||
<area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260">
|
||||
<!-- 4 bottom lines -->
|
||||
<area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370">
|
||||
<area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423">
|
||||
<area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450">
|
||||
<area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450">
|
||||
<area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378">
|
||||
</map>
|
||||
<br>
|
||||
<A NAME="start"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr NOSAVE>
|
||||
<td NOSAVE><tt><font color="#993300">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// FirstBitGA.cpp<br>
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
//*<br>
|
||||
// An instance of a VERY simple Bitstring Genetic Algorithm<br>
|
||||
//<br>
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// standard includes<br>
|
||||
<b>#include <stdexcept> </b>// runtime_error <br>
|
||||
<b>#include <iostream> </b>// cout<br>
|
||||
<b>#include <strstream> </b>// ostrstream, istrstream<br>
|
||||
// the general include for eo<br>
|
||||
<b>#include <eo></b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="representation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#999900">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// define your individuals<br>
|
||||
<b>typedef eoBin<double> Indi; </b>// A bitstring with fitness double<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#CC0000">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// a simple fitness function that computes the number of ones of a bitstring<br>
|
||||
// @param _indi A biststring individual<br>
|
||||
<a name="evalfunc"></a>
|
||||
<b>double binary_value(const Indi & _indi)</b><br>
|
||||
<b>{</b><br>
|
||||
<b> double sum = 0;</b><br>
|
||||
<b> for (unsigned i = 0; i < _indi.size(); i++)</b><br>
|
||||
<b> sum += _indi[i];</b><br>
|
||||
<b> return sum;</b><br>
|
||||
<b>}</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
<b>void main_function(int argc, char **argv)</b><br>
|
||||
<b>{</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="parametres"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>// all parameters are hard-coded!<br>
|
||||
<b> const unsigned int SEED = 42; </b>// seed for random number generator<br>
|
||||
<b> const unsigned int T_SIZE = 3; </b>// size for tournament selection<br>
|
||||
<b> const unsigned int VEC_SIZE = 8; </b>// Number of bits in genotypes<br>
|
||||
<b> const unsigned int POP_SIZE = 20; </b>// Size of population<br>
|
||||
<b> const unsigned int MAX_GEN = 100; </b>// Maximum number of generation before STOP<br>
|
||||
<b> const float CROSS_RATE = 0.8; </b>// Crossover rate<br>
|
||||
<b> const double P_MUT_PER_BIT = 0.01; </b>// probability of bit-flip mutation<br>
|
||||
<b> const float MUT_RATE = 1.0; </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> </b>//////////////////////////<br>
|
||||
<b> </b>// Random seed<br>
|
||||
<b> </b>//////////////////////////<br>
|
||||
<b> </b>//reproducible random seed: if you don't change SEED above, <br>
|
||||
<b> </b>// you'll aways get the same result, NOT a random run<br>
|
||||
<b> 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> </b>/////////////////////////////<br>
|
||||
<b> </b>// Fitness function<br>
|
||||
<b> </b>////////////////////////////<br>
|
||||
<b> </b>// Evaluation: from a plain C++ fn to an EvalFunc Object<br>
|
||||
<b> eoEvalFuncPtr<Indi> eval( 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> </b>////////////////////////////////<br>
|
||||
<b> </b>// Initilisation of population<br>
|
||||
<b> </b>////////////////////////////////<br>
|
||||
<b> </b>// declare the population<br>
|
||||
<b> eoPop<Indi> pop;</b><br>
|
||||
<b> </b>// fill it!<br>
|
||||
<b> for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> Indi v; </b>// void individual, to be filled<br>
|
||||
<b> for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> bool r = rng.flip(); </b>// new value, random in {0,1}<br>
|
||||
<b> v.push_back(r); </b>// append that random value to v<br>
|
||||
<b> }</b><br>
|
||||
<b> eval(v); </b>// evaluate it<br>
|
||||
<b> pop.push_back(v); </b>// and put it in the population<br>
|
||||
<b> }</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> </b>// sort pop before printing it!<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> </b>// Print (sorted) intial population (raw printout)<br>
|
||||
<b> cout << "Initial Population" << endl;</b><br>
|
||||
<b> cout << 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> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// selection and replacement<br>
|
||||
<b> </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> </b>// The robust tournament selection<br>
|
||||
<b> eoDetTournament<Indi> select(T_SIZE); </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> </b>// The simple GA evolution engine uses generational replacement<br>
|
||||
<b> </b>// so no replacement procedure is needed<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="stop"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="operators"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// The variation operators<br>
|
||||
<b> </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> </b>// 1-point mutation for bitstring<br>
|
||||
<b> eoBinCrossover<Indi> xover;</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="mutation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b><br>
|
||||
<b> </b>// standard bit-flip mutation for bitstring<br>
|
||||
<b> eoBinMutation<Indi> mutation(P_MUT_PER_BIT);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="checkpoint"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// termination condition<br>
|
||||
<b> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// stop after MAX_GEN generations<br>
|
||||
<b> eoGenContinue<Indi> continuator(MAX_GEN);</b><br>
|
||||
<b> </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> </b>/////////////////////////////////////////<br>
|
||||
<b> </b>// the algorithm<br>
|
||||
<b> </b>////////////////////////////////////////<br>
|
||||
<b> </b>// standard Generational GA requires as parameters<br>
|
||||
<b> </b>// selection, evaluation, crossover and mutation, stopping criterion<br>
|
||||
<b> </b><br>
|
||||
<b> eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE, </b><br>
|
||||
<b> eval, continuator);</b><br>
|
||||
<b> </b>// Apply algo to pop - that's it!<br>
|
||||
<b> gga(pop);</b><br>
|
||||
<b> </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> </b>// Print (sorted) intial population<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> cout << "FINAL Population\n" << pop << endl;</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="main"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
<b>}</b><br>
|
||||
<b> </b>// A main that catches the exceptions<br>
|
||||
<b>int main(int argc, char **argv)</b><br>
|
||||
<b>{</b><br>
|
||||
<b>#ifdef _MSC_VER</b><br>
|
||||
<b> </b>// rng.reseed(42);<br>
|
||||
<b> int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</b><br>
|
||||
<b> flag |= _CRTDBG_LEAK_CHECK_DF;</b><br>
|
||||
<b> _CrtSetDbgFlag(flag);</b><br>
|
||||
// _CrtSetBreakAlloc(100);<br>
|
||||
<b>#endif</b><br>
|
||||
<b> try</b><br>
|
||||
<b> {</b><br>
|
||||
<b> main_function(argc, argv);</b><br>
|
||||
<b> }</b><br>
|
||||
<b> catch(exception& e)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> cout << "Exception: " << e.what() << '\n';</b><br>
|
||||
<b> }</b><br>
|
||||
<b> return 1;</b><br>
|
||||
<b>}</b><br>
|
||||
</font></font></font></td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr WIDTH="100%"><a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last
|
||||
modified: Sun Nov 19 08:31:26 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
366
eo/tutorial/html/FirstRealEA.html
Normal file
366
eo/tutorial/html/FirstRealEA.html
Normal file
|
|
@ -0,0 +1,366 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="cpp2html Marc Schoenauer">
|
||||
<title>FirstRealEA.cpp</title>
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">FirstRealEA.cpp</font></h1></center>
|
||||
Click on the figure to see the corresponding code.<br>
|
||||
In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a><br>
|
||||
The actual code is in boldface and the comment in normal face.
|
||||
<br><img SRC="EA_tutorial.jpg" USEMAP="#Map" >
|
||||
<map NAME="Map">
|
||||
<!-- Init -->
|
||||
<area SHAPE="rect" HREF="#init" COORDS="14,31,135,70">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150">
|
||||
<!-- main loop -->
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150">
|
||||
<area SHAPE="rect" HREF="#output" COORDS="280,45,480,70">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150">
|
||||
<area SHAPE="rect" HREF="#select" COORDS="495,110,615,150">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230">
|
||||
<area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287">
|
||||
<area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310">
|
||||
<area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230">
|
||||
<!-- Center of loop -->
|
||||
<area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260">
|
||||
<!-- 4 bottom lines -->
|
||||
<area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370">
|
||||
<area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423">
|
||||
<area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450">
|
||||
<area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450">
|
||||
<area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378">
|
||||
</map>
|
||||
<br>
|
||||
<A NAME="start"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr NOSAVE>
|
||||
<td NOSAVE><tt><font color="#993300">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// FirstRealEA.cpp<br>
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
//*<br>
|
||||
// Still an instance of a VERY simple Real-coded Genetic Algorithm <br>
|
||||
// (see FirstBitGA.cpp) but now with Breeder - and Combined Ops<br>
|
||||
//<br>
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// standard includes<br>
|
||||
<b>#include <stdexcept> </b>// runtime_error <br>
|
||||
<b>#include <iostream> </b>// cout<br>
|
||||
<b>#include <strstream> </b>// ostrstream, istrstream<br>
|
||||
// the general include for eo<br>
|
||||
<b>#include <eo></b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="representation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#999900">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// define your individuals<br>
|
||||
<b>typedef eoReal<double> Indi; </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="evalfunc"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#CC0000">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// a simple fitness function that computes the euclidian norm of a real vector<br>
|
||||
// Now in a separate file, and declared as binary_value(const vector<bool> &)<br>
|
||||
<b>#include "real_value.h"</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
<b>void main_function(int argc, char **argv)</b><br>
|
||||
<b>{</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="parametres"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> const unsigned int SEED = 42; </b>// seed for random number generator<br>
|
||||
<b> const unsigned int T_SIZE = 3; </b>// size for tournament selection<br>
|
||||
<b> const unsigned int VEC_SIZE = 8; </b>// Number of object variables in genotypes<br>
|
||||
<b> const unsigned int POP_SIZE = 20; </b>// Size of population<br>
|
||||
<b> const unsigned int MAX_GEN = 500; </b>// Maximum number of generation before STOP<br>
|
||||
<b> const unsigned int MIN_GEN = 10; </b>// Minimum number of generation before ...<br>
|
||||
<b> const unsigned int STEADY_GEN = 50; </b>// stop after STEADY_GEN gen. without improvelent<br>
|
||||
<b> const float P_CROSS = 0.8; </b>// Crossover probability<br>
|
||||
<b> const float P_MUT = 0.5; </b>// mutation probability<br>
|
||||
<b> const double EPSILON = 0.01; </b>// range for real uniform mutation<br>
|
||||
<b> </b>// some parameters for chosing among different operators<br>
|
||||
<b> const double segmentRate = 0.5; </b>// rate for 1-pt Xover<br>
|
||||
<b> const double arithmeticRate = 0.5; </b>// rate for 2-pt Xover<br>
|
||||
<b> const double uniformMutRate = 0.5; </b>// rate for bit-flip mutation<br>
|
||||
<b> const double detMutRate = 0.5; </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> </b>//////////////////////////<br>
|
||||
<b> </b>// Random seed<br>
|
||||
<b> </b>//////////////////////////<br>
|
||||
<b> </b>//reproducible random seed: if you don't change SEED above, <br>
|
||||
<b> </b>// you'll aways get the same result, NOT a random run<br>
|
||||
<b> 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> </b>/////////////////////////////<br>
|
||||
<b> </b>// Fitness function<br>
|
||||
<b> </b>////////////////////////////<br>
|
||||
<b> </b>// Evaluation: from a plain C++ fn to an EvalFunc Object<br>
|
||||
<b> </b>// you need to give the full description of the function<br>
|
||||
<b> eoEvalFuncPtr<Indi, double, const vector<double>& > eval( 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> </b>////////////////////////////////<br>
|
||||
<b> </b>// Initilisation of population<br>
|
||||
<b> </b>////////////////////////////////<br>
|
||||
<b> </b>// based on a uniform generator<br>
|
||||
<b> eoInitFixedLength<Indi, uniform_generator<double> ></b><br>
|
||||
<b> random(VEC_SIZE, uniform_generator<double>(-1.0, 1.0));</b><br>
|
||||
<b> </b>// Initialization of the population<br>
|
||||
<b> eoPop<Indi> pop(POP_SIZE, random);</b><br>
|
||||
<b> </b><br>
|
||||
<b> </b>// and evaluate it in one loop<br>
|
||||
<b> apply<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> </b>// sort pop before printing it!<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> </b>// Print (sorted) intial population (raw printout)<br>
|
||||
<b> cout << "Initial Population" << endl;</b><br>
|
||||
<b> cout << 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> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// selection and replacement<br>
|
||||
<b> </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> </b>// The robust tournament selection<br>
|
||||
<b> eoDetTournament<Indi> selectOne(T_SIZE);</b><br>
|
||||
<a NAME="select_encapsulate"></a>
|
||||
<b> </b>// is now encapsulated in a eoSelectPerc (entage)<br>
|
||||
<b> eoSelectPerc<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> </b>// And we now have the full slection/replacement - though with <br>
|
||||
<b> </b>// no replacement (== generational replacement) at the moment :-)<br>
|
||||
<b> eoNoReplacement<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> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// The variation operators<br>
|
||||
<b> </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> </b>// uniform chooce on segment made by the parents<br>
|
||||
<b> eoSegmentCrossover<Indi> xoverS;</b><br>
|
||||
<b> </b>// uniform choice in hypercube built by the parents<br>
|
||||
<b> eoArithmeticCrossover<Indi> xoverA;</b><br>
|
||||
<b> </b>// Combine them with relative rates<br>
|
||||
<b> eoPropCombinedQuadOp<Indi> xover(xoverS, segmentRate);</b><br>
|
||||
<b> xover.add(xoverA, arithmeticRate, true);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="mutation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b><br>
|
||||
<b> </b>// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]<br>
|
||||
<b> eoUniformMutation<Indi> mutationU(EPSILON); </b><br>
|
||||
<b> </b>// k (=1) coordinates of parents are uniformly modified<br>
|
||||
<b> eoDetUniformMutation<Indi> mutationD(EPSILON); </b><br>
|
||||
<b> </b>// Combine them with relative rates<br>
|
||||
<b> eoPropCombinedMonOp<Indi> mutation(mutationU, uniformMutRate);</b><br>
|
||||
<b> mutation.add(mutationD, detMutRate, true);</b><br>
|
||||
<b> </b><br>
|
||||
<b> </b>// The operators are encapsulated into an eoTRansform object<br>
|
||||
<b> eoSGATransform<Indi> transform(xover, P_CROSS, mutation, P_MUT);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="stop"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="checkpoint"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// termination conditions: use more than one<br>
|
||||
<b> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// stop after MAX_GEN generations<br>
|
||||
<b> eoGenContinue<Indi> genCont(MAX_GEN);</b><br>
|
||||
<b> </b>// do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement<br>
|
||||
<b> eoSteadyFitContinue<Indi> steadyCont(MIN_GEN, STEADY_GEN);</b><br>
|
||||
<b> </b>// stop when fitness reaches a target (here VEC_SIZE)<br>
|
||||
<b> eoFitContinue<Indi> fitCont(0);</b><br>
|
||||
<b> </b>// do stop when one of the above says so<br>
|
||||
<b> eoCombinedContinue<Indi> continuator(genCont);</b><br>
|
||||
<b> continuator.add(steadyCont);</b><br>
|
||||
<b> 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> </b>/////////////////////////////////////////<br>
|
||||
<b> </b>// the algorithm<br>
|
||||
<b> </b>////////////////////////////////////////<br>
|
||||
<b> </b>// Easy EA requires <br>
|
||||
<b> </b>// selection, transformation, eval, replacement, and stopping criterion<br>
|
||||
<b> eoEasyEA<Indi> gga(continuator, eval, select, transform, replace);</b><br>
|
||||
<b> </b>// Apply algo to pop - that's it!<br>
|
||||
<b> cout << "\n Here we go\n\n";</b><br>
|
||||
<b> gga(pop);</b><br>
|
||||
<b> </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> </b>// Print (sorted) intial population<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> cout << "FINAL Population\n" << pop << 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> </b>// rng.reseed(42);<br>
|
||||
<b> int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</b><br>
|
||||
<b> flag |= _CRTDBG_LEAK_CHECK_DF;</b><br>
|
||||
<b> _CrtSetDbgFlag(flag);</b><br>
|
||||
// _CrtSetBreakAlloc(100);<br>
|
||||
<b>#endif</b><br>
|
||||
<b> try</b><br>
|
||||
<b> {</b><br>
|
||||
<b> main_function(argc, argv);</b><br>
|
||||
<b> }</b><br>
|
||||
<b> catch(exception& e)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> cout << "Exception: " << e.what() << '\n';</b><br>
|
||||
<b> }</b><br>
|
||||
<b> return 1;</b><br>
|
||||
<b>}</b><br>
|
||||
</font></font></font></td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr WIDTH="100%"><a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last
|
||||
modified: Wed Nov 29 07:38:36 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
349
eo/tutorial/html/FirstRealGA.html
Normal file
349
eo/tutorial/html/FirstRealGA.html
Normal file
|
|
@ -0,0 +1,349 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="cpp2html Marc Schoenauer">
|
||||
<title>../FirstRealGA.html</title>
|
||||
<!-- Changed by: Marc Schoenauer, 29-Nov-2000 -->
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">../FirstRealGA.html</font></h1></center>
|
||||
Click on the figure to see the corresponding code.<br>
|
||||
In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a><br>
|
||||
The actual code is in boldface and the comment in normal face.
|
||||
<br><img SRC="EA_tutorial.jpg" USEMAP="#Map" >
|
||||
<map NAME="Map">
|
||||
<!-- Init -->
|
||||
<area SHAPE="rect" HREF="#init" COORDS="14,31,135,70">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150">
|
||||
<!-- main loop -->
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150">
|
||||
<area SHAPE="rect" HREF="#output" COORDS="280,45,480,70">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150">
|
||||
<area SHAPE="rect" HREF="#select" COORDS="495,110,615,150">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230">
|
||||
<area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287">
|
||||
<area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310">
|
||||
<area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230">
|
||||
<!-- Center of loop -->
|
||||
<area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260">
|
||||
<!-- 4 bottom lines -->
|
||||
<area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370">
|
||||
<area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423">
|
||||
<area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450">
|
||||
<area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450">
|
||||
<area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378">
|
||||
</map>
|
||||
<br>
|
||||
<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 <stdexcept> </b>// runtime_error <br>
|
||||
<b>#include <iostream> </b>// cout<br>
|
||||
<b>#include <strstream> </b>// ostrstream, istrstream<br>
|
||||
// the general include for eo<br>
|
||||
<b>#include <eo></b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="representation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#999900">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// define your individuals<br>
|
||||
<b> typedef eoReal<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>
|
||||
// @param _indi A real-valued individual <br>
|
||||
<a name="evalfunc"></a>
|
||||
<b>double real_value(const Indi & _indi)</b><br>
|
||||
<b>{</b><br>
|
||||
<b> double sum = 0;</b><br>
|
||||
<b> for (unsigned i = 0; i < _indi.size(); i++)</b><br>
|
||||
<b> sum += _indi[i]*_indi[i];</b><br>
|
||||
<b> return (-sum); </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> </b>// all parameters are hard-coded!<br>
|
||||
<b> const unsigned int SEED = 42; </b>// seed for random number generator<br>
|
||||
<b> const unsigned int VEC_SIZE = 8; </b>// Number of object variables in genotypes<br>
|
||||
<b> const unsigned int POP_SIZE = 20; </b>// Size of population<br>
|
||||
<b> const unsigned int T_SIZE = 3; </b>// size for tournament selection<br>
|
||||
<b> const unsigned int MAX_GEN = 500; </b>// Maximum number of generation before STOP<br>
|
||||
<b> const float CROSS_RATE = 0.8; </b>// Crossover rate<br>
|
||||
<b> const double EPSILON = 0.01; </b>// range for real uniform mutation<br>
|
||||
<b> const float MUT_RATE = 0.5; </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> </b>//////////////////////////<br>
|
||||
<b> </b>// Random seed<br>
|
||||
<b> </b>//////////////////////////<br>
|
||||
<b> </b>//reproducible random seed: if you don't change SEED above, <br>
|
||||
<b> </b>// you'll aways get the same result, NOT a random run<br>
|
||||
<b> 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> </b>/////////////////////////////<br>
|
||||
<b> </b>// Fitness function<br>
|
||||
<b> </b>////////////////////////////<br>
|
||||
<b> </b>// Evaluation: from a plain C++ fn to an EvalFunc Object<br>
|
||||
<b> eoEvalFuncPtr<Indi> eval( 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> </b>////////////////////////////////<br>
|
||||
<b> </b>// Initilisation of population<br>
|
||||
<b> </b>////////////////////////////////<br>
|
||||
<b> </b>// declare the population<br>
|
||||
<b> eoPop<Indi> pop;</b><br>
|
||||
<b> </b>// fill it!<br>
|
||||
<b> for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> Indi v; </b>// void individual, to be filled<br>
|
||||
<b> for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> double r = 2*rng.uniform() - 1; </b>// new value, random in [-1,1)<br>
|
||||
<b> v.push_back(r); </b>// append that random value to v<br>
|
||||
<b> }</b><br>
|
||||
<b> eval(v); </b>// evaluate it<br>
|
||||
<b> pop.push_back(v); </b>// and put it in the population<br>
|
||||
<b> }</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> </b>// sort pop before printing it!<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> </b>// Print (sorted) intial population (raw printout)<br>
|
||||
<b> cout << "Initial Population" << endl;</b><br>
|
||||
<b> cout << 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> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// selection and replacement<br>
|
||||
<b> </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> </b>// The robust tournament selection<br>
|
||||
<b> eoDetTournament<Indi> select(T_SIZE); </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> </b>// eoSGA uses generational replacement by default<br>
|
||||
<b> </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> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// termination condition<br>
|
||||
<b> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// stop after MAX_GEN generations<br>
|
||||
<b> eoGenContinue<Indi> continuator(MAX_GEN);</b><br>
|
||||
<b> </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> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// The variation operators<br>
|
||||
<b> </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> </b>// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]<br>
|
||||
<b> eoUniformMutation<Indi> 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> </b>// offspring(i) is a linear combination of parent(i)<br>
|
||||
<b> eoArithmeticCrossover<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> </b>/////////////////////////////////////////<br>
|
||||
<b> </b>// the algorithm<br>
|
||||
<b> </b>////////////////////////////////////////<br>
|
||||
<b> </b>// standard Generational GA requires<br>
|
||||
<b> </b>// selection, evaluation, crossover and mutation, stopping criterion<br>
|
||||
<b> </b><br>
|
||||
<b> eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE, </b><br>
|
||||
<b> eval, continuator);</b><br>
|
||||
<b> </b>// Apply algo to pop - that's it!<br>
|
||||
<b> gga(pop);</b><br>
|
||||
<b> </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> </b>// Print (sorted) intial population<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> cout << "FINAL Population\n" << pop << 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> </b>// rng.reseed(42);<br>
|
||||
<b> int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</b><br>
|
||||
<b> flag |= _CRTDBG_LEAK_CHECK_DF;</b><br>
|
||||
<b> _CrtSetDbgFlag(flag);</b><br>
|
||||
// _CrtSetBreakAlloc(100);<br>
|
||||
<b>#endif</b><br>
|
||||
<b> try</b><br>
|
||||
<b> {</b><br>
|
||||
<b> main_function(argc, argv);</b><br>
|
||||
<b> }</b><br>
|
||||
<b> catch(exception& e)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> cout << "Exception: " << e.what() << '\n';</b><br>
|
||||
<b> }</b><br>
|
||||
<b> return 1;</b><br>
|
||||
<b>}</b><br>
|
||||
</font></font></font></td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr WIDTH="100%"><a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last
|
||||
modified: Sun Nov 19 08:31:29 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
278
eo/tutorial/html/FirstRealGA_old.html
Normal file
278
eo/tutorial/html/FirstRealGA_old.html
Normal file
|
|
@ -0,0 +1,278 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.51 [en] (X11; I; Linux 2.2.5-15 i686) [Netscape]">
|
||||
<title>First Real GA</title>
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">A First Real GA</font></h1></center>
|
||||
Click on the figure to see the corresponding code. Tutorial comments are
|
||||
in variable length fonts, after the code.
|
||||
<center>
|
||||
<p><img SRC="EA_tutorial.jpg" USEMAP="#Map" ></center>
|
||||
<map NAME="Map"><area SHAPE="rect" HREF="#init" COORDS="0,0,112,40"><area SHAPE="rect" HREF="#eval" COORDS="0,78,112,112"><area SHAPE="rect" HREF="#select" COORDS="395,70,507,107"><area SHAPE="rect" HREF="#replace" COORDS="145,144,253,182"><area SHAPE="rect" HREF="#crossover" COORDS="433,145,543,164"><area SHAPE="rect" HREF="#mutation" COORDS="433,164,543,182"><area SHAPE="rect" HREF="#eval" COORDS="218,217,325,252"><area SHAPE="rect" HREF="#output" COORDS="235,14,415,38"><area SHAPE="rect" HREF="#representation" COORDS="143,68,253,114"><area SHAPE="rect" HREF="#representation" COORDS="394,214,515,255"><area SHAPE="rect" HREF="#stop" COORDS="278,74,370,109"><area SHAPE="rect" HREF="#generation" COORDS="257,114,431,213"><!-- 4 lignes en bas --><area SHAPE="rect" HREF="#eval" COORDS="0,334,207,352"><area SHAPE="rect" HREF="#engine" COORDS="0,310,356,334"><area SHAPE="rect" HREF="#operators" COORDS="0,288,230,310"><area SHAPE="rect" HREF="#representation" COORDS="230,288,450,310"><area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378"></map>
|
||||
<p><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
|
||||
<p><font face="Courier New,Courier"><font color="#993300"><font size=+1>#include
|
||||
<stdexcept > // runtime_error</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// FirstRealGA.cpp</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>//*</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// An instance of a
|
||||
VERY simple Real-coded Genetic Algorithm</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>//</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// standard includes</font></font>
|
||||
<p><font face="Courier New,Courier"><font color="#993300"><font size=+1>#include
|
||||
<iostream>// cout</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>#include
|
||||
<strstream>// ostrstream, istrstream</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>// the general include
|
||||
for eo</font></font>
|
||||
<p><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>#include
|
||||
<eo></font></font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>// specific incluse,
|
||||
as Real is not (yet) in the standard eo source dir</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>#include
|
||||
"eoeal.h"</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>#include
|
||||
"eoRealOp.h"</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// define your individual:</font></font>
|
||||
<p><a NAME="representation"></a><font face="Courier New,Courier"><font color="#FFFF00"><font size=+1>typedef
|
||||
eoReal<double> Indi;</font></font></font>
|
||||
<p>You say here that you will be handling <font face="Arial,Helvetica">arrays
|
||||
of doubles</font>, whose fitness is a double
|
||||
<br>Note that this makes Indi derive from <a href="eoProgramming.html#STL">STL
|
||||
class</a> <font color="#993300">vector<double></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>/** a simple fitness
|
||||
function that computes the euclidian norm of a real vector</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1> @param
|
||||
_indi A real-valued individual</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>*/</font></font>
|
||||
<p><a NAME="evalfunc"></a><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>double
|
||||
real_value(const Indi & _indi)</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>{</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>
|
||||
double sum = 0;</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>
|
||||
for (unsigned i = 0; i < _indi.size(); i++)</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>
|
||||
sum += _indi[i]*_indi[i];</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>
|
||||
return (-sum); // maximizing</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>}</font></font></font>
|
||||
<p>This simple function computes the sum of the squares of the variables
|
||||
(remember Indi is here a vector of doubles)
|
||||
<br><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
|
||||
<p><font face="Courier New,Courier"><font color="#993300"><font size=+1>void
|
||||
main_function(int argc, char **argv)</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>{</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
|
||||
unsigned int SEED = 42;// seed for random number generator</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
|
||||
unsigned int VEC_SIZE = 8; // Number of object variables in genotypes</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
|
||||
unsigned int POP_SIZE = 20; // Size of population</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
|
||||
unsigned int T_SIZE = 3; // size for tournament selection</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
|
||||
unsigned int MAX_GEN = 500; // Maximum number of generation before STOP</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
|
||||
float CROSS_RATE = 0.8; // Crossover rate</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
|
||||
double EPSILON = 0.01; // range for real uniform mutation</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
|
||||
float MUT_RATE = 0.5; // mutation rate</font></font></font>
|
||||
<br>
|
||||
<p><font face="Courier New,Courier"><font size=+1>//////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// Random seed</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>//////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>//reproducible random
|
||||
seed: if you don't change SEED above,</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// you'll aways get
|
||||
the same result, NOT a random run</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>rng.reseed(SEED);</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>/////////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// Fitness function</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>////////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// Evaluation: from
|
||||
a plain C++ fn to an EvalFunc Object</font></font>
|
||||
<br><a NAME="eval"></a><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>eoEvalFuncPtr<Indi>
|
||||
eval( <a href="#evalfunc">real_value</a> );</font></font></font>
|
||||
<p>This encapsulate the C++ function real_value into a <a href="eoProgramming.html#functors">functor
|
||||
object</a>
|
||||
<p><font face="Courier New,Courier"><font size=+1>////////////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// Initilisation of
|
||||
population</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>////////////////////////////////</font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>// declare the population</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>eoPop<Indi>
|
||||
pop;</font></font></font>
|
||||
<p>Declares a <font face="Arial,Helvetica"><a href="doc/html/class_eopop.html">population
|
||||
object</a>, </font>which is basically an <a href="eoProgramming.html#STL">STL</a>
|
||||
<tt><font color="#993300"><font size=+1>vector<Indi></font></font></tt>
|
||||
<p><font face="Courier New,Courier"><font size=+1>// fill it!</font></font>
|
||||
<br><a NAME="init"></a><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>for
|
||||
(unsigned int igeno=0; igeno<POP_SIZE; igeno++)</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
{</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
Indi v; // generate a random individual</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
{</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
double r = 2*rng.uniform() - 1; // new value, random in [-1,1)</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
v.push_back(r); //</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
}</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
eval(v); // evaluate it</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
pop.push_back(v); // and put it in the population</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
}</font></font></font>
|
||||
<p>This initialization of the population is straightforward
|
||||
<br><font color="#CC33CC">rng.uniform()</font> generates a double <font face="Arial,Helvetica"><a href="doc/html/class_eorng.html">uniformly
|
||||
in [0,1)</a>,</font>
|
||||
<br><font color="#CC33CC">v.push_back</font> simply appends its argument
|
||||
at the end of <a href="eoProgramming.html#STL">STL</a> vector <font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>v</font></font></font>
|
||||
<br><font color="#CC33CC">eval(v)</font>evaluates individal <font color="#CC33CC">v</font>
|
||||
the <a href="eoProgramming.html#functors">functor way</a>, calling <font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>eval.operator()(v)</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>// sort pop before printing
|
||||
it!</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>pop.sort();</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>// Print (sorted) intial
|
||||
population (raw printout)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>cout
|
||||
<< "Initial Population" << endl;</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>cout
|
||||
<< pop;</font></font></font>
|
||||
<p>If you looked at <font face="Arial,Helvetica"><a href="doc/html/class_eopop.html">eoPop</a></font>
|
||||
inheritance diagram, you noticed that it derives from <font face="Arial,Helvetica"><a href="doc/html/class_eoprintable.html">eoPrintable</a>:</font>
|
||||
hence you can stream them using the << operator. Of course, Indis
|
||||
are <font face="Arial,Helvetica"><a href="doc/html/class_eo.html">EO objects</a>
|
||||
</font>, which also are eoPrintable, and the << operator for eoPop
|
||||
uses the << operator for Indi.
|
||||
<p><font face="Courier New,Courier"><font size=+1>/////////////////////////////////////</font></font>
|
||||
<br><a NAME="engine"></a><font face="Courier New,Courier"><font size=+1>//
|
||||
selection and replacement</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>////////////////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// The well-known roulette
|
||||
selection</font></font>
|
||||
<br><a NAME="select"></a><font face="Courier New,Courier"><font size=+1>//
|
||||
The robust tournament selection</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#009900"><font size=+1>eoDetTournament<Indi>
|
||||
select(T_SIZE); // SIZE in [2,POP_SIZE]</font></font></font>
|
||||
<p><a NAME="replace"></a><font face="Courier New,Courier"><font size=+1>//
|
||||
eoSGA uses <font color="#009900">generational replacement</font> by default</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// so no replacement
|
||||
procedure has to be given</font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>//////////////////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// termination condition</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>/////////////////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// stop after MAX_GEN
|
||||
generations</font></font>
|
||||
<br><a NAME="stop"></a><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>eoGenContinue<Indi>
|
||||
continuator(MAX_GEN);</font></font></font>
|
||||
<p>This class is called <font face="Arial,Helvetica"><a href="doc/html/class_eogencontinue.html">eoGenContinue</a>
|
||||
</font>because the main loop of all <font face="Arial,Helvetica"><a href="doc/html/class_eoalgo.html">eoAlgo</a>
|
||||
</font>says ... while continuator(pop)
|
||||
<p><font face="Courier New,Courier"><font size=+1>//////////////////////////////////////</font></font>
|
||||
<br><a NAME="operators"></a><font face="Courier New,Courier"><font size=+1>//
|
||||
The variation operators</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>//////////////////////////////////////</font></font>
|
||||
<p><a NAME="mutation"></a><font face="Courier New,Courier"><font size=+1>//
|
||||
offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>eoUniformMutation<Indi>
|
||||
mutation(EPSILON);</font></font></font>
|
||||
<br><a NAME="crossover"></a><font face="Courier New,Courier"><font size=+1>//
|
||||
offspring(i) is a linear combination of parent(i)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>eoArithmeticCrossover<Indi>
|
||||
xover;</font></font></font>
|
||||
<p>The <a href="eoOperators.html">variation operators</a> are respectively
|
||||
an <font face="Arial,Helvetica"><a href="doc/html/class_eomonop.html">eoMonOp</a></font>
|
||||
(unary operator) and an <font face="Arial,Helvetica"><a href="doc/html/class_eoquadraticop.html">eoQuadOp</a></font>
|
||||
(binary operator that modifies both its arguments).
|
||||
<p><font face="Courier New,Courier"><font size=+1>/////////////////////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// the algorithm</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>////////////////////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// standard Generational
|
||||
GA requires</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// selection, evaluation,
|
||||
crossover and mutation, stopping criterion</font></font>
|
||||
<br>
|
||||
<p><a NAME="generation"></a><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>eoSGA<Indi>
|
||||
gga(select, xover, CROSS_RATE, mutation, MUT_RATE,</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>eval,
|
||||
continuator);</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>// Apply algo to pop
|
||||
- that's it!</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>gga(pop);</font></font></font>
|
||||
<p>This simple algorithm of class eoSGA is a <a href="eoProgramming.html#functors">functor
|
||||
object</a>, and running the algorithm amounts to call the <font color="#993300">operator()</font>
|
||||
on the initial population!
|
||||
<p><font face="Courier New,Courier"><font size=+1>// Print (sorted) intial
|
||||
population (raw printout)</font></font>
|
||||
<br><a NAME="output"></a><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>pop.sort();</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>cout
|
||||
<< "FINAL Population\n" << pop << endl;</font></font></font>
|
||||
<p>that's it - just print the final population and you're done!!!
|
||||
<p><font face="Courier New,Courier"><font size=+1>}</font></font>
|
||||
<p><font face="Courier New,Courier"><font color="#000000"><font size=+1>//
|
||||
A main that catches the exceptions - do not modify!</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>int
|
||||
main(int argc, char **argv)</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>{</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>#ifdef
|
||||
_MSC_VER</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>//
|
||||
rng.reseed(42);</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>int
|
||||
flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>flag
|
||||
|= _CRTDBG_LEAK_CHECK_DF;</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>_CrtSetDbgFlag(flag);</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>//
|
||||
_CrtSetBreakAlloc(100);</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>#endif</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font color="#993300"><font size=+1>try</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>{</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>main_function(argc,
|
||||
argv);</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>}</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>catch(exception&
|
||||
e)</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>{</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>cout
|
||||
<< "Exception: " << e.what() << '\n';</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>}</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font color="#993300"><font size=+1>return
|
||||
1;</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>}</font></font></font>
|
||||
<p>
|
||||
<hr WIDTH="100%"><a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Wed Nov 1 07:18:21 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Wed Nov 6 17:22:43 CET 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
287
eo/tutorial/html/Firstmerge.html
Normal file
287
eo/tutorial/html/Firstmerge.html
Normal file
|
|
@ -0,0 +1,287 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdksmp i686) [Netscape]">
|
||||
<title>Differences</title>
|
||||
</head>
|
||||
<body text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000" background="beige009.jpg">
|
||||
<a href="eoLesson1.html">Back to Lesson 1</a> -
|
||||
<a href="eoTutorial.html">Tutorial
|
||||
main page</a> -
|
||||
<a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <a href="doc/html/index.html">EO
|
||||
documentation</a>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">FirstBitGA and FirstRealGA: differences</font></h1></center>
|
||||
Below is a comparison of the codes for both algorithms (comments have been
|
||||
removed).
|
||||
<br><b><font color="#FF6600">Warning</font></b>: the <font color="#FF6666">pink
|
||||
background</font> here denotes the differences, not the section 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
|
||||
<stdexcept ></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">#include <iostream></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">#include <strstream></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666">#include <eo></font></font></td>
|
||||
|
||||
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#993300">#include
|
||||
<stdexcept ></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">#include <iostream></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">#include <strstream></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666">#include <eo></font></font></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#FFFF00">typedef
|
||||
eoReal<double> Indi;</font></font></td>
|
||||
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#FFFF00">typedef
|
||||
eoBin<double> Indi;</font></font></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC0000">double
|
||||
real_value(const Indi & _indi)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000">{</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"> double
|
||||
sum = 0;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"> for (unsigned
|
||||
i = 0; i < _indi.size(); i++)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000">
|
||||
sum += _indi[i]*_indi[i];</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"> return
|
||||
(-sum); // 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 & _indi)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000">{</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"> double
|
||||
sum = 0;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"> for (unsigned
|
||||
i = 0; i < _indi.size(); i++)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000">
|
||||
sum += _indi[i];</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"> 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<Indi>
|
||||
eval(real_value);</font></font></td>
|
||||
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC0000">eoEvalFuncPtr<Indi>
|
||||
eval(binary_value);</font></font></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#CC33CC">eoPop<Indi>
|
||||
pop;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">for (unsigned
|
||||
int igeno=0; igeno<POP_SIZE; igeno++)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"> {</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
Indi v;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
{</font></font></td>
|
||||
|
||||
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#CC33CC">eoPop<Indi>
|
||||
pop;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">for (unsigned
|
||||
int igeno=0; igeno<POP_SIZE; igeno++)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"> {</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
Indi v;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
{</font></font></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
double r = 2*rng.uniform() - 1;</font></font></td>
|
||||
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
bool r = rng.flip(); </font></font></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
v.push_back(r); //</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
}</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
eval(v);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
pop.push_back(v);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"> }</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 <<
|
||||
"Initial Population" << endl;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">cout <<
|
||||
pop;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#009900">eoDetTournament<Indi>
|
||||
select(T_SIZE);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">eoGenContinue<Indi>
|
||||
continuator(MAX_GEN);</font></font></td>
|
||||
|
||||
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
v.push_back(r); //</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
}</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
eval(v);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
pop.push_back(v);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"> }</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 <<
|
||||
"Initial Population" << endl;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">cout <<
|
||||
pop;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#009900">eoDetTournament<Indi>
|
||||
select(T_SIZE);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">eoGenContinue<Indi>
|
||||
continuator(MAX_GEN);</font></font></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC33CC">eoUniformMutation<Indi>
|
||||
mutation(EPSILON);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">eoArithmeticCrossover<Indi>
|
||||
xover;</font></font></td>
|
||||
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC33CC">eoBinMutation<Indi>
|
||||
mutation(P_MUT_PER_BIT);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">eoBinCrossover<Indi>
|
||||
xover;</font></font></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td BGCOLOR="#C3C2B4">
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666">eoSGA<Indi>
|
||||
gga(select, xover, CROSS_RATE,</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666">
|
||||
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 <<
|
||||
"FINAL Population\n" << pop << endl;</font></font>
|
||||
<br><font face="Courier New,Courier">}</font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">int main(int
|
||||
argc, char **argv)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">{</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">... [technical
|
||||
code removed]</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">}</font></font></td>
|
||||
|
||||
<td BGCOLOR="#C3C2B4">
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666">eoSGA<Indi>
|
||||
gga(select, xover, CROSS_RATE,</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666">
|
||||
mutation, MUT_RATE,</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666">eval, continuator);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666">gga(pop);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">pop.sort();</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">cout <<
|
||||
"FINAL Population\n" << pop << endl;</font></font>
|
||||
<br><font face="Courier New,Courier">}</font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">int main(int
|
||||
argc, char **argv)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">{</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">[... technical
|
||||
code removed ]</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">}</font></font></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<hr WIDTH="100%"><a href="eoLesson1.html">Back to Lesson 1</a> -
|
||||
<a href="eoTutorial.html">Tutorial
|
||||
main page</a> -
|
||||
<a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <a href="doc/html/index.html">EO
|
||||
documentation</a>
|
||||
<br>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Tue Nov 7 07:04:15 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Tue Nov 7 07:49:47 CET 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
74
eo/tutorial/html/NoWay.html
Normal file
74
eo/tutorial/html/NoWay.html
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdksmp i686) [Netscape]">
|
||||
<title>Tutorial: Solutions</title>
|
||||
</head>
|
||||
<body text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000" background="beige009.jpg">
|
||||
<a href="eoTutorial.html">Tutorial main page</a> -
|
||||
<a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> -<font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">Tutorial:</font></h1></center>
|
||||
|
||||
<center><b><font color="#FF0000"><font size=+2>Solutions of exercises</font></font></b></center>
|
||||
|
||||
<p>No, <b><font color="#33CC00">w</font><font color="#FF9900">e </font><font color="#CC66CC">w</font><font color="#33CCFF">o</font><font color="#FFFF00">n</font><font color="#FF0000">'t</font><font color="#999999">
|
||||
p</font><font color="#999900">r</font><font color="#CC6600">o</font><font color="#993399">v</font><font color="#006600">i</font><font color="#990000">d</font><font color="#867CE9">e
|
||||
</font></b><font color="#000000">any
|
||||
hypertext link directly to the correct code, so</font> you get a chance
|
||||
to find out the solution by yourself without immediately going there
|
||||
:-)))
|
||||
<p><b><font color="#000099"><font size=+2>What you should do:</font></font></b>
|
||||
<ul>
|
||||
<li>
|
||||
copy the code of an example onto another name (e.g. <b><tt><font color="#660000">mytest.cpp</font></tt></b>)</li>
|
||||
|
||||
<li>
|
||||
edit and modify <b><tt><font color="#660000">mytest.cpp</font></tt></b>
|
||||
according to the corresponding instructions of the exercise</li>
|
||||
|
||||
<li>
|
||||
compile <b><tt><font color="#660000">mytest.cpp</font></tt></b> by typing
|
||||
<b><tt><font color="#FF6666">make mytest</font></tt></b> at system prompt,
|
||||
and you will hopefully get an executable file named <b><tt><font color="#660000">mytest</font></tt></b>
|
||||
(for Unix systems: if eventually someone tells me how to do that in Windows,
|
||||
apart that you'll probably end up with an executable file named <b><tt><font color="#660000">mytest.exe...</font></tt></b>).</li>
|
||||
</ul>
|
||||
|
||||
<hr WIDTH="100%">
|
||||
<br><b><font color="#000099"><font size=+2>What you may do later:</font></font></b>
|
||||
<p>All solutions to exercises are in the same<b><font color="#FF6600">
|
||||
sub-dir</font></b> of the Tutorial directory than the example files (i.e.
|
||||
Lesson1 for Lesson1, yes!). Hence you may browse through the code, eventually
|
||||
modifying it, and then simply type, in the corresponding directory,
|
||||
<br><b><tt><font color="#FF6666">... % make exerciseN</font></tt></b>
|
||||
<br>which will compile file <b><tt><font color="#660000">exerciseN.cpp</font></tt></b>
|
||||
into executable <b><tt><font color="#660000">exerciseN</font></tt></b>
|
||||
(for Unix systems: if eventually someone tells me how to do that in Windows,
|
||||
you'll probably end up with an executable file named <b><tt><font color="#660000">exerciseN.exe</font></tt></b>).
|
||||
<p>
|
||||
<hr WIDTH="100%">
|
||||
<br><b><font color="#000099"><font size=+2>What you may not do:</font></font></b>
|
||||
<center>Complain that it does not work under Windows :-)</center>
|
||||
|
||||
<hr WIDTH="100%"><a href="eoTutorial.html">Tutorial main page</a> -
|
||||
<a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font>
|
||||
<br>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:Marc.Schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Fri Nov 3 18:49:12 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Fri Nov 3 18:49:12 CET 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
612
eo/tutorial/html/SecondBitEA.html
Normal file
612
eo/tutorial/html/SecondBitEA.html
Normal file
|
|
@ -0,0 +1,612 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdk i686) [Netscape]">
|
||||
<title>SecondBitEA.cpp</title>
|
||||
<!-- Changed by: Marc Schoenauer, 29-Nov-2000 -->
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoLesson3.html">Back to Lesson 3</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">SecondBitEA.cpp</font></h1></center>
|
||||
Click on the figure to see the corresponding code.
|
||||
<br>In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a>
|
||||
<br>The actual code is in boldface and the comment in normal face.
|
||||
<p>For this particular program, as all new lines are concerned with program
|
||||
parameter input and information output, the <font color="#FF6600">font
|
||||
color</font> for program-parameter-related sections will <font color="#FF6600">refer
|
||||
to the section where the parameters are used</font> whereas the background
|
||||
color will remain blue.
|
||||
<p><img SRC="EA_tutorial.jpg" USEMAP="#Map" ><map NAME="Map"><!-- Init --><area SHAPE="rect" HREF="#init" COORDS="14,31,135,70"><area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150"><!-- main loop --><area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150"><area SHAPE="rect" HREF="#output" COORDS="280,45,480,70"><area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150"><area SHAPE="rect" HREF="#select" COORDS="495,110,615,150"><area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230"><area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287"><area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305"><area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310"><area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310"><area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230"><!-- Center of loop --><area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260"><!-- 4 bottom lines --><area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370"><area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370"><area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397"><area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423"><area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450"><area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450"><area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450"><area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378"></map>
|
||||
<br><a NAME="start"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr NOSAVE>
|
||||
<td NOSAVE><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#993300">// SecondGA.cpp</font></tt>
|
||||
<br><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#993300">//*</font></tt>
|
||||
<br><tt><font color="#993300">// Same code than FirstBitEA as far as Evolutionary
|
||||
Computation is concerned</font></tt>
|
||||
<br><tt><font color="#993300">// but now you learn to enter the parameters
|
||||
in a more flexible way</font></tt>
|
||||
<br><tt><font color="#993300">// and to twidle the output to your preferences!</font></tt>
|
||||
<br><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#993300">// standard includes</font></tt>
|
||||
<br><tt><font color="#993300"><b>#include <stdexcept> </b>// runtime_error </font></tt>
|
||||
<br><tt><font color="#993300"><b>#include <iostream> </b>//
|
||||
cout</font></tt>
|
||||
<br><tt><font color="#993300"><b>#include <strstream> </b>// ostrstream,
|
||||
istrstream</font></tt>
|
||||
<br><b><tt><font color="#993300">#include <fstream></font></tt></b>
|
||||
<br><tt><font color="#993300">// the general include for eo</font></tt>
|
||||
<br><b><tt><font color="#993300">#include <eo></font></tt></b>
|
||||
<br><tt><font color="#993300">// specific includes</font></tt>
|
||||
<br><b><tt><font color="#993300">#include "eoSGATransform.h"</font></tt></b>
|
||||
<br><b><tt><font color="#993300">#include "eoPropCombinedOp.h"</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="evalfunc"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td><b><tt><font color="#CC0000">#include "binary_value.h"</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="representation"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#999900">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#999900">// define your genotype and fitness types</font></tt>
|
||||
<br><b><tt><font color="#999900">typedef eoBin<double> Indi;</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="parametres"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#3366FF">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#3366FF">// instead of having all values of useful
|
||||
parameters as constants, read them:</font></tt>
|
||||
<br><tt><font color="#3366FF">// either on the command line (--option=value
|
||||
or -o=value)</font></tt>
|
||||
<br><tt><font color="#3366FF">//
|
||||
or in a parameter file (same syntax, order independent, </font></tt>
|
||||
<br><tt><font color="#3366FF">//
|
||||
# = usual comment character </font></tt>
|
||||
<br><tt><font color="#3366FF">//
|
||||
or in the environment (TODO)</font></tt>
|
||||
<br><tt><font color="#3366FF">// note that the parameters are passed by
|
||||
reference so they can be updated</font></tt>
|
||||
<br><b><tt><font color="#3366FF">void read_param(int argc, char *argv[], </font></tt></b>
|
||||
<br><a NAME="seed_passing"></a><b><tt><font color="#660000">uint32 &
|
||||
_seed</font><font color="#3366FF">,</font></tt></b>
|
||||
<br><b><tt><font color="#999900">unsigned int & _vecSize</font><font color="#3366FF">,</font></tt></b>
|
||||
<br><b><tt><font color="#009900">unsigned int & _popSize,</font></tt></b>
|
||||
<br><b><tt><font color="#009900">unsigned int & _tSize</font><font color="#3366FF">,</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">double & _pCross,</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">double & _pMut</font><font color="#3366FF">,</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">string & _load_name,</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">unsigned int & _maxGen,</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">unsigned int & _minGen,</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">unsigned int & _steadyGen,</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">double & _onePointRate, </font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">double & _twoPointsRate, </font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">double & _uRate, </font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">double & _pMutPerBit, </font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">double & _bitFlipRate, </font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">double & _oneBitRate</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">)</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">{ </font></tt></b>
|
||||
<br><a NAME="parser_declare"></a><tt><font color="#3366FF">// define a
|
||||
parser from the command-line arguments</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoParser parser(argc,
|
||||
argv);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// For each
|
||||
parameter, define Parameters directly in the parser, </font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// and assign
|
||||
the value to the variable</font></tt>
|
||||
<br><a NAME="random"></a><a NAME="_seed_declare"></a><b><tt><font color="#3366FF">
|
||||
</font><font color="#660000">eoValueParam<uint32>& seedParam = parser.createParam<uint32>(time(0),
|
||||
"seed", "Random number seed", 'S');</font></tt></b>
|
||||
<br><a NAME="seed_assign"></a><b><tt><font color="#660000">
|
||||
_seed = seedParam.value();</font></tt></b>
|
||||
<p><b><tt><font color="#3366FF"> </font><font color="#999900">eoValueParam<unsigned
|
||||
int>& vecSizeParam = parser.createParam<unsigned int>(8, "vecSize",
|
||||
"Genotype size",'V', "Representation");</font></tt></b>
|
||||
<br><b><tt><font color="#999900"> _vecSize = vecSizeParam.value();</font></tt></b>
|
||||
<p><b><tt><font color="#3366FF"> </font><font color="#009900">eoValueParam<unsigned
|
||||
int>& popSizeParam = parser.createParam<unsigned int>(10, "popSize",
|
||||
"Population size",'P', "Evolution");</font></tt></b>
|
||||
<br><b><tt><font color="#009900"> _popSize = popSizeParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#009900"> eoValueParam<unsigned
|
||||
int>& tSizeParam = parser.createParam<unsigned int>(10, "tSize",
|
||||
"Tournament size",'T', "Evolution");</font></tt></b>
|
||||
<br><b><tt><font color="#009900"> _tSize = tSizeParam.value();</font></tt></b>
|
||||
<br><a NAME="_load_name"></a>
|
||||
<br><b><tt><font color="#3366FF"> eoValueParam<string>&
|
||||
load_nameParam = parser.createParam<string>("", "Load","A save file
|
||||
to restart from",'L', "Persistence");</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> _load_name =
|
||||
load_nameParam.value();</font></tt></b>
|
||||
<p><b><tt><font color="#3366FF"> eoValueParam<unsigned
|
||||
int>& maxGenParam = parser.createParam<unsigned int>(100, "maxGen",
|
||||
"Maximum number of generations",'G', "Stopping criterion");</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> _maxGen = maxGenParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> eoValueParam<unsigned
|
||||
int>& minGenParam = parser.createParam<unsigned int>(100, "minGen",
|
||||
"Minimum number of generations",'g', "Stopping criterion");</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> _minGen = minGenParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> eoValueParam<unsigned
|
||||
int>& steadyGenParam = parser.createParam<unsigned int>(100, "steadyGen",
|
||||
"Number of generations with no improvement",'s', "Stopping criterion");</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> _steadyGen =
|
||||
steadyGenParam.value();</font></tt></b>
|
||||
<p><b><tt><font color="#CC33CC"> eoValueParam<double>&
|
||||
pCrossParam = parser.createParam<double>(0.6, "pCross", "Probability
|
||||
of Crossover", 'C', "Genetic Operators"); </font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> _pCross = pCrossParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> eoValueParam<double>&
|
||||
pMutParam = parser.createParam<double>(0.1, "pMut", "Probability of
|
||||
Mutation", 'M', "Genetic Operators");</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> _pMut = pMutParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> eoValueParam<double>&
|
||||
onePointRateParam = parser.createParam<double>(1, "onePointRate", "Relative
|
||||
rate for one point crossover", '1', "Genetic Operators");</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> _onePointRate
|
||||
= onePointRateParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> eoValueParam<double>&
|
||||
twoPointsRateParam = parser.createParam<double>(1, "twoPointRate", "Relative
|
||||
rate for two point crossover", '2', "Genetic Operators");</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> _twoPointsRate
|
||||
= twoPointsRateParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> eoValueParam<double>&
|
||||
uRateParam = parser.createParam<double>(2, "uRate", "Relative rate for
|
||||
uniform crossover", 'U', "Genetic Operators");</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> _uRate =
|
||||
uRateParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> eoValueParam<double>&
|
||||
pMutPerBitParam = parser.createParam<double>(0.01, "pMutPerBit", "Probability
|
||||
of flipping 1 bit in bit-flip mutation", 'b', "Genetic Operators");</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> _pMutPerBit =
|
||||
pMutPerBitParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> eoValueParam<double>&
|
||||
bitFlipRateParam = parser.createParam<double>(0.01, "bitFlipRate", "Relative
|
||||
rate for bit-flip mutation", 'B', "Genetic Operators");</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> _bitFlipRate
|
||||
= bitFlipRateParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> eoValueParam<double>&
|
||||
oneBitRateParam = parser.createParam<double>(0.01, "oneBitRate", "Relative
|
||||
rate for deterministic bit-flip mutation", 'D', "Genetic Operators");</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">
|
||||
_oneBitRate = oneBitRateParam.value();</font></tt></b>
|
||||
<p><tt><font color="#3366FF"><b> </b>// 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";</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> eoValueParam<string>&
|
||||
status_nameParam = parser.createParam<string>(str_status.c_str(), "status","Status
|
||||
file",'S', "Persistence");</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// do the following AFTER
|
||||
ALL PARAMETERS HAVE BEEN PROCESSED</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// 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">
|
||||
{</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">
|
||||
parser.printHelp(cout);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">
|
||||
exit(1);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">
|
||||
}</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> if (status_nameParam.value()
|
||||
!= "")</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">
|
||||
{</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">ofstream os(status_nameParam.value().c_str());</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b>os << parser; </b>// and you can
|
||||
use that file as parameter file</font></tt>
|
||||
<br><b><tt><font color="#3366FF">
|
||||
}</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">}</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#993300">// now the main_function: nothing changed,
|
||||
except input/output</font></tt>
|
||||
<br><b><tt><font color="#993300">void main_function(int argc, char **argv)</font></tt></b>
|
||||
<br><b><tt><font color="#993300">{</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="parametres"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td><a NAME="seed_declare"></a><b><tt><font color="#660000">
|
||||
uint32 seed;</font></tt></b>
|
||||
<br><tt><font color="#999900"><b> </b>// decription of genotype</font></tt>
|
||||
<br><b><tt><font color="#999900"> unsigned int vecSize;</font></tt></b>
|
||||
<br><tt><font color="#33CC00"><b> </b>// parameters for evolution
|
||||
engine</font></tt>
|
||||
<br><b><tt><font color="#33CC00"> unsigned int popSize;</font></tt></b>
|
||||
<br><b><tt><font color="#33CC00"> unsigned int tSize;</font></tt></b>
|
||||
<br><tt><font color="#CC33CC"><b> </b>// operators probabilities
|
||||
at the algorithm level</font></tt>
|
||||
<br><b><tt><font color="#CC33CC"> double pCross;</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> double pMut;</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// init and stop</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> string load_name;</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> unsigned int maxGen;</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> unsigned int minGen;</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> unsigned int steadyGen;</font></tt></b>
|
||||
<br><tt><font color="#CC33CC"><b> </b>// rates for crossovers</font></tt>
|
||||
<br><b><tt><font color="#CC33CC"> double onePointRate;</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> double twoPointsRate;</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> double URate;</font></tt></b>
|
||||
<br><tt><font color="#CC33CC"><b> </b>// rates and private
|
||||
parameters for mutations;</font></tt>
|
||||
<br><b><tt><font color="#CC33CC"> double pMutPerBit;</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> double bitFlipRate;</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> double oneBitRate;</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// Now read the parameters
|
||||
of the program</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> read_param(argc,
|
||||
argv, seed, vecSize, popSize, tSize,</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">
|
||||
pCross, pMut, load_name, maxGen, minGen, steadyGen,</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">
|
||||
onePointRate, twoPointsRate, URate, </font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">
|
||||
pMutPerBit, bitFlipRate, oneBitRate );</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="eval"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#CC0000"><b> </b>/////////////////////////////</font></tt>
|
||||
<br><tt><font color="#CC0000"><b> </b>// Fitness function</font></tt>
|
||||
<br><tt><font color="#CC0000"><b> </b>////////////////////////////</font></tt>
|
||||
<br><tt><font color="#CC0000"><b> </b>// Evaluation: from a plain
|
||||
C++ fn to an EvalFunc Object ...</font></tt>
|
||||
<br><b><tt><font color="#CC0000"> eoEvalFuncPtr<Indi, double, const
|
||||
vector<bool>& > 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"> eoEvalFuncCounter<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> </b>////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>// Initilisation of population</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>// Either load or initialize</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>// create an empty pop</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoPop<Indi> pop;</font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// create a state for reading</font></tt>
|
||||
<br><tt><font color="#993399"><b> 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> </b>// and the present run will
|
||||
be the exact conitnuation of the saved run</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>// eventually with different
|
||||
parameters</font></tt>
|
||||
<br><b><tt><font color="#993399"> inState.registerObject(rng);</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> 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"> {</font></tt></b>
|
||||
<br><a NAME="loadstate"></a><tt><font color="#993399"><b>
|
||||
inState.load(load_name); </b>// load the pop and the rng</font></tt>
|
||||
<br><tt><font color="#993399"><b>
|
||||
</b>// the fitness is read in the file: </font></tt>
|
||||
<br><tt><font color="#993399"><b>
|
||||
</b>// do only evaluate the pop if the fitness has changed</font></tt>
|
||||
<br><b><tt><font color="#993399"> }</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> else</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> {</font></tt></b>
|
||||
<br><b><tt><font color="#993399">
|
||||
rng.reseed(seed);</font></tt></b>
|
||||
<br><tt><font color="#993399"><b>
|
||||
</b>// a Indi random initializer</font></tt>
|
||||
<br><tt><font color="#993399"><b>
|
||||
</b>// based on boolean_generator class (see utils/rnd_generator.h)</font></tt>
|
||||
<br><b><tt><font color="#993399">
|
||||
eoInitFixedLength<Indi, boolean_generator> </font></tt></b>
|
||||
<br><b><tt><font color="#993399">random(vecSize, boolean_generator());</font></tt></b>
|
||||
<br><tt><font color="#993399"><b>
|
||||
</b>// Init pop from the randomizer: need to use the append function</font></tt>
|
||||
<br><b><tt><font color="#993399">
|
||||
pop.append(popSize, random); </font></tt></b>
|
||||
<br><tt><font color="#993399"><b>
|
||||
</b>// and evaluate pop (STL syntax) </font></tt>
|
||||
<br><b><tt><font color="#993399">
|
||||
apply<Indi>(eval, pop);</font></tt></b>
|
||||
<br><tt><font color="#993399"><b> } </b>// end
|
||||
of initializatio of the population</font></tt></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="output"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#3366FF"><b> </b>// sort pop for pretty printout</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> pop.sort();</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// Print (sorted) intial population
|
||||
(raw printout)</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> cout << "Initial Population"
|
||||
<< endl << pop << 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> </b>/////////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#009900"><b> </b>// selection and replacement</font></tt>
|
||||
<br><tt><font color="#009900"><b> </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> </b>// The robust tournament selection</font></tt>
|
||||
<br><tt><font color="#009900"><b> eoDetTournament<Indi> selectOne(tSize);
|
||||
</b>// tSize in [2,POPSIZE]</font></tt>
|
||||
<br><tt><font color="#009900"><b> </b>// is now encapsulated in a
|
||||
eoSelectPerc (entage)</font></tt>
|
||||
<br><tt><font color="#009900"><b> eoSelectPerc<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> </b>// And we now have the full
|
||||
slection/replacement - though with </font></tt>
|
||||
<br><tt><font color="#009900"><b> </b>// no replacement (== generational
|
||||
replacement) at the moment :-)</font></tt>
|
||||
<br><b><tt><font color="#009900"> eoNoReplacement<Indi> replace; </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> </b>//////////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>// The variation operators</font></tt>
|
||||
<br><tt><font color="#993399"><b> </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> </b>// 1-point crossover for bitstring</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoBinCrossover<Indi> xover1;</font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// uniform crossover for bitstring</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoBinUxOver<Indi> xoverU;</font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// 2-pots xover</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoBinNxOver<Indi> xover2(2);</font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// Combine them with relative
|
||||
rates</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoPropCombinedQuadOp<Indi> xover(xover1,
|
||||
onePointRate);</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> xover.add(xoverU, URate);</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> 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> </b>// standard bit-flip mutation
|
||||
for bitstring</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoBinMutation<Indi> mutationBitFlip(pMutPerBit);</font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// mutate exactly 1 bit per
|
||||
individual</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoDetBitFlip<Indi> mutationOneBit; </font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// Combine them with relative
|
||||
rates</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoPropCombinedMonOp<Indi> mutation(mutationBitFlip,
|
||||
bitFlipRate);</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> mutation.add(mutationOneBit, oneBitRate,
|
||||
true);</font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// The operators are encapsulated
|
||||
into an eoTRansform object</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoSGATransform<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> </b>//////////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// termination condition see
|
||||
FirstBitEA.cpp</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>/////////////////////////////////////</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoGenContinue<Indi> genCont(maxGen);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> eoSteadyFitContinue<Indi> steadyCont(minGen,
|
||||
steadyGen);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> eoFitContinue<Indi> fitCont(vecSize);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> eoCombinedContinue<Indi> continuator(genCont);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> continuator.add(steadyCont);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> 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> </b>// but now you want to make
|
||||
many different things every generation </font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// (e.g. statistics, plots,
|
||||
...).</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// the class eoCheckPoint is
|
||||
dedicated to just that:</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// Declare a checkpoint (from
|
||||
a continuator: an eoCheckPoint </font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// IS AN eoContinue and will
|
||||
be called in the loop of all algorithms)</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoCheckPoint<Indi> checkpoint(continuator);</font></tt></b>
|
||||
<p><a NAME="param_declare"></a><tt><font color="#3366FF"><b>
|
||||
</b>// Create a counter parameter</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoValueParam<unsigned>
|
||||
generationCounter(0, "Gen.");</font></tt></b>
|
||||
<p><a NAME="param_pass"></a><tt><font color="#3366FF"><b>
|
||||
</b>// Create an incrementor (sub-class of eoUpdater). Note that the </font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// parameter's
|
||||
value is passed by reference, </font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// so every
|
||||
time the incrementer is updated (every generation),</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// the data
|
||||
in generationCounter will change.</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoIncrementor<unsigned>
|
||||
increment(generationCounter.value());</font></tt></b>
|
||||
<br><a NAME="updater_pass"></a><tt><font color="#3366FF"><b>
|
||||
</b>// Add it to the checkpoint, </font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// so the
|
||||
counter is updated (here, incremented) every generation</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> checkpoint.add(increment);</font></tt></b>
|
||||
<br><a NAME="stat_declare"></a><tt><font color="#3366FF"><b>
|
||||
</b>// now some statistics on the population:</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// Best fitness
|
||||
in population</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoBestFitnessStat<Indi>
|
||||
bestStat;</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// Second
|
||||
moment stats: average and stdev</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoSecondMomentStats<Indi>
|
||||
SecondStat;</font></tt></b>
|
||||
<br><a NAME="stat_pass"></a><tt><font color="#3366FF"><b>
|
||||
</b>// Add them to the checkpoint to get them called at the appropriate
|
||||
time</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> checkpoint.add(bestStat);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> checkpoint.add(SecondStat);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// The Stdout
|
||||
monitor will print parameters to the screen ...</font></tt>
|
||||
<br><a NAME="monitor_declare"></a><b><tt><font color="#3366FF">
|
||||
eoStdoutMonitor monitor(false);</font></tt></b>
|
||||
<p><tt><font color="#3366FF"><b> </b>// when called
|
||||
by the checkpoint (i.e. at every generation)</font></tt>
|
||||
<br><a NAME="monitor_pass"></a><b><tt><font color="#3366FF">
|
||||
checkpoint.add(monitor);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// the monitor
|
||||
will output a series of parameters: add them </font></tt>
|
||||
<br><a NAME="monitor_add"></a><b><tt><font color="#3366FF">
|
||||
monitor.add(generationCounter);</font></tt></b>
|
||||
<a name="eval_monitor"></a>
|
||||
<br><tt><font color="#3366FF"><b> monitor.add(eval);
|
||||
</b>//
|
||||
because now eval is an eoEvalFuncCounter!</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> monitor.add(bestStat);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> monitor.add(SecondStat);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// A file
|
||||
monitor: will print parameters to ... a File, yes, you got it!</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoFileMonitor
|
||||
fileMonitor("stats.xg", " ");</font></tt></b>
|
||||
<p><tt><font color="#3366FF"><b> </b>// the checkpoint
|
||||
mechanism can handle multiple monitors</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> checkpoint.add(fileMonitor);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// the fileMonitor
|
||||
can monitor parameters, too, but you must tell it!</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> fileMonitor.add(generationCounter);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> fileMonitor.add(bestStat);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> fileMonitor.add(SecondStat);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// Last type
|
||||
of item the eoCheckpoint can handle: state savers:</font></tt>
|
||||
<br><a NAME="outstate_declare"></a><b><tt><font color="#3366FF">
|
||||
eoState outState;</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// Register
|
||||
the algorithm into the state (so it has something to save!!)</font></tt>
|
||||
<br><a NAME="outstate_register"></a><b><tt><font color="#3366FF">
|
||||
outState.registerObject(rng);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> outState.registerObject(pop);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// and feed
|
||||
the state to state savers</font></tt>
|
||||
<br><a NAME="statesaver_declare"></a><tt><font color="#3366FF"><b>
|
||||
</b>// save state every 100th generation</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoCountedStateSaver
|
||||
stateSaver1(100, outState, "generation"); </font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// save state
|
||||
every 1 seconds </font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoTimedStateSaver
|
||||
stateSaver2(1, outState, "time"); </font></tt></b>
|
||||
<br><a NAME="statesaver_pass"></a><tt><font color="#3366FF"><b>
|
||||
</b>// Don't forget to add the two savers to the checkpoint</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> checkpoint.add(stateSaver1);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> checkpoint.add(stateSaver2);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </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> </b>/////////////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#FF6666"><b> </b>// the algorithm</font></tt>
|
||||
<br><tt><font color="#FF6666"><b> </b>////////////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#FF6666"><b> </b>// Easy EA requires </font></tt>
|
||||
<br><tt><font color="#FF6666"><b> </b>// selection, transformation,
|
||||
eval, replacement, and stopping criterion</font></tt>
|
||||
<br><b><tt><font color="#FF6666"> eoEasyEA<Indi> gga(checkpoint,
|
||||
eval, select, transform, replace);</font></tt></b>
|
||||
<br><tt><font color="#FF6666"><b> </b>// Apply algo to pop - that's
|
||||
it!</font></tt>
|
||||
<br><b><tt><font color="#FF6666"> 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> </b>// Print (sorted) final population</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> pop.sort();</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> cout << "FINAL Population\n"
|
||||
<< pop << 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"> int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> flag
|
||||
|= _CRTDBG_LEAK_CHECK_DF;</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> _CrtSetDbgFlag(flag);</font></tt></b>
|
||||
<br><tt><font color="#993300">// _CrtSetBreakAlloc(100);</font></tt>
|
||||
<br><b><tt><font color="#993300">#endif</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> try</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> {</font></tt></b>
|
||||
<br><b><tt><font color="#993300">
|
||||
main_function(argc, argv);</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> }</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> catch(exception&
|
||||
e)</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> {</font></tt></b>
|
||||
<br><b><tt><font color="#993300">
|
||||
cout << "Exception: " << e.what() << '\n';</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> }</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> return 1;</font></tt></b>
|
||||
<br><b><tt><font color="#993300">}</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<hr WIDTH="100%"><a href="eoLesson3.html">Back to Lesson 3</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <b><font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font></b>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last modified: Sun Nov
|
||||
26 09:31:04 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
BIN
eo/tutorial/html/beige009.jpg
Normal file
BIN
eo/tutorial/html/beige009.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.5 KiB |
57
eo/tutorial/html/binary_value.html
Normal file
57
eo/tutorial/html/binary_value.html
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdk i686) [Netscape]">
|
||||
<title>binary_value.h</title>
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font></b>
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">binary_value.h</font></h1></center>
|
||||
<a NAME="start"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr NOSAVE>
|
||||
<td NOSAVE><b><tt><font color="#993300">#include <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"> the fitnes.</font></tt>
|
||||
<br><tt><font color="#993300"> @param _chrom A
|
||||
binary chromosome </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<bool>&
|
||||
_chrom)</font></tt></b>
|
||||
<br><b><tt><font color="#993399">{</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> double sum = 0;</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> for (unsigned i = 0; i < _chrom.size();
|
||||
i++)</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> sum += _chrom[i];</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> return sum;</font></tt></b>
|
||||
<br><b><tt><font color="#993399">}</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<hr WIDTH="100%"><a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font></b>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last modified: Wed Nov
|
||||
29 09:03:09 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
BIN
eo/tutorial/html/costume.jpg
Normal file
BIN
eo/tutorial/html/costume.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 209 KiB |
21
eo/tutorial/html/debut.html
Normal file
21
eo/tutorial/html/debut.html
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>TITRE</title>
|
||||
</head>
|
||||
|
||||
<body text="#000000" background="beige009.jpg" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoTutorial.html">Tutorial main page </a>-
|
||||
<a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO documentation</a></font>
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<!-- -------------- End of header ------------------ -->
|
||||
<!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1><font color="#FF0000">TITRE</font></h1>
|
||||
</center>
|
||||
|
||||
<font face="Courier New,Courier"><font size=+1>
|
||||
<p>
|
||||
74
eo/tutorial/html/eoBottomUp.html
Normal file
74
eo/tutorial/html/eoBottomUp.html
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.72 [en] (X11; U; Linux 2.2.16 i686) [Netscape]">
|
||||
<title>EO - The Bottom-Up approach</title>
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#CCCCCC" link="#0000EF" vlink="#51188E" alink="#FF0000">
|
||||
<a href="eoTutorial.html">Tutorial main page </a>-
|
||||
<a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <a href="doc/html/index.html">EO documentation</a>
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">EO - Bottom-Up approach</font></h1></center>
|
||||
|
||||
<p><br>Congratualtions - You have chosen the bottom-up approach!
|
||||
From here you will be allowed to browse into the different components of
|
||||
an Evolutionary Algorithm, and to see how to program your favorite using
|
||||
the EO library.
|
||||
|
||||
<h3>
|
||||
<font color="#000099">Table of Content</font></h3>
|
||||
|
||||
<center>
|
||||
<p><img SRC="EA_tutorial.jpg" USEMAP="#Map" ></center>
|
||||
|
||||
<p>Yes, this is the table of content of this part
|
||||
of the EO tutorial. If you don't already know what this symbolic
|
||||
representation of an EA means, you should try <a
|
||||
href="eoIntroEA.html">here</a>. Otherwise, <font color="#FF6600">click on the figure</font>
|
||||
to go directly to the corresponding section of the tutorial.
|
||||
<br>
|
||||
<map NAME="Map">
|
||||
<area SHAPE="rect" HREF="eoInit.html" COORDS="14,31,135,70">
|
||||
<area SHAPE="rect" HREF="eoEval.html" COORDS="14,110,135,150">
|
||||
<area SHAPE="rect" HREF="eoRepresentation.html" COORDS="240,270,465,310">
|
||||
<area SHAPE="rect" HREF="eoIo.html#output" COORDS="280,45,480,70">
|
||||
<area SHAPE="rect" HREF="eoIo.html#stop" COORDS="348,110,430,150">
|
||||
<area SHAPE="rect" HREF="eoEngine.html#selection" COORDS="495,110,615,150">
|
||||
<area SHAPE="rect" HREF="eoRepresentation.html" COORDS="495,190,615,230">
|
||||
<area SHAPE="rect" HREF="eoOperators.html#crossover" COORDS="495,265,625,287">
|
||||
<area SHAPE="rect" HREF="eoOperators.html#mutation" COORDS="495,287,625,305">
|
||||
<area SHAPE="rect" HREF="eoRepresentation.html" COORDS="170,110,295,150">
|
||||
<area SHAPE="rect" HREF="eoEval.html" COORDS="170,270,295,310">
|
||||
<area SHAPE="rect" HREF="eoEngine.html#replacement" COORDS="170,190,295,230">
|
||||
|
||||
<area SHAPE="rect" HREF="eoGeneration.html" COORDS="310,160,485,260">
|
||||
|
||||
|
||||
<area SHAPE="rect" HREF="eoOperators.html" COORDS="15,350,260,370">
|
||||
<area SHAPE="rect" HREF="eoRepresentation.html" COORDS="270,350,460,370">
|
||||
<area SHAPE="rect" HREF="eoEngine.html" COORDS="15,377,400,397">
|
||||
<area SHAPE="rect" HREF="eoEval.html" COORDS="15,403,230,423">
|
||||
<area SHAPE="rect" HREF="eoCheckpoint.html" COORDS="15,430,221,450">
|
||||
<area SHAPE="rect" HREF="eoCheckpoint.html#stop" COORDS="221,430,345,450">
|
||||
<area SHAPE="rect" HREF="eoCheckpoint.html#stat" COORDS="375,430,445,450">
|
||||
</map>
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<br><a href="eoTutorial.html">Tutorial main page </a>- <a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <a href="doc/html/index.html">EO documentation</a>
|
||||
<br>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Mon Oct 30 07:27:13 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Mon Oct 30 07:28:36 CET 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
59
eo/tutorial/html/eoEngine.html
Normal file
59
eo/tutorial/html/eoEngine.html
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.72 [en] (X11; U; Linux 2.2.16 i686) [Netscape]">
|
||||
<title>Genetic Engine</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<body text="#000000" link="#0000EF" vlink="#51188E" alink="#FF0000" background="beige009.jpg">
|
||||
<center>
|
||||
<h1>
|
||||
<b><font color="#CC0000">Evolution Engine</font></b></h1></center>
|
||||
|
||||
<p><br><b><font color="#000099"><font size=+2>Contents</font></font></b>
|
||||
<ul>
|
||||
<li>
|
||||
Introduction</li>
|
||||
|
||||
<li>
|
||||
Selection</li>
|
||||
|
||||
<li>
|
||||
Replacement</li>
|
||||
|
||||
<li>
|
||||
Popular evolution engines</li>
|
||||
</ul>
|
||||
|
||||
<p><br>The term evolution engine denotes the different parts that simulate
|
||||
the Darwinism in Evolutionary Algorithms.<font color="#33CC00"></font>
|
||||
<center>
|
||||
<p><i><font color="#33CC00">The fittest individuals are more likely to
|
||||
reproduce and survive.</font></i></center>
|
||||
|
||||
<p>Darwinism takes place in two different phases of an EA, though in many
|
||||
<a href="#popular">popular variants</a>, only one phase is activated.
|
||||
<p><a href="#selection">Selection</a> is the Darwinistic choice of parents
|
||||
that will be allowed to <font color="#33CC00">reproduce</font><font color="#CC33CC">.</font>
|
||||
<br><a href="#replacement">Replacement</a> takes place after reproduction,
|
||||
and is the Darwinistic choice of those individuals that will <font color="#33CC00">survive</font>,
|
||||
i.e. become the parents of the next generation.
|
||||
<p><a NAME="selection"></a><b><font color="#000099"><font size=+2>Selection</font></font></b>
|
||||
<br>
|
||||
<p><a NAME="replacement"></a><b><font color="#000099"><font size=+2>Replacement</font></font></b>
|
||||
<br>
|
||||
<p><a NAME="popular"></a><b><font color="#000099"><font size=+2>Popular
|
||||
evolution engines</font></font></b>
|
||||
<br>
|
||||
<br>
|
||||
<p>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Mon Oct 30 18:15:16 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Mon Oct 30 18:15:17 CET 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
34
eo/tutorial/html/eoEval.html
Normal file
34
eo/tutorial/html/eoEval.html
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>Evaluation</title>
|
||||
</head>
|
||||
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoTutorial.html">Tutorial main page </a>-
|
||||
<a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <a href="doc/html/index.html">EO documentation</a>
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
|
||||
<center>
|
||||
<h1><font color="#FF0000">Evaluation</font></h1>
|
||||
</center>
|
||||
|
||||
|
||||
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<a href="eoTutorial.html">Tutorial main page </a>-
|
||||
<a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <a href="doc/html/index.html">EO documentation</a>
|
||||
<hr>
|
||||
<address><a href="mailto:Marc.Schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<!-- Created: Mon Oct 30 17:51:43 CET 2000 -->
|
||||
<!-- hhmts start -->
|
||||
Last modified: Mon Nov 6 10:54:33 CET 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
19
eo/tutorial/html/eoGeneration.html
Normal file
19
eo/tutorial/html/eoGeneration.html
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>Generation</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Generation</h1>
|
||||
|
||||
|
||||
|
||||
<hr>
|
||||
<address><a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<!-- Created: Mon Oct 30 19:29:28 CET 2000 -->
|
||||
<!-- hhmts start -->
|
||||
Last modified: Mon Oct 30 19:29:29 CET 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
36
eo/tutorial/html/eoInit.html
Normal file
36
eo/tutorial/html/eoInit.html
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>Initialization</title>
|
||||
</head>
|
||||
<body text="#000000" background="beige009.jpg" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoTutorial.html">Tutorial main page </a>-
|
||||
<a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <a href="doc/html/index.html">EO documentation</a>
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<!-- -------------- End of header ------------------ -->
|
||||
<!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<b><font color="#CC0000">Initialization</font></b></h1></center>
|
||||
|
||||
|
||||
|
||||
<!-- Bottom of file: display some links and address -->
|
||||
<!-- ----------------------------------------------- -->
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<a href="eoTutorial.html">Tutorial main page </a>- <a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <a href="doc/html/index.html">EO documentation</a>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:Marc.Schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<!-- Created: Mon Oct 30 17:51:29 CET 2000 -->
|
||||
<!-- hhmts start -->
|
||||
Last modified: Mon Nov 6 11:25:18 CET 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
25
eo/tutorial/html/eoIo.html
Normal file
25
eo/tutorial/html/eoIo.html
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>Input / Output</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Input / Output</h1>
|
||||
|
||||
<p>
|
||||
<A NAME=stop>
|
||||
<h3>Stopping criteria</h3>
|
||||
|
||||
<p>
|
||||
<A NAME=output>
|
||||
<h3>Displaying statistics</h3>
|
||||
|
||||
<hr>
|
||||
<address><a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<!-- Created: Mon Oct 30 19:29:46 CET 2000 -->
|
||||
<!-- hhmts start -->
|
||||
Last modified: Tue Oct 31 18:32:22 CET 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
413
eo/tutorial/html/eoLesson1.html
Normal file
413
eo/tutorial/html/eoLesson1.html
Normal file
|
|
@ -0,0 +1,413 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdksmp i686) [Netscape]">
|
||||
<title>Tutorial: Lesson 1</title>
|
||||
</head>
|
||||
<body text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000" background="beige009.jpg">
|
||||
<a href="eoLesson2.html">Lesson 2</a> -
|
||||
<a href="eoTutorial.html">Tutorial
|
||||
main page</a> -
|
||||
<a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> -<font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">Tutorial: Lesson 1</font></h1></center>
|
||||
This lesson will let you
|
||||
<ul>
|
||||
<li>
|
||||
<a href="#run">run</a> your first Evolutionary Algorithm written within
|
||||
EO Library, choosing between evolving <font color="#999900">bitstrings</font>,
|
||||
or evolving <font color="#999900">vectors of real numbers</font>,</li>
|
||||
|
||||
<li>
|
||||
<a href="#browse">browse</a> through the code of these algorithms, or</li>
|
||||
|
||||
<li>
|
||||
follow the <a href="#tour">guided tour</a>.</li>
|
||||
</ul>
|
||||
Later you will be asked to
|
||||
<ul>
|
||||
<li>
|
||||
write your own <font color="#990000">fitness function</font>,</li>
|
||||
|
||||
<li>
|
||||
use different kinds of <font color="#009900">selection procedures</font>
|
||||
in the framework of a generational GA <font color="#009900">evolution engine</font>,</li>
|
||||
|
||||
<li>
|
||||
check that EO let you separate the <font color="#999900">representation</font>
|
||||
from the <font color="#009900">evolution engine</font>.</li>
|
||||
</ul>
|
||||
|
||||
<h3>
|
||||
<a NAME="run"></a><font color="#000099">I want to run an Evolutionary Algorithm
|
||||
<b>now</b></font></h3>
|
||||
You can choose to run a standard <a href="FirstBitGA.html">bitstring Genetic
|
||||
Algorithm</a> (as defined in Goldberg's book) or a standard <a href="FirstRealGA.html">real-valued
|
||||
genetic algorithm</a>, as proposed in Micahlewicz's book.
|
||||
<p>If you have not already done what was recommended in the <a href="eoTutorial.html#install">Tutorial
|
||||
main page</a> , do it <b><font color="#FF6600">NOW</font></b>. Then go
|
||||
to the Lesson1 sub-dir of the tutorial dir, and simply type at the system
|
||||
prompt
|
||||
<p><tt>(myname@myhost) EOdir/Tutorial/Lesson1 % <font color="#FF6666">FirstRealGA</font></tt>
|
||||
<br>or
|
||||
<br><tt>(myname@myhost) EOdir/Tutorial/Lesson1 % <font color="#FF6666">FirstBitGA</font></tt>
|
||||
<p>and something should happen.
|
||||
<h3>
|
||||
<font color="#000099">What is happening?</font></h3>
|
||||
At the moment, the <font color="#FF6600">FirstBitGA</font> maximizes the
|
||||
number of ones in the bitstring (also calls the <font color="#FF6600">OneMaxfunction</font>,
|
||||
whose solution is, as you can guess,
|
||||
<font color="#FF6600">11111111</font>),
|
||||
and the <font color="#FF6600">FirstRealGA</font> is maximizing (the default
|
||||
action for EO is to maximize) the inverse of the sum (i.e. minimizing the
|
||||
sum) of the square of its variables (also called the <font color="#FF6600">sphere
|
||||
function</font>, whose solution is ... <font color="#FF6600">all zeroes</font>).
|
||||
<p>And what you see on the screen when running one of these two programs
|
||||
is, in each case, the initial and final population of an Evolutionary run,
|
||||
one individual per line, its fitness first, then the number of items (bits
|
||||
or real numbers) of the genotype, and the genotype itself. The final population
|
||||
hopefully contains the solution in the discrete case, and is close to it
|
||||
in the continuous case.
|
||||
<p><a NAME="browse"></a>You need now to take a look at either programs
|
||||
by browsing alone through the sources for <a href="FirstBitGA.html">FirstBitGA</a>
|
||||
and <a href="FirstRealGA.html">FirstRealGA</a>, or follow the guided tour
|
||||
below, or go directly to the <a href="#exercise1">exercises</a>.
|
||||
<h3>
|
||||
<a NAME="tour"></a><font color="#000099">Browsing the code:</font></h3>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<b><font color="#993300">General includes:</font></b><font color="#000000">Like
|
||||
all C-like code, the file starts with include directives (<a href="FirstBitGA.html#start">Bit</a>
|
||||
- <a href="FirstRealGA.html#start">Real</a>). Apart from standard includes,
|
||||
the spedific file to include is </font><b><font color="#FF6600">eo</font></b><font color="#000000">:
|
||||
this is a file that contains the list of the most important EO files.</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<b><font color="#999900">Representation</font></b><font color="#000000">:
|
||||
you then have to delclare the type of individuals you will be handling.
|
||||
All evolution-related objects you will need are templatized w.r.t. the
|
||||
type of individuals.</font></li>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<a href="FirstBitGA.html#representation">Bit</a> You say here that you
|
||||
will be handling <b><font face="Arial,Helvetica"><a href="doc/html/class_eobin.html">Bitstring
|
||||
genotypes</a></font></b>, whose fitness is a double. This makes Indi derive
|
||||
from the <a href="eoProgramming.html#STL">STL class</a> <font color="#993300">vector<bool></font></li>
|
||||
|
||||
<li>
|
||||
<a href="FirstRealGA.html#representation">Real</a> You say here that you
|
||||
will be handling <b><font face="Arial,Helvetica"><a href="doc/html/class_eoreal.html">Real-valued
|
||||
genotypes</a></font></b>, whose fitness is a double. This makes Indi derive
|
||||
from the <a href="eoProgramming.html#STL">STL class</a> <font color="#993300">vector<double></font></li>
|
||||
|
||||
<br> </ul>
|
||||
|
||||
<li>
|
||||
<b><font color="#CC0000">Fitness function:</font></b><font color="#000000">
|
||||
the code for the fitness function is included in the file. It must take
|
||||
as argument a reference to an individual (at the moment).</font></li>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<a href="FirstBitGA.html#evalfunc">Bit</a> This function simply computes
|
||||
the number of ones of the bitstring (it's called the OneMax function).
|
||||
The optimum is of course the all-ones bitstring.</li>
|
||||
|
||||
<li>
|
||||
<a href="FirstRealGA.html#evalfunc">Real</a> This function simply computes
|
||||
the inverse of the sum of the squares of all variables (also called the
|
||||
sphere function). The optimum is of course the all-zeroes vector.</li>
|
||||
|
||||
<br> </ul>
|
||||
|
||||
<li>
|
||||
<a NAME="parametres"></a><b><font color="#3366FF">Parameters</font></b><font color="#000000">:
|
||||
all parameters of the algorithm are declared here (<a href="FirstBitGA.html#parametres">Bit</a>
|
||||
- <a href="FirstRealGA.html#parametres">Real</a>), and their values and
|
||||
assigned. Of course, this means that you will need to recompile to change
|
||||
these values - see Lesson 3 to get rid of that heavy requirement.</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<b><font color="#990000">Random seeding:</font></b><font color="#000000">
|
||||
Random numbers play an important role in Evolutionary Algorithms. See in
|
||||
<a href="eoProgramming.html#random">EO
|
||||
programming hints</a> more details about how this is simulated in EO -
|
||||
but as far as you are concerned now, remember that the </font><font color="#660000">global
|
||||
Random Number Generator</font><font color="#000000"> is called </font><b><tt><font color="#660000"><font size=+1>rng</font></font></tt></b><font color="#000000">
|
||||
and should be used everywhere you need a realization of a random variable
|
||||
of known law. Moreover, this RNG requires a </font><b><tt><font color="#660000"><font size=+1>seed</font></font></tt></b><font color="#000000">,
|
||||
which is set here (<a href="FirstBitGA.html#random">Bit</a> - <a href="FirstRealGA.html#random">Real</a>):
|
||||
everytime you run the algorithm with the </font><font color="#FF6600">same
|
||||
seed</font><font color="#000000">, you will get the </font><font color="#FF6600">same
|
||||
result</font><font color="#000000">. Hence, to test the robustness of your
|
||||
algorithm, you should run it with different seeds. This is rather time
|
||||
consuming in the present programs, so we suggest that you wait until Lesson
|
||||
3 to do so.</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<b><font color="#CC0000">Fitness function encapsulation: </font></b><font color="#000000">EO
|
||||
is based on the notion of <a href="eoProgramming.html#functors">functors</a>
|
||||
- hence you now need to encapsulate your fitness function into a functor
|
||||
object. This is what is done here (<a href="FirstBitGA.html#eval">Bit</a>
|
||||
- <a href="FirstRealGA.html#eval">Real</a>).</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<b><font color="#CC33CC">Initialization</font></b><font color="#000000">:
|
||||
to initialize the population, first declare an empty object of class </font><b><tt><font color="#990000">eoPop<Indi></font></tt></b><font color="#000000">,
|
||||
which is basically an <a href="eoProgramming.html#STL">STL</a> </font><b><tt><font color="#990000">vector<Indi></font></tt></b><font color="#000000">,
|
||||
then fill it with Indi's.</font> And remember that
|
||||
<b><tt><font color="#990000">v.push_back</font></tt></b>
|
||||
simply appends its argument at the end of <a href="eoProgramming.html#STL">STL</a>
|
||||
vector <b><tt><font color="#993300">v.</font></tt></b></li>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<a href="FirstBitGA.html#init">Bit</a> <b><tt><font color="#990000">rng.flip()</font></tt></b>
|
||||
return a <font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eorng.html">random
|
||||
boolean</a></font></font></li>
|
||||
|
||||
<li>
|
||||
<a href="FirstRealGA.html#init">Real</a> <b><tt><font color="#990000">rng.uniform()</font></tt></b>
|
||||
returns a <font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eorng.html">real
|
||||
value uniformly drawn in [0,1].</a></font></font></li>
|
||||
|
||||
<br> </ul>
|
||||
|
||||
<li>
|
||||
<b><font color="#3366FF">Output</font></b><font color="#000000">: take
|
||||
a snapshot at the initial population (<a href="FirstBitGA.html#output">Bit</a>
|
||||
- <a href="FirstRealGA.html#output">Real</a>). Sort it first, so the best
|
||||
individuals are first, and display it. Note that an eoPop has a </font><b><tt><font color="#990000"><<</font></tt></b><font color="#000000">
|
||||
method, which means that a simple </font><b><tt><font color="#990000">os
|
||||
<< pop </font></tt></b><font color="#000000">streams the </font><b><tt><font color="#990000">pop</font></tt></b><font color="#000000">
|
||||
onto the ostream </font><b><tt><font color="#990000">os</font></tt></b><font color="#000000">.
|
||||
This is true for </font><font color="#FF6600">all objects of of class </font><font color="#000000"><b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eoprintable.html">eoPrintable</a></font></font></b>
|
||||
(most EO objects) through the method </font><b><tt><font color="#990000">printOn</font></tt></b><font color="#000000">
|
||||
(which is then called by the </font><b><tt><font color="#990000"><<</font></tt></b><font color="#000000">
|
||||
operator).</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<b><font color="#009900">Evolution engine:</font></b><font color="#000000">
|
||||
The selection/replacement mechanism (<a href="FirstBitGA.html#engine">Bit</a>
|
||||
- <a href="FirstRealGA.html#engine">Real</a>) is a simple generational
|
||||
GA here: a simple selector, and a generational replacement. The <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eodettournament.html">eoDetTournament</a></font></font></b>
|
||||
has been chosen as a robust selection, and the generational replacement
|
||||
(all parents are replaced by the offspring) is hard-coded in the eoSGA
|
||||
<a href="#algorithm">algorithm</a>.</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<font color="#CC33CC"><b>Variation operators</b>:</font><font color="#000000">
|
||||
in the simple algorithm considered here, individuals undergo </font><font color="#CC33CC">crossover</font><font color="#000000">
|
||||
and </font><font color="#CC33CC">mutation</font><font color="#000000">.
|
||||
In EO, these operators are (<a href="eoProgramming.html#functors">functor</a>)
|
||||
objects of class</font> <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eoquadraticop.html">eoQuadOp</a></font></font></b>
|
||||
(binary operator that modifies both its arguments) and <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eomonop.html">eoMonOp</a></font></font></b>
|
||||
(unary operator). These operators are applied in turn to all selected
|
||||
parents, according to user-defined probabilities. These probabilities
|
||||
are defined with all other <a href="#parametres">parameters</a>, and will
|
||||
be passed to the <b><tt><font color="#FF6666"><font size=+1>eoSGA</font></font></tt></b><a href="#parametres">algorithm</a><font color="#000000">.</font></li>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<a href="FirstBitGA.html#operators">Bit</a> The crossover <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eobincrossover.html">eoBinCrossover</a></font></font></b>
|
||||
is the standard <font color="#CC33CC">1-point crossover</font>, and <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eobinmutation.html">eoBinMutation</a></font></font></b>
|
||||
is the standard <font color="#FF6600">bit-flip mutation</font> that randomly
|
||||
flips all bits with a given probability <b><tt>P_MUT_PER_BIT.</tt></b></li>
|
||||
|
||||
<br><b><font color="#FF0000">Warning</font></b>: the <b><tt>P_MUT_PER_BIT</tt></b>
|
||||
probability is an <font color="#FF6600">internal parameter</font> of the
|
||||
<b><tt><font color="#CC33CC"><font size=+1>eoBinMutation</font></font></tt></b>,
|
||||
it is <b><font color="#FF6600">NOT</font></b> the probability of mutation
|
||||
at the individual level. EO corrects what can be viewed as an inconsistency
|
||||
in Holland's original work, further used in Goldberg's book by separating
|
||||
the probability of mutation for each individual (independent of the type
|
||||
of mutation that will be applied) from the probability of flipping each
|
||||
bit, which is specific of the bit-flip mutation. Hence, to run the
|
||||
same algorithm as Goldberg's SGA, the mutation probability (at individual
|
||||
level) is 1, and the probability of flipping each bit is <b><tt>P_MUT_PER_BIT.</tt></b>
|
||||
<li>
|
||||
<a href="FirstRealGA.html#operators">Real</a> The crossover <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eoarithmeticcrossover.html">eoArithmeticCrossover</a></font></font></b>
|
||||
is the standard <font color="#CC33CC">arithmetic crossover</font> for real-valued
|
||||
vectors, that chooses a point randomly on the segment between both parents
|
||||
(also termed <font color="#CC33CC">BLX-0</font>). <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eouniformmutation.html">eoUniformMutation</a></font></font></b>
|
||||
is the <font color="#CC33CC">uniform mutation</font> for real-valued vectors
|
||||
that choses a new value for each variable uniformly on an interval centered
|
||||
on the parent value. The width of the interval is an <font color="#FF6600">internal
|
||||
parameter</font> of the object, here called <b><tt>EPSILON</tt></b>.</li>
|
||||
|
||||
<br> </ul>
|
||||
|
||||
<li>
|
||||
<b><font color="#3366FF">Stopping criterion:</font></b><font color="#000000">
|
||||
Specify a </font><font color="#3366FF">maximum number of generations</font><font color="#000000">
|
||||
to run (<a href="FirstBitGA.html#stop">Bit</a> - <a href="FirstRealGA.html#stop">Real</a>):
|
||||
the simplest of all stopping criteria at the moment, using an object of
|
||||
a sub-class of class </font><b><font face="Arial,Helvetica"><font color="#3366FF"><font size=+1><a href="doc/html/class_eocontinue.html">eoContinue</a></font></font></font></b><font color="#000000">.</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<a NAME="algorithm"></a><b><font color="#FF6666">The algorithm: </font></b><font color="#000000">the
|
||||
simple algorithm that is used here, called </font><b><tt><font color="#FF6666"><font size=+1>eoSGA
|
||||
</font></font></tt></b><font color="#000000">requires
|
||||
as parameters a </font><font color="#FF6600">selector</font><font color="#000000">,
|
||||
a </font><font color="#FF6600">crossover</font><font color="#000000"> and
|
||||
the associated </font><font color="#FF6600">crossover rate</font><font color="#000000">,
|
||||
a </font><font color="#FF6600">mutation</font><font color="#000000"> and
|
||||
the associated </font><font color="#FF6600">mutation rate,</font><font color="#000000">
|
||||
and a </font><font color="#FF6600">stopping criterion</font><font color="#000000">.
|
||||
Take a look at the corresponding
|
||||
<a href="eoSGA.html#constructor">constructor</a>
|
||||
of the class </font><b><tt><font color="#FF6666"><font size=+1>eoSGA</font></font></tt></b><font color="#000000">:
|
||||
it only initializes its <a href="eoSGA.html#parametres">private data</a>
|
||||
with the parameters. Now look at the <a href="eoSGA.html#generation">operator()</a>
|
||||
method - the one that is called in the code for <a href="FirstBitGA.html#generation">FirstBitGA</a>
|
||||
or <a href="FirstRealGA.html#generation">FirstRealGA</a> - and you'll find
|
||||
out that is is as simple as it sounds.</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<b><font color="#3366FF">Output</font></b><font color="#000000">: After
|
||||
running the algorithm, output the sorted final population (<a href="FirstBitGA.html#final_output">Bit</a>
|
||||
- <a href="FirstRealGA.html#final_output">Real</a>) - and look at the best
|
||||
individual: this is the result of the algorithm.</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<b><font color="#990000">Main body:</font></b><font color="#000000"> for
|
||||
technical reasons (intercepting the exceptions), we need a main like this
|
||||
one (<a href="FirstBitGA.html#main">Bit</a> - <a href="FirstRealGA.html#main">Real</a>).,
|
||||
and you should not touch it unless you know what you are doing. Simply
|
||||
note that this main calls the function main_function, which we have been
|
||||
discussing up to now!</font></li>
|
||||
</ul>
|
||||
|
||||
<h3>
|
||||
<a NAME="exercise1"></a><font color="#000099">Exercise 1: maximize your
|
||||
own function</font></h3>
|
||||
This is very easy - if your search space is that of bitstring or of unbounded
|
||||
real numbers.
|
||||
<ul>
|
||||
<li>
|
||||
Go to the tutorial directory, and <font color="#FF6600">copy</font> the
|
||||
program you want to modify onto <b><tt><font color="#663300">mytest.cpp.</font></tt></b></li>
|
||||
|
||||
<li>
|
||||
<font color="#FF6600">Edit</font> <b><tt><font color="#990000">mytest.cpp</font></tt></b>
|
||||
with any text editor:</li>
|
||||
|
||||
<li>
|
||||
<font color="#FF6600">Modify</font> the fitness function itself (<a href="FirstBitGA.html#evalfunc">binary_value</a>
|
||||
in <font color="#FF6666"><b><tt>FirstBitGA</tt></b><font face="Courier New,Courier">,</font></font><a href="FirstRealGA.html#evalfunc">real_value</a>
|
||||
in <font color="#FF6666"><b><tt>FirstRealGA</tt></b><font face="Courier New,Courier">)</font></font></li>
|
||||
|
||||
<li>
|
||||
<font color="#FF6600">Compile</font> the program by typing <b><tt><font color="#990000">make
|
||||
mytest</font></tt></b> at system prompt</li>
|
||||
|
||||
<li>
|
||||
<font color="#FF6600">Run</font> the new program by entering the command
|
||||
<b><tt><font color="#990000">mytest</font></tt></b>
|
||||
at system prompt.</li>
|
||||
</ul>
|
||||
|
||||
<h3>
|
||||
<font color="#000099">Exercise 2: check the differences between both programs</font></h3>
|
||||
Go and take a look at the code for these programs <font color="#000000">(<a href="FirstBitGA.html">Bit</a>
|
||||
- <a href="FirstRealGA.html">Real</a>)</font>. Use the symbolic representation
|
||||
of an Evolutionary Algorithm (you should understand that figure now, otherwise
|
||||
go <a href="eoIntro.html">there</a> and come back) to understand how each
|
||||
part of the EA is coded. Try to spot the differences between both codes:
|
||||
there are not so many!
|
||||
<br>After you've tried that alone, take a look at the <a href="Firstmerge.html">solution</a>
|
||||
:-)
|
||||
<br>
|
||||
<h3>
|
||||
<font color="#000099">Exercise 3: change the selection procedure</font></h3>
|
||||
This is rather straightforward ... if you know what other types of selection
|
||||
are available!
|
||||
<br>At the moment, let's only consider only the following simple ones:
|
||||
<ul>
|
||||
<li>
|
||||
You already know the <font color="#FF6600">tournament selection</font></li>
|
||||
|
||||
<br><font color="#FF0000">Syntax: </font><tt><font color="#009900"><b>eoDetTournament<Indi>
|
||||
select(T_SIZE); </b>// T_SIZE in [2,POP_SIZE)</font></tt>
|
||||
<li>
|
||||
Try the well-known <font color="#FF6600">roulette wheel</font></li>
|
||||
|
||||
<br> <font color="#FF0000">Syntax: </font> <b><tt><font color="#009900">eoProportional<Indi>
|
||||
select;</font></tt></b>
|
||||
<li>
|
||||
Or the <font color="#FF6600">stochastic binary tournament</font></li>
|
||||
|
||||
<br><font color="#FF0000">Syntax: </font><tt><font color="#009900"><b>eoStochTournament<Indi>
|
||||
select(RATE); </b> // RATE in ]0.5,1]</font></tt>
|
||||
<li>
|
||||
and of course the <font color="#FF6600">random</font> selection should
|
||||
give bad results!</li>
|
||||
|
||||
<br><font color="#FF0000">Syntax: </font><b><tt><font color="#009900">eoSelectRandom<Indi>
|
||||
select;</font></tt></b></ul>
|
||||
Note that all these classes of eoObjects are derived from the abstract
|
||||
class
|
||||
<b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eoselectone.html">eoSelectOne.</a></font></font></b>
|
||||
<p><b><font color="#000099"><font size=+2>Lessons:</font></font></b>
|
||||
<ul>
|
||||
<li>
|
||||
in EO, all actions are performed by <a href="eoProgramming.html#functors">functor
|
||||
objects</a> (this section is the last time in this tutorial that there
|
||||
is a direct link to the <a href="eoProgramming.html">EO Programming hints</a>
|
||||
page - though the link at top and bottom of all pages will remain there).</li>
|
||||
|
||||
<li>
|
||||
in EO, all object you will usually need to manipulate are <a href="eoProgramming.html#templates">templatized</a><b><font color="#FF6600">
|
||||
w.r.t. the type of the individual</font></b> you are handling.</li>
|
||||
|
||||
<li>
|
||||
The type of the individual is itself <a href="eoProgramming.html#templates">templatized</a><b><font color="#FF6600">
|
||||
w.r.t. the type of fitness</font></b> (double by default).</li>
|
||||
|
||||
<li>
|
||||
In EO (actually, in EC!) <font color="#CC33CC">initialization and variation</font>
|
||||
operators are <font color="#999900">representation</font>-dependent, while
|
||||
the <font color="#009900">evolution engine</font> is <font color="#999900">representation</font>-independent
|
||||
(well, like any rule, this one does have some exceptions).</li>
|
||||
|
||||
<li>
|
||||
Changing the <font color="#990000">fitness function</font>, or the <font color="#009900">selection
|
||||
procedure</font> inside the generational GA evolution engine is straightforward.</li>
|
||||
|
||||
<li>
|
||||
remember, all <font color="#FF6600">solutions</font> to exercises are in
|
||||
the same sub-dir of dir Tutorial than the lesson itself (see <a href="NoWay.html">here</a>).</li>
|
||||
</ul>
|
||||
|
||||
<hr WIDTH="100%"><a href="eoLesson2.html">Lesson 2</a> -
|
||||
<a href="eoTutorial.html">Tutorial
|
||||
main page</a> -
|
||||
<a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font>
|
||||
<br>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:Marc.Schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Fri Nov 3 18:49:12 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Fri Nov 3 18:49:12 CET 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue