📄 eolesson3.html
字号:
<br>and see the algorithm run as before (OneMax optimized on 8-bits bitstrings).But you can now type in<br> <b><tt><font color="#FF6666">SecondBitEA --vecSize=100</font></tt></b><br>and see the output of the optimization of OneMax on 100-bit bitstrings.<br> <li>Take a look at all available parameters by typing in</li><br> <b><tt><font color="#FF6666">SecondBitEA --help</font></tt></b><br>or by going into the code: all parameter inputs have been grouped inthe<a href="SecondBitEA.html#parametres">read_param</a> function.<br> <li>After running the algorithm, a new file has been created, named <b><tt><font color="#990000">SecondBitEA.status</font></tt></b>:it contains the list of all actual parameters used, and can directly beused as parameter input file: change the file name (e.g. to <b><tt><font color="#660000">SecondBitEA.param</font></tt></b>),edit it, change whichever parameter you want, and type in</li><br> <b><tt><font color="#FF6666">SecondBitEA @SecondBitEA.param</font></tt></b><br>and you will see all values that you defined into the file taken intoaccount.<br> <li>The <font color="#FF6600">priority</font> remains to the <font color="#FF6600">command-line</font>,so you can still override the values in the parameter file by giving anew value directly on the command-line.</li></ul><hr WIDTH="50%"><a NAME="parameters"></a><b><font color="#000099">eoParser:</font><font color="#FF0000">Programming parameter input:</font></b><br>The code of SeconBitEA provides examples of parameters reading. Letstake the example of the <a href="eoProgramming.html#random">random numbergenerator </a><b><tt><font color="#660000">seed</font></tt></b>. Of course, you first need to <a href="SecondBitEA.html#parser_declare">declarean eoParser object</a> (it needs the standard argc and argv in its constructor).<ul><li>You must first <a href="SecondBitEA.html#random">declare a parameter</a>of type <b><tt><font color="#660000">uint32</font></tt></b> (32-bits integer).The arguments are: default value, long keyword, comment (that will appearin the help message and in the output "status" file if any) and optionalcharacter keyword.</li><li>Then you must <a href="SecondBitEA.html#seed_process">pass it to the parser</a>using the processParam method. The optional argument is a section name,that will be used to make the output of the parser look clean and ordered.</li><li>Finally, you need to <a href="SecondBitEA.html#seed_assign">assign thevalue</a> to the variable <b><tt><font color="#660000">seed</font></tt></b>.Note that the <b><tt><font color="#990000">value()</font></tt></b> methodof eoParam returns a reference, so you can eventually modify its valuesomewhere else later (though of course this is not any useful for variable<b><tt><font color="#660000">seed</font></tt></b>!).</li></ul>There is however another way to achieve the same result in less lines ofcode - with a different memory management. This is what is done in thecode for eoRealEA. The same parameter for the <a href="eoProgramming.html#random">randomnumber generator </a><b><tt><font color="#660000">seed</font></tt></b>is read, but in <a href="SecondRealEA.html#random">one single line of code</a>. The only difference is that now you cannot access the eoValueParam objectitself - but this is not often necessary.<br>Be careful to ensure that the type of the default value in the callto <a href="../../doc/html/classeo_parameter_loader.html#a3">eoParameterLoader::createParam</a>method as this is the only way the compiler can desambiguate the template(remember that eoParameterLoader is <font color="#000000"><a href="../../doc/html/classeo_parser.html">abase class for eoParser</a></font>.<br><hr WIDTH="100%"><br><a NAME="state"></a><b><font color="#000099"><font size=+2>eoState:saving and loading</font></font></b><font color="#000000">You might havenoticed in the </font><b><tt><font color="#660000">read_param</font></tt></b><font color="#000000">described above a </font><font color="#FF6600">new parameter</font><font color="#000000">named </font><b><tt><font color="#660000"><a href="SecondBitEA.html#_load_name">load_name.</a></font></tt></b><font color="#000000">Now if you go to the <a href="SecondBitEA.html#init">init section</a> ofthe code, you will see an alternative way of </font><font color="#CC33CC">initializingthe population</font><font color="#000000">: if load_name is an empty string,then we do as in the preceding example and use an eoInitFixedLength object.However, if a load_name name was entered, the population is read throughthe </font><b><tt><font color="#660000"><a href="SecondBitEA.html#instate.load">inState.load(load_name)</a></font></tt></b><font color="#000000">instruction. Moreover, the comment says "Loading pop </font><font color="#FF6600">andrng</font><font color="#000000">".</font><p><font color="#000000">This is made possible using the </font><b><tt><font color="#3366FF">eoState</font></tt></b><font color="#000000">class. </font><b><tt><font color="#3366FF">eoState</font></tt></b><font color="#000000">objects maintain references to </font><b><tt><font color="#660000">eoObjects</font></tt></b><font color="#000000">that have both an input method (</font><b><tt><font color="#3366FF">readFrom</font></tt></b><font color="#000000">)and an output method (</font><b><tt><font color="#3366FF">printOn</font></tt></b><font color="#000000">),i.e. that derive from the base class </font><b><font face="Arial,Helvetica"><font color="#660000"><font size=+1><a href="../../doc/html/classeo_persistent.html">eoPersistent</a></font></font></font></b><font color="#000000">.You must first </font><b><tt><font color="#3366FF"><a href="SecondBitEA.html#register">register</a></font></tt></b><font color="#000000">object into a state, and can then save them to a (text) file, and laterread them from that file using the </font><b><tt><font color="#3366FF">load</font></tt></b><font color="#000000">method, as done <a href="SecondBitEA.html#loadstate">here</a>.</font><br><font color="#000000">Of course, you can call the </font><b><tt><font color="#3366FF">save</font></tt></b><font color="#000000">method for an </font><b><tt><font color="#3366FF">eoState</font></tt></b><font color="#000000">object anywhere in the code. But the <a href="#statesaver">checkpointing</a>mechanism offers you better ways to do that - and it's so easy ....</font><p>Note that an eoState alos has another use in EO whan it comes to memorymanagement: it can be a repository of pointers that are not allocated withinobects - allowing to delete them by simply deleting the eoState (see <a href="eoLesson4.html#memory">Lesson4)</a>.<br><hr WIDTH="100%"><a NAME="checkpoint"></a><b><font color="#000099"><font size=+2>eoCheckpoint:every generation I'd like to ...</font></font></b><br><font color="#000000">The checkpointing mechanism is a very powerfulconstruct to perform some </font><b><font color="#FF6600">systematic actions</font></b><font color="#000000">every generation - like </font><font color="#FF6600">saving things</font><font color="#000000">(using eoState objects described above), computing </font><font color="#FF6600">statistics</font><font color="#000000">on the population, </font><font color="#FF6600">updating</font><font color="#000000">dynamical parameters or </font><font color="#FF6600">displaying</font><font color="#000000">information.</font><p><b><tt><font color="#3366FF">eoCheckpoint</font></tt></b><font color="#000000">objects are </font><b><tt><font color="#3366FF">eoContinue</font></tt></b><font color="#000000">objects that contain </font><font color="#FF6600">pointers to differenttypes</font><font color="#000000"> of objects. When their </font><b><tt><font color="#660000">operator()</font></tt></b><font color="#000000">method is called (i.e. every generation in the examples up to now), theyfirst call the </font><b><tt><font color="#660000">operator()</font></tt></b><font color="#000000">methods of all object they contain, and then return their result as an</font><b><tt><font color="#3366FF">eoContinue</font></tt></b><font color="#000000">object (i.e. should we continue or stop).</font><br><b><font color="#FF0000">Programming: </font></b><font color="#000000">Todo something every generation, you simply need to </font><b><tt><font color="#3366FF">add</font></tt></b><font color="#000000">an object whose </font><b><tt><font color="#660000">operator()</font></tt></b><font color="#000000">does what you want to the eoState that you will use as continuator in thealgorithm.</font><br><hr WIDTH="50%"><a NAME="stop"></a><b><font color="#000099">eoCheckpoint:</font><font color="#FF0000">Stopping</font></b><br><font color="#000000">The </font><b><tt><font color="#3366FF">eoContinue</font></tt></b><font color="#000000">part of an </font><b><tt><font color="#3366FF">eoCheckpoint</font></tt></b><font color="#000000">is a single object, <a href="SecondBitEA.html#checkpoint">passed to theconstructor</a>. If you want more that one stopping criterion, use an <a href="SecondBitEA.html#stop">eoCombinedContinue</a>object as described in <a href="eoLesson2.html#combinedContinue">Lesson2</a>.</font><br><hr WIDTH="50%"><br><a NAME="stats"></a><b><font color="#000099">eoCheckpoint: </font><font color="#FF0000">Computingstatistics</font></b><br><font color="#000000">Statistics are computed using </font><b><tt><font color="#3366FF">eoStat</font></tt></b><font color="#000000">objects, i.e. functor objects whose </font><b><tt><font color="#660000">operator()</font></tt></b><font color="#000000">receives as argument a reference to a population as argument, and can hencecompute whatever is needed over that population. </font><b><tt><font color="#3366FF">eoStat</font></tt></b><font color="#000000">objects are </font><font color="#FF6600">templatized</font><font color="#000000">over the type of what they compute (e.g. </font><b><tt><font color="#660000">double</font></tt></b><font color="#000000">,or </font><b><tt><font color="#660000">pair<double></font></tt></b><font color="#000000">,or ...). But looking at the <a href="../../doc/html/classeo_stat.html">inheritancediagram</a> of the </font><b><tt><font color="#3366FF">eoStat</font></tt></b><font color="#000000">class, you find that </font><b><tt><font color="#3366FF">eoStat</font></tt></b><font color="#000000">objects are also </font><b><tt><font color="#3366FF">eoValueParam</font></tt></b><font color="#000000">objects. And this allows </font><b><tt><font color="#3366FF">eoStat</font></tt></b><font color="#000000">to be used within </font><b><tt><font color="#3366FF">eoMonitor</font></tt></b><font color="#000000">object, and hence </font><b><font color="#FF6600">displayed</font></b><font color="#000000">to the user!</font><p><b><font color="#000099">Statistics: </font><font color="#FF0000">Availableinstances</font></b><br>Some widely used statistics are already available (and of course youcan build you own!).<ul><li><b><tt><font color="#3366FF">eoBestFitnessStat</font></tt></b> returnsthe fitness value of the best individual in the population (of type <b><tt><font color="#660000">FitnessType</font></tt></b>,whatever this is).</li><li><b><tt><font color="#3366FF">eoAverageStat</font></tt></b> and <b><tt><font color="#3366FF">eoSecondMomentStat</font></tt></b>respectively return the average (type double, assumes that <b><tt><font color="#660000">FitnessType</font></tt></b>is castable to a double) and a pair made of the average and the standarddeviation (type <b><tt><font color="#660000">pair<double></font></tt></b>)of the fitnesses in the populations.</li><li><b><tt><font color="#3366FF">eoDiversityStat</font></tt></b> returns thediversity in the population: assuming that there is a distance functiondefined among individuals, it returns the average inter-individuals distance.See also Exercise 2.</li></ul><b><font color="#000099">Statistics: </font><font color="#FF0000">Addingto the checkpoint</font></b><br>To compute more statistics when your algorithm is running, simply <a href="SecondBitEA.html#stat_declare">declare</a>the corresponding eoStat objects, and <a href="SecondBitEA.html#stat_pass">add</a>them to the <b><tt><font color="#3366FF">eoCheckpoint</font></tt></b> youuse in the algorithm. But it hardly makes any sense if you don't <b><font color="#3366FF">monitor</font></b>those statistics (i.e. either displaying them on the screen, or storingthem into a file): see next section!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -