paradiseo/trunk/paradiseo-eo/doc/html/ga_2make__op_8h-source.html
legrand c3aec878e5 Paradiseo-eo sources added
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@40 331e1502-861f-0410-8da2-ba01fb791d7f
2006-12-12 14:49:08 +00:00

223 lines
22 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>EO: make_op.h Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3.9.1 -->
<div class="qindex"> <form class="search" action="search.php" method="get">
<a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a> | <span class="search"><u>S</u>earch&nbsp;for&nbsp;<input class="search" type="text" name="query" value="" size="20" accesskey="s"/></span></form></div>
<div class="nav">
<a class="el" href="dir_000000.html">src</a>&nbsp;/&nbsp;<a class="el" href="dir_000013.html">ga</a></div>
<h1>ga/make_op.h</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-</span>
00002
00003 <span class="comment">//-----------------------------------------------------------------------------</span>
00004 <span class="comment">// make_op.h</span>
00005 <span class="comment">// (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2001</span>
00006 <span class="comment">/* </span>
00007 <span class="comment"> This library is free software; you can redistribute it and/or</span>
00008 <span class="comment"> modify it under the terms of the GNU Lesser General Public</span>
00009 <span class="comment"> License as published by the Free Software Foundation; either</span>
00010 <span class="comment"> version 2 of the License, or (at your option) any later version.</span>
00011 <span class="comment"></span>
00012 <span class="comment"> This library is distributed in the hope that it will be useful,</span>
00013 <span class="comment"> but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
00014 <span class="comment"> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</span>
00015 <span class="comment"> Lesser General Public License for more details.</span>
00016 <span class="comment"></span>
00017 <span class="comment"> You should have received a copy of the GNU Lesser General Public</span>
00018 <span class="comment"> License along with this library; if not, write to the Free Software</span>
00019 <span class="comment"> Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
00020 <span class="comment"></span>
00021 <span class="comment"> Contact: todos@geneura.ugr.es, http://geneura.ugr.es</span>
00022 <span class="comment"> Marc.Schoenauer@polytechnique.fr</span>
00023 <span class="comment"> mkeijzer@dhi.dk</span>
00024 <span class="comment"> */</span>
00025 <span class="comment">//-----------------------------------------------------------------------------</span>
00026
00027 <span class="preprocessor">#ifndef _make_op_h</span>
00028 <span class="preprocessor"></span><span class="preprocessor">#define _make_op_h</span>
00029 <span class="preprocessor"></span>
00030 <span class="comment">// the operators</span>
00031 <span class="preprocessor">#include &lt;eoOp.h&gt;</span>
00032 <span class="preprocessor">#include &lt;eoGenOp.h&gt;</span>
00033 <span class="preprocessor">#include &lt;eoCloneOps.h&gt;</span>
00034 <span class="preprocessor">#include &lt;eoOpContainer.h&gt;</span>
00035 <span class="comment">// combinations of simple eoOps (eoMonOp and eoQuadOp)</span>
00036 <span class="preprocessor">#include &lt;eoProportionalCombinedOp.h&gt;</span>
00037
00038 <span class="comment">// the specialized GA stuff</span>
00039 <span class="preprocessor">#include &lt;ga/eoBit.h&gt;</span>
00040 <span class="preprocessor">#include &lt;ga/eoBitOp.h&gt;</span>
00041 <span class="comment">// also need the parser and param includes</span>
00042 <span class="preprocessor">#include &lt;utils/eoParser.h&gt;</span>
00043 <span class="preprocessor">#include &lt;utils/eoState.h&gt;</span>
00044
00045
00047 <span class="comment">// canonical (crossover + mutation) only at the moment //</span>
00048
00049 <span class="comment">/*</span>
00050 <span class="comment"> * This function builds the operators that will be applied to the bitstrings</span>
00051 <span class="comment"> *</span>
00052 <span class="comment"> * It uses a parser (to get user parameters) and a state (to store the memory)</span>
00053 <span class="comment"> * the last argument is an individual, needed for 2 reasons</span>
00054 <span class="comment"> * it disambiguates the call after instanciations</span>
00055 <span class="comment"> * some operator might need some private information about the indis</span>
00056 <span class="comment"> *</span>
00057 <span class="comment"> * This is why the template is the complete EOT even though only the fitness</span>
00058 <span class="comment"> * is actually templatized here: the following only applies to bitstrings</span>
00059 <span class="comment"> *</span>
00060 <span class="comment"> * Note : the last parameter is an eoInit: if some operator needs some info </span>
00061 <span class="comment"> * about the gneotypes, the init has it all (e.g. bounds, ...)</span>
00062 <span class="comment"> * Simply do </span>
00063 <span class="comment"> * EOT myEO;</span>
00064 <span class="comment"> * _init(myEO);</span>
00065 <span class="comment"> * and myEO is then an ACTUAL object</span>
00066 <span class="comment">*/</span>
00067
00068 <span class="keyword">template</span> &lt;<span class="keyword">class</span> EOT&gt;
00069 <a class="code" href="classeo_gen_op.html">eoGenOp&lt;EOT&gt;</a> &amp; do_make_op(<a class="code" href="classeo_parser.html">eoParser</a>&amp; _parser, <a class="code" href="classeo_state.html">eoState</a>&amp; _state, <a class="code" href="classeo_init.html">eoInit&lt;EOT&gt;</a>&amp; _init)
00070 {
00071 <span class="comment">// this is a temporary version, while Maarten codes the full tree-structured</span>
00072 <span class="comment">// general operator input</span>
00073 <span class="comment">// BTW we must leave that simple version available somehow, as it is the one</span>
00074 <span class="comment">// that 90% people use!</span>
00075 <a class="code" href="classeo_value_param.html">eoValueParam&lt;std::string&gt;</a>&amp; operatorParam = _parser.createParam(std::string(<span class="stringliteral">"SGA"</span>), <span class="stringliteral">"operator"</span>, <span class="stringliteral">"Description of the operator (SGA only now)"</span>, <span class="charliteral">'o'</span>, <span class="stringliteral">"Variation Operators"</span>);
00076
00077 <span class="keywordflow">if</span> (operatorParam.value() != std::string(<span class="stringliteral">"SGA"</span>))
00078 <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"Only SGA-like operator available right now\n"</span>);
00079
00080 <span class="comment">// now we read Pcross and Pmut, </span>
00081 <span class="comment">// the relative weights for all crossovers -&gt; proportional choice</span>
00082 <span class="comment">// the relative weights for all mutations -&gt; proportional choice</span>
00083 <span class="comment">// and create the eoGenOp that is exactly </span>
00084 <span class="comment">// crossover with pcross + mutation with pmut</span>
00085
00086 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a>&amp; pCrossParam = _parser.<a class="code" href="classeo_parameter_loader.html#a3">createParam</a>(0.6, <span class="stringliteral">"pCross"</span>, <span class="stringliteral">"Probability of Crossover"</span>, <span class="charliteral">'C'</span>, <span class="stringliteral">"Variation Operators"</span> );
00087 <span class="comment">// minimum check</span>
00088 <span class="keywordflow">if</span> ( (pCrossParam.<a class="code" href="classeo_value_param.html#a2">value</a>() &lt; 0) || (pCrossParam.<a class="code" href="classeo_value_param.html#a2">value</a>() &gt; 1) )
00089 <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"Invalid pCross"</span>);
00090
00091 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a>&amp; pMutParam = _parser.<a class="code" href="classeo_parameter_loader.html#a3">createParam</a>(0.1, <span class="stringliteral">"pMut"</span>, <span class="stringliteral">"Probability of Mutation"</span>, <span class="charliteral">'M'</span>, <span class="stringliteral">"Variation Operators"</span> );
00092 <span class="comment">// minimum check</span>
00093 <span class="keywordflow">if</span> ( (pMutParam.<a class="code" href="classeo_value_param.html#a2">value</a>() &lt; 0) || (pMutParam.<a class="code" href="classeo_value_param.html#a2">value</a>() &gt; 1) )
00094 <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"Invalid pMut"</span>);
00095
00096 <span class="comment">// the crossovers</span>
00098 <span class="comment"></span> <span class="comment">// the parameters</span>
00099 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a>&amp; onePointRateParam = _parser.<a class="code" href="classeo_parameter_loader.html#a3">createParam</a>(<span class="keywordtype">double</span>(1.0), <span class="stringliteral">"onePointRate"</span>, <span class="stringliteral">"Relative rate for one point crossover"</span>, <span class="charliteral">'1'</span>, <span class="stringliteral">"Variation Operators"</span> );
00100 <span class="comment">// minimum check</span>
00101 <span class="keywordflow">if</span> ( (onePointRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>() &lt; 0) )
00102 <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"Invalid onePointRate"</span>);
00103
00104 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a>&amp; twoPointsRateParam = _parser.<a class="code" href="classeo_parameter_loader.html#a3">createParam</a>(<span class="keywordtype">double</span>(1.0), <span class="stringliteral">"twoPointRate"</span>, <span class="stringliteral">"Relative rate for two point crossover"</span>, <span class="charliteral">'2'</span>, <span class="stringliteral">"Variation Operators"</span> );
00105 <span class="comment">// minimum check</span>
00106 <span class="keywordflow">if</span> ( (twoPointsRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>() &lt; 0) )
00107 <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"Invalid twoPointsRate"</span>);
00108
00109 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a>&amp; uRateParam = _parser.<a class="code" href="classeo_parameter_loader.html#a3">createParam</a>(<span class="keywordtype">double</span>(2.0), <span class="stringliteral">"uRate"</span>, <span class="stringliteral">"Relative rate for uniform crossover"</span>, <span class="charliteral">'U'</span>, <span class="stringliteral">"Variation Operators"</span> );
00110 <span class="comment">// minimum check</span>
00111 <span class="keywordflow">if</span> ( (uRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>() &lt; 0) )
00112 <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"Invalid uRate"</span>);
00113
00114 <span class="comment">// minimum check</span>
00115 <span class="keywordtype">bool</span> bCross = <span class="keyword">true</span>;
00116 <span class="keywordflow">if</span> (onePointRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>()+twoPointsRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>()+uRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>()==0)
00117 {
00118 std::cerr &lt;&lt; <span class="stringliteral">"Warning: no crossover"</span> &lt;&lt; std::endl;
00119 bCross = <span class="keyword">false</span>;
00120 }
00121
00122 <span class="comment">// Create the CombinedQuadOp</span>
00123 <a class="code" href="classeo_prop_combined_quad_op.html">eoPropCombinedQuadOp&lt;EOT&gt;</a> *ptCombinedQuadOp = NULL;
00124 <a class="code" href="classeo_quad_op.html">eoQuadOp&lt;EOT&gt;</a> *ptQuad = NULL;
00125 <span class="comment">// 1-point crossover for bitstring</span>
00126 ptQuad = <span class="keyword">new</span> eo1PtBitXover&lt;EOT&gt;;
00127 _state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(ptQuad);
00128 ptCombinedQuadOp = <span class="keyword">new</span> <a class="code" href="classeo_prop_combined_quad_op.html">eoPropCombinedQuadOp&lt;EOT&gt;</a>(*ptQuad, onePointRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>());
00129
00130 <span class="comment">// uniform crossover for bitstring</span>
00131 ptQuad = <span class="keyword">new</span> <a class="code" href="classeo_u_bit_xover.html">eoUBitXover&lt;EOT&gt;</a>;
00132 _state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(ptQuad);
00133 ptCombinedQuadOp-&gt;<a class="code" href="classeo_prop_combined_quad_op.html#a2">add</a>(*ptQuad, uRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>());
00134
00135 <span class="comment">// 2-points xover</span>
00136 ptQuad = <span class="keyword">new</span> <a class="code" href="classeo_n_pts_bit_xover.html">eoNPtsBitXover&lt;EOT&gt;</a>;
00137 _state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(ptQuad);
00138 ptCombinedQuadOp-&gt;<a class="code" href="classeo_prop_combined_quad_op.html#a2">add</a>(*ptQuad, twoPointsRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>());
00139
00140 <span class="comment">// don't forget to store the CombinedQuadOp</span>
00141 _state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(ptCombinedQuadOp);
00142
00143 <span class="comment">// the mutations</span>
00145 <span class="comment"></span> <span class="comment">// the parameters</span>
00146 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a> &amp; pMutPerBitParam = _parser.<a class="code" href="classeo_parameter_loader.html#a3">createParam</a>(0.01, <span class="stringliteral">"pMutPerBit"</span>, <span class="stringliteral">"Probability of flipping 1 bit in bit-flip mutation"</span>, <span class="charliteral">'b'</span>, <span class="stringliteral">"Variation Operators"</span> );
00147 <span class="comment">// minimum check</span>
00148 <span class="keywordflow">if</span> ( (pMutPerBitParam.<a class="code" href="classeo_value_param.html#a2">value</a>() &lt; 0) || (pMutPerBitParam.<a class="code" href="classeo_value_param.html#a2">value</a>() &gt; 0.5) )
00149 <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"Invalid pMutPerBit"</span>);
00150
00151 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a> &amp; bitFlipRateParam = _parser.<a class="code" href="classeo_parameter_loader.html#a3">createParam</a>(0.01, <span class="stringliteral">"bitFlipRate"</span>, <span class="stringliteral">"Relative rate for bit-flip mutation"</span>, <span class="charliteral">'s'</span>, <span class="stringliteral">"Variation Operators"</span> );
00152 <span class="comment">// minimum check</span>
00153 <span class="keywordflow">if</span> ( (bitFlipRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>() &lt; 0) )
00154 <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"Invalid bitFlipRate"</span>);
00155
00156 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a> &amp; oneBitRateParam = _parser.<a class="code" href="classeo_parameter_loader.html#a3">createParam</a>(0.01, <span class="stringliteral">"oneBitRate"</span>, <span class="stringliteral">"Relative rate for deterministic bit-flip mutation"</span>, <span class="charliteral">'d'</span>, <span class="stringliteral">"Variation Operators"</span> );
00157 <span class="comment">// minimum check</span>
00158 <span class="keywordflow">if</span> ( (oneBitRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>() &lt; 0) )
00159 <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"Invalid oneBitRate"</span>);
00160
00161 <span class="comment">// minimum check</span>
00162 <span class="keywordtype">bool</span> bMut = <span class="keyword">true</span>;
00163 <span class="keywordflow">if</span> (bitFlipRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>()+oneBitRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>()==0)
00164 {
00165 std::cerr &lt;&lt; <span class="stringliteral">"Warning: no mutation"</span> &lt;&lt; std::endl;
00166 bMut = <span class="keyword">false</span>;
00167 }
00168
00169 <span class="comment">// Create the CombinedMonOp</span>
00170 <a class="code" href="classeo_prop_combined_mon_op.html">eoPropCombinedMonOp&lt;EOT&gt;</a> *ptCombinedMonOp = NULL;
00171 <a class="code" href="classeo_mon_op.html">eoMonOp&lt;EOT&gt;</a> *ptMon = NULL;
00172
00173 <span class="comment">// standard bit-flip mutation for bitstring</span>
00174 ptMon = <span class="keyword">new</span> <a class="code" href="classeo_bit_mutation.html">eoBitMutation&lt;EOT&gt;</a>(pMutPerBitParam.<a class="code" href="classeo_value_param.html#a2">value</a>());
00175 _state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(ptMon);
00176 <span class="comment">// create the CombinedMonOp</span>
00177 ptCombinedMonOp = <span class="keyword">new</span> <a class="code" href="classeo_prop_combined_mon_op.html">eoPropCombinedMonOp&lt;EOT&gt;</a>(*ptMon, bitFlipRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>());
00178
00179 <span class="comment">// mutate exactly 1 bit per individual</span>
00180 ptMon = <span class="keyword">new</span> <a class="code" href="classeo_det_bit_flip.html">eoDetBitFlip&lt;EOT&gt;</a>;
00181 _state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(ptMon);
00182 ptCombinedMonOp-&gt;<a class="code" href="classeo_prop_combined_mon_op.html#a2">add</a>(*ptMon, oneBitRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>());
00183
00184 _state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(ptCombinedMonOp);
00185
00186 <span class="comment">// now build the eoGenOp:</span>
00187 <span class="comment">// to simulate SGA (crossover with proba pCross + mutation with proba pMut</span>
00188 <span class="comment">// we must construct</span>
00189 <span class="comment">// a sequential combination of</span>
00190 <span class="comment">// with proba 1, a proportional combination of </span>
00191 <span class="comment">// a QuadCopy and our crossover</span>
00192 <span class="comment">// with proba pMut, our mutation</span>
00193
00194 <span class="comment">// the crossover - with probability pCross</span>
00195 <a class="code" href="classeo_proportional_op.html">eoProportionalOp&lt;EOT&gt;</a> * cross = <span class="keyword">new</span> <a class="code" href="classeo_proportional_op.html">eoProportionalOp&lt;EOT&gt;</a> ;
00196 _state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(cross);
00197 ptQuad = <span class="keyword">new</span> <a class="code" href="classeo_quad_clone_op.html">eoQuadCloneOp&lt;EOT&gt;</a>;
00198 _state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(ptQuad);
00199 cross-&gt;<a class="code" href="classeo_op_container.html#a3">add</a>(*ptCombinedQuadOp, pCrossParam.<a class="code" href="classeo_value_param.html#a2">value</a>()); <span class="comment">// user crossover</span>
00200 cross-&gt;<a class="code" href="classeo_op_container.html#a3">add</a>(*ptQuad, 1-pCrossParam.<a class="code" href="classeo_value_param.html#a2">value</a>()); <span class="comment">// clone operator</span>
00201
00202 <span class="comment">// now the sequential</span>
00203 <a class="code" href="classeo_sequential_op.html">eoSequentialOp&lt;EOT&gt;</a> *op = <span class="keyword">new</span> <a class="code" href="classeo_sequential_op.html">eoSequentialOp&lt;EOT&gt;</a>;
00204 _state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(op);
00205 op-&gt;<a class="code" href="classeo_op_container.html#a3">add</a>(*cross, 1.0); <span class="comment">// always crossover (but clone with prob 1-pCross</span>
00206 op-&gt;<a class="code" href="classeo_op_container.html#a3">add</a>(*ptCombinedMonOp, pMutParam.<a class="code" href="classeo_value_param.html#a2">value</a>());
00207
00208 <span class="comment">// that's it!</span>
00209 <span class="keywordflow">return</span> *op;
00210 }
00211 <span class="preprocessor">#endif</span>
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:41 2006 for EO by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.3.9.1 </small></address>
</body>
</html>