569 lines
34 KiB
HTML
569 lines
34 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 <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></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="evalfunc"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#CC0000">// a simple fitness function that computes
|
|
the number of ones of a bitstring</font></tt>
|
|
<br><b><tt><font color="#CC0000">#include "binary_value.h"</font></tt></b></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="representation"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#999900">//-----------------------------------------------------------------------------</font></tt>
|
|
<br><tt><font color="#999900">// define your genotype and fitness types</font></tt>
|
|
<br><b><tt><font color="#999900">typedef eoBit<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">//
|
|
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>
|
|
<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<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<unsigned int>& 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<unsigned int>& 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<unsigned int>& 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> </b>// init and stop</font></tt>
|
|
<br><b><tt><font color="#3366FF">eoValueParam<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<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<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<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<double> pCrossParam(0.6,
|
|
"pCross", "Probability of Crossover", 'C'); </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<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<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<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<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 = 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<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<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 = bitFlipRateParam.value();</font></tt></b>
|
|
<p><b><tt><font color="#CC33CC">eoValueParam<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<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"> {</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 (statusParam.value() != "")</font></tt></b>
|
|
<br><b><tt><font color="#3366FF"> {</font></tt></b>
|
|
<br><b><tt><font color="#3366FF"> ofstream
|
|
os(statusParam.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></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> </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 eoUniformGenerator class (see utils/eoRndGenerators.h)</font></tt>
|
|
<br><b><tt><font color="#993399"> eoUniformGenerator<bool>
|
|
uGen;</font></tt></b>
|
|
<br><b><tt><font color="#993399"> eoInitFixedLength<Indi>
|
|
random(vecSize, uGen);</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 initialization of the population</font></tt></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="output"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#3366FF"><b> </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> eoDetTournamentSelect<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><b><tt><font color="#009900"> eoSelectPerc<Indi> select(selectOne);</font></tt></b>
|
|
<br><tt><font color="#009900"> // or eoSelectPerc<Indi> select(selectOne,
|
|
rate); </font></tt>
|
|
<br><tt><font color="#009900"> // 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> </b>// And we now have the full
|
|
slection/replacement - though with </font></tt>
|
|
<br><tt><font color="#009900"><b> </b>// the same generational replacement
|
|
at the moment :-)</font></tt>
|
|
<br><b><tt><font color="#009900"> eoGenerationalReplacement<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"> eo1PtBitXover<Indi> xover1;</font></tt></b>
|
|
<br><tt><font color="#993399"><b> </b>// uniform crossover for bitstring</font></tt>
|
|
<br><b><tt><font color="#993399"> eoUBitXover<Indi> xoverU;</font></tt></b>
|
|
<br><tt><font color="#993399"><b> </b>// 2-pots xover</font></tt>
|
|
<br><b><tt><font color="#993399"> eoNPtsBitXover<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"> eoBitMutation<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</font></tt>
|
|
<br><a NAME="outstate_register"></a><b><tt><font color="#3366FF">
|
|
outState.registerObject(parser);</font></tt></b>
|
|
<br><b><tt><font color="#3366FF"> outState.registerObject(pop);</font></tt></b>
|
|
<br><b><tt><font color="#3366FF"> outState.registerObject(rng);</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">// 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"> // stopping criterion, eval, selection,
|
|
transformation, replacement</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">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based
|
|
</a>-
|
|
<a href="eoProgramming.html">Programming
|
|
hints</a> - <b><font face="Arial,Helvetica"><a href="../../doc/html/index.html">EO
|
|
documentation</a></font></b>
|
|
<hr>
|
|
<address>
|
|
<a href="mailto:Marc.Schoenauer@inria.fr">Marc Schoenauer</a></address>
|
|
|
|
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last modified: Sun Nov
|
|
26 09:31:04 2000<!-- hhmts end -->
|
|
</body>
|
|
</html>
|