paradiseo/trunk/paradiseo-eo/doc/html/exercise2_83_8cpp-source.html
legrand c3aec878e5 Paradiseo-eo sources added
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@40 331e1502-861f-0410-8da2-ba01fb791d7f
2006-12-12 14:49:08 +00:00

198 lines
15 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: exercise2.3.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&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_000003.html">tutorial</a>&nbsp;/&nbsp;<a class="el" href="dir_000004.html">Lesson2</a></div>
<h1>exercise2.3.cpp</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">//-----------------------------------------------------------------------------</span>
00002 <span class="comment">// FirstBitEA.cpp</span>
00003 <span class="comment">//-----------------------------------------------------------------------------</span>
00004 <span class="comment">//*</span>
00005 <span class="comment">// Still an instance of a VERY simple Bitstring Genetic Algorithm</span>
00006 <span class="comment">// (see FirstBitGA.cpp) but now with Breeder - and Combined Ops</span>
00007 <span class="comment">//</span>
00008 <span class="comment">//-----------------------------------------------------------------------------</span>
00009 <span class="preprocessor">#ifdef HAVE_CONFIG_H</span>
00010 <span class="preprocessor"></span><span class="preprocessor">#include &lt;config.h&gt;</span>
00011 <span class="preprocessor">#endif</span>
00012 <span class="preprocessor"></span>
00013 <span class="comment">// standard includes</span>
00014 <span class="preprocessor">#include &lt;stdexcept&gt;</span> <span class="comment">// runtime_error</span>
00015 <span class="preprocessor">#include &lt;iostream&gt;</span> <span class="comment">// cout</span>
00016
00017 <span class="comment">// the general include for eo</span>
00018 <span class="preprocessor">#include &lt;eo&gt;</span>
00019
00020 <span class="comment">// REPRESENTATION</span>
00021 <span class="comment">//-----------------------------------------------------------------------------</span>
00022 <span class="comment">// Include the corresponding file</span>
00023 <span class="preprocessor">#include &lt;ga.h&gt;</span> <span class="comment">// bitstring representation &amp; operators</span>
00024 <span class="comment">// define your individuals</span>
00025 <span class="keyword">typedef</span> <a class="code" href="classeo_bit.html">eoBit&lt;double&gt;</a> <a class="code" href="classeo_real.html">Indi</a>; <span class="comment">// A bitstring with fitness double</span>
00026
00027 <span class="comment">// EVAL</span>
00028 <span class="comment">//-----------------------------------------------------------------------------</span>
00029 <span class="comment">// a simple fitness function that computes the number of ones of a bitstring</span>
00030 <span class="comment">// Now in a separate file, and declared as binary_value(const vector&lt;bool&gt; &amp;)</span>
00031
00032 <span class="preprocessor">#include "binary_value.h"</span>
00033
00034 <span class="comment">// GENERAL</span>
00035 <span class="comment">//-----------------------------------------------------------------------------</span>
00036
00037 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
00038
00039 <span class="keywordtype">void</span> main_function(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
00040 {
00041 <span class="comment">// PARAMETRES</span>
00042 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> SEED = 42; <span class="comment">// seed for random number generator</span>
00043 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> T_SIZE = 3; <span class="comment">// size for tournament selection</span>
00044 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> VEC_SIZE = 20; <span class="comment">// Number of bits in genotypes</span>
00045 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> POP_SIZE = 20; <span class="comment">// Size of population</span>
00046
00047 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> MAX_GEN = 500; <span class="comment">// Maximum number of generation before STOP</span>
00048 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> MIN_GEN = 10; <span class="comment">// Minimum number of generation before ...</span>
00049 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> STEADY_GEN = 50; <span class="comment">// stop after STEADY_GEN gen. without improvelent</span>
00050
00051 <span class="keyword">const</span> <span class="keywordtype">double</span> P_CROSS = 0.8; <span class="comment">// Crossover probability</span>
00052 <span class="keyword">const</span> <span class="keywordtype">double</span> P_MUT = 1.0; <span class="comment">// mutation probability</span>
00053
00054 <span class="keyword">const</span> <span class="keywordtype">double</span> P_MUT_PER_BIT = 0.01; <span class="comment">// internal probability for bit-flip mutation</span>
00055 <span class="comment">// some parameters for chosing among different operators</span>
00056 <span class="keyword">const</span> <span class="keywordtype">double</span> onePointRate = 0.5; <span class="comment">// rate for 1-pt Xover</span>
00057 <span class="keyword">const</span> <span class="keywordtype">double</span> twoPointsRate = 0.5; <span class="comment">// rate for 2-pt Xover</span>
00058 <span class="keyword">const</span> <span class="keywordtype">double</span> URate = 0.5; <span class="comment">// rate for Uniform Xover</span>
00059 <span class="keyword">const</span> <span class="keywordtype">double</span> bitFlipRate = 0.5; <span class="comment">// rate for bit-flip mutation</span>
00060 <span class="keyword">const</span> <span class="keywordtype">double</span> oneBitRate = 0.5; <span class="comment">// rate for one-bit mutation</span>
00061
00062 <span class="comment">// GENERAL</span>
00064 <span class="comment"></span> <span class="comment">// Random seed</span>
00066 <span class="comment"></span> <span class="comment">//reproducible random seed: if you don't change SEED above,</span>
00067 <span class="comment">// you'll aways get the same result, NOT a random run</span>
00068 rng.<a class="code" href="classeo_rng.html#a2">reseed</a>(SEED);
00069
00070 <span class="comment">// EVAL</span>
00072 <span class="comment"></span> <span class="comment">// Fitness function</span>
00074 <span class="comment"></span> <span class="comment">// Evaluation: from a plain C++ fn to an EvalFunc Object</span>
00075 <span class="comment">// you need to give the full description of the function</span>
00076 <a class="code" href="structeo_eval_func_ptr.html">eoEvalFuncPtr&lt;Indi, double, const vector&lt;bool&gt;</a>&amp; &gt; eval( binary_value );
00077
00078 <span class="comment">// INIT</span>
00080 <span class="comment"></span> <span class="comment">// Initilisation of population</span>
00082 <span class="comment"></span>
00083 <span class="comment">// based on boolean_generator class (see utils/rnd_generator.h)</span>
00084 <a class="code" href="classeo_uniform_generator.html">eoUniformGenerator&lt;bool&gt;</a> uGen;
00085 <a class="code" href="classeo_init_fixed_length.html">eoInitFixedLength&lt;Indi&gt;</a> random(VEC_SIZE, uGen);
00086 <span class="comment">// Initialization of the population</span>
00087 <a class="code" href="classeo_pop.html">eoPop&lt;Indi&gt;</a> pop(POP_SIZE, random);
00088
00089 <span class="comment">// and evaluate it in one loop</span>
00090 apply&lt;Indi&gt;(eval, pop); <span class="comment">// STL syntax</span>
00091
00092 <span class="comment">// OUTPUT</span>
00093 <span class="comment">// sort pop before printing it!</span>
00094 pop.sort();
00095 <span class="comment">// Print (sorted) intial population (raw printout)</span>
00096 cout &lt;&lt; <span class="stringliteral">"Initial Population"</span> &lt;&lt; endl;
00097 cout &lt;&lt; pop;
00098
00099 <span class="comment">// ENGINE</span>
00101 <span class="comment"></span> <span class="comment">// selection and replacement</span>
00103 <span class="comment"></span><span class="comment">// SELECT</span>
00104 <span class="comment">// The robust tournament selection</span>
00105 <a class="code" href="classeo_det_tournament_select.html">eoDetTournamentSelect&lt;Indi&gt;</a> selectOne(T_SIZE); <span class="comment">// T_SIZE in [2,POP_SIZE]</span>
00106 <span class="comment">// solution solution solution solution solution solution solution</span>
00107 <span class="comment">// modify the nb offspring / rate in the constructor. 2 ways:</span>
00108 <span class="comment">// second arg treated as integer</span>
00109 <a class="code" href="classeo_select_many.html">eoSelectMany&lt;Indi&gt;</a> select(selectOne,2, eo_is_an_integer);
00110 <span class="comment">// second arg treated as a rate (default behavior)</span>
00111 <span class="comment">// eoSelectMany&lt;Indi&gt; select(selectOne,0.1);</span>
00112
00113 <span class="comment">// REPLACE</span>
00114 <span class="comment">// solution solution solution solution solution solution solution</span>
00115 <span class="comment">// eoCommaReplacement keeps the best among offspring</span>
00116 <span class="comment">// eoPlusReplacement keeps the best among parents + offspring</span>
00117 <span class="comment">// eoCommaReplacement&lt;Indi&gt; replace;</span>
00118 <a class="code" href="classeo_plus_replacement.html">eoPlusReplacement&lt;Indi&gt;</a> replace;
00119
00120 <span class="comment">// OPERATORS</span>
00122 <span class="comment"></span> <span class="comment">// The variation operators</span>
00124 <span class="comment"></span><span class="comment">// CROSSOVER</span>
00125 <span class="comment">// 1-point crossover for bitstring</span>
00126 eo1PtBitXover&lt;Indi&gt; xover1;
00127 <span class="comment">// uniform crossover for bitstring</span>
00128 <a class="code" href="classeo_u_bit_xover.html">eoUBitXover&lt;Indi&gt;</a> xoverU;
00129 <span class="comment">// 2-pots xover</span>
00130 <a class="code" href="classeo_n_pts_bit_xover.html">eoNPtsBitXover&lt;Indi&gt;</a> xover2(2);
00131 <span class="comment">// Combine them with relative rates</span>
00132 <a class="code" href="classeo_prop_combined_quad_op.html">eoPropCombinedQuadOp&lt;Indi&gt;</a> xover(xover1, onePointRate);
00133 xover.add(xoverU, URate);
00134 xover.add(xover2, twoPointsRate, <span class="keyword">true</span>);
00135
00136 <span class="comment">// MUTATION</span>
00137 <span class="comment">// standard bit-flip mutation for bitstring</span>
00138 <a class="code" href="classeo_bit_mutation.html">eoBitMutation&lt;Indi&gt;</a> mutationBitFlip(P_MUT_PER_BIT);
00139 <span class="comment">// mutate exactly 1 bit per individual</span>
00140 <a class="code" href="classeo_det_bit_flip.html">eoDetBitFlip&lt;Indi&gt;</a> mutationOneBit;
00141 <span class="comment">// Combine them with relative rates</span>
00142 <a class="code" href="classeo_prop_combined_mon_op.html">eoPropCombinedMonOp&lt;Indi&gt;</a> mutation(mutationBitFlip, bitFlipRate);
00143 mutation.add(mutationOneBit, oneBitRate, <span class="keyword">true</span>);
00144
00145 <span class="comment">// The operators are encapsulated into an eoTRansform object</span>
00146 <a class="code" href="classeo_s_g_a_transform.html">eoSGATransform&lt;Indi&gt;</a> transform(xover, P_CROSS, mutation, P_MUT);
00147
00148 <span class="comment">// STOP</span>
00149 <span class="comment">// CHECKPOINT</span>
00151 <span class="comment"></span> <span class="comment">// termination conditions: use more than one</span>
00153 <span class="comment"></span> <span class="comment">// stop after MAX_GEN generations</span>
00154 <a class="code" href="classeo_gen_continue.html">eoGenContinue&lt;Indi&gt;</a> genCont(MAX_GEN);
00155 <span class="comment">// do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement</span>
00156 <a class="code" href="classeo_steady_fit_continue.html">eoSteadyFitContinue&lt;Indi&gt;</a> steadyCont(MIN_GEN, STEADY_GEN);
00157 <span class="comment">// stop when fitness reaches a target (here VEC_SIZE)</span>
00158 <a class="code" href="classeo_fit_continue.html">eoFitContinue&lt;Indi&gt;</a> fitCont(VEC_SIZE);
00159 <span class="comment">// do stop when one of the above says so</span>
00160 <a class="code" href="classeo_combined_continue.html">eoCombinedContinue&lt;Indi&gt;</a> continuator(genCont);
00161 continuator.add(steadyCont);
00162 continuator.add(fitCont);
00163
00164 <span class="comment">// GENERATION</span>
00166 <span class="comment"></span> <span class="comment">// the algorithm</span>
00168 <span class="comment"></span>
00169 <span class="comment">// Easy EA requires</span>
00170 <span class="comment">// selection, transformation, eval, replacement, and stopping criterion</span>
00171 <a class="code" href="classeo_easy_e_a.html">eoEasyEA&lt;Indi&gt;</a> gga(continuator, eval, select, transform, replace);
00172
00173 <span class="comment">// Apply algo to pop - that's it!</span>
00174 gga(pop);
00175
00176 <span class="comment">// OUTPUT</span>
00177 <span class="comment">// Print (sorted) intial population</span>
00178 pop.sort();
00179 cout &lt;&lt; <span class="stringliteral">"FINAL Population\n"</span> &lt;&lt; pop &lt;&lt; endl;
00180 <span class="comment">// GENERAL</span>
00181 }
00182
00183 <span class="comment">// A main that catches the exceptions</span>
00184
00185 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
00186 {
00187 <span class="keywordflow">try</span>
00188 {
00189 main_function(argc, argv);
00190 }
00191 <span class="keywordflow">catch</span>(exception&amp; e)
00192 {
00193 cout &lt;&lt; <span class="stringliteral">"Exception: "</span> &lt;&lt; e.what() &lt;&lt; <span class="charliteral">'\n'</span>;
00194 }
00195
00196 <span class="keywordflow">return</span> 1;
00197 }
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:39 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>