paradiseo/eo/tutorial/html/SecondBitEA.html

572 lines
35 KiB
HTML

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