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

📄 eolesson5.html

📁 这是linux下的进化计算的源代码。 === === === === === === === === === === === ===== check latest news at http:
💻 HTML
📖 第 1 页 / 共 2 页
字号:
a genotype built by the default constructor</font></b> of the EO class(here, an eoOneMax object) . Here you must at least fill <a href="eoOneMaxInit.html#operator">thecode for such randomization.</a><br>But you might also need some parameters (e.g. the size of the bitstringin eoOneMax): you should then <a href="eoOneMaxInit.html#constructor">passthem through the constructor</a> of the eoOneMaxInit class, and <a href="eoOneMaxInit.html#data">storeit in its private data</a>.<br>And of course you might need to add a destructor (no example here)if you use complex data type in the object.<p><font size=+1><font color="#FF0000">Parameters: </font><b><tt><font color="#993300">make_genotype_OneMax.h</font></tt></b></font><br>There is another file you will probably want to modify as far as initializationis concerned, that is <a href="make_genotype_OneMax.html">make_genotype_OneMax.h.</a>Such helper files are used for all components of advanced EO programs (seeLesson 4). The main reason for that is to allow separate compilation ofsuch sub-components for known EO types, as this is done in the directoriessrc/ga and src/es. But a useful consequence is to make your code modular.For instance, the <a href="make_genotype_OneMax.html#representation">make_genotype_OneMax.h</a>file takes care of all the preparation of all data regarding the eoInitobject - and returns to the main fonction a reference to an eoInit thatwill later be used to <a href="OneMaxEA.html#initPop">initialize the wholepopulation</a> in a representation-independent way.<br>What you have to do in that file is to set values for all parametersneeded by the eoOneMaxInit class, for instance by reading them from theparser: this allows later to modify them easily from the command-line.Note however that an alternative could be to pass the parser to the constructorof the eoOneMaxInit class, and to read all parameters there...<br><b><font color="#FF0000">Note</font></b>: Remember that the make_xxxfiles were first introduced to allow the user to compile sepearately mostof the code of standard Evolutionary Algorithm written in EO for bitstringand real vector representations (<br><hr WIDTH="100%"><a NAME="evaluation"></a><b><font color="#000099"><font size=+2>Evaluation:</font></font><tt><font color="#993300"><font size=+1>eoOneMaxEvalFunc.h</font></font></tt></b><p>The <b><tt><font color="#CC0000">eoOneMaxEvalFunc</font></tt></b> isthe object that will <b><font color="#FF6600">compute the fitness of aneoOneMax object</font></b>. You have to fill in the <a href="eoOneMaxEvalFunc.html#operator">codefor the computation of the fitness value</a> (there is no way that thiscan be done automatically :-) Note that this code must&nbsp; be run <b><font color="#FF6600">onlyif</font></b> the <b><tt><font color="#993300">_eo.invalid() </font></tt></b>testreturns true, to avoid computing the fitness of an object that has not&nbsp;been modified and thus still holds a valid fitness value. After computingthe fitness, store it into the object by calling the <b><tt><font color="#993300">fitness(FitnessType)</font></tt></b>method.<br>Should you need some specific data for that, the <a href="eoOneMaxEvalFunc.html#constructor">constructorof the object</a> is the place to get such data, and you should, again,store it in some <a href="eoOneMaxEvalFunc.html#data">private data</a>of the class.<br><hr WIDTH="100%"><a NAME="variation"></a><b><font color="#000099"><font size=+2>VariationOperators</font></font></b><br>You can write as many crossover and mutation operators as you like.However, we only provide the template files for <a href="eoOperators.html#crossover">quadraticcrossover </a>and <a href="eoOperators.html#mutation">mutation</a>, butyou could then easily write the equivalent code for binary crossover, orgeneral variation operator.<p><font size=+1><font color="#FF0000">Crossover: </font><b><tt><font color="#993300">eoOneMaxQuadCrossover.h</font></tt></b></font><br>As usual, you must go and write the <a href="eoOneMaxQuadCrossover.html#operator">codefor the operator()</a> that will perform the crossover, possibly modifyingboth arguments. Don't forget to <b><font color="#FF6600">update the booleanparameter</font></b> that will report whether the genotypes have been modified- allowing to recompute the fitness only of the ones that have actuallybeen modified. You can also have <b><font color="#FF6600">parameters</font></b>to the crossover by <a href="eoOneMaxQuadCrossover.html#constructor">passingthem to the constructor</a>, ans storing them in the <a href="eoOneMaxQuadCrossover.html#data">privatedata of the crossover object</a>.<p><font size=+1><font color="#FF0000">Mutation: </font><b><tt><font color="#993300">eoOneMaxMutation.h</font></tt></b></font><br>Here again, you must go and write the code for the operator() thatwill perform the mutation, eventually modifying its arguments. Don't forgetto <b><font color="#FF6600">update the boolean parameter</font></b> thatwill report whether the genotype has been modified - allowing to recomputethe fitness only of the ones that have actually been modified. You canalso have <b><font color="#FF6600">parameters</font></b> to the mutationby <a href="eoOneMaxMutation.html#constructor">passing them to the constructor</a>,ans storing them in the <a href="eoOneMaxMutation.html#data">private dataof the mutation object</a>.<p><font size=+1><font color="#FF0000">Parameters: </font><b><tt><font color="#993300">make_op_OneMax.h</font></tt></b></font><br>First of all, if you intend to use <b><font color="#FF6600">only onecrossover</font></b> operator and <b><font color="#FF6600">one mutation</font></b>operator, you have <b><font color="#FF6600">nothing to modify</font></b>in <a href="make_op_OneMax.html">make_op_OneMax function</a>, except maybereading user-defined parameters (copy the code from <a href="make_genotype_OneMax.html#representation">make_genotype_OneMax</a>)and passing them to <a href="make_op_OneMax.html#cross_ctor">the appropriateoperator constructor</a>.<br>As it is written now, it allows you enter a crossover probability <b><tt><font color="#CC33CC">Pcross</font></tt></b>and a mutation probability <b><tt><font color="#CC33CC">Pmut</font></tt></b>,and to build an eoGeneralOp that will call in sequence the <b><tt><font color="#CC33CC">eoOneMaxQuadCrossover</font></tt></b>that is defined above with probability <b><tt><font color="#CC33CC">Pcross</font></tt></b>and the <b><tt><font color="#CC33CC">eoOneMaxMutation</font></tt></b> alsodefined above with probability <b><tt><font color="#CC33CC">Pmut</font></tt></b>.&nbsp;Beware that all allocated objects must be stored in the eoState otherwiseyou will run into trouble (see <a href="eoProgramming.html#memory">EO MemoryManagement explanations</a>).<p>However, everything is there (commented out) to allow you to use <a href="make_op_OneMax.html#crossover">morethan one crossover</a> and <a href="make_op_OneMax.html#mutation">one mutation</a>- provided you write the code for them , of course.<p>The code starts by defining an <b><tt><font color="#CC33CC">eoOneMaxQuadCrossover</font></tt></b>object,then reads some application rate that is totally useless if you have onlyone crossover, then creates an <b><tt><font color="#CC33CC"><a href="eoLesson2.html#combined_operators">eoPropCombinedQuadOp</a></font></tt></b>with this simple oeprator. The same story repeats for the mutation. <a href="make_op_OneMax.html#donottouch">Finally</a>,the <b><tt><font color="#CC33CC"><a href="eoOperators.html#general_combination">eoGeneralOp</a></font></tt></b>is created from those combined operators and the individulal level probabilities<b><tt><font color="#CC33CC">Pcross</font></tt></b>and&nbsp; <b><tt><font color="#CC33CC">Pmut</font></tt></b>.<p>In order to add a second crossover operator for instance (called <b><tt><font color="#CC33CC">eoOneMaxSecondCrossover</font></tt></b>in the commented code) all you need to is<ul><li>Create the code for the new class&nbsp; <b><tt><font color="#CC33CC">eoOneMaxSecondCrossover</font></tt></b>:simply copy the <b><tt><font color="#993300">eoOneMaxQuadCrossover.h</font></tt></b>file into <b><tt><font color="#993300">eoOneMaxSecondCrossover.h</font></tt></b>&nbsp;and change all names <b><tt><font color="#CC33CC">eoOneMaxQuadCrossover</font></tt></b>to&nbsp; <b><tt><font color="#CC33CC">eoOneMaxSecondCrossover </font></tt></b>(inlcludingafter the #ifdef statement!!!);</li><li>Uncomment the <a href="make_op_OneMax.html#crossover">corresponding linesin make_op_OneMax.h</a>, possibly adding user-defined parameter reading</li><li>Repeat as many times as you have operators, using of course different names!The same recommendations hold for mutations.</li></ul>In case you have more than one operator of a kind, then of course the relativeweights of application do make sense, allowing you to tune with command-lineparameters the proportion with which each operator that will be applied.<p><hr WIDTH="100%"><a NAME="main"></a><b><font color="#000099"><font size=+2>Mainfiles: </font></font><tt><font color="#993300"><font size=+1>OneMaxEA.cpp</font></font></tt></b>and<b><tt><font color="#993300"><font size=+1> OneMaxLibEA.cpp</font></font></tt></b><font color="#FF0000"><font size=+1></font></font><p>As a start, you should only (eventually) modify in <b><tt><font color="#993300">OneMaxEA.cpp</font></tt></b>the <a href="OneMax.cpp#fitness">type of fitness</a> youwill be handling, namely <b><tt><font color="#CC0000">double</font></tt></b>if you are <b><font color="#FF6600">maximizing</font></b>, or <b><tt><font color="#CC0000">eoMinimizingFitness</font></tt></b>if you are <b><font color="#FF6600">minimizing</font></b>. Then running<b><tt><font color="#FF6666">make</font></tt></b> will result in a perfectlyvalid executable named <b><tt><font color="#FF6666">OneMaxEA</font></tt></b>.<p>The skeleton of the main file here mimics that of the main file in<a href="eoLesson4.html">Lesson4</a>,and <a href="eoLesson4.html#programmerguide">uses the make_xxx separatefiles construct</a>: the part of an Evolutionary Algorithm related to the<b><font color="#009900">evolutionengine</font></b> is <b><font color="#FF6600">indepenent of the representation</font></b>,and can be directly used ... provided it is compiled with the right template(remember everything in EO is <a href="eoProgramming.html#templates">templatizedover the EO objects it handles</a>.&nbsp; Main file <b><tt><font color="#993300">OneMaxEA.cpp</font></tt></b>is written so that it includes eveything - and thus everytime you run <b><tt><font color="#FF6666">make</font></tt></b>(or <b><tt><font color="#FF6666">make OneMaxEA</font></tt></b>), you compilethe code for make_pop, make_continue and make_checkpoint that is definedin the .../src/do directory.<p>The basic construct is (for instance to build the evolution engine)<br>&nbsp;<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE ><tr><td><b><tt><font color="#009900">#include &lt;do/make_algo_scalar.h></font></tt></b><br><b><tt><font color="#009900">eoAlgo&lt;Indi>&amp;&nbsp; make_algo_scalar(eoParser&amp;_parser, eoState&amp; _state, eoEvalFunc&lt;Indi>&amp; _eval, eoContinue&lt;Indi>&amp;_continue, eoGenOp&lt;Indi>&amp; _op)</font></tt></b><br><b><tt><font color="#009900">{</font></tt></b><br><b><tt><font color="#009900">&nbsp;return do_make_algo_scalar(_parser,_state, _eval, _continue, _op);</font></tt></b><br><b><tt><font color="#009900">}</font></tt></b></td></tr></table>First, include the code (from the do directory). Then define the make_xxxfunction from the do_make_xxx function.<br>Of course, such construct is stupid here, as you could perfectly calldirectly the do_make_xxx function in the main. However, if you ever wantto do separate compilation of some parts, you will need such construct(see <a href="eoLesson4.html">Lesson4</a>) so we have kept it here forconsistency reasons.<p>Go in your application directory, and look at the differences betweenboth files and you'll see how this is handled in both cases.<p><b><font color="#FF0000">Reducing compilation time:</font></b><br>However, we also provide another main file (<b><tt><font color="#993300">OneMaxLibEA.cpp</font></tt></b>)thatonly includes the code that is specific to your application, and is supposedto be linked with another object file that will contain the code that isrepresentation independent (<b><tt><font color="#993300">make_OneMax.cpp</font></tt></b>).&nbsp;This is done by running <b><tt><font color="#FF6666">make OneMaxLibEA</font></tt></b>on the command-line.<br>For example, on a PentiumIII 400MHz with g++ 2.96, compiling <b><tt><font color="#FF6666">OneMaxEA</font></tt></b>takes about <b><font color="#FF6600">33s</font></b>, compiling both <b><tt><font color="#FF6666">make_OneMax.o</font></tt></b>and <b><tt><font color="#FF6666">OneMaxLibEA</font></tt></b> takes about<b><font color="#FF6600">54s</font></b> but compiling only <b><tt><font color="#FF6666">OneMaxLibEA</font></tt></b>takes only <b><font color="#FF6600">14s</font></b>&nbsp; <b><tt><font color="#FF6666">make_OneMax.o</font></tt></b>is up-to-date ...<p><b><font color="#FF0000">Hints:</font></b><br>While developping the genotype structure itself in file <b><tt><font color="#993300">eoOneMax.h</font></tt></b>,you should use the <b><tt><font color="#993300">OneMaxEA.cpp</font></tt></b>file. But after the <b><tt><font color="#993300">eoOneMax.h</font></tt></b>file is frozen, you should use the <b><tt><font color="#993300">eoOneMaxLibEA.cpp</font></tt></b>file. Of course, both&nbsp; resulting programs are strictly identical!<p><hr WIDTH="100%"><font color="#000000"><a href="eoLesson4.html">Lesson4</a> -<a href="eoLesson6.html">Lesson 6</a> -<a href="eoTutorial.html">Mainpage</a> -<a href="eoTopDown.html">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based</a>- <a href="eoProgramming.html">Hints</a> -<b> <font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/index.html">EOdocumentation</a></font></font></b></font><br><hr><address><font color="#000000"><a href="mailto:Marc.Schoenauer@inria.fr">Marc Schoenauer</a></font></address></body></html>

⌨️ 快捷键说明

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