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,198 @@
<!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-eoCheckpointing.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_000002.html">test</a></div>
<h1>t-eoCheckpointing.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 &lt;stdexcept&gt;</span> <span class="comment">// runtime_error</span>
00009
00010 <span class="comment">//-----------------------------------------------------------------------------</span>
00011 <span class="comment">// tt.cpp:</span>
00012 <span class="comment">//</span>
00013 <span class="comment">//-----------------------------------------------------------------------------</span>
00014
00015
00016 <span class="comment">// general</span>
00017 <span class="preprocessor">#include &lt;utils/eoRNG.h&gt;</span> <span class="comment">// Random number generators</span>
00018 <span class="preprocessor">#include &lt;ga.h&gt;</span>
00019 <span class="preprocessor">#include &lt;utils/eoParser.h&gt;</span>
00020 <span class="preprocessor">#include &lt;utils/eoState.h&gt;</span>
00021 <span class="preprocessor">#include &lt;eoGenContinue.h&gt;</span>
00022 <span class="comment">//-----------------------------------------------------------------------------</span>
00023
00024 <span class="comment">// include package checkpointing</span>
00025 <span class="preprocessor">#include &lt;utils/checkpointing&gt;</span>
00026
<a name="l00027"></a><a class="code" href="struct_dummy.html">00027</a> <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>&lt;double&gt;
00028 {
00029 <span class="keyword">typedef</span> <span class="keywordtype">double</span> Type;
00030 };
00031
00032
00033 <span class="keyword">struct </span>eoDummyPop : <span class="keyword">public</span> <a class="code" href="classeo_pop.html">eoPop</a>&lt;Dummy&gt;
00034 {
00035 <span class="keyword">public</span> :
00036 eoDummyPop(<span class="keywordtype">int</span> s = 2) { resize(s); }
00037 };
00038
00039 <span class="comment">//-----------------------------------------------------------------------------</span>
00040
00041 <span class="keywordtype">int</span> the_main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
00042 { <span class="comment">// ok, we have a command line parser and a state</span>
00043
00044 <span class="keyword">typedef</span> <a class="code" href="classeo_bit.html">eoBit&lt;float&gt;</a> Chrom;
00045
00046 <a class="code" href="classeo_parser.html">eoParser</a> parser(argc, argv);
00047
00048 <span class="comment">// Define Parameters</span>
00049 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a> rate(0.01, <span class="stringliteral">"mutationRatePerBit"</span>, <span class="stringliteral">"Initial value for mutation rate per bit"</span>);
00050 <a class="code" href="classeo_value_param.html">eoValueParam&lt;double&gt;</a> factor(0.99, <span class="stringliteral">"mutationFactor"</span>, <span class="stringliteral">"Decrease factor for mutation rate"</span>);
00051 <a class="code" href="classeo_value_param.html">eoValueParam&lt;uint32_t&gt;</a> seed(time(0), <span class="stringliteral">"seed"</span>, <span class="stringliteral">"Random number seed"</span>);
00052 <a class="code" href="classeo_value_param.html">eoValueParam&lt;std::string&gt;</a> load_name(<span class="stringliteral">""</span>, <span class="stringliteral">"Load"</span>,<span class="stringliteral">"Load"</span>,<span class="charliteral">'L'</span>);
00053 <a class="code" href="classeo_value_param.html">eoValueParam&lt;std::string&gt;</a> save_name(<span class="stringliteral">""</span>, <span class="stringliteral">"Save"</span>,<span class="stringliteral">"Save"</span>,<span class="charliteral">'S'</span>);
00054
00055 <span class="comment">// Register them</span>
00056 parser.processParam(rate, <span class="stringliteral">"Genetic Operators"</span>);
00057 parser.processParam(factor, <span class="stringliteral">"Genetic Operators"</span>);
00058 parser.processParam(load_name, <span class="stringliteral">"Persistence"</span>);
00059 parser.processParam(save_name, <span class="stringliteral">"Persistence"</span>);
00060 parser.processParam(seed, <span class="stringliteral">"Rng seeding"</span>);
00061
00062 <a class="code" href="classeo_state.html">eoState</a> state;
00063 state.<a class="code" href="classeo_state.html#a2">registerObject</a>(parser);
00064
00065 <span class="keywordflow">if</span> (load_name.value() != <span class="stringliteral">""</span>)
00066 { <span class="comment">// load the parser. This is only neccessary when the user wants to</span>
00067 <span class="comment">// be able to change the parameters in the state file by hand.</span>
00068 state.<a class="code" href="classeo_state.html#a5">load</a>(load_name.value()); <span class="comment">// load the parser</span>
00069 }
00070
00071 <span class="comment">// Create the algorithm here</span>
00072 <span class="keyword">typedef</span> <a class="code" href="struct_dummy.html">Dummy</a> <a class="code" href="classeo_parse_tree.html">EoType</a>;
00073
00074 eoDummyPop pop;
00075
00076 <a class="code" href="classeo_gen_continue.html">eoGenContinue&lt;EoType&gt;</a> genTerm(5); <span class="comment">// run for 5 generations</span>
00077
00078 <a class="code" href="classeo_check_point.html">eoCheckPoint&lt;EoType&gt;</a> checkpoint(genTerm);
00079 <span class="comment">// The algorithm will now quit after five generations</span>
00080
00081 <span class="comment">// Create a counter parameter</span>
00082 <a class="code" href="classeo_value_param.html">eoValueParam&lt;unsigned&gt;</a> generationCounter(0, <span class="stringliteral">"Generation"</span>);
00083
00084 <span class="comment">// Create an incrementor (wich is an eoUpdater). Note that the</span>
00085 <span class="comment">// Parameter's value is passed by reference, so every time the incrementer increments,</span>
00086 <span class="comment">// the data in generationCounter will change.</span>
00087 <a class="code" href="classeo_incrementor.html">eoIncrementor&lt;unsigned&gt;</a> increment(generationCounter.value());
00088
00089 <span class="comment">// Add it to the checkpoint, this will result in the counter being incremented every generation</span>
00090 checkpoint.add(increment);
00091
00092 <span class="comment">// The file monitor will print parameters to a comma seperated file</span>
00093 <a class="code" href="classeo_file_monitor.html">eoFileMonitor</a> monitor(<span class="stringliteral">"monitor.csv"</span>);
00094
00095 <span class="comment">// the checkpoint mechanism can handle multiple monitors</span>
00096 checkpoint.add(monitor);
00097
00098 <span class="comment">// the monitor can monitor parameters such as the generationCounter</span>
00099 monitor.add(generationCounter);
00100
00101 <span class="comment">// Second moment stats: average and stdev</span>
00102 <a class="code" href="classeo_second_moment_stats.html">eoSecondMomentStats&lt;EoType&gt;</a> stats;
00103
00104 <span class="comment">// Add it to the checkpoint to get it called at the appropriate time</span>
00105 checkpoint.add(stats);
00106
00107 <span class="comment">// Add it to the monitor to get it written to the file</span>
00108 monitor.add(stats);
00109
00110 <span class="comment">// save state every third generation</span>
00111 <a class="code" href="classeo_counted_state_saver.html">eoCountedStateSaver</a> stateSaver1(3, state, <span class="stringliteral">"generation"</span>);
00112 <span class="comment">// save state every 2 seconds</span>
00113 <a class="code" href="classeo_timed_state_saver.html">eoTimedStateSaver</a> stateSaver2(2, state, <span class="stringliteral">"time"</span>);
00114
00115 <span class="comment">// And add the two savers to the checkpoint</span>
00116 checkpoint.add(stateSaver1);
00117 checkpoint.add(stateSaver2);
00118
00119 <span class="comment">// Register the algorithm</span>
00120 state.<a class="code" href="classeo_state.html#a2">registerObject</a>(rng);
00121 state.<a class="code" href="classeo_state.html#a2">registerObject</a>(pop);
00122
00123 <span class="keywordflow">if</span> (parser.userNeedsHelp())
00124 {
00125 parser.printHelp(std::cout);
00126 <span class="keywordflow">return</span> 0;
00127 }
00128
00129 <span class="comment">// Either load or initialize</span>
00130 <span class="keywordflow">if</span> (load_name.value() != <span class="stringliteral">""</span>)
00131 {
00132 state.<a class="code" href="classeo_state.html#a5">load</a>(load_name.value()); <span class="comment">// load the rest</span>
00133 }
00134 <span class="keywordflow">else</span>
00135 {
00136 <span class="comment">// else</span>
00137
00138 <span class="comment">// initialize rng and population</span>
00139
00140 rng.<a class="code" href="classeo_rng.html#a2">reseed</a>(seed.value());
00141
00142 pop.resize(2);
00143
00144 pop[0].fitness(1);
00145 pop[1].fitness(2);
00146 }
00147
00148 <span class="keywordflow">while</span>(checkpoint(pop))
00149 {
00150 pop[0].fitness(pop[0].fitness() + 1);
00151
00152 time_t now = time(0);
00153
00154 <span class="keywordflow">while</span> (time(0) == now) {} <span class="comment">// wait a second to test timed saver</span>
00155
00156 std::cout &lt;&lt; <span class="stringliteral">"gen "</span> &lt;&lt; generationCounter.value() &lt;&lt; std::endl;
00157 }
00158
00159 <span class="comment">// run the algorithm</span>
00160
00161 <span class="comment">// Save when needed</span>
00162 <span class="keywordflow">if</span> (save_name.value() != <span class="stringliteral">""</span>)
00163 {
00164 std::string file_name = save_name.value();
00165 save_name.value() = <span class="stringliteral">""</span>; <span class="comment">// so that it does not appear in the parser section of the state file</span>
00166 state.<a class="code" href="classeo_state.html#a7">save</a>(file_name);
00167 }
00168
00169 <span class="keywordflow">return</span> 1;
00170 }
00171
00172 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
00173 {
00174 <span class="keywordflow">try</span>
00175 {
00176 the_main(argc, argv);
00177 }
00178 <span class="keywordflow">catch</span>(std::exception&amp; e)
00179 {
00180 std::cout &lt;&lt; <span class="stringliteral">"Exception: "</span> &lt;&lt; e.what() &lt;&lt; std::endl;
00181 }
00182
00183 }
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:43 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>