📄 secondbitea.html
字号:
<a NAME="stop"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE ><tr><td><tt><font color="#3366FF"><b> </b>//////////////////////////////////////</font></tt><br><tt><font color="#3366FF"><b> </b>// termination condition seeFirstBitEA.cpp</font></tt><br><tt><font color="#3366FF"><b> </b>/////////////////////////////////////</font></tt><br><b><tt><font color="#3366FF"> eoGenContinue<Indi> genCont(maxGen);</font></tt></b><br><b><tt><font color="#3366FF"> eoSteadyFitContinue<Indi> steadyCont(minGen,steadyGen);</font></tt></b><br><b><tt><font color="#3366FF"> eoFitContinue<Indi> fitCont(vecSize);</font></tt></b><br><b><tt><font color="#3366FF"> eoCombinedContinue<Indi> continuator(genCont);</font></tt></b><br><b><tt><font color="#3366FF"> continuator.add(steadyCont);</font></tt></b><br><b><tt><font color="#3366FF"> continuator.add(fitCont);</font></tt></b></td></tr></table><a NAME="checkpoint"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE ><tr><td><tt><font color="#3366FF"><b> </b>// but now you want to makemany different things every generation </font></tt><br><tt><font color="#3366FF"><b> </b>// (e.g. statistics, plots,...).</font></tt><br><tt><font color="#3366FF"><b> </b>// the class eoCheckPoint isdedicated to just that:</font></tt><br><tt><font color="#3366FF"><b> </b>// Declare a checkpoint (froma continuator: an eoCheckPoint </font></tt><br><tt><font color="#3366FF"><b> </b>// IS AN eoContinue and willbe called in the loop of all algorithms)</font></tt><br><b><tt><font color="#3366FF"> eoCheckPoint<Indi> checkpoint(continuator);</font></tt></b><p><a NAME="param_declare"></a><tt><font color="#3366FF"><b> </b>//Create a counter parameter</font></tt><br><b><tt><font color="#3366FF"> eoValueParam<unsigned>generationCounter(0, "Gen.");</font></tt></b><p><a NAME="param_pass"></a><tt><font color="#3366FF"><b> </b>//Create an incrementor (sub-class of eoUpdater). Note that the </font></tt><br><tt><font color="#3366FF"><b> </b>// parameter'svalue is passed by reference, </font></tt><br><tt><font color="#3366FF"><b> </b>// so everytime the incrementer is updated (every generation),</font></tt><br><tt><font color="#3366FF"><b> </b>// the datain generationCounter will change.</font></tt><br><b><tt><font color="#3366FF"> eoIncrementor<unsigned>increment(generationCounter.value());</font></tt></b><br><a NAME="updater_pass"></a><tt><font color="#3366FF"><b> </b>//Add it to the checkpoint, </font></tt><br><tt><font color="#3366FF"><b> </b>// so thecounter is updated (here, incremented) every generation</font></tt><br><b><tt><font color="#3366FF"> checkpoint.add(increment);</font></tt></b><br><a NAME="stat_declare"></a><tt><font color="#3366FF"><b> </b>//now some statistics on the population:</font></tt><br><tt><font color="#3366FF"><b> </b>// Best fitnessin population</font></tt><br><b><tt><font color="#3366FF"> eoBestFitnessStat<Indi>bestStat;</font></tt></b><br><tt><font color="#3366FF"><b> </b>// Secondmoment stats: average and stdev</font></tt><br><b><tt><font color="#3366FF"> eoSecondMomentStats<Indi>SecondStat;</font></tt></b><br><a NAME="stat_pass"></a><tt><font color="#3366FF"><b> </b>//Add them to the checkpoint to get them called at the appropriate time</font></tt><br><b><tt><font color="#3366FF"> checkpoint.add(bestStat);</font></tt></b><br><b><tt><font color="#3366FF"> checkpoint.add(SecondStat);</font></tt></b><br><tt><font color="#3366FF"><b> </b>// The Stdoutmonitor will print parameters to the screen ...</font></tt><br><a NAME="monitor_declare"></a><b><tt><font color="#3366FF"> eoStdoutMonitor monitor(false);</font></tt></b><p><tt><font color="#3366FF"><b> </b>// when calledby the checkpoint (i.e. at every generation)</font></tt><br><a NAME="monitor_pass"></a><b><tt><font color="#3366FF"> checkpoint.add(monitor);</font></tt></b><br><tt><font color="#3366FF"><b> </b>// the monitorwill output a series of parameters: add them </font></tt><br><a NAME="monitor_add"></a><b><tt><font color="#3366FF"> monitor.add(generationCounter);</font></tt></b><a NAME="eval_monitor"></a><br><tt><font color="#3366FF"><b> monitor.add(eval);</b>//because now eval is an eoEvalFuncCounter!</font></tt><br><b><tt><font color="#3366FF"> monitor.add(bestStat);</font></tt></b><br><b><tt><font color="#3366FF"> monitor.add(SecondStat);</font></tt></b><br><tt><font color="#3366FF"><b> </b>// A filemonitor: will print parameters to ... a File, yes, you got it!</font></tt><br><b><tt><font color="#3366FF"> eoFileMonitorfileMonitor("stats.xg", " ");</font></tt></b><p><tt><font color="#3366FF"><b> </b>// the checkpointmechanism can handle multiple monitors</font></tt><br><b><tt><font color="#3366FF"> checkpoint.add(fileMonitor);</font></tt></b><br><tt><font color="#3366FF"><b> </b>// the fileMonitorcan monitor parameters, too, but you must tell it!</font></tt><br><b><tt><font color="#3366FF"> fileMonitor.add(generationCounter);</font></tt></b><br><b><tt><font color="#3366FF"> fileMonitor.add(bestStat);</font></tt></b><br><b><tt><font color="#3366FF"> fileMonitor.add(SecondStat);</font></tt></b><br><tt><font color="#3366FF"><b> </b>// Last typeof item the eoCheckpoint can handle: state savers:</font></tt><br><a NAME="outstate_declare"></a><b><tt><font color="#3366FF"> eoState outState;</font></tt></b><br><tt><font color="#3366FF"><b> </b>// Registerthe algorithm into the state</font></tt><br><a NAME="outstate_register"></a><b><tt><font color="#3366FF"> outState.registerObject(parser);</font></tt></b><br><b><tt><font color="#3366FF"> outState.registerObject(pop);</font></tt></b><br><b><tt><font color="#3366FF"> outState.registerObject(rng);</font></tt></b><br><tt><font color="#3366FF"><b> </b>// and feedthe state to state savers</font></tt><br><a NAME="statesaver_declare"></a><tt><font color="#3366FF">// savestate every 100th generation</font></tt><br><b><tt><font color="#3366FF"> eoCountedStateSaverstateSaver1(100, outState, "generation"); </font></tt></b><br><tt><font color="#3366FF"><b> </b>// save stateevery 1 seconds </font></tt><br><b><tt><font color="#3366FF"> eoTimedStateSaver stateSaver2(1, outState, "time"); </font></tt></b><br><a NAME="statesaver_pass"></a><tt><font color="#3366FF"><b> </b>//Don't forget to add the two savers to the checkpoint</font></tt><br><b><tt><font color="#3366FF"> checkpoint.add(stateSaver1);</font></tt></b><br><b><tt><font color="#3366FF"> checkpoint.add(stateSaver2);</font></tt></b><br><tt><font color="#3366FF"><b> </b>// and that'sit for the (control and) output</font></tt></td></tr></table><a NAME="generation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE ><tr><td><tt><font color="#FF6666"><b> </b>/////////////////////////////////////////</font></tt><br><tt><font color="#FF6666"><b> </b>// the algorithm</font></tt><br><tt><font color="#FF6666"><b> </b>////////////////////////////////////////</font></tt><br><tt><font color="#FF6666"><b> </b>// Easy EA requires </font></tt><br><tt><font color="#FF6666"> // stopping criterion, eval, selection,transformation, replacement</font></tt><br><b><tt><font color="#FF6666"> eoEasyEA<Indi> gga(checkpoint,eval, select, transform, replace);</font></tt></b><br><tt><font color="#FF6666"><b> </b>// Apply algo to pop - that'sit!</font></tt><br><b><tt><font color="#FF6666"> gga(pop);</font></tt></b></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>// Print (sorted) final population</font></tt><br><b><tt><font color="#3366FF"> pop.sort();</font></tt></b><br><b><tt><font color="#3366FF"> cout << "FINAL Population\n"<< pop << endl;</font></tt></b></td></tr></table><a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE ><tr><td><b><tt><font color="#993300">}</font></tt></b><br><tt><font color="#993300">// A main that catches the exceptions</font></tt><br><b><tt><font color="#993300">int main(int argc, char **argv)</font></tt></b><br><b><tt><font color="#993300">{</font></tt></b><br><b><tt><font color="#993300">#ifdef _MSC_VER</font></tt></b><br><b><tt><font color="#993300"> int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</font></tt></b><br><b><tt><font color="#993300"> flag|= _CRTDBG_LEAK_CHECK_DF;</font></tt></b><br><b><tt><font color="#993300"> _CrtSetDbgFlag(flag);</font></tt></b><br><tt><font color="#993300">// _CrtSetBreakAlloc(100);</font></tt><br><b><tt><font color="#993300">#endif</font></tt></b><br><b><tt><font color="#993300"> try</font></tt></b><br><b><tt><font color="#993300"> {</font></tt></b><br><b><tt><font color="#993300"> main_function(argc, argv);</font></tt></b><br><b><tt><font color="#993300"> }</font></tt></b><br><b><tt><font color="#993300"> catch(exception&e)</font></tt></b><br><b><tt><font color="#993300"> {</font></tt></b><br><b><tt><font color="#993300"> cout << "Exception: " << e.what() << '\n';</font></tt></b><br><b><tt><font color="#993300"> }</font></tt></b><br><b><tt><font color="#993300"> return 1;</font></tt></b><br><b><tt><font color="#993300">}</font></tt></b></td></tr></table><hr WIDTH="100%"><a href="eoLesson3.html">Back to Lesson 3</a> - <a href="eoTutorial.html">Tutorialmain page </a>- <a href="eoTopDown.html">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based</a>-<a href="eoProgramming.html">Programminghints</a> - <b><font face="Arial,Helvetica"><a href="../../doc/html/index.html">EOdocumentation</a></font></b><hr><address><a href="mailto:Marc.Schoenauer@inria.fr">Marc Schoenauer</a></address><br><!-- Created: Nov 2000 --><!-- hhmts start -->Last modified: Sun Nov26 09:31:04 2000<!-- hhmts end --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -