610 lines
39 KiB
HTML
610 lines
39 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">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>
|
|
</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 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><a NAME="seed_passing"></a><b><tt><font color="#3366FF">void read_param(eoParser & _parser, </font></tt></b>
|
|
<br><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><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><a NAME="parser_declare"></a><tt><font color="#3366FF"><b> </b>// 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>// 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</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"><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>
|