📄 secondrealea.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="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">Tutorialmain page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-uppage</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EOdocumentation</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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -