Paradiseo-eo sources added

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@40 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
legrand 2006-12-12 14:49:08 +00:00
commit c3aec878e5
3609 changed files with 342772 additions and 0 deletions

View file

@ -0,0 +1,280 @@
<!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: eoReduce.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></div>
<h1>eoReduce.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">// eoReduce.h</span>
00005 <span class="comment">// Base class for population-merging classes</span>
00006 <span class="comment">// (c) GeNeura Team, 1998</span>
00007 <span class="comment">/* </span>
00008 <span class="comment"> This library is free software; you can redistribute it and/or</span>
00009 <span class="comment"> modify it under the terms of the GNU Lesser General Public</span>
00010 <span class="comment"> License as published by the Free Software Foundation; either</span>
00011 <span class="comment"> version 2 of the License, or (at your option) any later version.</span>
00012 <span class="comment"></span>
00013 <span class="comment"> This library is distributed in the hope that it will be useful,</span>
00014 <span class="comment"> but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
00015 <span class="comment"> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</span>
00016 <span class="comment"> Lesser General Public License for more details.</span>
00017 <span class="comment"></span>
00018 <span class="comment"> You should have received a copy of the GNU Lesser General Public</span>
00019 <span class="comment"> License along with this library; if not, write to the Free Software</span>
00020 <span class="comment"> Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
00021 <span class="comment"></span>
00022 <span class="comment"> Contact: todos@geneura.ugr.es, http://geneura.ugr.es</span>
00023 <span class="comment"> */</span>
00024 <span class="comment">//-----------------------------------------------------------------------------</span>
00025
00026 <span class="preprocessor">#ifndef eoReduce_h</span>
00027 <span class="preprocessor"></span><span class="preprocessor">#define eoReduce_h</span>
00028 <span class="preprocessor"></span>
00029 <span class="comment">//-----------------------------------------------------------------------------</span>
00030
00031 <span class="preprocessor">#include &lt;iostream&gt;</span>
00032
00033 <span class="comment">// EO includes</span>
00034 <span class="preprocessor">#include &lt;eoPop.h&gt;</span> <span class="comment">// eoPop</span>
00035 <span class="preprocessor">#include &lt;eoFunctor.h&gt;</span> <span class="comment">// eoReduce</span>
00036 <span class="preprocessor">#include &lt;utils/selectors.h&gt;</span>
00037
<a name="l00045"></a><a class="code" href="classeo_reduce.html">00045</a> <span class="keyword">template</span>&lt;<span class="keyword">class</span> EOT&gt; <span class="keyword">class </span><a class="code" href="classeo_reduce.html">eoReduce</a>: <span class="keyword">public</span> <a class="code" href="classeo_b_f.html">eoBF</a>&lt;eoPop&lt;EOT&gt;&amp;, unsigned, void&gt;
00046 {};
00047
<a name="l00049"></a><a class="code" href="classeo_truncate.html">00049</a> <span class="keyword">template</span> &lt;<span class="keyword">class</span> EOT&gt; <span class="keyword">class </span><a class="code" href="classeo_truncate.html">eoTruncate</a> : <span class="keyword">public</span> <a class="code" href="classeo_reduce.html">eoReduce</a>&lt;EOT&gt;
00050 {
<a name="l00051"></a><a class="code" href="classeo_truncate.html#d0">00051</a> <span class="keywordtype">void</span> <a class="code" href="classeo_truncate.html#d0">operator()</a>(<a class="code" href="classeo_pop.html">eoPop&lt;EOT&gt;</a>&amp; _newgen, <span class="keywordtype">unsigned</span> _newsize)
00052 {
00053 <span class="keywordflow">if</span> (_newgen.size() == _newsize)
00054 <span class="keywordflow">return</span>;
00055 <span class="keywordflow">if</span> (_newgen.size() &lt; _newsize)
00056 <span class="keywordflow">throw</span> std::logic_error(<span class="stringliteral">"eoTruncate: Cannot truncate to a larger size!\n"</span>);
00057
00058 _newgen.<a class="code" href="classeo_pop.html#a13">nth_element</a>(_newsize);
00059 _newgen.resize(_newsize);
00060 }
00061 };
00062
<a name="l00064"></a><a class="code" href="classeo_random_reduce.html">00064</a> <span class="keyword">template</span> &lt;<span class="keyword">class</span> EOT&gt; <span class="keyword">class </span><a class="code" href="classeo_random_reduce.html">eoRandomReduce</a> : <span class="keyword">public</span> <a class="code" href="classeo_reduce.html">eoReduce</a>&lt;EOT&gt;
00065 {
<a name="l00066"></a><a class="code" href="classeo_random_reduce.html#d0">00066</a> <span class="keywordtype">void</span> <a class="code" href="classeo_random_reduce.html#d0">operator()</a>(<a class="code" href="classeo_pop.html">eoPop&lt;EOT&gt;</a>&amp; _newgen, <span class="keywordtype">unsigned</span> _newsize)
00067 {
00068 <span class="keywordflow">if</span> (_newgen.size() == _newsize)
00069 <span class="keywordflow">return</span>;
00070 <span class="keywordflow">if</span> (_newgen.size() &lt; _newsize)
00071 <span class="keywordflow">throw</span> std::logic_error(<span class="stringliteral">"eoRandomReduce: Cannot truncate to a larger size!\n"</span>);
00072
00073 <span class="comment">// shuffle the population, then trucate</span>
00074 _newgen.<a class="code" href="classeo_pop.html#a7">shuffle</a>();
00075 _newgen.resize(_newsize);
00076 }
00077 };
00078
<a name="l00083"></a><a class="code" href="classeo_e_p_reduce.html">00083</a> <span class="keyword">template</span> &lt;<span class="keyword">class</span> EOT&gt; <span class="keyword">class </span><a class="code" href="classeo_e_p_reduce.html">eoEPReduce</a> : <span class="keyword">public</span> <a class="code" href="classeo_reduce.html">eoReduce</a>&lt;EOT&gt;
00084 {
00085 <span class="keyword">public</span>:
00086 <span class="keyword">typedef</span> <span class="keyword">typename</span> EOT::Fitness Fitness;
00087
00088 <a class="code" href="classeo_e_p_reduce.html">eoEPReduce</a>(<span class="keywordtype">unsigned</span> _t_size ):
00089 t_size(_t_size)
00090 {
00091 <span class="keywordflow">if</span> (t_size &lt; 2)
00092 {
00093 std::cout &lt;&lt; <span class="stringliteral">"Warning: EP tournament size should be &gt;= 2. Adjusted"</span> &lt;&lt; std::endl;
00094 t_size = 2;
00095 }
00096 }
00097
00099 <span class="comment">// compares the scores</span>
00100 <span class="comment">// uses the fitness if scores are equals ????</span>
<a name="l00101"></a><a class="code" href="classeo_e_p_reduce.html#w1">00101</a> <span class="keyword">typedef</span> std::pair&lt;float, typename eoPop&lt;EOT&gt;::iterator&gt; EPpair;
00102 <span class="keyword">struct </span>Cmp {
00103 <span class="keywordtype">bool</span> <a class="code" href="classeo_e_p_reduce.html#a1">operator()</a>(<span class="keyword">const</span> <a class="code" href="classeo_e_p_reduce.html#w1">EPpair</a> a, <span class="keyword">const</span> <a class="code" href="classeo_e_p_reduce.html#w1">EPpair</a> b)<span class="keyword"> const</span>
00104 <span class="keyword"> </span>{
00105 <span class="keywordflow">if</span> (b.first == a.first)
00106 <span class="keywordflow">return</span> (*b.second &lt; *a.second);
00107 <span class="keywordflow">return</span> b.first &lt; a.first;
00108 }
00109 };
00110
00111
<a name="l00112"></a><a class="code" href="classeo_e_p_reduce.html#a1">00112</a> <span class="keywordtype">void</span> <a class="code" href="classeo_e_p_reduce.html#a1">operator()</a>(<a class="code" href="classeo_pop.html">eoPop&lt;EOT&gt;</a>&amp; _newgen, <span class="keywordtype">unsigned</span> _newsize)
00113 {
00114 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> presentSize = _newgen.size();
00115
00116 <span class="keywordflow">if</span> (presentSize == _newsize)
00117 <span class="keywordflow">return</span>;
00118 <span class="keywordflow">if</span> (presentSize &lt; _newsize)
00119 <span class="keywordflow">throw</span> std::logic_error(<span class="stringliteral">"eoTruncate: Cannot truncate to a larger size!\n"</span>);
00120 std::vector&lt;EPpair&gt; scores(presentSize);
00121 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i&lt;presentSize; i++)
00122 {
00123 scores[i].second = _newgen.begin()+i;
00124 Fitness fit = _newgen[i].fitness();
00125 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> itourn = 0; itourn &lt; t_size; ++itourn)
00126 {
00127 <span class="keyword">const</span> <a class="code" href="struct_dummy.html">EOT</a> &amp; competitor = _newgen[rng.<a class="code" href="classeo_rng.html#a5">random</a>(presentSize)];
00128 <span class="keywordflow">if</span> (fit &gt; competitor.<a class="code" href="class_e_o.html#a2">fitness</a>())
00129 scores[i].first += 1;
00130 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (fit == competitor.<a class="code" href="class_e_o.html#a2">fitness</a>())
00131 scores[i].first += 0.5;
00132 }
00133 }
00134
00135 <span class="comment">// now we have the scores</span>
00136 <span class="keyword">typename</span> std::vector&lt;EPpair&gt;::iterator it = scores.begin() + _newsize;
00137 std::nth_element(scores.begin(), it, scores.end(), Cmp());
00138 <span class="comment">// sort(scores.begin(), scores.end(), Cmp());</span>
00139 <span class="keywordtype">unsigned</span> j;
00140 <span class="comment">// std::cout &lt;&lt; "Les scores apres tri\n";</span>
00141 <span class="comment">// for (j=0; j&lt;scores.size(); j++)</span>
00142 <span class="comment">// {</span>
00143 <span class="comment">// std::cout &lt;&lt; scores[j].first &lt;&lt; " " &lt;&lt; *scores[j].second &lt;&lt; std::endl;</span>
00144 <span class="comment">// }</span>
00145 <a class="code" href="classeo_pop.html">eoPop&lt;EOT&gt;</a> tmPop;
00146 <span class="keywordflow">for</span> (j=0; j&lt;_newsize; j++)
00147 {
00148 tmPop.push_back(*scores[j].second);
00149 }
00150 _newgen.<a class="code" href="classeo_pop.html#a16">swap</a>(tmPop);
00151 <span class="comment">// erase does not work, but I'm sure there is a way in STL to mark</span>
00152 <span class="comment">// and later delete all inside a std::vector ??????</span>
00153 <span class="comment">// this would avoid all copies here</span>
00154
00155 <span class="comment">// it = scores.begin() + _newsize;</span>
00156 <span class="comment">// while (it &lt; scores.end())</span>
00157 <span class="comment">// _newgen.erase(it-&gt;second);</span>
00158 }
00159 <span class="keyword">private</span>:
00160 <span class="keywordtype">unsigned</span> t_size;
00161 };
00162
00166 <span class="keyword">template</span> &lt;<span class="keyword">class</span> EOT&gt;
<a name="l00167"></a><a class="code" href="classeo_linear_truncate.html">00167</a> <span class="keyword">class </span><a class="code" href="classeo_linear_truncate.html">eoLinearTruncate</a> : <span class="keyword">public</span> <a class="code" href="classeo_reduce.html">eoReduce</a>&lt;EOT&gt;
00168 {
<a name="l00169"></a><a class="code" href="classeo_linear_truncate.html#d0">00169</a> <span class="keywordtype">void</span> <a class="code" href="classeo_linear_truncate.html#d0">operator()</a>(<a class="code" href="classeo_pop.html">eoPop&lt;EOT&gt;</a>&amp; _newgen, <span class="keywordtype">unsigned</span> _newsize)
00170 {
00171 <span class="keywordtype">unsigned</span> oldSize = _newgen.size();
00172 <span class="keywordflow">if</span> (oldSize == _newsize)
00173 <span class="keywordflow">return</span>;
00174 <span class="keywordflow">if</span> (oldSize &lt; _newsize)
00175 <span class="keywordflow">throw</span> std::logic_error(<span class="stringliteral">"eoLinearTruncate: Cannot truncate to a larger size!\n"</span>);
00176 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i&lt;oldSize - _newsize; i++)
00177 {
00178 <span class="keyword">typename</span> <a class="code" href="classeo_pop.html">eoPop&lt;EOT&gt;</a>::iterator it = _newgen.<a class="code" href="classeo_pop.html#a12">it_worse_element</a>();
00179 _newgen.erase(it);
00180 }
00181 }
00182 };
00183
00187 <span class="keyword">template</span> &lt;<span class="keyword">class</span> EOT&gt;
<a name="l00188"></a><a class="code" href="classeo_det_tournament_truncate.html">00188</a> <span class="keyword">class </span><a class="code" href="classeo_det_tournament_truncate.html">eoDetTournamentTruncate</a> : <span class="keyword">public</span> <a class="code" href="classeo_reduce.html">eoReduce</a>&lt;EOT&gt;
00189 {
00190 <span class="keyword">public</span>:
00191 <a class="code" href="classeo_det_tournament_truncate.html">eoDetTournamentTruncate</a>(<span class="keywordtype">unsigned</span> _t_size):
00192 t_size(_t_size)
00193 {
00194 <span class="keywordflow">if</span> (t_size &lt; 2)
00195 {
00196 std::cout &lt;&lt; <span class="stringliteral">"Warning, Size for eoDetTournamentTruncate adjusted to 2\n"</span>;
00197 t_size = 2;
00198 }
00199 }
00200
<a name="l00201"></a><a class="code" href="classeo_det_tournament_truncate.html#a1">00201</a> <span class="keywordtype">void</span> <a class="code" href="classeo_det_tournament_truncate.html#a1">operator()</a>(<a class="code" href="classeo_pop.html">eoPop&lt;EOT&gt;</a>&amp; _newgen, <span class="keywordtype">unsigned</span> _newsize)
00202 {
00203 <span class="keywordtype">unsigned</span> oldSize = _newgen.size();
00204 <span class="keywordflow">if</span> (_newsize == 0)
00205 {
00206 _newgen.resize(0);
00207 <span class="keywordflow">return</span>;
00208 }
00209 <span class="keywordflow">if</span> (oldSize == _newsize)
00210 <span class="keywordflow">return</span>;
00211 <span class="keywordflow">if</span> (oldSize &lt; _newsize)
00212 <span class="keywordflow">throw</span> std::logic_error(<span class="stringliteral">"eoDetTournamentTruncate: Cannot truncate to a larger size!\n"</span>);
00213
00214 <span class="comment">// Now OK to erase some losers</span>
00215 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i&lt;oldSize - _newsize; i++)
00216 {
00217 <span class="comment">//OLDCODE EOT &amp; eo = inverse_deterministic_tournament&lt;EOT&gt;(_newgen, t_size);</span>
00218 <span class="comment">//OLDCODE _newgen.erase(&amp;eo);</span>
00219
00220 <span class="comment">// Jeroen Eggermont stdc++v3 patch</span>
00221 <span class="comment">// in the new code from stdc++v3 an iterator from a container&lt;T&gt; is no longer an pointer to T</span>
00222 <span class="comment">// Because eo already contained a fuction using eoPop&lt;EOT&gt;::iterator's we will use the following</span>
00223
00224 _newgen.erase( inverse_deterministic_tournament(_newgen.begin(), _newgen.end(), t_size) );
00225
00226 }
00227 }
00228 <span class="keyword">private</span>:
00229 <span class="keywordtype">unsigned</span> t_size;
00230 };
00231
00235 <span class="keyword">template</span> &lt;<span class="keyword">class</span> EOT&gt;
<a name="l00236"></a><a class="code" href="classeo_stoch_tournament_truncate.html">00236</a> <span class="keyword">class </span><a class="code" href="classeo_stoch_tournament_truncate.html">eoStochTournamentTruncate</a> : <span class="keyword">public</span> <a class="code" href="classeo_reduce.html">eoReduce</a>&lt;EOT&gt;
00237 {
00238 <span class="keyword">public</span>:
00239 <a class="code" href="classeo_stoch_tournament_truncate.html">eoStochTournamentTruncate</a>(<span class="keywordtype">double</span> _t_rate):
00240 t_rate(_t_rate)
00241 {
00242 <span class="keywordflow">if</span> (t_rate &lt;= 0.5)
00243 {
00244 std::cout &lt;&lt; <span class="stringliteral">"Warning, Rate for eoStochTournamentTruncate adjusted to 0.51\n"</span>;
00245 t_rate = 0.51;
00246 }
00247 <span class="keywordflow">if</span> (t_rate &gt; 1)
00248 {
00249 std::cout &lt;&lt; <span class="stringliteral">"Warning, Rate for eoStochTournamentTruncate adjusted to 1\n"</span>;
00250 t_rate = 1;
00251 }
00252 }
00253
<a name="l00254"></a><a class="code" href="classeo_stoch_tournament_truncate.html#a1">00254</a> <span class="keywordtype">void</span> <a class="code" href="classeo_stoch_tournament_truncate.html#a1">operator()</a>(<a class="code" href="classeo_pop.html">eoPop&lt;EOT&gt;</a>&amp; _newgen, <span class="keywordtype">unsigned</span> _newsize)
00255 {
00256 <span class="keywordtype">unsigned</span> oldSize = _newgen.size();
00257 <span class="keywordflow">if</span> (_newsize == 0)
00258 {
00259 _newgen.resize(0);
00260 <span class="keywordflow">return</span>;
00261 }
00262 <span class="keywordflow">if</span> (oldSize == _newsize)
00263 <span class="keywordflow">return</span>;
00264 <span class="keywordflow">if</span> (oldSize &lt; _newsize)
00265 <span class="keywordflow">throw</span> std::logic_error(<span class="stringliteral">"eoStochTournamentTruncate: Cannot truncate to a larger size!\n"</span>);
00266 <span class="comment">// Now OK to erase some losers</span>
00267 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i&lt;oldSize - _newsize; i++)
00268 {
00269 <span class="comment">//OLDCODE EOT &amp; eo = inverse_stochastic_tournament&lt;EOT&gt;(_newgen, t_rate);</span>
00270 <span class="comment">//OLDCODE _newgen.erase(&amp;eo);</span>
00271
00272 <span class="comment">// Jeroen Eggermont stdc++v3 patch</span>
00273 <span class="comment">// in the new code from stdc++v3 an iterator from a container&lt;T&gt; is no longer an pointer to T</span>
00274 <span class="comment">// Because eo already contained a fuction using eoPop&lt;EOT&gt;::iterator's we will use the following</span>
00275
00276 _newgen.erase( inverse_stochastic_tournament(_newgen.begin(), _newgen.end(), t_rate) );
00277
00278
00279 }
00280 }
00281
00282 <span class="keyword">private</span>:
00283 <span class="keywordtype">double</span> t_rate;
00284 };
00285
00286 <span class="comment">//-----------------------------------------------------------------------------</span>
00287
00288 <span class="preprocessor">#endif</span>
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:37 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>