⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 secondrealea.html

📁 这是linux下的进化计算的源代码。 === === === === === === === === === === === ===== check latest news at http:
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!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>&nbsp;<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 &lt;stdexcept>  &nbsp;</b>// runtime_error <br><b>#include &lt;iostream>  &nbsp; &nbsp;</b>// cout<br><b>#include &lt;strstream>  &nbsp;</b>// ostrstream, istrstream<br>// the general include for eo<br><b>#include &lt;eo></b><br><b>#include &lt;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&lt;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&lt;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>//  &nbsp; &nbsp; &nbsp; &nbsp;or in a parameter file (same syntax, order independent, <br>//  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# = usual comment character <br>//  &nbsp; &nbsp; &nbsp; &nbsp;or in the environment (TODO)<br><b>  &nbsp;</b>// First define a parser from the command-line arguments<br><b>  &nbsp;eoParser parser(argc, argv);</b><br><b>  &nbsp;</b><br><b>  &nbsp;</b>// For each parameter, you can in on single line<br><b>  &nbsp;</b>// define the parameter, read it through the parser, and assign it<br><b>  &nbsp;</b><br><a NAME="random"></a><b>  &nbsp;unsigned seed = parser.createParam(unsigned(time(0)), "seed", "Random number seed", 'S').value(); </b>// will be in default section General<br><b>  &nbsp;</b><br><b>  &nbsp;</b>// description of genotype<br><b>  &nbsp;unsigned vecSize = parser.createParam(unsigned(8), "vecSize", "Genotype size",'V', "Representation" ).value();</b><br><b>  &nbsp; &nbsp;</b>// parameters for evolution engine<br><b>  &nbsp; &nbsp; &nbsp;unsigned popSize = parser.createParam(unsigned(10), "popSize", "Population size",'P', "Evolution engine" ).value();</b><br><b>  &nbsp; &nbsp; &nbsp;unsigned tSize = parser.createParam(unsigned(2), "tSize", "Tournament size",'T', "Evolution Engine" ).value();</b><br><b>  &nbsp; &nbsp;</b>// init and stop<br><b>  &nbsp; &nbsp; &nbsp;string loadName = parser.createParam(string(""), "Load","A save file to restart from",'L', "Persistence" ).value();</b><br><b> </b><br><b>  &nbsp; &nbsp; &nbsp;unsigned maxGen = parser.createParam(unsigned(100), "maxGen", "Maximum number of generations",'G', "Stopping criterion" ).value();</b><br><b>  &nbsp; &nbsp; &nbsp;unsigned minGen = parser.createParam(unsigned(100), "minGen", "Minimum number of generations",'g', "Stopping criterion" ).value();</b><br><b>  &nbsp; &nbsp; &nbsp;unsigned steadyGen = parser.createParam(unsigned(100), "steadyGen", "Number of generations with no improvement",'s', "Stopping criterion" ).value();</b><br><b>  &nbsp; &nbsp;</b>// operators probabilities at the algorithm level<br><b>  &nbsp; &nbsp; &nbsp;double pCross = parser.createParam(double(0.6), "pCross", "Probability of Crossover", 'C', "Genetic Operators" ).value();</b><br><b>  &nbsp; &nbsp; &nbsp;double pMut = parser.createParam(double(0.1), "pMut", "Probability of Mutation", 'M', "Genetic Operators" ).value();</b><br><b>  &nbsp; &nbsp;</b>// relative rates for crossovers<br><b>  &nbsp; &nbsp; &nbsp;double hypercubeRate = parser.createParam(double(1), "hypercubeRate", "Relative rate for hypercube crossover", '\0', "Genetic Operators" ).value();</b><br><b>  &nbsp; &nbsp; &nbsp;double segmentRate = parser.createParam(double(1), "segmentRate", "Relative rate for segment crossover", '\0', "Genetic Operators" ).value();</b><br><b>  &nbsp; &nbsp; &nbsp;</b>// internal parameters for the mutations<br><b>  &nbsp; &nbsp; &nbsp;double EPSILON = parser.createParam(double(0.01), "EPSILON", "Width for uniform mutation", '\0', "Genetic Operators" ).value();</b><br><b>  &nbsp; &nbsp; &nbsp;</b><br><b>  &nbsp; &nbsp; &nbsp;double SIGMA = parser.createParam(double(0.3), "SIGMA", "Sigma for normal mutation", '\0', "Genetic Operators" ).value();</b><br><b>  &nbsp; &nbsp;</b>// relative rates for mutations<br><b>  &nbsp; &nbsp; &nbsp;double uniformMutRate = parser.createParam(double(1), "uniformMutRate", "Relative rate for uniform mutation", '\0', "Genetic Operators" ).value();</b><br><b>  &nbsp; &nbsp; &nbsp;double detMutRate = parser.createParam(double(1), "detMutRate", "Relative rate for det-uniform mutation", '\0', "Genetic Operators" ).value();</b><br><b>  &nbsp; &nbsp; &nbsp;double normalMutRate = parser.createParam(double(1), "normalMutRate", "Relative rate for normal mutation", '\0', "Genetic Operators" ).value();</b><br><b>  &nbsp; &nbsp; &nbsp;</b>// the name of the "status" file where all actual parameter values will be saved <br><b>  &nbsp; &nbsp; &nbsp;string str_status = parser.ProgramName() + ".status"; </b>// default value<br><b>  &nbsp; &nbsp; &nbsp;string statusName = parser.createParam(str_status, "status","Status file",'S', "Persistence" ).value();</b><br><b>  &nbsp; &nbsp;</b>// do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED<br><b>  &nbsp; &nbsp;</b>// i.e. in case you need parameters somewhere else, postpone these<br><b>  &nbsp; &nbsp; &nbsp;if (parser.userNeedsHelp())</b><br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</b><br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;parser.printHelp(cout);</b><br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit(1);</b><br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</b><br><b>  &nbsp; &nbsp; &nbsp;if (statusName != "")</b><br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</b><br><b>ofstream os(statusName.c_str());</b><br><b>os &lt;&lt; parser;		</b>// and you can use that file as parameter file<br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</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>  &nbsp;</b>/////////////////////////////<br><b>  &nbsp;</b>// Fitness function<br><b>  &nbsp;</b>////////////////////////////<br><b>  &nbsp;</b>// Evaluation: from a plain C++ fn to an EvalFunc Object<br><b>  &nbsp;</b>// you need to give the full description of the function<br><b>  &nbsp;eoEvalFuncPtr&lt;Indi, double, const vector&lt;double>& > plainEval(  &nbsp;real_value );</b><br><b>  &nbsp;</b>// ... to an object that counts the nb of actual evaluations<br><b>  &nbsp;eoEvalFuncCounter&lt;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>  &nbsp;</b>////////////////////////////////<br><b>  &nbsp;</b>// Initilisation of population<br><b>  &nbsp;</b>////////////////////////////////<br><b>  &nbsp;</b>// Either load or initialize<br><b>  &nbsp;</b>// create an empty pop<br><b>  &nbsp;eoPop&lt;Indi> pop;</b><br><b>  &nbsp;</b>// create a state for reading<br><b>  &nbsp;eoState inState;		</b>// a state for loading - WITHOUT the parser<br><b>  &nbsp;</b>// register the rng and the pop in the state, so they can be loaded,<br><b>  &nbsp;</b>// and the present run will be the exact conitnuation of the saved run<br><b>  &nbsp;</b>// eventually with different parameters<br><b>  &nbsp;inState.registerObject(rng);</b><br><b>  &nbsp;inState.registerObject(pop);</b><br><b>  &nbsp; &nbsp; &nbsp;</b><br><b>  &nbsp;if (loadName != "")</b><br><b>  &nbsp; &nbsp; &nbsp;{</b><br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inState.load(loadName); </b>//  &nbsp;load the pop and the rng<br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// the fitness is read in the file: <br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// do only evaluate the pop if the fitness has changed<br><b>  &nbsp; &nbsp; &nbsp;}</b><br><b>  &nbsp;else</b><br><b>  &nbsp; &nbsp; &nbsp;{</b><br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rng.reseed(seed);</b><br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// a Indi random initializer<br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// based on boolean_generator class (see utils/rnd_generator.h)<br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eoUniformGenerator&lt;double> uGen(-1.0, 1.0);</b><br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eoInitFixedLength&lt;Indi> random(vecSize, uGen);</b><br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// Init pop from the randomizer: need to use the append function<br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pop.append(popSize, random);  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b><br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b>// and evaluate pop (STL syntax)  &nbsp; &nbsp; &nbsp;<br><b>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;apply&lt;Indi>(eval, pop);</b><br><b>  &nbsp; &nbsp; &nbsp;} </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>  &nbsp;</b>// sort pop before printing it!<br><b>  &nbsp;pop.sort();</b><br><b>  &nbsp;</b>// Print (sorted) intial population (raw printout)<br><b>  &nbsp;cout &lt;&lt; "Initial Population" &lt;&lt; endl;</b><br>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -