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,243 @@
<!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: eoPop.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>eoPop.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">// eoPop.h </span>
00005 <span class="comment">// (c) GeNeura Team, 1998</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"> */</span>
00023 <span class="comment">//-----------------------------------------------------------------------------</span>
00024
00025 <span class="preprocessor">#ifndef _EOPOP_H</span>
00026 <span class="preprocessor"></span><span class="preprocessor">#define _EOPOP_H</span>
00027 <span class="preprocessor"></span>
00028 <span class="preprocessor">#include &lt;algorithm&gt;</span>
00029 <span class="preprocessor">#include &lt;iostream&gt;</span>
00030 <span class="preprocessor">#include &lt;iterator&gt;</span> <span class="comment">// needed for GCC 3.2</span>
00031 <span class="preprocessor">#include &lt;vector&gt;</span>
00032
00033 <span class="comment">// EO includes</span>
00034 <span class="preprocessor">#include &lt;eoOp.h&gt;</span> <span class="comment">// for eoInit</span>
00035 <span class="preprocessor">#include &lt;eoPersistent.h&gt;</span>
00036 <span class="preprocessor">#include &lt;eoInit.h&gt;</span>
00037 <span class="preprocessor">#include &lt;utils/rnd_generators.h&gt;</span> <span class="comment">// for shuffle method</span>
00038
00053 <span class="keyword">template</span>&lt;<span class="keyword">class</span> EOT&gt;
<a name="l00054"></a><a class="code" href="classeo_pop.html">00054</a> <span class="keyword">class </span><a class="code" href="classeo_pop.html">eoPop</a>: <span class="keyword">public</span> std::vector&lt;EOT&gt;, <span class="keyword">public</span> <a class="code" href="classeo_object.html">eoObject</a>, <span class="keyword">public</span> <a class="code" href="classeo_persistent.html">eoPersistent</a>
00055 {
00056
00057 <span class="keyword">public</span>:
00058
00059 <span class="keyword">using</span> std::vector&lt;EOT&gt;::size;
00060 <span class="keyword">using</span> std::vector&lt;EOT&gt;::resize;
00061 <span class="keyword">using</span> std::vector&lt;EOT&gt;::operator[];
00062 <span class="keyword">using</span> std::vector&lt;EOT&gt;::begin;
00063 <span class="keyword">using</span> std::vector&lt;EOT&gt;::end;
00064
00065 <span class="keyword">typedef</span> <span class="keyword">typename</span> EOT::Fitness Fitness;
<a name="l00068"></a><a class="code" href="classeo_pop.html#a0">00068</a> <a class="code" href="classeo_pop.html#a0">eoPop</a>() : std::vector&lt;<a class="code" href="struct_dummy.html">EOT</a>&gt;(), <a class="code" href="classeo_object.html">eoObject</a>(), <a class="code" href="classeo_persistent.html">eoPersistent</a>() {};
00069
<a name="l00075"></a><a class="code" href="classeo_pop.html#a1">00075</a> <a class="code" href="classeo_pop.html#a0">eoPop</a>( <span class="keywordtype">unsigned</span> _popSize, <a class="code" href="classeo_init.html">eoInit&lt;EOT&gt;</a>&amp; _chromInit )
00076 :std::vector&lt;<a class="code" href="struct_dummy.html">EOT</a>&gt;()
00077 {
00078 resize(_popSize);
00079 <span class="keywordflow">for</span> ( <span class="keywordtype">unsigned</span> i = 0; i &lt; _popSize; i++ )
00080 {
00081 _chromInit(<span class="keyword">operator</span>[](i));
00082 }
00083 };
00084
<a name="l00091"></a><a class="code" href="classeo_pop.html#a2">00091</a> <span class="keywordtype">void</span> <a class="code" href="classeo_pop.html#a2">append</a>( <span class="keywordtype">unsigned</span> _newPopSize, <a class="code" href="classeo_init.html">eoInit&lt;EOT&gt;</a>&amp; _chromInit )
00092 {
00093 <span class="keywordtype">unsigned</span> oldSize = size();
00094 <span class="keywordflow">if</span> (_newPopSize &lt; oldSize)
00095 {
00096 <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"New size smaller than old size in pop.append"</span>);
00097 <span class="keywordflow">return</span>;
00098 }
00099 <span class="keywordflow">if</span> (_newPopSize == oldSize)
00100 <span class="keywordflow">return</span>;
00101 resize(_newPopSize); <span class="comment">// adjust the size</span>
00102 <span class="keywordflow">for</span> ( <span class="keywordtype">unsigned</span> i = oldSize; i &lt; _newPopSize; i++ )
00103 {
00104 _chromInit(<span class="keyword">operator</span>[](i));
00105 }
00106 };
00107
00108
<a name="l00113"></a><a class="code" href="classeo_pop.html#a3">00113</a> <a class="code" href="classeo_pop.html#a0">eoPop</a>( std::istream&amp; _is ) :std::vector&lt;<a class="code" href="struct_dummy.html">EOT</a>&gt;() {
00114 <a class="code" href="classeo_pop.html#z19_0">readFrom</a>( _is );
00115 }
00116
<a name="l00118"></a><a class="code" href="classeo_pop.html#a4">00118</a> <span class="keyword">virtual</span> <a class="code" href="classeo_pop.html#a4">~eoPop</a>() {};
00119
00120
<a name="l00122"></a><a class="code" href="structeo_pop_1_1_ref.html">00122</a> <span class="keyword">struct </span><a class="code" href="structeo_pop_1_1_ref.html">Ref</a> { <span class="keyword">const</span> <a class="code" href="struct_dummy.html">EOT</a>* operator()(<span class="keyword">const</span> <a class="code" href="struct_dummy.html">EOT</a>&amp; eot) { <span class="keywordflow">return</span> &amp;eot;}};
<a name="l00124"></a><a class="code" href="structeo_pop_1_1_cmp.html">00124</a> <span class="keyword">struct </span><a class="code" href="structeo_pop_1_1_cmp.html">Cmp</a> {
00125 <span class="keywordtype">bool</span> operator()(<span class="keyword">const</span> <a class="code" href="struct_dummy.html">EOT</a>* a, <span class="keyword">const</span> <a class="code" href="struct_dummy.html">EOT</a>* b)<span class="keyword"> const</span>
00126 <span class="keyword"> </span>{ <span class="keywordflow">return</span> b-&gt;operator&lt;(*a); }
00127 };
00128
<a name="l00133"></a><a class="code" href="classeo_pop.html#a5">00133</a> <span class="keywordtype">void</span> <a class="code" href="classeo_pop.html#a5">sort</a>(<span class="keywordtype">void</span>)
00134 {
00135 std::sort(begin(), end(), std::greater&lt;EOT&gt;());
00136 }
00137
<a name="l00139"></a><a class="code" href="classeo_pop.html#a6">00139</a> <span class="keywordtype">void</span> <a class="code" href="classeo_pop.html#a5">sort</a>(std::vector&lt;const EOT*&gt;&amp; result)<span class="keyword"> const</span>
00140 <span class="keyword"> </span>{
00141 result.resize(size());
00142
00143 std::transform(begin(), end(), result.begin(), Ref());
00144
00145 std::sort(result.begin(), result.end(), Cmp());
00146 }
00147
<a name="l00152"></a><a class="code" href="classeo_pop.html#a7">00152</a> <span class="keywordtype">void</span> <a class="code" href="classeo_pop.html#a7">shuffle</a>(<span class="keywordtype">void</span>)
00153 {
00154 <a class="code" href="class_u_f__random__generator.html">UF_random_generator&lt;unsigned int&gt;</a> gen;
00155 std::random_shuffle(begin(), end(), gen);
00156 }
00157
<a name="l00159"></a><a class="code" href="classeo_pop.html#a8">00159</a> <span class="keywordtype">void</span> <a class="code" href="classeo_pop.html#a7">shuffle</a>(std::vector&lt;const EOT*&gt;&amp; result)<span class="keyword"> const</span>
00160 <span class="keyword"> </span>{
00161 result.resize(size());
00162
00163 std::transform(begin(), end(), result.begin(), Ref());
00164
00165 <a class="code" href="class_u_f__random__generator.html">UF_random_generator&lt;unsigned int&gt;</a> gen;
00166 std::random_shuffle(result.begin(), result.end(), gen);
00167 }
00168
<a name="l00170"></a><a class="code" href="classeo_pop.html#a9">00170</a> <span class="keyword">typename</span> <a class="code" href="classeo_pop.html">eoPop&lt;EOT&gt;</a>::iterator <a class="code" href="classeo_pop.html#a9">it_best_element</a>()
00171 {
00172 <span class="keyword">typename</span> <a class="code" href="classeo_pop.html">eoPop&lt;EOT&gt;</a>::iterator it = std::max_element(begin(), end());
00173 <span class="keywordflow">return</span> it;
00174 }
00175
<a name="l00177"></a><a class="code" href="classeo_pop.html#a10">00177</a> <span class="keyword">const</span> <a class="code" href="struct_dummy.html">EOT</a> &amp; <a class="code" href="classeo_pop.html#a10">best_element</a>()<span class="keyword"> const</span>
00178 <span class="keyword"> </span>{
00179 <span class="keyword">typename</span> <a class="code" href="classeo_pop.html">eoPop&lt;EOT&gt;</a>::const_iterator it = std::max_element(begin(), end());
00180 <span class="keywordflow">return</span> (*it);
00181 }
00182
<a name="l00184"></a><a class="code" href="classeo_pop.html#a11">00184</a> <span class="keyword">const</span> <a class="code" href="struct_dummy.html">EOT</a> &amp; <a class="code" href="classeo_pop.html#a11">worse_element</a>()<span class="keyword"> const</span>
00185 <span class="keyword"> </span>{
00186 <span class="keyword">typename</span> <a class="code" href="classeo_pop.html">eoPop&lt;EOT&gt;</a>::const_iterator it = std::min_element(begin(), end());
00187 <span class="keywordflow">return</span> (*it);
00188 }
00189
<a name="l00191"></a><a class="code" href="classeo_pop.html#a12">00191</a> <span class="keyword">typename</span> <a class="code" href="classeo_pop.html">eoPop&lt;EOT&gt;</a>::iterator <a class="code" href="classeo_pop.html#a12">it_worse_element</a>()
00192 {
00193 <span class="keyword">typename</span> <a class="code" href="classeo_pop.html">eoPop&lt;EOT&gt;</a>::iterator it = std::min_element(begin(), end());
00194 <span class="keywordflow">return</span> it;
00195 }
00196
<a name="l00201"></a><a class="code" href="classeo_pop.html#a13">00201</a> <span class="keyword">typename</span> <a class="code" href="classeo_pop.html">eoPop&lt;EOT&gt;</a>::iterator <a class="code" href="classeo_pop.html#a13">nth_element</a>(<span class="keywordtype">int</span> nth)
00202 {
00203 <span class="keyword">typename</span> <a class="code" href="classeo_pop.html">eoPop&lt;EOT&gt;</a>::iterator it = begin() + nth;
00204 std::nth_element(begin(), it, end(), std::greater&lt;EOT&gt;());
00205 <span class="keywordflow">return</span> it;
00206 }
00207
00208 <span class="keyword">struct </span>GetFitness { Fitness operator()(<span class="keyword">const</span> <a class="code" href="struct_dummy.html">EOT</a>&amp; _eo)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> _eo.<a class="code" href="class_e_o.html#a2">fitness</a>(); } };
00209
<a name="l00211"></a><a class="code" href="classeo_pop.html#a14">00211</a> Fitness <a class="code" href="classeo_pop.html#a14">nth_element_fitness</a>(<span class="keywordtype">int</span> which)<span class="keyword"> const</span>
00212 <span class="keyword"> </span>{ <span class="comment">// probably not the fastest way to do this, but what the heck</span>
00213
00214 std::vector&lt;Fitness&gt; fitness(size());
00215 std::transform(begin(), end(), fitness.begin(), GetFitness());
00216
00217 <span class="keyword">typename</span> std::vector&lt;Fitness&gt;::iterator it = fitness.begin() + which;
00218 std::nth_element(fitness.begin(), it, fitness.end(), std::greater&lt;Fitness&gt;());
00219 <span class="keywordflow">return</span> *it;
00220 }
00221
<a name="l00225"></a><a class="code" href="classeo_pop.html#a15">00225</a> <span class="keywordtype">void</span> <a class="code" href="classeo_pop.html#a13">nth_element</a>(<span class="keywordtype">int</span> which, std::vector&lt;const EOT*&gt;&amp; result)<span class="keyword"> const</span>
00226 <span class="keyword"> </span>{
00227
00228 result.resize(size());
00229 std::transform(begin(), end(), result.begin(), Ref());
00230
00231 <span class="keyword">typename</span> std::vector&lt;const EOT*&gt;::iterator it = result.begin() + which;
00232
00233 std::nth_element(result.begin(), it, result.end(), Cmp());
00234 }
00235
<a name="l00237"></a><a class="code" href="classeo_pop.html#a16">00237</a> <span class="keywordtype">void</span> <a class="code" href="classeo_pop.html#a16">swap</a>(<a class="code" href="classeo_pop.html">eoPop&lt;EOT&gt;</a>&amp; other)
00238 {
00239 std::swap(<span class="keyword">static_cast</span>&lt;std::vector&lt;EOT&gt;&amp; &gt;(*<span class="keyword">this</span>), <span class="keyword">static_cast</span>&lt;std::vector&lt;EOT&gt;&amp; &gt;(other));
00240 }
00241
<a name="l00247"></a><a class="code" href="classeo_pop.html#a17">00247</a> <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classeo_pop.html#a17">sortedPrintOn</a>(std::ostream&amp; _os)<span class="keyword"> const </span>
00248 <span class="keyword"> </span>{
00249 std::vector&lt;const EOT*&gt; result;
00250 <a class="code" href="classeo_pop.html#a5">sort</a>(result);
00251 _os &lt;&lt; size() &lt;&lt; <span class="charliteral">'\n'</span>;
00252 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i &lt; size(); ++i)
00253 {
00254 _os &lt;&lt; *result[i] &lt;&lt; std::endl;
00255 }
00256 }
00257
<a name="l00262"></a><a class="code" href="classeo_pop.html#a18">00262</a> <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classeo_pop.html#a18">printOn</a>(std::ostream&amp; _os)<span class="keyword"> const </span>
00263 <span class="keyword"> </span>{
00264 _os &lt;&lt; size() &lt;&lt; <span class="charliteral">'\n'</span>;
00265 std::copy( begin(), end(), std::ostream_iterator&lt;EOT&gt;( _os, <span class="stringliteral">"\n"</span>) );
00266 }
00267
<a name="l00274"></a><a class="code" href="classeo_pop.html#z19_0">00274</a> <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classeo_pop.html#z19_0">readFrom</a>(std::istream&amp; _is)
00275 {
00276 size_t sz;
00277 _is &gt;&gt; sz;
00278
00279 resize(sz);
00280
00281 <span class="keywordflow">for</span> (size_t i = 0; i &lt; sz; ++i) {
00282 operator[](i).<a class="code" href="class_e_o.html#z10_1">readFrom</a>( _is );
00283 }
00284 }
00285
<a name="l00289"></a><a class="code" href="classeo_pop.html#z19_1">00289</a> <span class="keyword">virtual</span> std::string <a class="code" href="classeo_pop.html#z19_1">className</a>()<span class="keyword"> const </span>{<span class="keywordflow">return</span> <span class="stringliteral">"eoPop"</span>;};
00291
00292 <span class="keyword">virtual</span> <span class="keywordtype">void</span> invalidate()
00293 {
00294 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i&lt;size(); i++)
00295 this-&gt;operator[](i).<a class="code" href="class_e_o.html#a3">invalidate</a>();
00296 }
00297
00298
00299 <span class="keyword">protected</span>:
00300
00301 };
00302
00303 <span class="preprocessor">#endif</span>
00304 <span class="preprocessor"></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>