git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@40 331e1502-861f-0410-8da2-ba01fb791d7f
255 lines
18 KiB
HTML
255 lines
18 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: t-eoSharing.cpp 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 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 Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Class List</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="namespacemembers.html">Namespace Members</a> | <a class="qindex" href="functions.html">Class Members</a> | <a class="qindex" href="pages.html">Related Pages</a> | <span class="search"><u>S</u>earch for <input class="search" type="text" name="query" value="" size="20" accesskey="s"/></span></form></div>
|
|
<div class="nav">
|
|
<a class="el" href="dir_000002.html">test</a></div>
|
|
<h1>t-eoSharing.cpp</h1><div class="fragment"><pre class="fragment">00001 <span class="preprocessor">#ifdef HAVE_CONFIG_H</span>
|
|
00002 <span class="preprocessor"></span><span class="preprocessor">#include <config.h></span>
|
|
00003 <span class="preprocessor">#endif</span>
|
|
00004 <span class="preprocessor"></span>
|
|
00005 <span class="comment">// to avoid long name warnings</span>
|
|
00006 <span class="preprocessor">#ifdef _MSC_VER</span>
|
|
00007 <span class="preprocessor"></span><span class="preprocessor">#pragma warning(disable:4786)</span>
|
|
00008 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
|
|
00009 <span class="preprocessor"></span>
|
|
00010 <span class="preprocessor">#include <iostream></span>
|
|
00011 <span class="preprocessor">#include <sstream></span>
|
|
00012 <span class="preprocessor">#include <stdexcept></span>
|
|
00013 <span class="preprocessor">#include <vector></span>
|
|
00014
|
|
00015 <span class="comment">// general</span>
|
|
00016 <span class="preprocessor">#include <eo></span>
|
|
00017 <span class="preprocessor">#include <utils/eoDistance.h></span>
|
|
00018
|
|
00019 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00020
|
|
00021 <span class="keyword">struct </span><a class="code" href="struct_dummy.html">Dummy</a> : <span class="keyword">public</span> <a class="code" href="class_e_o.html">EO</a><double>
|
|
00022 {
|
|
00023 <span class="keyword">typedef</span> <span class="keywordtype">double</span> Type;
|
|
<a name="l00024"></a><a class="code" href="struct_dummy.html#a4">00024</a> <span class="keywordtype">void</span> <a class="code" href="struct_dummy.html#a1">printOn</a>(std::ostream & _os)<span class="keyword"> const</span>
|
|
00025 <span class="keyword"> </span>{
|
|
00026 <a class="code" href="class_e_o.html#z10_2">EO<double>::printOn</a>(_os);
|
|
00027 std::cout << <span class="stringliteral">" "</span> << xdist ;
|
|
00028 }
|
|
00029 <span class="keywordtype">double</span> xdist;
|
|
00030 };
|
|
00031
|
|
00032 <span class="keyword">class</span>
|
|
00033 <span class="keyword"></span>eoDummyDistance : <span class="keyword">public</span> <a class="code" href="classeo_distance.html">eoDistance</a><Dummy>
|
|
00034 {
|
|
00035 <span class="keywordtype">double</span> <a class="code" href="classeo_b_f.html#a1">operator()</a>(<span class="keyword">const</span> <a class="code" href="struct_dummy.html">Dummy</a> & _v1, <span class="keyword">const</span> <a class="code" href="struct_dummy.html">Dummy</a> & _v2)
|
|
00036 {
|
|
00037 <span class="keywordtype">double</span> r= _v1.<a class="code" href="struct_dummy.html#o1">xdist</a> - _v2.<a class="code" href="struct_dummy.html#o1">xdist</a>;
|
|
00038 <span class="keywordflow">return</span> sqrt(r*r);
|
|
00039 }
|
|
00040 };
|
|
00041
|
|
00042
|
|
00043 <span class="keywordtype">bool</span> operator==(<span class="keyword">const</span> <a class="code" href="struct_dummy.html">Dummy</a> & _d1, <span class="keyword">const</span> <a class="code" href="struct_dummy.html">Dummy</a> & _d2)
|
|
00044 {
|
|
00045 <span class="keywordflow">return</span> _d1.<a class="code" href="class_e_o.html#a2">fitness</a>() == _d2.<a class="code" href="class_e_o.html#a2">fitness</a>();
|
|
00046 }
|
|
00047
|
|
00048 <span class="keyword">struct </span>eoDummyPop : <span class="keyword">public</span> <a class="code" href="classeo_pop.html">eoPop</a><Dummy>
|
|
00049 {
|
|
00050 <span class="keyword">public</span> :
|
|
00051 eoDummyPop(<span class="keywordtype">int</span> s=0) { resize(s); }
|
|
00052 };
|
|
00053
|
|
00054 <span class="comment">// helper - DOES NOT WORK if different individuals have same fitness!!!</span>
|
|
00055 <span class="keyword">template</span> <<span class="keyword">class</span> EOT>
|
|
00056 <span class="keywordtype">unsigned</span> isInPop(<a class="code" href="struct_dummy.html">EOT</a> & _indi, <a class="code" href="classeo_pop.html">eoPop<EOT></a> & _pop)
|
|
00057 {
|
|
00058 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i<_pop.size(); i++)
|
|
00059 <span class="keywordflow">if</span> (_pop[i] == _indi)
|
|
00060 <span class="keywordflow">return</span> i;
|
|
00061 <span class="keywordflow">return</span> _pop.size();
|
|
00062 }
|
|
00063
|
|
00064 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> pSize; <span class="comment">// global variable, bouh!</span>
|
|
00065 std::string fitnessType; <span class="comment">// yes, a global variable :-)</span>
|
|
00066 eoDummyPop parentsOrg;
|
|
00067
|
|
00068 <span class="keyword">template</span> <<span class="keyword">class</span> EOT>
|
|
00069 <span class="keywordtype">void</span> testSelectMany(<a class="code" href="classeo_select.html">eoSelect<EOT></a> & _select, std::string _name)
|
|
00070 {
|
|
00071 <span class="keywordtype">unsigned</span> i;
|
|
00072 std::cout << <span class="stringliteral">"\n\n"</span> << fitnessType + _name << std::endl;
|
|
00073 std::cout << <span class="stringliteral">"===============\n"</span>;
|
|
00074
|
|
00075 eoDummyPop parents(parentsOrg);
|
|
00076 eoDummyPop offspring(0);
|
|
00077
|
|
00078 <span class="comment">// do the selection</span>
|
|
00079 _select(parents, offspring);
|
|
00080
|
|
00081 <span class="comment">// cout << "Pop offspring \n" << offspring << endl;</span>
|
|
00082
|
|
00083 <span class="comment">// compute stats</span>
|
|
00084 std::vector<unsigned> nb(parents.size(), 0);
|
|
00085 <span class="keywordflow">for</span> (i=0; i<offspring.size(); i++)
|
|
00086 {
|
|
00087 <span class="keywordtype">unsigned</span> trouve = isInPop<Dummy>(offspring[i], parents);
|
|
00088 <span class="keywordflow">if</span> (trouve == parents.size()) <span class="comment">// pas trouve</span>
|
|
00089 <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"Pas trouve ds parents"</span>);
|
|
00090 nb[trouve]++;
|
|
00091 }
|
|
00092 <span class="comment">// dump to file so you can plot using gnuplot - dir name is hardcoded!</span>
|
|
00093 std::string fName = <span class="stringliteral">"ResSelect/"</span> + fitnessType + _name + <span class="stringliteral">".select"</span>;
|
|
00094 std::ofstream os(fName.c_str());
|
|
00095 <span class="keywordflow">for</span> (i=0; i<parents.size(); i++)
|
|
00096 {
|
|
00097 std::cout << i << <span class="stringliteral">" -> "</span> << ( (double)nb[i])/offspring.size() << std::endl;
|
|
00098 os << i << <span class="stringliteral">" "</span> << ( (double)nb[i])/offspring.size() << std::endl;
|
|
00099 }
|
|
00100
|
|
00101 }
|
|
00102
|
|
00103 <span class="keyword">template</span> <<span class="keyword">class</span> EOT>
|
|
00104 <span class="keywordtype">void</span> testSelectOne(<a class="code" href="classeo_select_one.html">eoSelectOne<EOT></a> & _select, <a class="code" href="classeo_how_many.html">eoHowMany</a> & _offspringRate,
|
|
00105 <a class="code" href="classeo_how_many.html">eoHowMany</a> & _fertileRate, std::string _name)
|
|
00106 {
|
|
00107 <a class="code" href="classeo_truncated_select_one.html">eoTruncatedSelectOne<EOT></a> truncSelect(_select, _fertileRate);
|
|
00108 <a class="code" href="classeo_select_many.html">eoSelectMany<EOT></a> percSelect(truncSelect, _offspringRate);
|
|
00109 testSelectMany<EOT>(percSelect, _name);
|
|
00110 }
|
|
00111
|
|
00112
|
|
00113 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00114
|
|
00115 <span class="keywordtype">int</span> the_main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
|
|
00116 {
|
|
00117 <a class="code" href="classeo_parser.html">eoParser</a> parser(argc, argv);
|
|
00118
|
|
00119 <span class="comment">// random seed</span>
|
|
00120 <a class="code" href="classeo_value_param.html">eoValueParam<uint32_t></a>& seedParam = parser.createParam(uint32_t(0), <span class="stringliteral">"seed"</span>, <span class="stringliteral">"Random number seed"</span>, <span class="charliteral">'S'</span>);
|
|
00121 <span class="keywordflow">if</span> (seedParam.<a class="code" href="classeo_value_param.html#a2">value</a>() == 0)
|
|
00122 seedParam.<a class="code" href="classeo_value_param.html#a2">value</a>() = time(0);
|
|
00123 rng.<a class="code" href="classeo_rng.html#a2">reseed</a>(seedParam.<a class="code" href="classeo_value_param.html#a2">value</a>());
|
|
00124
|
|
00125
|
|
00126 <span class="comment">// pSize global variable !</span>
|
|
00127 <a class="code" href="classeo_value_param.html">eoValueParam<unsigned></a> pSizeParam = parser.createParam(<span class="keywordtype">unsigned</span>(10), <span class="stringliteral">"parentSize"</span>, <span class="stringliteral">"Parent size"</span>,<span class="charliteral">'P'</span>);
|
|
00128 pSize = pSizeParam.<a class="code" href="classeo_value_param.html#a2">value</a>();
|
|
00129
|
|
00130 <a class="code" href="classeo_how_many.html">eoHowMany</a> oRate = parser.createParam(<a class="code" href="classeo_how_many.html">eoHowMany</a>(1.0), <span class="stringliteral">"offsrpringRate"</span>, <span class="stringliteral">"Offsrpring rate (% or absolute)"</span>,<span class="charliteral">'O'</span>).<a class="code" href="classeo_value_param.html#a2">value</a>();
|
|
00131
|
|
00132 <a class="code" href="classeo_how_many.html">eoHowMany</a> fRate = parser.createParam(<a class="code" href="classeo_how_many.html">eoHowMany</a>(1.0), <span class="stringliteral">"fertileRate"</span>, <span class="stringliteral">"Fertility rate (% or absolute)"</span>,<span class="charliteral">'F'</span>).<a class="code" href="classeo_value_param.html#a2">value</a>();
|
|
00133
|
|
00134
|
|
00135 <span class="keywordtype">double</span> nicheSize = parser.createParam(0.1, <span class="stringliteral">"nicheSize"</span>, <span class="stringliteral">"Paramter Sigma for Sharing"</span>,<span class="charliteral">'\0'</span>).<a class="code" href="classeo_value_param.html#a2">value</a>();
|
|
00136
|
|
00137 <a class="code" href="classeo_param_param_type.html">eoParamParamType</a> & peakParam = parser.createParam(<a class="code" href="classeo_param_param_type.html">eoParamParamType</a>(<span class="stringliteral">"2(1,2)"</span>), <span class="stringliteral">"peaks"</span>, <span class="stringliteral">"Description of the peaks: N(nb1,nb2,...,nbN)"</span>, <span class="charliteral">'p'</span>).<a class="code" href="classeo_value_param.html#a2">value</a>();
|
|
00138
|
|
00139 <span class="comment">// the number of peaks: first item of the paramparam</span>
|
|
00140 <span class="keywordtype">unsigned</span> peakNumber = atoi(peakParam.first.c_str());
|
|
00141 <span class="keywordflow">if</span> (peakNumber < 2)
|
|
00142 {
|
|
00143 std::cerr << <span class="stringliteral">"WARNING, nb of peaks must be larger than 2, using 2"</span> << std::endl;
|
|
00144 peakNumber = 2;
|
|
00145 }
|
|
00146
|
|
00147 std::vector<unsigned> nbIndiPerPeak(peakNumber);
|
|
00148 <span class="keywordtype">unsigned</span> i, sum=0;
|
|
00149
|
|
00150 <span class="comment">// the second item is a vector<string> containing all values</span>
|
|
00151 <span class="keywordflow">if</span> (!peakParam.second.size()) <span class="comment">// no other parameter : equal peaks</span>
|
|
00152 {
|
|
00153 std::cerr << <span class="stringliteral">"WARNING, no nb of indis per peaks, using equal nbs"</span> << std::endl;
|
|
00154 <span class="keywordflow">for</span> (i=0; i<peakNumber; i++)
|
|
00155 nbIndiPerPeak[i] = pSize/peakNumber;
|
|
00156 }
|
|
00157 <span class="keywordflow">else</span> <span class="comment">// parameters passed by user</span>
|
|
00158 <span class="keywordflow">if</span> (peakParam.second.size() != peakNumber)
|
|
00159 {
|
|
00160 std::cerr << <span class="stringliteral">"ERROR, not enough nb of indis per peaks"</span> << std::endl;
|
|
00161 exit(1);
|
|
00162 }
|
|
00163 <span class="keywordflow">else</span> <span class="comment">// now we have in peakParam.second all numbers</span>
|
|
00164 {
|
|
00165 <span class="keywordflow">for</span> (i=0; i<peakNumber; i++)
|
|
00166 sum += ( nbIndiPerPeak[i] = atoi(peakParam.second[i].c_str()) );
|
|
00167 <span class="comment">// now normalize</span>
|
|
00168 <span class="keywordflow">for</span> (i=0; i<peakNumber; i++)
|
|
00169 nbIndiPerPeak[i] = nbIndiPerPeak[i] * pSize / sum;
|
|
00170 }
|
|
00171
|
|
00172 <span class="comment">// compute exact total</span>
|
|
00173 sum = 0;
|
|
00174 <span class="keywordflow">for</span> (i=0; i<peakNumber; i++)
|
|
00175 sum += nbIndiPerPeak[i];
|
|
00176 <span class="keywordflow">if</span> (sum != pSize)
|
|
00177 {
|
|
00178 pSize = pSizeParam.<a class="code" href="classeo_value_param.html#a2">value</a>() = sum;
|
|
00179 std::cerr << <span class="stringliteral">"WARNING, adjusting pSize to "</span> << pSize << std::endl;
|
|
00180 }
|
|
00181
|
|
00182 make_help(parser);
|
|
00183
|
|
00184 <span class="comment">// hard-coded directory name ...</span>
|
|
00185 std::cout << <span class="stringliteral">"Testing the Sharing\n"</span>;
|
|
00186 std::cout << <span class="stringliteral">" There will be "</span> << peakNumber << <span class="stringliteral">" peaks"</span>;
|
|
00187 std::cout << <span class="stringliteral">" with respective pops "</span>;
|
|
00188 <span class="keywordflow">for</span> (i=0; i<peakNumber; i++)
|
|
00189 std::cout << nbIndiPerPeak[i] << <span class="stringliteral">", "</span>;
|
|
00190 std::cout << <span class="stringliteral">"\n Peaks are at distance 1 from one-another (dim 1),\n"</span>;
|
|
00191 std::cout << <span class="stringliteral">" fitness of each peak is nb of peak, and\n"</span>;
|
|
00192 std::cout << <span class="stringliteral">" fitness of individuals = uniform[fitness of peak +- 0.01]\n\n"</span>;
|
|
00193
|
|
00194 std::cout << <span class="stringliteral">"The resulting file (in dir ResSelect), contains \n"</span>;
|
|
00195 std::cout << <span class="stringliteral">" the empirical proba. for each indi to be selected."</span> << std::endl;
|
|
00196 system(<span class="stringliteral">"mkdir ResSelect"</span>);
|
|
00197
|
|
00198 <span class="comment">// initialize parent population</span>
|
|
00199 parentsOrg.resize(pSize);
|
|
00200
|
|
00201 <span class="comment">// all peaks of equal size in fitness, with different nn of individuals</span>
|
|
00202 <span class="keywordtype">unsigned</span> index=0;
|
|
00203 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> nbP=0; nbP<peakNumber; nbP++)
|
|
00204 <span class="keywordflow">for</span> (i=0; i<nbIndiPerPeak[nbP]; i++)
|
|
00205 {
|
|
00206 parentsOrg[index].fitness(nbP+1 + 0.02*eo::rng.uniform() - 0.01);
|
|
00207 parentsOrg[index].xdist = nbP+1 + 0.02*eo::rng.uniform() - 0.01;
|
|
00208 index++;
|
|
00209 }
|
|
00210
|
|
00211 std::cout << <span class="stringliteral">"Initial population\n"</span> << parentsOrg << std::endl;
|
|
00212
|
|
00213 <span class="keywordtype">char</span> fileName[1024];
|
|
00214
|
|
00215 <span class="comment">// the selection procedures under test</span>
|
|
00216 <span class="comment">// eoDetSelect<Dummy> detSelect(oRate);</span>
|
|
00217 <span class="comment">// testSelectMany(detSelect, "detSelect");</span>
|
|
00218
|
|
00219 <span class="comment">// Sharing using the perf2Worth construct</span>
|
|
00220 <span class="comment">// need a distance for that</span>
|
|
00221 eoDummyDistance dist;
|
|
00222 <a class="code" href="classeo_sharing_select.html">eoSharingSelect<Dummy></a> newSharingSelect(nicheSize, dist);
|
|
00223 sprintf(fileName,<span class="stringliteral">"Niche_%g"</span>,nicheSize);
|
|
00224 testSelectOne<Dummy>(newSharingSelect, oRate, fRate, fileName);
|
|
00225
|
|
00226 <span class="keywordflow">return</span> 1;
|
|
00227 }
|
|
00228
|
|
00229 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
|
|
00230 {
|
|
00231 <span class="keywordflow">try</span>
|
|
00232 {
|
|
00233 the_main(argc, argv);
|
|
00234 }
|
|
00235 <span class="keywordflow">catch</span>(std::exception& e)
|
|
00236 {
|
|
00237 std::cout << <span class="stringliteral">"Exception: "</span> << e.what() << std::endl;
|
|
00238 <span class="keywordflow">return</span> 1;
|
|
00239 }
|
|
00240 }
|
|
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:43 2006 for EO by
|
|
<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>
|