Adding SecondRealEA
This commit is contained in:
parent
5c34d8ee79
commit
361741b726
2 changed files with 490 additions and 11 deletions
468
eo/tutorial/html/SecondRealEA.html
Normal file
468
eo/tutorial/html/SecondRealEA.html
Normal file
|
|
@ -0,0 +1,468 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="cpp2html Marc Schoenauer">
|
||||
<title>SecondRealEA</title>
|
||||
</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">SecondRealEA</font></h1></center>
|
||||
Click on the figure to see the corresponding code.<br>
|
||||
In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a><br>
|
||||
The actual code is in boldface and the comment in normal face.
|
||||
<br><img SRC="EA_tutorial.jpg" USEMAP="#Map" >
|
||||
<map NAME="Map">
|
||||
<!-- Init -->
|
||||
<area SHAPE="rect" HREF="#init" COORDS="14,31,135,70">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150">
|
||||
<!-- main loop -->
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150">
|
||||
<area SHAPE="rect" HREF="#output" COORDS="280,45,480,70">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150">
|
||||
<area SHAPE="rect" HREF="#select" COORDS="495,110,615,150">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230">
|
||||
<area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287">
|
||||
<area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310">
|
||||
<area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230">
|
||||
<!-- Center of loop -->
|
||||
<area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260">
|
||||
<!-- 4 bottom lines -->
|
||||
<area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370">
|
||||
<area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423">
|
||||
<area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450">
|
||||
<area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450">
|
||||
<area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378">
|
||||
</map>
|
||||
<br>
|
||||
<A NAME="start"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr NOSAVE>
|
||||
<td NOSAVE><tt><font color="#993300">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// SecondRealEA.cpp<br>
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
//*<br>
|
||||
// Same code than FirstBitEA as far as Evolutionary Computation is concerned<br>
|
||||
// but now you learn to enter the parameters in a more flexible way<br>
|
||||
// (also slightly different than in SecondBitEA.cpp)<br>
|
||||
// and to twidle the output to your preferences (as in SecondBitEA.cpp)<br>
|
||||
//<br>
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// standard includes<br>
|
||||
<b>#include <stdexcept> </b>// runtime_error <br>
|
||||
<b>#include <iostream> </b>// cout<br>
|
||||
<b>#include <strstream> </b>// ostrstream, istrstream<br>
|
||||
// the general include for eo<br>
|
||||
<b>#include <eo></b><br>
|
||||
<b>#include <es.h></b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="representation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#999900">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// define your individuals<br>
|
||||
<b>typedef eoReal<eoMinimizingFitness> Indi; </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="evalfunc"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#CC0000">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// a simple fitness function that computes the euclidian norm of a real vector<br>
|
||||
// Now in a separate file, and declared as binary_value(const vector<bool> &)<br>
|
||||
<b>#include "real_value.h"</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
<b>void main_function(int argc, char **argv)</b><br>
|
||||
<b>{</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="parametres"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// instead of having all values of useful parameters as constants, read them:<br>
|
||||
// either on the command line (--option=value or -o=value)<br>
|
||||
// or in a parameter file (same syntax, order independent, <br>
|
||||
// # = usual comment character <br>
|
||||
// or in the environment (TODO)<br>
|
||||
<b> </b>// First define a parser from the command-line arguments<br>
|
||||
<b> eoParser parser(argc, argv);</b><br>
|
||||
<b> </b><br>
|
||||
<b> </b>// For each parameter, you can in on single line<br>
|
||||
<b> </b>// define the parameter, read it through the parser, and assign it<br>
|
||||
<b> </b><br>
|
||||
<a NAME="random"></a>
|
||||
<b> unsigned seed = parser.createParam(unsigned(time(0)), "seed", "Random number seed", 'S').value(); </b>// will be in default section General<br>
|
||||
<b> </b><br>
|
||||
<b> </b>// description of genotype<br>
|
||||
<b> unsigned vecSize = parser.createParam(unsigned(8), "vecSize", "Genotype size",'V', "Representation" ).value();</b><br>
|
||||
<b> </b>// parameters for evolution engine<br>
|
||||
<b> unsigned popSize = parser.createParam(unsigned(10), "popSize", "Population size",'P', "Evolution engine" ).value();</b><br>
|
||||
<b> unsigned tSize = parser.createParam(unsigned(2), "tSize", "Tournament size",'T', "Evolution Engine" ).value();</b><br>
|
||||
<b> </b>// init and stop<br>
|
||||
<b> string loadName = parser.createParam(string(""), "Load","A save file to restart from",'L', "Persistence" ).value();</b><br>
|
||||
<b> </b><br>
|
||||
<b> unsigned maxGen = parser.createParam(unsigned(100), "maxGen", "Maximum number of generations",'G', "Stopping criterion" ).value();</b><br>
|
||||
<b> unsigned minGen = parser.createParam(unsigned(100), "minGen", "Minimum number of generations",'g', "Stopping criterion" ).value();</b><br>
|
||||
<b> unsigned steadyGen = parser.createParam(unsigned(100), "steadyGen", "Number of generations with no improvement",'s', "Stopping criterion" ).value();</b><br>
|
||||
<b> </b>// operators probabilities at the algorithm level<br>
|
||||
<b> double pCross = parser.createParam(double(0.6), "pCross", "Probability of Crossover", 'C', "Genetic Operators" ).value();</b><br>
|
||||
<b> double pMut = parser.createParam(double(0.1), "pMut", "Probability of Mutation", 'M', "Genetic Operators" ).value();</b><br>
|
||||
<b> </b>// relative rates for crossovers<br>
|
||||
<b> double hypercubeRate = parser.createParam(double(1), "hypercubeRate", "Relative rate for hypercube crossover", '\0', "Genetic Operators" ).value();</b><br>
|
||||
<b> double segmentRate = parser.createParam(double(1), "segmentRate", "Relative rate for segment crossover", '\0', "Genetic Operators" ).value();</b><br>
|
||||
<b> </b>// internal parameters for the mutations<br>
|
||||
<b> double EPSILON = parser.createParam(double(0.01), "EPSILON", "Width for uniform mutation", '\0', "Genetic Operators" ).value();</b><br>
|
||||
<b> </b><br>
|
||||
<b> double SIGMA = parser.createParam(double(0.3), "SIGMA", "Sigma for normal mutation", '\0', "Genetic Operators" ).value();</b><br>
|
||||
<b> </b>// relative rates for mutations<br>
|
||||
<b> double uniformMutRate = parser.createParam(double(1), "uniformMutRate", "Relative rate for uniform mutation", '\0', "Genetic Operators" ).value();</b><br>
|
||||
<b> double detMutRate = parser.createParam(double(1), "detMutRate", "Relative rate for det-uniform mutation", '\0', "Genetic Operators" ).value();</b><br>
|
||||
<b> double normalMutRate = parser.createParam(double(1), "normalMutRate", "Relative rate for normal mutation", '\0', "Genetic Operators" ).value();</b><br>
|
||||
<b> </b>// the name of the "status" file where all actual parameter values will be saved <br>
|
||||
<b> string str_status = parser.ProgramName() + ".status"; </b>// default value<br>
|
||||
<b> string statusName = parser.createParam(str_status, "status","Status file",'S', "Persistence" ).value();</b><br>
|
||||
<b> </b>// do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED<br>
|
||||
<b> </b>// i.e. in case you need parameters somewhere else, postpone these<br>
|
||||
<b> if (parser.userNeedsHelp())</b><br>
|
||||
<b> {</b><br>
|
||||
<b> parser.printHelp(cout);</b><br>
|
||||
<b> exit(1);</b><br>
|
||||
<b> }</b><br>
|
||||
<b> if (statusName != "")</b><br>
|
||||
<b> {</b><br>
|
||||
<b>ofstream os(statusName.c_str());</b><br>
|
||||
<b>os << parser; </b>// and you can use that file as parameter file<br>
|
||||
<b> }</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="eval"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#CC0000">
|
||||
<b> </b>/////////////////////////////<br>
|
||||
<b> </b>// Fitness function<br>
|
||||
<b> </b>////////////////////////////<br>
|
||||
<b> </b>// Evaluation: from a plain C++ fn to an EvalFunc Object<br>
|
||||
<b> </b>// you need to give the full description of the function<br>
|
||||
<b> eoEvalFuncPtr<Indi, double, const vector<double>& > plainEval( real_value );</b><br>
|
||||
<b> </b>// ... to an object that counts the nb of actual evaluations<br>
|
||||
<b> eoEvalFuncCounter<Indi> eval(plainEval);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="init"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>////////////////////////////////<br>
|
||||
<b> </b>// Initilisation of population<br>
|
||||
<b> </b>////////////////////////////////<br>
|
||||
<b> </b>// Either load or initialize<br>
|
||||
<b> </b>// create an empty pop<br>
|
||||
<b> eoPop<Indi> pop;</b><br>
|
||||
<b> </b>// create a state for reading<br>
|
||||
<b> eoState inState; </b>// a state for loading - WITHOUT the parser<br>
|
||||
<b> </b>// register the rng and the pop in the state, so they can be loaded,<br>
|
||||
<b> </b>// and the present run will be the exact conitnuation of the saved run<br>
|
||||
<b> </b>// eventually with different parameters<br>
|
||||
<b> inState.registerObject(rng);</b><br>
|
||||
<b> inState.registerObject(pop);</b><br>
|
||||
<b> </b><br>
|
||||
<b> if (loadName != "")</b><br>
|
||||
<b> {</b><br>
|
||||
<b> inState.load(loadName); </b>// load the pop and the rng<br>
|
||||
<b> </b>// the fitness is read in the file: <br>
|
||||
<b> </b>// do only evaluate the pop if the fitness has changed<br>
|
||||
<b> }</b><br>
|
||||
<b> else</b><br>
|
||||
<b> {</b><br>
|
||||
<b> rng.reseed(seed);</b><br>
|
||||
<b> </b>// a Indi random initializer<br>
|
||||
<b> </b>// based on boolean_generator class (see utils/rnd_generator.h)<br>
|
||||
<b> eoUniformGenerator<double> uGen(-1.0, 1.0);</b><br>
|
||||
<b> eoInitFixedLength<Indi> random(vecSize, uGen);</b><br>
|
||||
<b> </b>// Init pop from the randomizer: need to use the append function<br>
|
||||
<b> pop.append(popSize, random); </b><br>
|
||||
<b> </b>// and evaluate pop (STL syntax) <br>
|
||||
<b> apply<Indi>(eval, pop);</b><br>
|
||||
<b> } </b>// end of initializatio of the population<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="stop"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>// sort pop before printing it!<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> </b>// Print (sorted) intial population (raw printout)<br>
|
||||
<b> cout << "Initial Population" << endl;</b><br>
|
||||
<b> cout << pop;</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="engine"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// selection and replacement<br>
|
||||
<b> </b>////////////////////////////////////<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="select"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>// The robust tournament selection<br>
|
||||
<b> eoDetTournamentSelect<Indi> selectOne(tSize);</b><br>
|
||||
<b> </b>// is now encapsulated in a eoSelectPerc (entage)<br>
|
||||
<b> eoSelectPerc<Indi> select(selectOne);</b>// by default rate==1<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="replace"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>// And we now have the full slection/replacement - though with <br>
|
||||
<b> </b>// no replacement (== generational replacement) at the moment :-)<br>
|
||||
<b> eoGenerationalReplacement<Indi> replace; </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="operators"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// The variation operators<br>
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="crossover"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>// uniform chooce on segment made by the parents<br>
|
||||
<b> eoSegmentCrossover<Indi> xoverS;</b><br>
|
||||
<b> </b>// uniform choice in hypercube built by the parents<br>
|
||||
<b> eoHypercubeCrossover<Indi> xoverA;</b><br>
|
||||
<b> </b>// Combine them with relative weights<br>
|
||||
<b> eoPropCombinedQuadOp<Indi> xover(xoverS, segmentRate);</b><br>
|
||||
<b> xover.add(xoverA, hypercubeRate, true);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="mutation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]<br>
|
||||
<b> eoUniformMutation<Indi> mutationU(EPSILON); </b><br>
|
||||
<b> </b>// k (=1) coordinates of parents are uniformly modified<br>
|
||||
<b> eoDetUniformMutation<Indi> mutationD(EPSILON); </b><br>
|
||||
<b> </b>// all coordinates of parents are normally modified (stDev SIGMA)<br>
|
||||
<b> eoNormalMutation<Indi> mutationN(SIGMA); </b><br>
|
||||
<b> </b>// Combine them with relative weights<br>
|
||||
<b> eoPropCombinedMonOp<Indi> mutation(mutationU, uniformMutRate);</b><br>
|
||||
<b> mutation.add(mutationD, detMutRate);</b><br>
|
||||
<b> mutation.add(mutationN, normalMutRate, true);</b><br>
|
||||
<b> </b>// The operators are encapsulated into an eoTRansform object<br>
|
||||
<b> eoSGATransform<Indi> transform(xover, pCross, mutation, pMut);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="stop"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// termination condition see FirstBitEA.cpp<br>
|
||||
<b> </b>/////////////////////////////////////<br>
|
||||
<b> eoGenContinue<Indi> genCont(maxGen);</b><br>
|
||||
<b> eoSteadyFitContinue<Indi> steadyCont(minGen, steadyGen);</b><br>
|
||||
<b> eoFitContinue<Indi> fitCont(0);</b><br>
|
||||
<b> eoCombinedContinue<Indi> continuator(genCont);</b><br>
|
||||
<b> continuator.add(steadyCont);</b><br>
|
||||
<b> continuator.add(fitCont);</b><br>
|
||||
<b> </b><br>
|
||||
<b> </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="checkpoint"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>// but now you want to make many different things every generation <br>
|
||||
<b> </b>// (e.g. statistics, plots, ...).<br>
|
||||
<b> </b>// the class eoCheckPoint is dedicated to just that:<br>
|
||||
<b> </b>// Declare a checkpoint (from a continuator: an eoCheckPoint <br>
|
||||
<b> </b>// IS AN eoContinue and will be called in the loop of all algorithms)<br>
|
||||
<b> eoCheckPoint<Indi> checkpoint(continuator);</b><br>
|
||||
<b> </b><br>
|
||||
<b> </b>// Create a counter parameter<br>
|
||||
<b> eoValueParam<unsigned> generationCounter(0, "Gen.");</b><br>
|
||||
<b> </b><br>
|
||||
<b> </b>// Create an incrementor (sub-class of eoUpdater). Note that the <br>
|
||||
<b> </b>// parameter's value is passed by reference, <br>
|
||||
<b> </b>// so every time the incrementer is updated (every generation),<br>
|
||||
<b> </b>// the data in generationCounter will change.<br>
|
||||
<b> eoIncrementor<unsigned> increment(generationCounter.value());</b><br>
|
||||
<b> </b>// Add it to the checkpoint, <br>
|
||||
<b> </b>// so the counter is updated (here, incremented) every generation<br>
|
||||
<b> checkpoint.add(increment);</b><br>
|
||||
<b> </b>// now some statistics on the population:<br>
|
||||
<b> </b>// Best fitness in population<br>
|
||||
<b> eoBestFitnessStat<Indi> bestStat;</b><br>
|
||||
<b> </b>// Second moment stats: average and stdev<br>
|
||||
<b> eoSecondMomentStats<Indi> SecondStat;</b><br>
|
||||
<b> </b>// Add them to the checkpoint to get them called at the appropriate time<br>
|
||||
<b> checkpoint.add(bestStat);</b><br>
|
||||
<b> checkpoint.add(SecondStat);</b><br>
|
||||
<b> </b>// The Stdout monitor will print parameters to the screen ...<br>
|
||||
<b> eoStdoutMonitor monitor(false);</b><br>
|
||||
<b> </b><br>
|
||||
<b> </b>// when called by the checkpoint (i.e. at every generation)<br>
|
||||
<b> checkpoint.add(monitor);</b><br>
|
||||
<b> </b>// the monitor will output a series of parameters: add them <br>
|
||||
<b> monitor.add(generationCounter);</b><br>
|
||||
<b> monitor.add(eval); </b>// because now eval is an eoEvalFuncCounter!<br>
|
||||
<b> monitor.add(bestStat);</b><br>
|
||||
<b> monitor.add(SecondStat);</b><br>
|
||||
<b> </b>// A file monitor: will print parameters to ... a File, yes, you got it!<br>
|
||||
<b> eoFileMonitor fileMonitor("stats.xg", " ");</b><br>
|
||||
<b> </b><br>
|
||||
<b> </b>// the checkpoint mechanism can handle multiple monitors<br>
|
||||
<b> checkpoint.add(fileMonitor);</b><br>
|
||||
<b> </b>// the fileMonitor can monitor parameters, too, but you must tell it!<br>
|
||||
<b> fileMonitor.add(generationCounter);</b><br>
|
||||
<b> fileMonitor.add(bestStat);</b><br>
|
||||
<b> fileMonitor.add(SecondStat);</b><br>
|
||||
<b> </b>// Last type of item the eoCheckpoint can handle: state savers:<br>
|
||||
<b> eoState outState;</b><br>
|
||||
<b> </b>// Register the algorithm into the state (so it has something to save!!)<br>
|
||||
<b> outState.registerObject(parser);</b><br>
|
||||
<b> outState.registerObject(pop);</b><br>
|
||||
<b> outState.registerObject(rng);</b><br>
|
||||
<b> </b>// and feed the state to state savers<br>
|
||||
<b> </b>// save state every 100th generation<br>
|
||||
<b> eoCountedStateSaver stateSaver1(20, outState, "generation"); </b><br>
|
||||
<b> </b>// save state every 1 seconds <br>
|
||||
<b> eoTimedStateSaver stateSaver2(1, outState, "time"); </b><br>
|
||||
<b> </b>// Don't forget to add the two savers to the checkpoint<br>
|
||||
<b> checkpoint.add(stateSaver1);</b><br>
|
||||
<b> checkpoint.add(stateSaver2);</b><br>
|
||||
<b> </b>// and that's it for the (control and) output<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="generation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#FF6666">
|
||||
<b> </b>/////////////////////////////////////////<br>
|
||||
<b> </b>// the algorithm<br>
|
||||
<b> </b>////////////////////////////////////////<br>
|
||||
<b> </b>// Easy EA requires <br>
|
||||
<b> </b>// stopping criterion, eval, selection, transformation, replacement<br>
|
||||
<b> eoEasyEA<Indi> gga(checkpoint, eval, select, transform, replace);</b><br>
|
||||
<b> </b>// Apply algo to pop - that's it!<br>
|
||||
<b> gga(pop);</b><br>
|
||||
<b> </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="stop"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>// Print (sorted) intial population<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> cout << "FINAL Population\n" << pop << endl;</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
<b>}</b><br>
|
||||
// A main that catches the exceptions<br>
|
||||
<b>int main(int argc, char **argv)</b><br>
|
||||
<b>{</b><br>
|
||||
<b> try</b><br>
|
||||
<b> {</b><br>
|
||||
<b> main_function(argc, argv);</b><br>
|
||||
<b> }</b><br>
|
||||
<b> catch(exception& e)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> cout << "Exception: " << e.what() << '\n';</b><br>
|
||||
<b> }</b><br>
|
||||
<b> return 1;</b><br>
|
||||
<b>}</b><br>
|
||||
</font></font></font></td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr WIDTH="100%"><a href="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>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last
|
||||
modified: Sun Apr 28 06:42:44 2002
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -42,13 +42,13 @@ First, but you should now have done it without being told, go into the
|
|||
sub-dir</font> of the tutorial dir and type
|
||||
<b><tt><font color="#990000"><font size=+1>make</font></font></tt></b>.
|
||||
This will compile the <b><tt><font color="#660000"><font size=+1>SecondBitEA</font></font></tt></b>
|
||||
program (and, some day, SecondRealEA).
|
||||
and <b><tt><font color="#660000"><font size=+1>SecondRealEA</font></font></tt></b>
|
||||
programs.
|
||||
<p>You can then either
|
||||
<ul>
|
||||
<li>
|
||||
browse the corresponding code (only <a href="SecondBitEA.html">SecondBitEA</a>
|
||||
available right now, but you can figure out how SecondRealEA will look
|
||||
like),</li>
|
||||
browse the corresponding code (<a href="SecondBitEA.html">SecondBitEA</a>
|
||||
and <a href="SecondRealEA.html">SecondRealEA</a>),</li>
|
||||
|
||||
<li>
|
||||
look at the <a href="#changes">summary of changes</a>,</li>
|
||||
|
|
@ -139,7 +139,8 @@ See the parameter section of the Component-Based tutorial, or wait until
|
|||
|
||||
<li>
|
||||
<font color="#000000">The </font><b><tt><font color="#3366FF">eoParser</font></tt></b><font color="#000000">
|
||||
class, whose only purpose is the input of parameters.</font></li>
|
||||
class, whose only purpose is the input of parameters. Read <a href="../../doc/html/classeo_parser.html">its
|
||||
description</a> if you are interested.</font></li>
|
||||
</ul>
|
||||
|
||||
<hr WIDTH="50%"><a NAME="paraminput"></a><b><font color="#000099">eoParser:</font><font color="#FF0000">
|
||||
|
|
@ -171,7 +172,7 @@ and optionally by a <font color="#FF6600">short (1 character) keyword</font>.</l
|
|||
the general syntax to modify parameter value at run-time is (either from
|
||||
the command-line or in a text file)</li>
|
||||
|
||||
<br>
|
||||
<br>
|
||||
|
||||
<b><tt><font color="#660000">--longKeyword=value</font></tt></b>
|
||||
or <b><tt><font color="#660000">-cvalue</font></tt></b>
|
||||
|
|
@ -183,12 +184,12 @@ so, after compiling the executable for Lesson 3 (<b><tt><font color="#FF6666">ma
|
|||
lesson3</font></tt></b> at system prompt in Unix), you can try to type
|
||||
in</li>
|
||||
|
||||
<br>
|
||||
<br>
|
||||
|
||||
<b><tt><font color="#FF6666">SecondBitEA</font></tt></b>
|
||||
<br>and see the algorithm run as before (OneMax optimized on 8-bits bitstrings).
|
||||
But you can now type in
|
||||
<br>
|
||||
<br>
|
||||
|
||||
<b><tt><font color="#FF6666">SecondBitEA --vecSize=100</font></tt></b>
|
||||
<br>and see the output of the optimization of OneMax on 100-bit bitstrings.
|
||||
|
|
@ -196,7 +197,7 @@ But you can now type in
|
|||
<li>
|
||||
Take a look at all available parameters by typing in</li>
|
||||
|
||||
<br>
|
||||
<br>
|
||||
|
||||
<b><tt><font color="#FF6666">SecondBitEA --help</font></tt></b>
|
||||
<br>or by going into the code: all parameter inputs have been grouped in
|
||||
|
|
@ -209,7 +210,7 @@ it contains the list of all actual parameters used, and can directly be
|
|||
used as parameter input file: change the file name (e.g. to <b><tt><font color="#660000">SecondBitEA.param</font></tt></b>),
|
||||
edit it, change whichever parameter you want, and type in</li>
|
||||
|
||||
<br>
|
||||
<br>
|
||||
<b><tt><font color="#FF6666">SecondBitEA @SecondBitEA.param</font></tt></b>
|
||||
<br>and you will see all values that you defined into the file taken into
|
||||
account.
|
||||
|
|
@ -249,7 +250,17 @@ somewhere else later (though of course this is not any useful for variable
|
|||
<b><tt><font color="#660000">seed</font></tt></b>!).</li>
|
||||
</ul>
|
||||
There is however another way to achieve the same result in less lines of
|
||||
code - with a different memory management (see <a href="eoLesson4.html#parameters">Lesson4</a>).
|
||||
code - with a different memory management. This is what is done in the
|
||||
code for eoRealEA. The same parameter for the <a href="eoProgramming.html#random">random
|
||||
number generator </a><b><tt><font color="#660000">seed</font></tt></b>
|
||||
is read, but in <a href="SecondRealEA.html#random">one single line of code</a>.
|
||||
The only difference is that now you cannot access the eoValueParam object
|
||||
itself - but this is not often necessary.
|
||||
<br>Be careful to ensure that the type of the default value in the call
|
||||
to <a href="../../doc/html/classeo_parameter_loader.html#a3">eoParameterLoader::createParam</a>
|
||||
method as this is the only way the compiler can desambiguate the template
|
||||
(remember that eoParameterLoader is <font color="#000000"><a href="../../doc/html/classeo_parser.html">a
|
||||
base class for eoParser</a></font>.
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<br><a NAME="state"></a><b><font color="#000099"><font size=+2>eoState:
|
||||
|
|
|
|||
Reference in a new issue