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

📄 secondbitea.html

📁 这是linux下的进化计算的源代码。 === === === === === === === === === === === ===== check latest news at http:
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<p><tt><font color="#3366FF">// the name of the "status" file where allactual 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&lt;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 HAVEBEEN PROCESSED</font></tt><br><tt><font color="#3366FF">// i.e. in case you need parameters somewhereelse, postpone these</font></tt><br><b><tt><font color="#3366FF">if (parser.userNeedsHelp())</font></tt></b><br><b><tt><font color="#3366FF">&nbsp;&nbsp; {</font></tt></b><br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; parser.printHelp(cout);</font></tt></b><br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);</font></tt></b><br><b><tt><font color="#3366FF">&nbsp;&nbsp; }</font></tt></b><br><b><tt><font color="#3366FF">if (statusParam.value() != "")</font></tt></b><br><b><tt><font color="#3366FF">&nbsp;&nbsp; {</font></tt></b><br><b><tt><font color="#3366FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ofstreamos(statusParam.value().c_str());</font></tt></b><br><tt><font color="#3366FF"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; os &lt;&lt;parser; </b>// and you can use that file as parameter file</font></tt><br><b><tt><font color="#3366FF">&nbsp;&nbsp; }</font></tt></b></td></tr><tr><td></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>/////////////////////////////</font></tt><br><tt><font color="#CC0000"><b>&nbsp;</b>// Fitness function</font></tt><br><tt><font color="#CC0000"><b>&nbsp;</b>////////////////////////////</font></tt><br><tt><font color="#CC0000"><b>&nbsp;</b>// Evaluation: from a plainC++ fn to an EvalFunc Object ...</font></tt><br><b><tt><font color="#CC0000">&nbsp;eoEvalFuncPtr&lt;Indi, double, constvector&lt;bool>&amp; > plainEval( binary_value );</font></tt></b><br><a NAME="evalcounter"></a><tt><font color="#CC0000">// ... to an objectthat counts the nb of actual evaluations</font></tt><br><b><tt><font color="#CC0000">&nbsp;eoEvalFuncCounter&lt;Indi> eval(plainEval);</font></tt></b></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>////////////////////////////////</font></tt><br><tt><font color="#993399"><b>&nbsp;</b>// Initilisation of population</font></tt><br><tt><font color="#993399"><b>&nbsp;</b>////////////////////////////////</font></tt><br><tt><font color="#993399"><b>&nbsp;</b>// Either load or initialize</font></tt><br><tt><font color="#993399"><b>&nbsp;</b>// create an empty pop</font></tt><br><b><tt><font color="#993399">&nbsp;eoPop&lt;Indi> pop;</font></tt></b><br><tt><font color="#993399"><b>&nbsp;</b>// create a state for reading</font></tt><br><tt><font color="#993399"><b>&nbsp;eoState inState; </b>// a statefor loading - WITHOUT the parser</font></tt><br><a NAME="register"></a><tt><font color="#993399">// register the rngand the pop in the state, so they can be loaded,</font></tt><br><tt><font color="#993399"><b>&nbsp;</b>// and the present run willbe the exact conitnuation of the saved run</font></tt><br><tt><font color="#993399"><b>&nbsp;</b>// eventually with differentparameters</font></tt><br><b><tt><font color="#993399">&nbsp;inState.registerObject(rng);</font></tt></b><br><b><tt><font color="#993399">&nbsp;inState.registerObject(pop);</font></tt></b><p><a NAME="instate.load"></a><b><tt><font color="#993399">if (load_name!= "")</font></tt></b><br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp; {</font></tt></b><br><a NAME="loadstate"></a><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inState.load(load_name); </b>//&nbsp; load the pop and the rng</font></tt><br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b>//the fitness is read in the file:&nbsp;</font></tt><br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b>//do only evaluate the pop if the fitness has changed</font></tt><br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp; }</font></tt></b><br><b><tt><font color="#993399">&nbsp;else</font></tt></b><br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp; {</font></tt></b><br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rng.reseed(seed);</font></tt></b><br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b>//a Indi random initializer</font></tt><br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b>//based on eoUniformGenerator class (see utils/eoRndGenerators.h)</font></tt><br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eoUniformGenerator&lt;bool>uGen;</font></tt></b><br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eoInitFixedLength&lt;Indi>random(vecSize, uGen);</font></tt></b><br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b>//Init pop from the randomizer: need to use the append function</font></tt><br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pop.append(popSize, random);&nbsp;</font></tt></b><br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b>//and evaluate pop (STL syntax)&nbsp;</font></tt><br><b><tt><font color="#993399">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;apply&lt;Indi>(eval, pop);</font></tt></b><br><tt><font color="#993399"><b>&nbsp;&nbsp;&nbsp;&nbsp; } </b>// endof initialization of the population</font></tt></td></tr></table><a NAME="output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE ><tr><td><tt><font color="#3366FF"><b>&nbsp;</b>// sort pop for pretty printout</font></tt><br><b><tt><font color="#3366FF">&nbsp;pop.sort();</font></tt></b><br><tt><font color="#3366FF"><b>&nbsp;</b>// Print (sorted) intial population(raw printout)</font></tt><br><b><tt><font color="#3366FF">&nbsp;cout &lt;&lt; "Initial Population"&lt;&lt; endl &lt;&lt; pop &lt;&lt; endl;</font></tt></b></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>&nbsp;</b>/////////////////////////////////////</font></tt><br><tt><font color="#009900"><b>&nbsp;</b>// selection and replacement</font></tt><br><tt><font color="#009900"><b>&nbsp;</b>////////////////////////////////////</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>&nbsp;</b>// The robust tournament selection</font></tt><br><tt><font color="#009900"><b>&nbsp;eoDetTournamentSelect&lt;Indi> selectOne(tSize);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b>// tSize in [2,POPSIZE]</font></tt><br><tt><font color="#009900"><b>&nbsp;</b>// is now encapsulated in aeoSelectPerc (entage)</font></tt><br><b><tt><font color="#009900">&nbsp;eoSelectPerc&lt;Indi> select(selectOne);</font></tt></b><br><tt><font color="#009900">&nbsp;// or eoSelectPerc&lt;Indi> select(selectOne,rate);&nbsp;</font></tt><br><tt><font color="#009900">&nbsp;// but by default rate==1</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>&nbsp;</b>// And we now have the fullslection/replacement - though with&nbsp;</font></tt><br><tt><font color="#009900"><b>&nbsp;</b>// the same generational replacementat the moment :-)</font></tt><br><b><tt><font color="#009900">&nbsp;eoGenerationalReplacement&lt;Indi>replace;&nbsp;</font></tt></b></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>&nbsp;</b>//////////////////////////////////////</font></tt><br><tt><font color="#993399"><b>&nbsp;</b>// The variation operators</font></tt><br><tt><font color="#993399"><b>&nbsp;</b>//////////////////////////////////////</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>&nbsp;</b>// 1-point crossover for bitstring</font></tt><br><b><tt><font color="#993399">&nbsp;eo1PtBitXover&lt;Indi> xover1;</font></tt></b><br><tt><font color="#993399"><b>&nbsp;</b>// uniform crossover for bitstring</font></tt><br><b><tt><font color="#993399">&nbsp;eoUBitXover&lt;Indi> xoverU;</font></tt></b><br><tt><font color="#993399"><b>&nbsp;</b>// 2-pots xover</font></tt><br><b><tt><font color="#993399">&nbsp;eoNPtsBitXover&lt;Indi> xover2(2);</font></tt></b><br><tt><font color="#993399"><b>&nbsp;</b>// Combine them with relativerates</font></tt><br><b><tt><font color="#993399">&nbsp;eoPropCombinedQuadOp&lt;Indi> xover(xover1,onePointRate);</font></tt></b><br><b><tt><font color="#993399">&nbsp;xover.add(xoverU, URate);</font></tt></b><br><b><tt><font color="#993399">&nbsp;xover.add(xover2, twoPointsRate,true);</font></tt></b></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>&nbsp;</b>// standard bit-flip mutationfor bitstring</font></tt><br><b><tt><font color="#993399">&nbsp;eoBitMutation&lt;Indi>&nbsp; mutationBitFlip(pMutPerBit);</font></tt></b><br><tt><font color="#993399"><b>&nbsp;</b>// mutate exactly 1 bit perindividual</font></tt><br><b><tt><font color="#993399">&nbsp;eoDetBitFlip&lt;Indi> mutationOneBit;&nbsp;</font></tt></b><br><tt><font color="#993399"><b>&nbsp;</b>// Combine them with relativerates</font></tt><br><b><tt><font color="#993399">&nbsp;eoPropCombinedMonOp&lt;Indi> mutation(mutationBitFlip,bitFlipRate);</font></tt></b><br><b><tt><font color="#993399">&nbsp;mutation.add(mutationOneBit, oneBitRate,true);</font></tt></b><br><tt><font color="#993399"><b>&nbsp;</b>// The operators are&nbsp; encapsulatedinto an eoTRansform object</font></tt><br><b><tt><font color="#993399">&nbsp;eoSGATransform&lt;Indi> transform(xover,pCross, mutation, pMut);</font></tt></b></td></tr></table>

⌨️ 快捷键说明

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