git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@40 331e1502-861f-0410-8da2-ba01fb791d7f
259 lines
18 KiB
HTML
259 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-eoPareto.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-eoPareto.cpp</h1><div class="fragment"><pre class="fragment">00001
|
|
00002 <span class="preprocessor">#include <eo></span>
|
|
00003
|
|
00004 <span class="comment">//#include <utils/eoMOFitnessStat.h></span>
|
|
00005 <span class="preprocessor">#include <eoNDSorting.h></span>
|
|
00006 <span class="preprocessor">#include <eoParetoFitness.h></span>
|
|
00007
|
|
00008 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
|
|
00009
|
|
00010 <span class="comment">// Look: overloading the maximization without overhead (thing can be inlined)</span>
|
|
00011 <span class="keyword">class </span>MinimizingFitnessTraits : <span class="keyword">public</span> <a class="code" href="classeo_pareto_fitness_traits.html">eoParetoFitnessTraits</a>
|
|
00012 {
|
|
00013 <span class="keyword">public</span> :
|
|
00014 <span class="keyword">static</span> <span class="keywordtype">bool</span> maximizing(<span class="keywordtype">int</span>) { <span class="keywordflow">return</span> <span class="keyword">false</span>; }
|
|
00015 };
|
|
00016
|
|
00017 <span class="keyword">typedef</span> <a class="code" href="classeo_pareto_fitness.html">eoParetoFitness<MinimizingFitnessTraits></a> <a class="code" href="classeo_pareto_fitness.html">fitness_type</a>;
|
|
00018
|
|
00019 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> chromsize=3;
|
|
00020 <span class="keyword">const</span> <span class="keywordtype">double</span> minval = -5;
|
|
00021 <span class="keyword">const</span> <span class="keywordtype">double</span> maxval = 5;
|
|
00022
|
|
00023 <span class="keyword">struct </span>eoDouble : <span class="keyword">public</span> EO<fitness_type>
|
|
00024 {
|
|
00025 <span class="keywordtype">double</span> value[chromsize];
|
|
00026 };
|
|
00027
|
|
00028 <span class="keyword">class </span>Mutate : <span class="keyword">public</span> <a class="code" href="classeo_mon_op.html">eoMonOp</a><eoDouble>
|
|
00029 {
|
|
00030 <span class="keywordtype">bool</span> <a class="code" href="classeo_u_f.html#a1">operator()</a>(eoDouble& _eo)
|
|
00031 {
|
|
00032 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < chromsize; ++i)
|
|
00033 {
|
|
00034 <span class="keywordflow">if</span> (rng.<a class="code" href="classeo_rng.html#a6">flip</a>(1./chromsize))
|
|
00035 _eo.value[i] += rng.<a class="code" href="classeo_rng.html#a7">normal</a>() * 0.1 * _eo.value[i];
|
|
00036
|
|
00037 <span class="keywordflow">if</span> (_eo.value[i] < minval)
|
|
00038 _eo.value[i] = minval;
|
|
00039 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (_eo.value[i] > maxval)
|
|
00040 _eo.value[i] = maxval;
|
|
00041 }
|
|
00042
|
|
00043 <span class="keywordflow">return</span> <span class="keyword">true</span>;
|
|
00044 }
|
|
00045 };
|
|
00046
|
|
00047 <span class="keyword">class </span>Eval : <span class="keyword">public</span> <a class="code" href="classeo_eval_func.html">eoEvalFunc</a><eoDouble>
|
|
00048 {
|
|
00049 <span class="keywordtype">void</span> <a class="code" href="classeo_u_f.html#a1">operator()</a>(eoDouble& _eo)
|
|
00050 {
|
|
00051 vector<double> x(_eo.value, _eo.value + chromsize);
|
|
00052 fitness_type f;
|
|
00053
|
|
00054 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < chromsize; ++i)
|
|
00055 {
|
|
00056 <span class="keywordflow">if</span> (i < chromsize-1)
|
|
00057 {
|
|
00058 f[0] += -10.0 * exp(-0.2 * sqrt(x[i]*x[i] + x[i+1]*x[i+1]));
|
|
00059 }
|
|
00060
|
|
00061 f[1] += pow(fabs(x[i]), 0.8) + 5 * pow(sin(x[i]),3.);
|
|
00062 }
|
|
00063
|
|
00064 _eo.fitness(f);
|
|
00065 }
|
|
00066 };
|
|
00067
|
|
00068 <span class="keyword">class </span>Init : <span class="keyword">public</span> <a class="code" href="classeo_init.html">eoInit</a><eoDouble>
|
|
00069 {
|
|
00070 <span class="keywordtype">void</span> <a class="code" href="classeo_u_f.html#a1">operator()</a>(eoDouble& _eo)
|
|
00071 {
|
|
00072 _eo.value[0] = rng.<a class="code" href="classeo_rng.html#a4">uniform</a>();
|
|
00073
|
|
00074 <span class="keywordtype">double</span> range = maxval - minval;
|
|
00075
|
|
00076 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 1; i < chromsize; ++i)
|
|
00077 _eo.value[i] = rng.<a class="code" href="classeo_rng.html#a4">uniform</a>() * range + minval;
|
|
00078 _eo.invalidate();
|
|
00079 }
|
|
00080 };
|
|
00081
|
|
00086 <span class="keyword">template</span> <<span class="keyword">class</span> EOT, <span class="keyword">class</span> WorthT = <span class="keywordtype">double</span>>
|
|
00087 <span class="keyword">class </span><a class="code" href="classeo_n_d_plus_replacement.html">eoNDPlusReplacement</a> : <span class="keyword">public</span> <a class="code" href="classeo_replacement.html">eoReplacement</a><EOT>
|
|
00088 {
|
|
00089 <span class="keyword">public</span>:
|
|
00090
|
|
00091 <span class="comment">// using eoNDPlusReplacement< EOT, WorthT >::first;</span>
|
|
00092
|
|
00093 <a class="code" href="classeo_n_d_plus_replacement.html">eoNDPlusReplacement</a>(<a class="code" href="classeo_perf2_worth.html">eoPerf2Worth<EOT, WorthT></a>& _perf2worth)
|
|
00094 : perf2worth(_perf2worth)
|
|
00095 {}
|
|
00096
|
|
00097 <span class="keyword">struct </span>WorthPair : <span class="keyword">public</span> pair<WorthT, const EOT*>
|
|
00098 {
|
|
00099 <span class="keywordtype">bool</span> operator<(<span class="keyword">const</span> WorthPair& other)<span class="keyword"> const</span>
|
|
00100 <span class="keyword"> </span>{ <span class="keywordflow">return</span> other.first < this->first; }
|
|
00101 };
|
|
00102
|
|
00103
|
|
<a name="l00104"></a><a class="code" href="classeo_n_d_plus_replacement.html#a3">00104</a> <span class="keywordtype">void</span> <a class="code" href="classeo_n_d_plus_replacement.html#a1">operator()</a>(<a class="code" href="classeo_pop.html">eoPop<EOT></a>& _parents, <a class="code" href="classeo_pop.html">eoPop<EOT></a>& _offspring)
|
|
00105 {
|
|
00106 <span class="keywordtype">unsigned</span> sz = _parents.size();
|
|
00107 _parents.reserve(_parents.size() + _offspring.size());
|
|
00108 std::copy(_offspring.begin(), _offspring.end(), back_inserter(_parents));
|
|
00109
|
|
00110 <span class="comment">// calculate worths</span>
|
|
00111 perf2worth(_parents);
|
|
00112 perf2worth.sort_pop(_parents);
|
|
00113 perf2worth.resize(_parents, sz);
|
|
00114
|
|
00115 _offspring.clear();
|
|
00116 }
|
|
00117
|
|
00118 <span class="keyword">private</span> :
|
|
00119 <a class="code" href="classeo_perf2_worth.html">eoPerf2Worth<EOT, WorthT></a>& perf2worth;
|
|
00120 };
|
|
00121
|
|
00122 <span class="keyword">template</span> <<span class="keyword">class</span> EOT>
|
|
00123 <a class="code" href="classeo_perf2_worth.html">eoPerf2Worth<EOT, double></a>& make_perf2worth(eoParser& parser, <a class="code" href="classeo_state.html">eoState</a>& state)
|
|
00124 {
|
|
00125
|
|
00126 <span class="keywordtype">unsigned</span> what = parser.createParam(<span class="keywordtype">unsigned</span>(1), <span class="stringliteral">"perf2worth"</span>, <span class="stringliteral">"worth mapping indicator : \n\t \</span>
|
|
00127 <span class="stringliteral"> 0: non_dominated sorting \n\t\</span>
|
|
00128 <span class="stringliteral"> 1: non_dominated sorting 2 \n\t\</span>
|
|
00129 <span class="stringliteral"> 2: simple ranking \n\t"</span>, <span class="charliteral">'w'</span>).<a class="code" href="classeo_value_param.html#a2">value</a>();
|
|
00130
|
|
00131 <span class="keywordflow">switch</span> (what)
|
|
00132 {
|
|
00133 <span class="keywordflow">case</span> 1 : <span class="keywordflow">return</span> state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(<span class="keyword">new</span> <a class="code" href="classeo_n_d_sorting___i_i.html">eoNDSorting_II<EOT></a>());
|
|
00134 <span class="keywordflow">case</span> 2 :
|
|
00135 {
|
|
00136 <a class="code" href="classeo_dominance_map.html">eoDominanceMap<eoDouble></a>& dominance = state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(<span class="keyword">new</span> <a class="code" href="classeo_dominance_map.html">eoDominanceMap<EOT></a>);
|
|
00137 <span class="keywordflow">return</span> state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(<span class="keyword">new</span> <a class="code" href="classeo_pareto_ranking.html">eoParetoRanking<EOT></a>(dominance));
|
|
00138 }
|
|
00139 }
|
|
00140 <span class="comment">//default</span>
|
|
00141
|
|
00142 <span class="keywordflow">if</span> (what > 2)
|
|
00143 {
|
|
00144 std::cout << <span class="stringliteral">"Warning, need an integer < 3 for perf2worth"</span> << std::endl;
|
|
00145 <span class="comment">// should actually set parser flag, but I don't care</span>
|
|
00146 }
|
|
00147
|
|
00148 <span class="keywordflow">return</span> state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(<span class="keyword">new</span> <a class="code" href="classeo_n_d_sorting___i.html">eoNDSorting_I<EOT></a>(0.5));
|
|
00149 }
|
|
00150
|
|
00151 <span class="keyword">template</span> <<span class="keyword">class</span> EOT>
|
|
00152 <a class="code" href="classeo_select_one.html">eoSelectOne<EOT></a>& make_selector(eoParser& parser, <a class="code" href="classeo_state.html">eoState</a>& state, <a class="code" href="classeo_perf2_worth.html">eoPerf2Worth<EOT, double></a>& perf2worth)
|
|
00153 {
|
|
00154 <span class="keywordtype">unsigned</span> tournamentsize = parser.createParam(<span class="keywordtype">unsigned</span>(2), <span class="stringliteral">"tournament_size"</span>, <span class="stringliteral">"Tournament Size"</span>, <span class="charliteral">'t'</span>).value();
|
|
00155 <span class="keywordtype">double</span> stochtour = parser.createParam(<span class="keywordtype">unsigned</span>(0.95), <span class="stringliteral">"tournament_prob"</span>, <span class="stringliteral">"Probability in stochastic tournament"</span>).value();
|
|
00156
|
|
00157 <span class="keywordflow">switch</span> (parser.createParam(<span class="keywordtype">unsigned</span>(0), <span class="stringliteral">"selector"</span>, <span class="stringliteral">"Which selector (too lazy to explain: use the source)"</span>, <span class="charliteral">'s'</span>).value())
|
|
00158 {
|
|
00159 <span class="keywordflow">case</span> 1 : <span class="keywordflow">return</span> state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(<span class="keyword">new</span> <a class="code" href="classeo_stoch_tournament_worth_select.html">eoStochTournamentWorthSelect<eoDouble></a>(perf2worth, stochtour));
|
|
00160 <span class="keywordflow">case</span> 2 : <span class="keywordflow">return</span> state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(<span class="keyword">new</span> <a class="code" href="classeo_roulette_worth_select.html">eoRouletteWorthSelect<eoDouble></a>(perf2worth));
|
|
00161 <span class="keywordflow">case</span> 3 : <span class="keywordflow">return</span> state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(<span class="keyword">new</span> <a class="code" href="classeo_random_select.html">eoRandomSelect<EOT></a>);
|
|
00162 }
|
|
00163 <span class="comment">// default</span>
|
|
00164
|
|
00165 <span class="keywordflow">return</span> state.<a class="code" href="classeo_functor_store.html#a2">storeFunctor</a>(<span class="keyword">new</span> <a class="code" href="classeo_det_tournament_worth_select.html">eoDetTournamentWorthSelect<eoDouble></a>(perf2worth, tournamentsize));
|
|
00166 }
|
|
00167
|
|
00168 <span class="comment">// Test pareto dominance and perf2worth, and while you're at it, test the eoGnuPlot monitor as well</span>
|
|
00169 <span class="keywordtype">void</span> the_main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[])
|
|
00170 {
|
|
00171 Init init;
|
|
00172 Eval eval;
|
|
00173 Mutate mutate;
|
|
00174
|
|
00175 eoParser parser(argc, argv);
|
|
00176 <a class="code" href="classeo_state.html">eoState</a> state;
|
|
00177
|
|
00178 <span class="keywordtype">unsigned</span> num_gen = parser.createParam(<span class="keywordtype">unsigned</span>(50), <span class="stringliteral">"num_gen"</span>, <span class="stringliteral">"number of generations to run"</span>, <span class="charliteral">'g'</span>).value();
|
|
00179 <span class="keywordtype">unsigned</span> pop_size = parser.createParam(<span class="keywordtype">unsigned</span>(100), <span class="stringliteral">"pop_size"</span>, <span class="stringliteral">"population size"</span>, <span class="charliteral">'p'</span>).<a class="code" href="classeo_value_param.html#a2">value</a>();
|
|
00180 <a class="code" href="classeo_pop.html">eoPop<eoDouble></a> pop(pop_size, init);
|
|
00181
|
|
00182 <span class="comment">// Look, a factory function</span>
|
|
00183 <a class="code" href="classeo_perf2_worth.html">eoPerf2Worth<eoDouble, double></a>& perf2worth = make_perf2worth<eoDouble>(parser, state);
|
|
00184
|
|
00185 <span class="comment">// Look: another factory function, now for selection</span>
|
|
00186 <a class="code" href="classeo_select_one.html">eoSelectOne<eoDouble></a>& select = make_selector<eoDouble>(parser, state, perf2worth);
|
|
00187
|
|
00188 <span class="comment">// One general operator</span>
|
|
00189 <a class="code" href="classeo_proportional_op.html">eoProportionalOp<eoDouble></a> opsel;
|
|
00190 opsel.<a class="code" href="classeo_op_container.html#a3">add</a>(mutate, 1.0);
|
|
00191
|
|
00192 <span class="comment">// the breeder</span>
|
|
00193 <a class="code" href="classeo_general_breeder.html">eoGeneralBreeder<eoDouble></a> breeder(select, opsel);
|
|
00194
|
|
00195 <span class="comment">// replacement</span>
|
|
00196 <a class="code" href="classeo_n_d_plus_replacement.html">eoNDPlusReplacement<eoDouble></a> replace(perf2worth);
|
|
00197
|
|
00198 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> generation = 0;
|
|
00199 <a class="code" href="classeo_gen_continue.html">eoGenContinue<eoDouble></a> gen(num_gen, generation);
|
|
00200 <a class="code" href="classeo_check_point.html">eoCheckPoint<eoDouble></a> cp(gen);
|
|
00201
|
|
00202 <a class="code" href="classeo_m_o_fitness_stat.html">eoMOFitnessStat<eoDouble></a> fitness0(0, <span class="stringliteral">"FirstObjective"</span>);
|
|
00203 <a class="code" href="classeo_m_o_fitness_stat.html">eoMOFitnessStat<eoDouble></a> fitness1(1, <span class="stringliteral">"SecondObjective"</span>);
|
|
00204
|
|
00205 cp.<a class="code" href="classeo_op_container.html#a3">add</a>(fitness0);
|
|
00206 cp.<a class="code" href="classeo_op_container.html#a3">add</a>(fitness1);
|
|
00207
|
|
00208 <a class="code" href="classeo_gnuplot1_d_snapshot.html">eoGnuplot1DSnapshot</a> snapshot(<span class="stringliteral">"pareto"</span>);
|
|
00209 <span class="comment">//snapshot.with(eoGnuplot::Points(3));</span>
|
|
00210
|
|
00211 cp.<a class="code" href="classeo_op_container.html#a3">add</a>(snapshot);
|
|
00212
|
|
00213 snapshot.<a class="code" href="classeo_op_container.html#a3">add</a>(fitness0);
|
|
00214 snapshot.<a class="code" href="classeo_op_container.html#a3">add</a>(fitness1);
|
|
00215
|
|
00216 <span class="comment">// the algo</span>
|
|
00217 <a class="code" href="classeo_easy_e_a.html">eoEasyEA<eoDouble></a> ea(cp, eval, breeder, replace);
|
|
00218
|
|
00219 <span class="keywordflow">if</span> (parser.userNeedsHelp())
|
|
00220 {
|
|
00221 parser.printHelp(std::cout);
|
|
00222 <span class="keywordflow">return</span>;
|
|
00223 }
|
|
00224
|
|
00225 apply<eoDouble>(eval, pop);
|
|
00226 ea(pop);
|
|
00227 }
|
|
00228
|
|
00229
|
|
00230 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[])
|
|
00231 {
|
|
00232 <span class="keywordflow">try</span>
|
|
00233 {
|
|
00234 the_main(argc, argv);
|
|
00235 }
|
|
00236 <span class="keywordflow">catch</span> (std::exception& e)
|
|
00237 {
|
|
00238 std::cout << <span class="stringliteral">"Exception thrown: "</span> << e.what() << std::endl;
|
|
00239 <span class="keywordflow">throw</span> e; <span class="comment">// make sure it does not pass the test</span>
|
|
00240 }
|
|
00241 }
|
|
00242
|
|
00243
|
|
00244
|
|
00245 <span class="comment">// Local Variables:</span>
|
|
00246 <span class="comment">// mode: C++</span>
|
|
00247 <span class="comment">// c-file-style: "Stroustrup"</span>
|
|
00248 <span class="comment">// End:</span>
|
|
</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>
|