eooperators.html

来自「这是linux下的进化计算的源代码。 === === === === ==」· HTML 代码 · 共 819 行 · 第 1/4 页

HTML
819 行
字号
of remaining modified parents</font></tt><br><tt><font color="#993300">&nbsp; <b>parent1.invalidate();</b></font></tt><br><b><tt><font color="#993300">&nbsp; ...</font></tt></b><br><b><tt><font color="#993300">&nbsp; parentN.invalidate();</font></tt></b><br><b><tt><font color="#993300">}</font></tt></b><br>where select is any selector you like. <font color="#FF6600">Note theconst:</font> you are not allowed to modify an element of the originalpopulation (but you could of course have copied it!). As usual, the <b><tt><font color="#009900">select</font></tt></b>selector was passed to the operator at construct time. This typically allowsone to use a different selector for one parent and the others, as demonstrated<a href="#advanced_general">here</a>.<br>&nbsp;<li>If you don't care about the size of the offspring population (that is,if that size os controlled elsewhere, e.g. in some external loop), youcan use the inbedded select method of the class <b><font color="#999900">eoPopulator</font></b>.For instance</li><br>&nbsp;<p>&nbsp;<p><b><tt><font color="#993300">void apply()(</font><font color="#999900">eoPopulator</font><font color="#993300">&amp;_pop)</font></tt></b><br><b><tt><font color="#993300">{</font></tt></b><br><tt><font color="#993300"><b>&nbsp; </b>// get as many parents as youwill have offspring (see <a href="#apply">above</a>)</font></tt><br><tt><font color="#993300">&nbsp; ...</font></tt><br><tt><font color="#993300"><b>&nbsp; </b>// get extra parents - usepopulator selector</font></tt><br><b><tt><font color="#993300">&nbsp; const EOT&amp; parentN+1 = _pop.</font><font color="#009900">select</font><font color="#993300">();</font></tt></b><br><b><tt><font color="#993300">&nbsp; ...</font></tt></b><br><b><tt><font color="#993300">&nbsp; const EOT&amp; parentM = _pop.</font><font color="#009900">select</font><font color="#993300">();</font></tt></b><br><tt><font color="#993300">&nbsp; // do whatever needs to be done</font></tt><br><tt><font color="#993300">&nbsp; ...</font></tt><br><tt><font color="#993300">&nbsp; // and of course invalidate fitnessesof remaining modified parents</font></tt><br><tt><font color="#993300">&nbsp; <b>parent1.invalidate();</b></font></tt><br><b><tt><font color="#993300">&nbsp; ...</font></tt></b><br><b><tt><font color="#993300">&nbsp; parentN.invalidate();</font></tt></b><br><tt><font color="#993300">}</font></tt></ul></ul><b><font color="#FF6600">Warning</font></b>: if you use operators thathave different number of parents than offspring, you are deviating fromthe simple generational approach. Be careful to have the proper replacementprocedure to take care of the population size: in most instances of algorithmsthat come within EO, this is enforced (an exception is thrown if populationsize varies from one genertaion to the other) but this might not be truefor all forthcoming EO algorithms.<p><b><font color="#FF0000">Using general operators</font></b>:<br>Directly applying general operators to given individuals is impossiblein EO, due to its <a href="#interface">interface</a>. You need the helpof an individual dispenser of class <b><font color="#999900">eoPopulator</font></b>.But anyway general operators were thought to be used putely in <b><tt><font color="#CC33CC">eoOpContainer</font></tt></b>,as described <a href="#general_combination">below</a>.<p><a NAME="writing_mutation"></a><b><font color="#FF0000">Writing a generaloperator:</font></b><br>There are many things to do to write a general operator - but the Templatesdirectory contains some sample tempaltes files to help you. It all dependson whether you want more or less offspring than parents, and whetehr youwant the same selector for every parent or more specialized selectors.<ul><li>It you want <font color="#FF6600">more (or as many) </font>offspring thanparents, you should use the <a href="../Templates/moreOffspringGenOp.tmpl">moreOffspringGenOp.tmpl</a>template - if you want to use the same selector for all parents, the oneembedded in the argument <b><font color="#999900">eoPopulator</font></b>.Otherwise, you'll have to write your own operator based on an externalselector, as described in <a href="../Templates/lessOffspringExternalSelectorGenOp.tmpl">lessOffspringExternalSelectorGenOp.tmpl</a>.</li><li>If you decide to have more parents than offspring, you can decide eitherto get the extra parents using the <b><font color="#999900">eoPopulator</font></b>&nbsp;own selector (see <a href="../Templates/lessOffspringSameSelectorGenOp.tmpl">lessOffspringSameSelectorGenOp.tmpl</a>,or to use an external selector (passed at construct-time) as describedin <a href="../Templates/lessOffspringExternalSelectorGenOp.tmpl">lessOffspringExternalSelectorGenOp.tmpl</a>.</li><li>Now you can modify the <font color="#FF6600">constructor</font>, whereyou pass to the object any useful parameter (see private data at end ofclass definition). In case you need an external selector, you have to chooseit (or write it!) - it should be an <a href="eoEngine.html#selection">eoSelectOne</a>object.</li><li>Finally, write the core of the operator in method <font color="#FF6600">apply()</font>.Remember you must use the argument <b><font color="#999900">eoPopulator</font></b>toaccess the members of the population in turn (method<b><tt><font color="#993300">operator*</font></tt></b>),you may use the initial population (method <b><tt><font color="#993300">source()</font></tt></b>),as well as the <b><tt><font color="#993300">insert</font></tt></b> methods.</li></ul><ul><b><font color="#FF6600">Warning</font></b>: in general operators,you must not forget to <b><font color="#FF6600">invalidate</font></b> thefitness of any individual that has actually been modified. this implicitelyimplies that general operators can only be applied to EO object (i.e. objectswith a fitness), and not to any type of structure.<br>It you don't invalidate the individual, the <a href="eoEval.html#lazy">lazyfitness evaluation procedure</a> in EO will not know it should computethe fitness again and will keep the old obsolete value.</ul><hr WIDTH="100%"><a NAME="populators"></a><b><font color="#000099"><font size=+2>Thepopulators:</font></font></b><br>As has been said above, an<b><font color="#999900">eoPopulator</font></b>mainly behaves like an <b><font color="#FF6600">iterator</font></b> overa population (hence the name, <b><font color="#FF6600">Popul</font></b>ation-Iter<b><font color="#FF6600">ator</font></b>).<p><a NAME="populator_interface"></a>The <b><font color="#FF6600">basicinterface</font></b> of an <b><font color="#999900">eoPopulator</font></b>(see also <a href="../../doc/html/classeo_gen_op.html">the documentation</a>,of course) is the following:<ul><li>Individuals are accessed through the <b><tt><font color="#993300">operator*</font></tt></b>;</li><li>Basic iterator operations are available, like (pre)incrementation through<b><tt><font color="#993300">operator++</font></tt></b>,position management through <b><tt><font color="#993300">seekp</font></tt></b>(returns the current position) and <b><tt><font color="#993300">tellp</font></tt></b>(go to a given position);</li><li>Individuals can also be <b><font color="#993300">insert</font></b>ed atcurrent position using the corresponding methods;</li><li>last but not least, as the individuals are returned by reference, it ismandatory to ensure that they will not be moved around later: the memorymanagement&nbsp; routine <b><tt><font color="#993300">reserve</font></tt></b>is called whenever there is a chance to add some individuals in the population- i.e. in the eoGenOp base class operator() method.</li></ul>Moreover, a public method termed <b><tt><font color="#993300">select</font></tt></b>,&nbsp;is used inside the object to get new parents for the following <b><tt><font color="#993300">operator*</font></tt></b>,and its implementation distinguishes two types of <b><font color="#999900">eoPopulator</font></b>:<ul><li>The <b><font color="#999900">eoSeqPopulator</font></b> gets new parentsfrom its source (the initial population). When the source is exhausted,an exception if thrown. The idea of such pooulator is to start from a populationof already selected individuals.</li><br>The programmer should hence be very careful that the number of availableparents matches the requirements of the operators when using an <b><font color="#999900">eoSeqPopulator</font></b>object.<li>The <b><font color="#999900">eoSelectivePopulator</font></b> , on the opposite,always gets new parents using its private <b><font color="#009900">eoSelectOne</font></b>object (passed at construct time). Hence it can handle any number of parentsat will. The idea of such populator is to handle the whole <b><font color="#FF6600">breeding</font></b>process, i.e. selection and variation operators.</li></ul>An immediate consequence is that if you are not sure of the number of&nbsp;parents you will need in some operators (e.g. because of some stochasticproportional selection ebtween operators that don't need the same numberof parents, then you <b><font color="#FF6600">must</font></b> use an <b><font color="#CC33CC">eoSelectivePopulator</font></b>to apply the variation operators to the population, and thus get exactlythe number of offspring you want.<p><b><font color="#FF0000">Example</font></b>: An <b><font color="#CC33CC">eoSelectivePopulator</font></b>is the main ingredient of the <b><tt><font color="#FF6666">eoGeneralBreeder</font></tt></b><a href="../../doc/html/eo_general_breeder_8h-source.html#l00061">operator()method</a> - a class that creates a population of offspring from the parentsapplying an eoGenOp (usually an eoOpContainer) to all selected parentsin turn.<br><hr WIDTH="100%"><a NAME="general_combination"></a><b><font color="#000099"><font size=+2>GeneralOperator Containers:</font></font></b><br>General operators in EO are meant to be used withing eoOpContainerobjects, that allow to combine them in a hierarchical and flexible way.There are two ways to do that: the <b><font color="#FF6600">proportionalcombination</font></b>, similar to what has been described for simple operators<a href="#proportional_simple">above</a>,and the <b><font color="#FF6600">sequential combination</font></b>, whichamounts to apply all operators in turn to a bunch of individuals, eachoperator being applied with a specific probability.<p><a NAME="prop_container"></a><b><font color="#FF0000">Proportional combinations</font></b><br>When called upon a population (through an <b><font color="#999900">eoPopulator</font></b>object), an <b><font color="#CC33CC">eoProportionalOpContainer</font></b>enters the following loop:<p>while there are individuals left in the list<ul><li>choose one of the included operators according to their relative rates(by some roulette wheel random choice)</li><li>applies the chosen operator. The parents are dispensed to the operatorfrom the list on demand.</li><li>What happens next exactly depends on the type of operator, but basically,some of the parents get modified, some might get removed from the listand some new individual might get inserted on the list.</li><li>updates the list pointer (if needed) to the individual following the onesthat just have been modified/inserted/deleted.</li></ul><a NAME="seqential_container"></a><b><font color="#FF0000">Sequential combinations</font></b><br>When it is called upon a list of pending candidates, an<b><font color="#CC33CC">eoSequentialOpContainer</font></b>enters the following loop:<p>mark the current position<br>for all operators it contains,

⌨️ 快捷键说明

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