📄 secondbitea.html
字号:
<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<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"> {</font></tt></b><br><b><tt><font color="#3366FF"> parser.printHelp(cout);</font></tt></b><br><b><tt><font color="#3366FF"> exit(1);</font></tt></b><br><b><tt><font color="#3366FF"> }</font></tt></b><br><b><tt><font color="#3366FF">if (statusParam.value() != "")</font></tt></b><br><b><tt><font color="#3366FF"> {</font></tt></b><br><b><tt><font color="#3366FF"> ofstreamos(statusParam.value().c_str());</font></tt></b><br><tt><font color="#3366FF"><b> os <<parser; </b>// and you can use that file as parameter file</font></tt><br><b><tt><font color="#3366FF"> }</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> </b>/////////////////////////////</font></tt><br><tt><font color="#CC0000"><b> </b>// Fitness function</font></tt><br><tt><font color="#CC0000"><b> </b>////////////////////////////</font></tt><br><tt><font color="#CC0000"><b> </b>// Evaluation: from a plainC++ fn to an EvalFunc Object ...</font></tt><br><b><tt><font color="#CC0000"> eoEvalFuncPtr<Indi, double, constvector<bool>& > 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"> eoEvalFuncCounter<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> </b>////////////////////////////////</font></tt><br><tt><font color="#993399"><b> </b>// Initilisation of population</font></tt><br><tt><font color="#993399"><b> </b>////////////////////////////////</font></tt><br><tt><font color="#993399"><b> </b>// Either load or initialize</font></tt><br><tt><font color="#993399"><b> </b>// create an empty pop</font></tt><br><b><tt><font color="#993399"> eoPop<Indi> pop;</font></tt></b><br><tt><font color="#993399"><b> </b>// create a state for reading</font></tt><br><tt><font color="#993399"><b> 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> </b>// and the present run willbe the exact conitnuation of the saved run</font></tt><br><tt><font color="#993399"><b> </b>// eventually with differentparameters</font></tt><br><b><tt><font color="#993399"> inState.registerObject(rng);</font></tt></b><br><b><tt><font color="#993399"> 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"> {</font></tt></b><br><a NAME="loadstate"></a><tt><font color="#993399"><b> inState.load(load_name); </b>// load the pop and the rng</font></tt><br><tt><font color="#993399"><b> </b>//the fitness is read in the file: </font></tt><br><tt><font color="#993399"><b> </b>//do only evaluate the pop if the fitness has changed</font></tt><br><b><tt><font color="#993399"> }</font></tt></b><br><b><tt><font color="#993399"> else</font></tt></b><br><b><tt><font color="#993399"> {</font></tt></b><br><b><tt><font color="#993399"> rng.reseed(seed);</font></tt></b><br><tt><font color="#993399"><b> </b>//a Indi random initializer</font></tt><br><tt><font color="#993399"><b> </b>//based on eoUniformGenerator class (see utils/eoRndGenerators.h)</font></tt><br><b><tt><font color="#993399"> eoUniformGenerator<bool>uGen;</font></tt></b><br><b><tt><font color="#993399"> eoInitFixedLength<Indi>random(vecSize, uGen);</font></tt></b><br><tt><font color="#993399"><b> </b>//Init pop from the randomizer: need to use the append function</font></tt><br><b><tt><font color="#993399"> pop.append(popSize, random); </font></tt></b><br><tt><font color="#993399"><b> </b>//and evaluate pop (STL syntax) </font></tt><br><b><tt><font color="#993399"> apply<Indi>(eval, pop);</font></tt></b><br><tt><font color="#993399"><b> } </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> </b>// sort pop for pretty printout</font></tt><br><b><tt><font color="#3366FF"> pop.sort();</font></tt></b><br><tt><font color="#3366FF"><b> </b>// Print (sorted) intial population(raw printout)</font></tt><br><b><tt><font color="#3366FF"> cout << "Initial Population"<< endl << pop << 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> </b>/////////////////////////////////////</font></tt><br><tt><font color="#009900"><b> </b>// selection and replacement</font></tt><br><tt><font color="#009900"><b> </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> </b>// The robust tournament selection</font></tt><br><tt><font color="#009900"><b> eoDetTournamentSelect<Indi> selectOne(tSize); </b>// tSize in [2,POPSIZE]</font></tt><br><tt><font color="#009900"><b> </b>// is now encapsulated in aeoSelectPerc (entage)</font></tt><br><b><tt><font color="#009900"> eoSelectPerc<Indi> select(selectOne);</font></tt></b><br><tt><font color="#009900"> // or eoSelectPerc<Indi> select(selectOne,rate); </font></tt><br><tt><font color="#009900"> // 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> </b>// And we now have the fullslection/replacement - though with </font></tt><br><tt><font color="#009900"><b> </b>// the same generational replacementat the moment :-)</font></tt><br><b><tt><font color="#009900"> eoGenerationalReplacement<Indi>replace; </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> </b>//////////////////////////////////////</font></tt><br><tt><font color="#993399"><b> </b>// The variation operators</font></tt><br><tt><font color="#993399"><b> </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> </b>// 1-point crossover for bitstring</font></tt><br><b><tt><font color="#993399"> eo1PtBitXover<Indi> xover1;</font></tt></b><br><tt><font color="#993399"><b> </b>// uniform crossover for bitstring</font></tt><br><b><tt><font color="#993399"> eoUBitXover<Indi> xoverU;</font></tt></b><br><tt><font color="#993399"><b> </b>// 2-pots xover</font></tt><br><b><tt><font color="#993399"> eoNPtsBitXover<Indi> xover2(2);</font></tt></b><br><tt><font color="#993399"><b> </b>// Combine them with relativerates</font></tt><br><b><tt><font color="#993399"> eoPropCombinedQuadOp<Indi> xover(xover1,onePointRate);</font></tt></b><br><b><tt><font color="#993399"> xover.add(xoverU, URate);</font></tt></b><br><b><tt><font color="#993399"> 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> </b>// standard bit-flip mutationfor bitstring</font></tt><br><b><tt><font color="#993399"> eoBitMutation<Indi> mutationBitFlip(pMutPerBit);</font></tt></b><br><tt><font color="#993399"><b> </b>// mutate exactly 1 bit perindividual</font></tt><br><b><tt><font color="#993399"> eoDetBitFlip<Indi> mutationOneBit; </font></tt></b><br><tt><font color="#993399"><b> </b>// Combine them with relativerates</font></tt><br><b><tt><font color="#993399"> eoPropCombinedMonOp<Indi> mutation(mutationBitFlip,bitFlipRate);</font></tt></b><br><b><tt><font color="#993399"> mutation.add(mutationOneBit, oneBitRate,true);</font></tt></b><br><tt><font color="#993399"><b> </b>// The operators are encapsulatedinto an eoTRansform object</font></tt><br><b><tt><font color="#993399"> eoSGATransform<Indi> transform(xover,pCross, mutation, pMut);</font></tt></b></td></tr></table>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -