Paradiseo-eo sources added

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@40 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
legrand 2006-12-12 14:49:08 +00:00
commit c3aec878e5
3609 changed files with 342772 additions and 0 deletions

View file

@ -0,0 +1,407 @@
<!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: exercise3.1.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_000005.html">Lesson3</a></div>
<h1>exercise3.1.cpp</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">//-----------------------------------------------------------------------------</span>
00002 <span class="comment">// SecondBitGA.cpp</span>
00003 <span class="comment">//-----------------------------------------------------------------------------</span>
00004 <span class="comment">//*</span>
00005 <span class="comment">// Same code than FirstBitEA as far as Evolutionary Computation is concerned</span>
00006 <span class="comment">// but now you learn to enter the parameters in a more flexible way</span>
00007 <span class="comment">// and to twidle the output to your preferences!</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;fstream&gt;</span>
00015 <span class="preprocessor">#include &lt;iostream&gt;</span> <span class="comment">// cout</span>
00016 <span class="preprocessor">#include &lt;stdexcept&gt;</span> <span class="comment">// runtime_error</span>
00017
00018 <span class="comment">// the general include for eo</span>
00019 <span class="preprocessor">#include &lt;eo&gt;</span>
00020
00021 <span class="comment">// EVAL</span>
00022 <span class="preprocessor">#include "binary_value.h"</span>
00023
00024 <span class="comment">// REPRESENTATION</span>
00025 <span class="comment">//-----------------------------------------------------------------------------</span>
00026 <span class="comment">// Include the corresponding file</span>
00027 <span class="preprocessor">#include &lt;ga.h&gt;</span> <span class="comment">// bitstring representation &amp; operators</span>
00028 <span class="comment">// define your genotype and fitness types</span>
00029 <span class="keyword">typedef</span> <a class="code" href="classeo_bit.html">eoBit&lt;eoMinimizingFitness&gt;</a> <a class="code" href="classeo_real.html">Indi</a>;
00030
00031 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
00032
00033 <span class="comment">// the main_function: nothing changed(!), except variable initialization</span>
00034 <span class="keywordtype">void</span> main_function(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
00035 {
00036 <span class="comment">// PARAMETRES</span>
00037 <span class="comment">//-----------------------------------------------------------------------------</span>
00038 <span class="comment">// instead of having all values of useful parameters as constants, read them:</span>
00039 <span class="comment">// either on the command line (--option=value or -o=value)</span>
00040 <span class="comment">// or in a parameter file (same syntax, order independent,</span>
00041 <span class="comment">// # = usual comment character</span>
00042 <span class="comment">// or in the environment (TODO)</span>
00043
00044 <span class="comment">// First define a parser from the command-line arguments</span>
00045 eoParser parser(argc, argv);
00046
00047 <span class="comment">// For each parameter, define Parameter, read it through the parser,</span>
00048 <span class="comment">// and assign the value to the variable</span>
00049
00050 <a class="code" href="classeo_value_param.html">eoValueParam&lt;uint32_t&gt;</a> seedParam(time(0), <span class="stringliteral">"seed"</span>, <span class="stringliteral">"Random number seed"</span>, <span class="charliteral">'S'</span>);
00051 parser.processParam( seedParam );
00052 <span class="keywordtype">unsigned</span> seed = seedParam.value();
00053
00054 <span class="comment">// description of genotype</span>
00055 <a class="code" href="classeo_value_param.html">eoValueParam&lt;unsigned int&gt;</a> vecSizeParam(100, <span class="stringliteral">"vecSize"</span>, <span class="stringliteral">"Genotype size"</span>,<span class="charliteral">'V'</span>);
00056 parser.processParam( vecSizeParam, <span class="stringliteral">"Representation"</span> );
00057 <span class="keywordtype">unsigned</span> vecSize = vecSizeParam.value();
00058
00059 <span class="comment">// parameters for evolution engine</span>
00060 <a class="code" href="classeo_value_param.html">eoValueParam&lt;unsigned int&gt;</a> popSizeParam(100, <span class="stringliteral">"popSize"</span>, <span class="stringliteral">"Population size"</span>,<span class="charliteral">'P'</span>);
00061 parser.processParam( popSizeParam, <span class="stringliteral">"Evolution engine"</span> );
00062 <span class="keywordtype">unsigned</span> popSize = popSizeParam.value();
00063
00064 <a class="code" href="classeo_value_param.html">eoValueParam&lt;unsigned int&gt;</a> tSizeParam(10, <span class="stringliteral">"tSize"</span>, <span class="stringliteral">"Tournament size"</span>,<span class="charliteral">'T'</span>);
00065 parser.processParam( tSizeParam, <span class="stringliteral">"Evolution Engine"</span> );
00066 <span class="keywordtype">unsigned</span> tSize = tSizeParam.value();
00067
00068 <span class="comment">// init and stop</span>
00069 <a class="code" href="classeo_value_param.html">eoValueParam&lt;string&gt;</a> loadNameParam(<span class="stringliteral">""</span>, <span class="stringliteral">"Load"</span>,<span class="stringliteral">"A save file to restart from"</span>,<span class="charliteral">'L'</span>);
00070 parser.processParam( loadNameParam, <span class="stringliteral">"Persistence"</span> );
00071 string loadName = loadNameParam.value();
00072
00073 <a class="code" href="classeo_value_param.html">eoValueParam&lt;unsigned int&gt;</a> maxGenParam(500, <span class="stringliteral">"maxGen"</span>, <span class="stringliteral">"Maximum number of generations"</span>,<span class="charliteral">'G'</span>);
00074 parser.processParam( maxGenParam, <span class="stringliteral">"Stopping criterion"</span> );
00075 <span class="keywordtype">unsigned</span> maxGen = maxGenParam.value();
00076
00077 <a class="code" href="classeo_value_param.html">eoValueParam&lt;unsigned int&gt;</a> minGenParam(500, <span class="stringliteral">"minGen"</span>, <span class="stringliteral">"Minimum number of generations"</span>,<span class="charliteral">'g'</span>);
00078 parser.processParam( minGenParam, <span class="stringliteral">"Stopping criterion"</span> );
00079 <span class="keywordtype">unsigned</span> minGen = minGenParam.value();
00080
00081 <a class="code" href="classeo_value_param.html">eoValueParam&lt;unsigned int&gt;</a> steadyGenParam(100, <span class="stringliteral">"steadyGen"</span>, <span class="stringliteral">"Number of generations with no improvement"</span>,<span class="charliteral">'s'</span>);
00082 parser.processParam( steadyGenParam, <span class="stringliteral">"Stopping criterion"</span> );
00083 <span class="keywordtype">unsigned</span> steadyGen = steadyGenParam.value();
00084
00085 <span class="comment">// operators probabilities at the algorithm level</span>
00086 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a> pCrossParam(0.6, <span class="stringliteral">"pCross"</span>, <span class="stringliteral">"Probability of Crossover"</span>, <span class="charliteral">'C'</span>);
00087 parser.processParam( pCrossParam, <span class="stringliteral">"Genetic Operators"</span> );
00088 <span class="keywordtype">double</span> pCross = pCrossParam.value();
00089
00090 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a> pMutParam(0.1, <span class="stringliteral">"pMut"</span>, <span class="stringliteral">"Probability of Mutation"</span>, <span class="charliteral">'M'</span>);
00091 parser.processParam( pMutParam, <span class="stringliteral">"Genetic Operators"</span> );
00092 <span class="keywordtype">double</span> pMut = pMutParam.value();
00093
00094 <span class="comment">// relative rates for crossovers</span>
00095 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a> onePointRateParam(1, <span class="stringliteral">"onePointRate"</span>, <span class="stringliteral">"Relative rate for one point crossover"</span>, <span class="charliteral">'1'</span>);
00096 parser.processParam( onePointRateParam, <span class="stringliteral">"Genetic Operators"</span> );
00097 <span class="keywordtype">double</span> onePointRate = onePointRateParam.value();
00098
00099 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a> twoPointsRateParam(1, <span class="stringliteral">"twoPointRate"</span>, <span class="stringliteral">"Relative rate for two point crossover"</span>, <span class="charliteral">'2'</span>);
00100 parser.processParam( twoPointsRateParam, <span class="stringliteral">"Genetic Operators"</span> );
00101 <span class="keywordtype">double</span> twoPointsRate = twoPointsRateParam.value();
00102
00103 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a> uRateParam(2, <span class="stringliteral">"uRate"</span>, <span class="stringliteral">"Relative rate for uniform crossover"</span>, <span class="charliteral">'U'</span>);
00104 parser.processParam( uRateParam, <span class="stringliteral">"Genetic Operators"</span> );
00105 <span class="keywordtype">double</span> URate = uRateParam.value();
00106
00107 <span class="comment">// relative rates and private parameters for mutations;</span>
00108 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a> pMutPerBitParam(0.01, <span class="stringliteral">"pMutPerBit"</span>, <span class="stringliteral">"Probability of flipping 1 bit in bit-flip mutation"</span>, <span class="charliteral">'b'</span>);
00109 parser.processParam( pMutPerBitParam, <span class="stringliteral">"Genetic Operators"</span> );
00110 <span class="keywordtype">double</span> pMutPerBit = pMutPerBitParam.value();
00111
00112 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a> bitFlipRateParam(0.01, <span class="stringliteral">"bitFlipRate"</span>, <span class="stringliteral">"Relative rate for bit-flip mutation"</span>, <span class="charliteral">'B'</span>);
00113 parser.processParam( bitFlipRateParam, <span class="stringliteral">"Genetic Operators"</span> );
00114 <span class="keywordtype">double</span> bitFlipRate = bitFlipRateParam.value();
00115
00116 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a> oneBitRateParam(0.01, <span class="stringliteral">"oneBitRate"</span>, <span class="stringliteral">"Relative rate for deterministic bit-flip mutation"</span>, <span class="charliteral">'D'</span>);
00117 parser.processParam( oneBitRateParam, <span class="stringliteral">"Genetic Operators"</span> );
00118 <span class="keywordtype">double</span> oneBitRate = oneBitRateParam.value();
00119
00120 <span class="comment">// the name of the "status" file where all actual parameter values will be saved</span>
00121 string str_status = parser.ProgramName() + <span class="stringliteral">".status"</span>; <span class="comment">// default value</span>
00122 <a class="code" href="classeo_value_param.html">eoValueParam&lt;string&gt;</a> statusParam(str_status.c_str(), <span class="stringliteral">"status"</span>,<span class="stringliteral">"Status file"</span>,<span class="charliteral">'S'</span>);
00123 parser.processParam( statusParam, <span class="stringliteral">"Persistence"</span> );
00124
00125 <span class="comment">// do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED</span>
00126 <span class="comment">// i.e. in case you need parameters somewhere else, postpone these</span>
00127 <span class="keywordflow">if</span> (parser.userNeedsHelp())
00128 {
00129 parser.printHelp(cout);
00130 exit(1);
00131 }
00132 <span class="keywordflow">if</span> (statusParam.value() != <span class="stringliteral">""</span>)
00133 {
00134 ofstream os(statusParam.value().c_str());
00135 os &lt;&lt; parser; <span class="comment">// and you can use that file as parameter file</span>
00136 }
00137
00138 <span class="comment">// EVAL</span>
00140 <span class="comment"></span> <span class="comment">// Fitness function</span>
00142 <span class="comment"></span> <span class="comment">// Evaluation: from a plain C++ fn to an EvalFunc Object ...</span>
00143 <a class="code" href="structeo_eval_func_ptr.html">eoEvalFuncPtr&lt;Indi, double, const vector&lt;bool&gt;</a>&amp; &gt; plainEval( binary_value );
00144 <span class="comment">// ... to an object that counts the nb of actual evaluations</span>
00145 <a class="code" href="classeo_eval_func_counter.html">eoEvalFuncCounter&lt;Indi&gt;</a> eval(plainEval);
00146
00147 <span class="comment">// INIT</span>
00149 <span class="comment"></span> <span class="comment">// Initilisation of population</span>
00151 <span class="comment"></span> <span class="comment">// Either load or initialize</span>
00152 <span class="comment">// create an empty pop</span>
00153 <a class="code" href="classeo_pop.html">eoPop&lt;Indi&gt;</a> pop;
00154 <span class="comment">// create a state for reading</span>
00155 <a class="code" href="classeo_state.html">eoState</a> inState; <span class="comment">// a state for loading - WITHOUT the parser</span>
00156 <span class="comment">// register the rng and the pop in the state, so they can be loaded,</span>
00157 <span class="comment">// and the present run will be the exact conitnuation of the saved run</span>
00158 <span class="comment">// eventually with different parameters</span>
00159 inState.<a class="code" href="classeo_state.html#a2">registerObject</a>(rng);
00160 inState.<a class="code" href="classeo_state.html#a2">registerObject</a>(pop);
00161
00162 <span class="keywordflow">if</span> (loadName != <span class="stringliteral">""</span>)
00163 {
00164 inState.<a class="code" href="classeo_state.html#a5">load</a>(loadName); <span class="comment">// load the pop and the rng</span>
00165 <span class="comment">// the fitness is read in the file:</span>
00166 <span class="comment">// do only evaluate the pop if the fitness has changed</span>
00167 }
00168 <span class="keywordflow">else</span>
00169 {
00170 rng.<a class="code" href="classeo_rng.html#a2">reseed</a>(seed);
00171 <span class="comment">// a Indi random initializer</span>
00172 <span class="comment">// based on boolean_generator class (see utils/rnd_generator.h)</span>
00173 <a class="code" href="classeo_uniform_generator.html">eoUniformGenerator&lt;bool&gt;</a> uGen;
00174 <a class="code" href="classeo_init_fixed_length.html">eoInitFixedLength&lt;Indi&gt;</a> random(vecSize, uGen);
00175
00176 <span class="comment">// Init pop from the randomizer: need to use the append function</span>
00177 pop.<a class="code" href="classeo_pop.html#a2">append</a>(popSize, random);
00178 <span class="comment">// and evaluate pop (STL syntax)</span>
00179 apply&lt;Indi&gt;(eval, pop);
00180 } <span class="comment">// end of initializatio of the population</span>
00181
00182 <span class="comment">// OUTPUT</span>
00183 <span class="comment">// sort pop for pretty printout</span>
00184 <span class="comment">// pop.sort();</span>
00185 <span class="comment">// Print (sorted) intial population (raw printout)</span>
00186 cout &lt;&lt; <span class="stringliteral">"Initial Population"</span> &lt;&lt; endl &lt;&lt; pop ;
00187 cout &lt;&lt; <span class="stringliteral">"and best is "</span> &lt;&lt; pop.<a class="code" href="classeo_pop.html#a10">best_element</a>() &lt;&lt; <span class="stringliteral">"\n\n"</span>;
00188 cout &lt;&lt; <span class="stringliteral">"and worse is "</span> &lt;&lt; pop.worse_element() &lt;&lt; <span class="stringliteral">"\n\n"</span>;
00189 <span class="comment">// ENGINE</span>
00191 <span class="comment"></span> <span class="comment">// selection and replacement</span>
00193 <span class="comment"></span><span class="comment">// SELECT</span>
00194 <span class="comment">// The robust tournament selection</span>
00195 <a class="code" href="classeo_det_tournament_select.html">eoDetTournamentSelect&lt;Indi&gt;</a> selectOne(tSize); <span class="comment">// tSize in [2,POPSIZE]</span>
00196 <span class="comment">// is now encapsulated in a eoSelectPerc (entage)</span>
00197 <a class="code" href="classeo_select_perc.html">eoSelectPerc&lt;Indi&gt;</a> select(selectOne);<span class="comment">// by default rate==1</span>
00198
00199 <span class="comment">// REPLACE</span>
00200 <span class="comment">// And we now have the full slection/replacement - though with</span>
00201 <span class="comment">// generational replacement at the moment :-)</span>
00202 <a class="code" href="classeo_generational_replacement.html">eoGenerationalReplacement&lt;Indi&gt;</a> replace;
00203 <span class="comment">// want to add (weak) elitism? easy!</span>
00204 <span class="comment">// rename the eoGenerationalReplacement replace_main,</span>
00205 <span class="comment">// then encapsulate it in the elitist replacement</span>
00206 <span class="comment">// eoWeakElitistReplacement&lt;Indi&gt; replace(replace_main);</span>
00207
00208 <span class="comment">// OPERATORS</span>
00210 <span class="comment"></span> <span class="comment">// The variation operators</span>
00212 <span class="comment"></span><span class="comment">// CROSSOVER</span>
00213 <span class="comment">// 1-point crossover for bitstring</span>
00214 eo1PtBitXover&lt;Indi&gt; xover1;
00215 <span class="comment">// uniform crossover for bitstring</span>
00216 <a class="code" href="classeo_u_bit_xover.html">eoUBitXover&lt;Indi&gt;</a> xoverU;
00217 <span class="comment">// 2-pots xover</span>
00218 <a class="code" href="classeo_n_pts_bit_xover.html">eoNPtsBitXover&lt;Indi&gt;</a> xover2(2);
00219 <span class="comment">// Combine them with relative rates</span>
00220 <a class="code" href="classeo_prop_combined_quad_op.html">eoPropCombinedQuadOp&lt;Indi&gt;</a> xover(xover1, onePointRate);
00221 xover.add(xoverU, URate);
00222 xover.add(xover2, twoPointsRate, <span class="keyword">true</span>);
00223
00224 <span class="comment">// MUTATION</span>
00225 <span class="comment">// standard bit-flip mutation for bitstring</span>
00226 <a class="code" href="classeo_bit_mutation.html">eoBitMutation&lt;Indi&gt;</a> mutationBitFlip(pMutPerBit);
00227 <span class="comment">// mutate exactly 1 bit per individual</span>
00228 <a class="code" href="classeo_det_bit_flip.html">eoDetBitFlip&lt;Indi&gt;</a> mutationOneBit;
00229 <span class="comment">// Combine them with relative rates</span>
00230 <a class="code" href="classeo_prop_combined_mon_op.html">eoPropCombinedMonOp&lt;Indi&gt;</a> mutation(mutationBitFlip, bitFlipRate);
00231 mutation.add(mutationOneBit, oneBitRate, <span class="keyword">true</span>);
00232
00233 <span class="comment">// The operators are encapsulated into an eoTRansform object</span>
00234 <a class="code" href="classeo_s_g_a_transform.html">eoSGATransform&lt;Indi&gt;</a> transform(xover, pCross, mutation, pMut);
00235
00236 <span class="comment">// STOP</span>
00238 <span class="comment"></span> <span class="comment">// termination condition see FirstBitEA.cpp</span>
00240 <span class="comment"></span> <a class="code" href="classeo_gen_continue.html">eoGenContinue&lt;Indi&gt;</a> genCont(maxGen);
00241 <a class="code" href="classeo_steady_fit_continue.html">eoSteadyFitContinue&lt;Indi&gt;</a> steadyCont(minGen, steadyGen);
00242 <span class="comment">// eoFitContinue&lt;Indi&gt; fitCont(vecSize); // remove if minimizing :-)</span>
00243 <a class="code" href="classeo_combined_continue.html">eoCombinedContinue&lt;Indi&gt;</a> continuator(genCont);
00244 continuator.add(steadyCont);
00245 <span class="comment">// continuator.add(fitCont);</span>
00246 <span class="comment">// Ctrl C signal handling: don't know if that works in MSC ...</span>
00247 <span class="preprocessor">#ifndef _MSC_VER</span>
00248 <span class="preprocessor"></span> <a class="code" href="classeo_ctrl_c_continue.html">eoCtrlCContinue&lt;Indi&gt;</a> ctrlC;
00249 continuator.add(ctrlC);
00250 <span class="preprocessor">#endif</span>
00251 <span class="preprocessor"></span>
00252 <span class="comment">// CHECKPOINT</span>
00253 <span class="comment">// but now you want to make many different things every generation</span>
00254 <span class="comment">// (e.g. statistics, plots, ...).</span>
00255 <span class="comment">// the class eoCheckPoint is dedicated to just that:</span>
00256
00257 <span class="comment">// Declare a checkpoint (from a continuator: an eoCheckPoint</span>
00258 <span class="comment">// IS AN eoContinue and will be called in the loop of all algorithms)</span>
00259 <a class="code" href="classeo_check_point.html">eoCheckPoint&lt;Indi&gt;</a> checkpoint(continuator);
00260
00261 <span class="comment">// Create a counter parameter</span>
00262 <a class="code" href="classeo_value_param.html">eoValueParam&lt;unsigned&gt;</a> generationCounter(0, <span class="stringliteral">"Gen."</span>);
00263
00264 <span class="comment">// Create an incrementor (sub-class of eoUpdater). Note that the</span>
00265 <span class="comment">// parameter's value is passed by reference,</span>
00266 <span class="comment">// so every time the incrementer is updated (every generation),</span>
00267 <span class="comment">// the data in generationCounter will change.</span>
00268 <a class="code" href="classeo_incrementor.html">eoIncrementor&lt;unsigned&gt;</a> increment(generationCounter.value());
00269
00270 <span class="comment">// Add it to the checkpoint,</span>
00271 <span class="comment">// so the counter is updated (here, incremented) every generation</span>
00272 checkpoint.add(increment);
00273
00274 <span class="comment">// now some statistics on the population:</span>
00275 <span class="comment">// Best fitness in population</span>
00276 <a class="code" href="classeo_best_fitness_stat.html">eoBestFitnessStat&lt;Indi&gt;</a> bestStat;
00277 <a class="code" href="classeo_average_stat.html">eoAverageStat&lt;Indi&gt;</a> averageStat;
00278 <span class="comment">// Second moment stats: average and stdev</span>
00279 <a class="code" href="classeo_second_moment_stats.html">eoSecondMomentStats&lt;Indi&gt;</a> SecondStat;
00280 <span class="comment">// the Fitness Distance Correlation</span>
00281 <span class="comment">// need first an object to compute the distances</span>
00282 <a class="code" href="classeo_quad_distance.html">eoQuadDistance&lt;Indi&gt;</a> dist; <span class="comment">// Hamming distance</span>
00283 <a class="code" href="classeo_f_d_c_stat.html">eoFDCStat&lt;Indi&gt;</a> fdcStat(dist);
00284
00285 <span class="comment">// Add them to the checkpoint to get them called at the appropriate time</span>
00286 checkpoint.add(bestStat);
00287 checkpoint.add(averageStat);
00288 checkpoint.add(SecondStat);
00289 checkpoint.add(fdcStat);
00290
00291 <span class="comment">// The Stdout monitor will print parameters to the screen ...</span>
00292 <a class="code" href="classeo_stdout_monitor.html">eoStdoutMonitor</a> monitor(<span class="keyword">false</span>);
00293
00294 <span class="comment">// when called by the checkpoint (i.e. at every generation)</span>
00295 checkpoint.add(monitor);
00296
00297 <span class="comment">// the monitor will output a series of parameters: add them</span>
00298 monitor.add(generationCounter);
00299 monitor.add(eval); <span class="comment">// because now eval is an eoEvalFuncCounter!</span>
00300 monitor.add(bestStat);
00301 monitor.add(SecondStat);
00302 monitor.add(fdcStat);
00303
00304 <span class="comment">// test de eoPopStat and/or eoSortedPopStat.</span>
00305 <span class="comment">// Dumps the whole pop every 10 gen.</span>
00306 <span class="comment">// eoSortedPopStat&lt;Indi&gt; popStat(10, "Dump of whole population");</span>
00307 <span class="comment">// eoPopStat&lt;Indi&gt; popStat(10, "Dump of whole population");</span>
00308 <span class="comment">// checkpoint.add(popStat);</span>
00309 <span class="comment">// monitor.add(popStat);</span>
00310
00311 <span class="comment">// A file monitor: will print parameters to ... a File, yes, you got it!</span>
00312 <a class="code" href="classeo_file_monitor.html">eoFileMonitor</a> fileMonitor(<span class="stringliteral">"stats.xg"</span>, <span class="stringliteral">" "</span>);
00313
00314 <span class="comment">// the checkpoint mechanism can handle monitors</span>
00315 checkpoint.add(fileMonitor);
00316
00317 <span class="comment">// the fileMonitor can monitor parameters, too, but you must tell it!</span>
00318 fileMonitor.add(generationCounter);
00319 fileMonitor.add(bestStat);
00320 fileMonitor.add(SecondStat);
00321
00322 <span class="preprocessor">#ifndef _MSC_VER</span>
00323 <span class="preprocessor"></span> <span class="comment">// and an eoGnuplot1DMonitor will 1-print to a file, and 2- plot on screen</span>
00324 <a class="code" href="classeo_gnuplot1_d_monitor.html">eoGnuplot1DMonitor</a> gnuMonitor(<span class="stringliteral">"best_average.xg"</span>,minimizing_fitness&lt;Indi&gt;());
00325 <span class="comment">// the checkpoint mechanism can handle multiple monitors</span>
00326 checkpoint.add(gnuMonitor);
00327 <span class="comment">// the gnuMonitor can monitor parameters, too, but you must tell it!</span>
00328 gnuMonitor.add(eval);
00329 gnuMonitor.add(bestStat);
00330 gnuMonitor.add(averageStat);
00331
00332 <span class="comment">// send a scaling command to gnuplot</span>
00333 gnuMonitor.gnuplotCommand(<span class="stringliteral">"set yrange [0:500]"</span>);
00334
00335 <span class="comment">// a specific plot monitor for FDC</span>
00336 <span class="comment">// first into a file (it adds everything ti itself</span>
00337 <a class="code" href="classeo_f_d_c_file_snapshot.html">eoFDCFileSnapshot&lt;Indi&gt;</a> fdcFileSnapshot(fdcStat);
00338 <span class="comment">// then to a Gnuplot monitor</span>
00339 <a class="code" href="classeo_gnuplot1_d_snapshot.html">eoGnuplot1DSnapshot</a> fdcGnuplot(fdcFileSnapshot);
00340 <span class="comment">// and of coruse add them to the checkPoint</span>
00341 checkpoint.add(fdcFileSnapshot);
00342 checkpoint.add(fdcGnuplot);
00343
00344 <span class="comment">// want to see how the fitness is spread?</span>
00345 <a class="code" href="classeo_scalar_fitness_stat.html">eoScalarFitnessStat&lt;Indi&gt;</a> fitStat;
00346 checkpoint.add(fitStat);
00347 <span class="comment">// a gnuplot-based monitor for snapshots: needs a dir name</span>
00348 <span class="comment">// where to store the files</span>
00349 <a class="code" href="classeo_gnuplot1_d_snapshot.html">eoGnuplot1DSnapshot</a> fitSnapshot(<span class="stringliteral">"Fitnesses"</span>);
00350 <span class="comment">// add any stat that is a vector&lt;double&gt; to it</span>
00351 fitSnapshot.add(fitStat);
00352 <span class="comment">// and of course add it to the checkpoint</span>
00353 checkpoint.add(fitSnapshot);
00354 <span class="preprocessor">#endif</span>
00355 <span class="preprocessor"></span> <span class="comment">// Last type of item the eoCheckpoint can handle: state savers:</span>
00356 <a class="code" href="classeo_state.html">eoState</a> outState;
00357 <span class="comment">// Register the algorithm into the state (so it has something to save!!)</span>
00358 outState.<a class="code" href="classeo_state.html#a2">registerObject</a>(rng);
00359 outState.<a class="code" href="classeo_state.html#a2">registerObject</a>(pop);
00360
00361 <span class="comment">// and feed the state to state savers</span>
00362 <span class="comment">// save state every 100th generation</span>
00363 <a class="code" href="classeo_counted_state_saver.html">eoCountedStateSaver</a> stateSaver1(100, outState, <span class="stringliteral">"generation"</span>);
00364 <span class="comment">// save state every 1 seconds</span>
00365 <a class="code" href="classeo_timed_state_saver.html">eoTimedStateSaver</a> stateSaver2(1, outState, <span class="stringliteral">"time"</span>);
00366
00367 <span class="comment">// Don't forget to add the two savers to the checkpoint</span>
00368 checkpoint.add(stateSaver1);
00369 checkpoint.add(stateSaver2);
00370 <span class="comment">// and that's it for the (control and) output</span>
00371
00372 <span class="comment">// GENERATION</span>
00374 <span class="comment"></span> <span class="comment">// the algorithm</span>
00376 <span class="comment"></span>
00377 <span class="comment">// Easy EA requires</span>
00378 <span class="comment">// selection, transformation, eval, replacement, and stopping criterion</span>
00379 <a class="code" href="classeo_easy_e_a.html">eoEasyEA&lt;Indi&gt;</a> gga(checkpoint, eval, select, transform, replace);
00380
00381 <span class="comment">// Apply algo to pop - that's it!</span>
00382 gga(pop);
00383
00384 <span class="comment">// OUTPUT</span>
00385 <span class="comment">// Print (sorted) intial population</span>
00386 pop.sort();
00387 cout &lt;&lt; <span class="stringliteral">"FINAL Population\n"</span> &lt;&lt; pop &lt;&lt; endl;
00388 <span class="comment">// GENERAL</span>
00389 }
00390
00391 <span class="comment">// A main that catches the exceptions</span>
00392 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
00393 {
00394 <span class="keywordflow">try</span>
00395 {
00396 main_function(argc, argv);
00397 }
00398 <span class="keywordflow">catch</span>(exception&amp; e)
00399 {
00400 cout &lt;&lt; <span class="stringliteral">"Exception: "</span> &lt;&lt; e.what() &lt;&lt; <span class="charliteral">'\n'</span>;
00401 }
00402
00403 <span class="keywordflow">return</span> 1;
00404 }
</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>