git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@40 331e1502-861f-0410-8da2-ba01fb791d7f
239 lines
19 KiB
HTML
239 lines
19 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-eoSelect.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-eoSelect.cpp</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00002
|
|
00003 <span class="comment">// to avoid long name warnings</span>
|
|
00004 <span class="preprocessor">#ifdef _MSC_VER</span>
|
|
00005 <span class="preprocessor"></span><span class="preprocessor">#pragma warning(disable:4786)</span>
|
|
00006 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
|
|
00007 <span class="preprocessor"></span>
|
|
00008 <span class="preprocessor">#include <stdexcept></span> <span class="comment">// runtime_error</span>
|
|
00009
|
|
00010 <span class="comment">// general</span>
|
|
00011 <span class="preprocessor">#include <eo></span>
|
|
00012
|
|
00013 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00014
|
|
00015 <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>
|
|
00016 {
|
|
00017 <span class="keyword">typedef</span> <span class="keywordtype">double</span> Type;
|
|
<a name="l00018"></a><a class="code" href="struct_dummy.html#a3">00018</a> <span class="keywordtype">void</span> <a class="code" href="struct_dummy.html#a1">printOn</a>(std::ostream & _os)<span class="keyword"> const</span>
|
|
00019 <span class="keyword"> </span>{
|
|
00020 _os << <span class="stringliteral">" - "</span>;
|
|
00021 <a class="code" href="class_e_o.html#z10_2">EO<double>::printOn</a>(_os);
|
|
00022 }
|
|
00023 };
|
|
00024
|
|
00025 <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)
|
|
00026 {
|
|
00027 <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>();
|
|
00028 }
|
|
00029
|
|
00030 <span class="keyword">struct </span>eoDummyPop : <span class="keyword">public</span> <a class="code" href="classeo_pop.html">eoPop</a><Dummy>
|
|
00031 {
|
|
00032 <span class="keyword">public</span> :
|
|
00033 eoDummyPop(<span class="keywordtype">int</span> s=0) { resize(s); }
|
|
00034 };
|
|
00035
|
|
00036 <span class="comment">// helper - DOES NOT WORK if different individuals have same fitness!!!</span>
|
|
00037 <span class="keyword">template</span> <<span class="keyword">class</span> EOT>
|
|
00038 <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)
|
|
00039 {
|
|
00040 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i<_pop.size(); i++)
|
|
00041 <span class="keywordflow">if</span> (_pop[i] == _indi)
|
|
00042 <span class="keywordflow">return</span> i;
|
|
00043 <span class="keywordflow">return</span> _pop.size();
|
|
00044 }
|
|
00045
|
|
00046 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> pSize; <span class="comment">// global variable, bouh!</span>
|
|
00047 std::string fitnessType; <span class="comment">// yes, a global variable :-)</span>
|
|
00048 eoDummyPop parentsOrg;
|
|
00049
|
|
00050 <span class="keyword">template</span> <<span class="keyword">class</span> EOT>
|
|
00051 <span class="keywordtype">void</span> testSelectMany(<a class="code" href="classeo_select.html">eoSelect<EOT></a> & _select, std::string _name)
|
|
00052 {
|
|
00053 <span class="keywordtype">unsigned</span> i;
|
|
00054 std::cout << <span class="stringliteral">"\n\n"</span> << fitnessType + _name << std::endl;
|
|
00055 std::cout << <span class="stringliteral">"===============\n"</span>;
|
|
00056
|
|
00057 eoDummyPop parents(parentsOrg);
|
|
00058 eoDummyPop offspring(0);
|
|
00059
|
|
00060 <span class="comment">// do the selection</span>
|
|
00061 _select(parents, offspring);
|
|
00062
|
|
00063 <span class="comment">// compute stats</span>
|
|
00064 std::vector<unsigned> nb(parents.size(), 0);
|
|
00065 <span class="keywordflow">for</span> (i=0; i<offspring.size(); i++)
|
|
00066 {
|
|
00067 <span class="keywordtype">unsigned</span> trouve = isInPop<Dummy>(offspring[i], parents);
|
|
00068 <span class="keywordflow">if</span> (trouve == parents.size()) <span class="comment">// pas trouve</span>
|
|
00069 <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"Pas trouve ds parents"</span>);
|
|
00070 nb[trouve]++;
|
|
00071 }
|
|
00072 <span class="comment">// dump to file so you can plot using gnuplot - dir name is hardcoded!</span>
|
|
00073 std::string fName = <span class="stringliteral">"ResSelect/"</span> + fitnessType + _name + <span class="stringliteral">".select"</span>;
|
|
00074 std::ofstream os(fName.c_str());
|
|
00075 <span class="keywordflow">for</span> (i=0; i<parents.size(); i++)
|
|
00076 {
|
|
00077 std::cout << i << <span class="stringliteral">" -> "</span> << ( (double)nb[i])/offspring.size() << std::endl;
|
|
00078 os << i << <span class="stringliteral">" "</span> << ( (double)nb[i])/offspring.size() << std::endl;
|
|
00079 }
|
|
00080
|
|
00081 }
|
|
00082
|
|
00083 <span class="keyword">template</span> <<span class="keyword">class</span> EOT>
|
|
00084 <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,
|
|
00085 <a class="code" href="classeo_how_many.html">eoHowMany</a> & _fertileRate, std::string _name)
|
|
00086 {
|
|
00087 <a class="code" href="classeo_truncated_select_one.html">eoTruncatedSelectOne<EOT></a> truncSelect(_select, _fertileRate);
|
|
00088 <a class="code" href="classeo_select_many.html">eoSelectMany<EOT></a> percSelect(truncSelect, _offspringRate);
|
|
00089 testSelectMany<EOT>(percSelect, _name);
|
|
00090 }
|
|
00091
|
|
00092
|
|
00093 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00094
|
|
00095 <span class="keywordtype">int</span> the_main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
|
|
00096 {
|
|
00097 <a class="code" href="classeo_parser.html">eoParser</a> parser(argc, argv);
|
|
00098 <a class="code" href="classeo_value_param.html">eoValueParam<unsigned></a> parentSizeParam = parser.createParam(<span class="keywordtype">unsigned</span>(10), <span class="stringliteral">"parentSize"</span>, <span class="stringliteral">"Parent size"</span>,<span class="charliteral">'P'</span>);
|
|
00099 pSize = parentSizeParam.<a class="code" href="classeo_value_param.html#a2">value</a>(); <span class="comment">// global variable</span>
|
|
00100
|
|
00101 <span class="comment">// eoValueParam<double> offsrpringRateParam = parser.createParam<double>(1.0, "offsrpringRate", "Offsrpring rate",'O');</span>
|
|
00102 <span class="comment">// double oRate = offsrpringRateParam.value();</span>
|
|
00103 <a class="code" href="classeo_value_param.html">eoValueParam<eoHowMany></a> offsrpringRateParam = 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>);
|
|
00104 <a class="code" href="classeo_how_many.html">eoHowMany</a> oRate = offsrpringRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>();
|
|
00105
|
|
00106 <a class="code" href="classeo_value_param.html">eoValueParam<eoHowMany></a> fertileRateParam = 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>);
|
|
00107 <a class="code" href="classeo_how_many.html">eoHowMany</a> fRate = fertileRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>();
|
|
00108
|
|
00109 <a class="code" href="classeo_value_param.html">eoValueParam<unsigned></a> tournamentSizeParam = parser.createParam(<span class="keywordtype">unsigned</span>(2), <span class="stringliteral">"tournamentSize"</span>, <span class="stringliteral">"Deterministic tournament size"</span>,<span class="charliteral">'T'</span>);
|
|
00110 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> tSize = tournamentSizeParam.<a class="code" href="classeo_value_param.html#a2">value</a>();
|
|
00111
|
|
00112 <a class="code" href="classeo_value_param.html">eoValueParam<double></a> tournamentRateParam = parser.createParam(1.0, <span class="stringliteral">"tournamentRate"</span>, <span class="stringliteral">"Stochastic tournament rate"</span>,<span class="charliteral">'t'</span>);
|
|
00113 <span class="keywordtype">double</span> tRate = tournamentRateParam.<a class="code" href="classeo_value_param.html#a2">value</a>();
|
|
00114
|
|
00115 <a class="code" href="classeo_value_param.html">eoValueParam<double></a> rankingPressureParam = parser.createParam(2.0, <span class="stringliteral">"rankingPressure"</span>, <span class="stringliteral">"Selective pressure for the ranking selection"</span>,<span class="charliteral">'p'</span>);
|
|
00116 <span class="keywordtype">double</span> rankingPressure = rankingPressureParam.<a class="code" href="classeo_value_param.html#a2">value</a>();
|
|
00117
|
|
00118 <a class="code" href="classeo_value_param.html">eoValueParam<double></a> rankingExponentParam = parser.createParam(1.0, <span class="stringliteral">"rankingExponent"</span>, <span class="stringliteral">"Exponent for the ranking selection"</span>,<span class="charliteral">'e'</span>);
|
|
00119 <span class="keywordtype">double</span> rankingExponent = rankingExponentParam.<a class="code" href="classeo_value_param.html#a2">value</a>();
|
|
00120
|
|
00121 <a class="code" href="classeo_value_param.html">eoValueParam<std::string></a> fitTypeParam = parser.createParam(std::string(<span class="stringliteral">"linear"</span>), <span class="stringliteral">"fitType"</span>, <span class="stringliteral">"Type of fitness (linear, exp, log, super"</span>,<span class="charliteral">'f'</span>);
|
|
00122 fitnessType = fitTypeParam.<a class="code" href="classeo_value_param.html#a2">value</a>();
|
|
00123
|
|
00124 <span class="keywordflow">if</span> (parser.userNeedsHelp())
|
|
00125 {
|
|
00126 parser.printHelp(std::cout);
|
|
00127 exit(0);
|
|
00128 }
|
|
00129
|
|
00130 <span class="comment">// hard-coded directory name ...</span>
|
|
00131 system(<span class="stringliteral">"mkdir ResSelect"</span>);
|
|
00132 std::cout << <span class="stringliteral">"Testing the Selections\nParents size = "</span> << pSize
|
|
00133 << <span class="stringliteral">", offspring rate = "</span> << oRate;
|
|
00134 std::cout << <span class="stringliteral">" and putting rsulting files in dir ResSelect"</span> << std::endl;
|
|
00135
|
|
00136 <span class="comment">// initialize parent population</span>
|
|
00137 parentsOrg.resize(pSize);
|
|
00138 <span class="keywordflow">if</span> (fitnessType == std::string(<span class="stringliteral">"linear"</span>))
|
|
00139 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i<pSize; i++)
|
|
00140 parentsOrg[i].fitness(i);
|
|
00141 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (fitnessType == std::string(<span class="stringliteral">"exp"</span>))
|
|
00142 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i<pSize; i++)
|
|
00143 parentsOrg[i].fitness(exp((<span class="keywordtype">double</span>)i));
|
|
00144 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (fitnessType == std::string(<span class="stringliteral">"log"</span>))
|
|
00145 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i<pSize; i++)
|
|
00146 parentsOrg[i].fitness(log(i+1.));
|
|
00147 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (fitnessType == std::string(<span class="stringliteral">"super"</span>))
|
|
00148 {
|
|
00149 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i<pSize-1; i++)
|
|
00150 parentsOrg[i].fitness(i);
|
|
00151 parentsOrg[pSize-1].fitness(10*pSize);
|
|
00152 }
|
|
00153 <span class="keywordflow">else</span>
|
|
00154 <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"Invalid fitness Type"</span>+fitnessType);
|
|
00155
|
|
00156 std::cout << <span class="stringliteral">"Initial parents (odd)\n"</span> << parentsOrg << std::endl;
|
|
00157
|
|
00158 <span class="comment">// random seed</span>
|
|
00159 <a class="code" href="classeo_value_param.html">eoValueParam<uint32_t></a>& seedParam = parser.createParam(uint32_t(0), <span class="stringliteral">"seed"</span>,
|
|
00160 <span class="stringliteral">"Random number seed"</span>, <span class="charliteral">'S'</span>);
|
|
00161 <span class="keywordflow">if</span> (seedParam.<a class="code" href="classeo_value_param.html#a2">value</a>() == 0)
|
|
00162 seedParam.<a class="code" href="classeo_value_param.html#a2">value</a>() = time(0);
|
|
00163 rng.<a class="code" href="classeo_rng.html#a2">reseed</a>(seedParam.<a class="code" href="classeo_value_param.html#a2">value</a>());
|
|
00164
|
|
00165 <span class="keywordtype">char</span> fileName[1024];
|
|
00166
|
|
00167 <span class="comment">// the selection procedures under test</span>
|
|
00168 <span class="comment">// eoDetSelect<Dummy> detSelect(oRate);</span>
|
|
00169 <span class="comment">// testSelectMany(detSelect, "detSelect");</span>
|
|
00170
|
|
00171 <span class="comment">// Roulette</span>
|
|
00172 <a class="code" href="classeo_proportional_select.html">eoProportionalSelect<Dummy></a> propSelect;
|
|
00173 testSelectOne<Dummy>(propSelect, oRate, fRate, <span class="stringliteral">"PropSelect"</span>);
|
|
00174
|
|
00175 <span class="comment">// Linear ranking using the perf2Worth construct</span>
|
|
00176 <a class="code" href="classeo_ranking_select.html">eoRankingSelect<Dummy></a> newRankingSelect(rankingPressure);
|
|
00177 sprintf(fileName,<span class="stringliteral">"LinRank_%g"</span>,rankingPressure);
|
|
00178 testSelectOne<Dummy>(newRankingSelect, oRate, fRate, fileName);
|
|
00179
|
|
00180 <span class="comment">// Exponential ranking using the perf2Worth construct</span>
|
|
00181 std::cout << <span class="stringliteral">"rankingExponent "</span> << rankingExponent << std::endl;
|
|
00182 <a class="code" href="classeo_ranking_select.html">eoRankingSelect<Dummy></a> expRankingSelect(rankingPressure,rankingExponent);
|
|
00183 sprintf(fileName,<span class="stringliteral">"ExpRank_%g_%g"</span>,rankingPressure, rankingExponent);
|
|
00184 testSelectOne<Dummy>(expRankingSelect, oRate, fRate, fileName);
|
|
00185
|
|
00186 <span class="comment">// Det tournament</span>
|
|
00187 <a class="code" href="classeo_det_tournament_select.html">eoDetTournamentSelect<Dummy></a> detTourSelect(tSize);
|
|
00188 sprintf(fileName,<span class="stringliteral">"DetTour_%d"</span>,tSize);
|
|
00189 testSelectOne<Dummy>(detTourSelect, oRate, fRate, fileName);
|
|
00190
|
|
00191 <span class="comment">// Stoch tournament</span>
|
|
00192 <a class="code" href="classeo_stoch_tournament_select.html">eoStochTournamentSelect<Dummy></a> stochTourSelect(tRate);
|
|
00193 sprintf(fileName,<span class="stringliteral">"StochTour_%g"</span>,tRate);
|
|
00194 testSelectOne<Dummy>(stochTourSelect, oRate, fRate, fileName);
|
|
00195
|
|
00196 <span class="comment">// Fitness scaling</span>
|
|
00197 <a class="code" href="classeo_fitness_scaling_select.html">eoFitnessScalingSelect<Dummy></a> newFitScaleSelect(rankingPressure);
|
|
00198 sprintf(fileName,<span class="stringliteral">"LinFitScale_%g"</span>,rankingPressure);
|
|
00199 testSelectOne<Dummy>(newFitScaleSelect, oRate, fRate, fileName);
|
|
00200
|
|
00201 <span class="comment">// Sequential selections</span>
|
|
00202 <a class="code" href="classeo_sequential_select.html">eoSequentialSelect<Dummy></a> seqSel(<span class="keyword">false</span>);
|
|
00203 strcpy(fileName,<span class="stringliteral">"Sequential"</span>);
|
|
00204 testSelectOne<Dummy>(seqSel, oRate, fRate, fileName);
|
|
00205
|
|
00206 <a class="code" href="classeo_elite_sequential_select.html">eoEliteSequentialSelect<Dummy></a> eliteSeqSel;
|
|
00207 strcpy(fileName,<span class="stringliteral">"EliteSequential"</span>);
|
|
00208 testSelectOne<Dummy>(eliteSeqSel, oRate, fRate, fileName);
|
|
00209
|
|
00210 <span class="keywordflow">return</span> 1;
|
|
00211 }
|
|
00212
|
|
00213 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
|
|
00214 {
|
|
00215 <span class="keywordflow">try</span>
|
|
00216 {
|
|
00217 the_main(argc, argv);
|
|
00218 }
|
|
00219 <span class="keywordflow">catch</span>(std::exception& e)
|
|
00220 {
|
|
00221 std::cout << <span class="stringliteral">"Exception: "</span> << e.what() << std::endl;
|
|
00222 <span class="keywordflow">return</span> 1;
|
|
00223 }
|
|
00224 }
|
|
</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>
|