I had to change the whole way the parameters are read, because EGCS did not
allow the nice constructs I had imagined (and compiled with g++) - I removed the createParam method in Parser class - that was creating the parameters on the heap. Not allowed to have a templatized method ??? - I removed the subroutine read_param in SecondBitEA, as you need to create permanent parameters (eoParser only holds references), and egcs did not allow to create them by reference, i.e. in the line eoValueParam<xxx> & blablaParam(...); So now everything is done in the main_function, and 3 lines are needed to create and read every paramter (sigh ...)
This commit is contained in:
parent
769b1fe31e
commit
04e429fdb1
2 changed files with 230 additions and 317 deletions
|
|
@ -4,12 +4,14 @@
|
|||
<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 -->
|
||||
<!-- 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
|
||||
</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 ------------------ --><!-- ----------------------------------------------- -->
|
||||
|
|
@ -46,14 +48,15 @@ cout</font></tt>
|
|||
istrstream</font></tt>
|
||||
<br><b><tt><font color="#993300">#include <fstream></font></tt></b>
|
||||
<br><tt><font color="#993300">// the general include for eo</font></tt>
|
||||
<br><b><tt><font color="#993300">#include <eo></font></tt></b>
|
||||
<br><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>
|
||||
<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>
|
||||
|
|
@ -64,6 +67,15 @@ istrstream</font></tt>
|
|||
<br><b><tt><font color="#999900">typedef eoBin<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>
|
||||
|
|
@ -78,190 +90,130 @@ or in a parameter file (same syntax, order independent, </font></tt>
|
|||
# = 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),
|
||||
<p><tt><font color="#3366FF">// First define a parser from the command-line
|
||||
arguments</font></tt>
|
||||
<br><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_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><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><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>
|
||||
<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 (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></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="eval"></a>
|
||||
|
|
@ -304,29 +256,29 @@ parameters</font></tt>
|
|||
<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><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><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><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><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
|
||||
|
|
@ -356,7 +308,8 @@ of initializatio of the population</font></tt></td>
|
|||
<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);
|
||||
<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>
|
||||
|
|
@ -369,9 +322,10 @@ by default rate==1</font></tt></td>
|
|||
<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>
|
||||
<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>
|
||||
|
|
@ -451,12 +405,12 @@ 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>
|
||||
<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>
|
||||
<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
|
||||
|
|
@ -465,13 +419,13 @@ time the incrementer is updated (every generation),</font></tt>
|
|||
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><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><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>
|
||||
|
|
@ -480,9 +434,8 @@ bestStat;</font></tt></b>
|
|||
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><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
|
||||
|
|
@ -496,8 +449,7 @@ 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>
|
||||
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>
|
||||
|
|
@ -521,21 +473,23 @@ of item the eoCheckpoint can handle: state savers:</font></tt>
|
|||
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><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><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><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
|
||||
|
|
@ -599,7 +553,9 @@ cout << "Exception: " << e.what() << '\n';</font></tt></b>
|
|||
|
||||
<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
|
||||
</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>
|
||||
|
|
|
|||
Reference in a new issue