paradiseo/trunk/paradiseo-eo/doc/html/exercise1_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

159 lines
11 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: exercise1.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_000019.html">Lesson1</a></div>
<h1>exercise1.3.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 &lt;config.h&gt;</span>
00003 <span class="preprocessor">#endif</span>
00004 <span class="preprocessor"></span>
00005 <span class="comment">//-----------------------------------------------------------------------------</span>
00006 <span class="comment">// FirstBitGA.cpp</span>
00007 <span class="comment">//-----------------------------------------------------------------------------</span>
00008 <span class="comment">//*</span>
00009 <span class="comment">// An instance of a VERY simple Bitstring Genetic Algorithm</span>
00010 <span class="comment">//</span>
00011 <span class="comment">//-----------------------------------------------------------------------------</span>
00012 <span class="comment">// standard includes</span>
00013 <span class="preprocessor">#include &lt;iostream&gt;</span>
00014 <span class="preprocessor">#include &lt;stdexcept&gt;</span>
00015
00016 <span class="comment">// the general include for eo</span>
00017 <span class="preprocessor">#include &lt;eo&gt;</span>
00018
00019 <span class="comment">//-----------------------------------------------------------------------------</span>
00020 <span class="comment">// Include the corresponding file</span>
00021 <span class="preprocessor">#include &lt;ga.h&gt;</span> <span class="comment">// bitstring representation &amp; operators</span>
00022 <span class="comment">// define your individuals</span>
00023 <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>
00024
00025 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
00026
00027 <span class="comment">//-----------------------------------------------------------------------------</span>
00032 <span class="comment"></span><span class="keywordtype">double</span> binary_value(<span class="keyword">const</span> Indi &amp; _indi)
00033 {
00034 <span class="keywordtype">double</span> sum = 0;
00035 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i &lt; _indi.size(); i++)
00036 sum += _indi[i];
00037 <span class="keywordflow">return</span> sum;
00038 }
00039
00040 <span class="comment">//-----------------------------------------------------------------------------</span>
00041
00042 <span class="keywordtype">void</span> main_function(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
00043 {
00044 <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>
00045 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> VEC_SIZE = 8; <span class="comment">// Number of bits in genotypes</span>
00046 <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>
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">float</span> CROSS_RATE = 0.8; <span class="comment">// Crossover rate</span>
00049 <span class="keyword">const</span> <span class="keywordtype">double</span> P_MUT_PER_BIT = 0.01; <span class="comment">// probability of bit-flip mutation</span>
00050 <span class="keyword">const</span> <span class="keywordtype">float</span> MUT_RATE = 1.0; <span class="comment">// mutation rate</span>
00051
00053 <span class="comment">// Random seed</span>
00055 <span class="comment"></span> <span class="comment">//reproducible random seed: if you don't change SEED above,</span>
00056 <span class="comment">// you'll aways get the same result, NOT a random run</span>
00057 rng.<a class="code" href="classeo_rng.html#a2">reseed</a>(SEED);
00058
00060 <span class="comment">// Fitness function</span>
00062 <span class="comment"></span> <span class="comment">// Evaluation: from a plain C++ fn to an EvalFunc Object</span>
00063 <a class="code" href="structeo_eval_func_ptr.html">eoEvalFuncPtr&lt;Indi&gt;</a> eval( binary_value );
00064
00066 <span class="comment">// Initilisation of population</span>
00068 <span class="comment"></span>
00069 <span class="comment">// declare the population</span>
00070 <a class="code" href="classeo_pop.html">eoPop&lt;Indi&gt;</a> pop;
00071 <span class="comment">// fill it!</span>
00072 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> igeno=0; igeno&lt;POP_SIZE; igeno++)
00073 {
00074 Indi v; <span class="comment">// void individual, to be filled</span>
00075 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> ivar=0; ivar&lt;VEC_SIZE; ivar++)
00076 {
00077 <span class="keywordtype">bool</span> r = rng.<a class="code" href="classeo_rng.html#a6">flip</a>(); <span class="comment">// new value, random in {0,1}</span>
00078 v.push_back(r); <span class="comment">// append that random value to v</span>
00079 }
00080 eval(v); <span class="comment">// evaluate it</span>
00081 pop.push_back(v); <span class="comment">// and put it in the population</span>
00082 }
00083
00084 <span class="comment">// sort pop before printing it!</span>
00085 pop.<a class="code" href="classeo_pop.html#a5">sort</a>();
00086 <span class="comment">// Print (sorted) intial population (raw printout)</span>
00087 cout &lt;&lt; <span class="stringliteral">"Initial Population"</span> &lt;&lt; endl;
00088 cout &lt;&lt; pop;
00089
00091 <span class="comment">// selection and replacement</span>
00093 <span class="comment"></span>
00094 <span class="comment">// solution solution solution: uncomment one of the following,</span>
00095 <span class="comment">// comment out the eoDetTournament lines</span>
00096
00097 <span class="comment">// The well-known roulette</span>
00098 <span class="comment">// eoProportionalSelect&lt;Indi&gt; select;</span>
00099
00100 <span class="comment">// could also use stochastic binary tournament selection</span>
00101 <span class="comment">//</span>
00102 <span class="comment">// const double RATE = 0.75;</span>
00103 <span class="comment">// eoStochTournamentSelect&lt;Indi&gt; select(RATE); // RATE in ]0.5,1]</span>
00104 <span class="comment">// The robust tournament selection</span>
00105 <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>
00106 <a class="code" href="classeo_det_tournament_select.html">eoDetTournamentSelect&lt;Indi&gt;</a> select(T_SIZE); <span class="comment">// T_SIZE in [2,POP_SIZE]</span>
00107
00108 <span class="comment">// and of course the random selection</span>
00109 <span class="comment">// eoRandomSelect&lt;Indi&gt; select;</span>
00110
00111 <span class="comment">// The simple GA evolution engine uses generational replacement</span>
00112 <span class="comment">// so no replacement procedure is needed</span>
00113
00115 <span class="comment">// termination condition</span>
00117 <span class="comment"></span> <span class="comment">// stop after MAX_GEN generations</span>
00118 <a class="code" href="classeo_gen_continue.html">eoGenContinue&lt;Indi&gt;</a> continuator(MAX_GEN);
00119
00120
00122 <span class="comment">// The variation operators</span>
00124 <span class="comment"></span> <span class="comment">// standard bit-flip mutation for bitstring</span>
00125 <a class="code" href="classeo_bit_mutation.html">eoBitMutation&lt;Indi&gt;</a> mutation(P_MUT_PER_BIT);
00126 <span class="comment">// 1-point mutation for bitstring</span>
00127 eo1PtBitXover&lt;Indi&gt; xover;
00128
00130 <span class="comment">// the algorithm</span>
00132 <span class="comment"></span> <span class="comment">// standard Generational GA requires as parameters</span>
00133 <span class="comment">// selection, evaluation, crossover and mutation, stopping criterion</span>
00134
00135
00136 <a class="code" href="classeo_s_g_a.html">eoSGA&lt;Indi&gt;</a> gga(select, xover, CROSS_RATE, mutation, MUT_RATE,
00137 eval, continuator);
00138
00139 <span class="comment">// Apply algo to pop - that's it!</span>
00140 gga(pop);
00141
00142 <span class="comment">// Print (sorted) intial population</span>
00143 pop.<a class="code" href="classeo_pop.html#a5">sort</a>();
00144 cout &lt;&lt; <span class="stringliteral">"FINAL Population\n"</span> &lt;&lt; pop &lt;&lt; endl;
00145 }
00146
00147 <span class="comment">// A main that catches the exceptions</span>
00148
00149 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
00150 {
00151
00152 <span class="keywordflow">try</span>
00153 {
00154 main_function(argc, argv);
00155 }
00156 <span class="keywordflow">catch</span>(exception&amp; e)
00157 {
00158 cout &lt;&lt; <span class="stringliteral">"Exception: "</span> &lt;&lt; e.what() &lt;&lt; <span class="charliteral">'\n'</span>;
00159 }
00160
00161 <span class="keywordflow">return</span> 1;
00162 }
</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>