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

243 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: fitness.h 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_000020.html">app</a>&nbsp;/&nbsp;<a class="el" href="dir_000021.html">gpsymreg</a></div>
<h1>fitness.h</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">/*</span>
00002 <span class="comment"> This library is free software; you can redistribute it and/or modify</span>
00003 <span class="comment"> it under the terms of the GNU General Public License as published by</span>
00004 <span class="comment"> the Free Software Foundation; either version 2 of the License, or</span>
00005 <span class="comment"> (at your option) any later version.</span>
00006 <span class="comment"></span>
00007 <span class="comment"> This library is distributed in the hope that it will be useful,</span>
00008 <span class="comment"> but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
00009 <span class="comment"> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</span>
00010 <span class="comment"> Lesser General Public License for more details.</span>
00011 <span class="comment"></span>
00012 <span class="comment"> You should have received a copy of the GNU General Public License</span>
00013 <span class="comment"> along with this library; if not, write to the Free Software</span>
00014 <span class="comment"> Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
00015 <span class="comment"></span>
00016 <span class="comment"> Contact: todos@geneura.ugr.es, http://geneura.ugr.es</span>
00017 <span class="comment"> jeggermo@liacs.nl</span>
00018 <span class="comment">*/</span>
00019
00020 <span class="preprocessor">#ifndef _FITNESS_FUNCTION_H</span>
00021 <span class="preprocessor"></span><span class="preprocessor">#define _FITNESS_FUNCTION_H</span>
00022 <span class="preprocessor"></span>
00023 <span class="preprocessor">#include &lt;gp/eoParseTree.h&gt;</span>
00024 <span class="preprocessor">#include &lt;eo&gt;</span>
00025
00026 <span class="preprocessor">#include &lt;cmath&gt;</span>
00027 <span class="preprocessor">#include "parameters.h"</span>
00028 <span class="preprocessor">#include "node.h"</span>
00029
00030 <span class="keyword">using</span> <span class="keyword">namespace </span>gp_parse_tree;
00031 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
00032
00033
00034
00035 <span class="comment">// the first fitness is the normal goal fitness</span>
00036 <span class="comment">// the second fitness is the tree size (we prefer smaller trees)</span>
00037 <span class="comment">// lets use names to define the different fitnesses</span>
00038 <span class="preprocessor">#define NORMAL 0 // Stepwise Adaptation of Weights Fitness</span>
00039 <span class="preprocessor"></span><span class="preprocessor">#define SMALLESTSIZE 1 // The size of the tree, we want to minimize this one -- statistics will tell us the smallest tree size</span>
00040 <span class="preprocessor"></span>
00041
00042 <span class="comment">// Look: overloading the maximization without overhead (thing can be inlined)</span>
00043 <span class="keyword">class </span>MinimizingFitnessTraits : <span class="keyword">public</span> <a class="code" href="classeo_pareto_fitness_traits.html">eoParetoFitnessTraits</a>
00044 {
00045 <span class="keyword">public</span> :
00046 <span class="keyword">static</span> <span class="keywordtype">bool</span> maximizing(<span class="keywordtype">int</span> which) { <span class="keywordflow">return</span> <span class="keyword">false</span>;} <span class="comment">// we want to minimize both fitnesses</span>
00047 <span class="keyword">static</span> <span class="keywordtype">unsigned</span> nObjectives() { <span class="keywordflow">return</span> 2;} <span class="comment">// the number of fitnesses }</span>
00048 };
00049
00050 <span class="comment">// Lets define our MultiObjective FitnessType</span>
00051 <span class="keyword">typedef</span> <a class="code" href="classeo_pareto_fitness.html">eoParetoFitness&lt;MinimizingFitnessTraits&gt;</a> <a class="code" href="classeo_pareto_fitness.html">FitnessType</a>;
00052
00053
00054 <span class="comment">// John Koza's sextic polynomial (our example problem)</span>
00055
00056 <span class="keywordtype">double</span> sextic_polynomial(<span class="keywordtype">double</span> x)
00057 {
00058 <span class="keywordtype">double</span> result=0;
00059 result = pow(x,6) - (2*pow(x,4)) + pow(x,2);
00060 <span class="keywordflow">return</span> result;
00061 };
00062
00063 <span class="comment">// we use the following functions for the basic math functions</span>
00064
00065 <span class="keywordtype">double</span> _plus(<span class="keywordtype">double</span> arg1, <span class="keywordtype">double</span> arg2)
00066 {
00067 <span class="keywordflow">return</span> arg1 + arg2;
00068 }
00069
00070 <span class="keywordtype">double</span> _minus(<span class="keywordtype">double</span> arg1, <span class="keywordtype">double</span> arg2)
00071 {
00072 <span class="keywordflow">return</span> arg1 - arg2;
00073 }
00074
00075 <span class="keywordtype">double</span> _multiplies(<span class="keywordtype">double</span> arg1, <span class="keywordtype">double</span> arg2)
00076 {
00077 <span class="keywordflow">return</span> arg1 * arg2;
00078 }
00079
00080 <span class="comment">// the function for a protected divide looks a little bit different</span>
00081 <span class="keywordtype">double</span> _divides(<span class="keywordtype">double</span> arg1, <span class="keywordtype">double</span> arg2)
00082 {
00083 <span class="keywordflow">if</span> (arg2 ==0)
00084 <span class="keywordflow">return</span> 0;
00085 <span class="keywordflow">else</span>
00086 <span class="keywordflow">return</span> arg1 / arg2;
00087 }
00088
00089 <span class="keywordtype">double</span> _negate(<span class="keywordtype">double</span> arg1)
00090 {
00091 <span class="keywordflow">return</span> -arg1;
00092 }
00093
00094
00095
00096 <span class="comment">// now let's define our tree nodes</span>
00097
00098 <span class="keywordtype">void</span> init(vector&lt;Node&gt; &amp;initSequence)
00099 {
00100
00101 <span class="comment">// we have only one variable (X)</span>
00102 Operation varX( (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>) 0, string(<span class="stringliteral">"X"</span>) );
00103
00104
00105 <span class="comment">// the main binary operators</span>
00106 Operation OpPLUS ( _plus, string(<span class="stringliteral">"+"</span>));
00107 Operation OpMINUS( _minus,string(<span class="stringliteral">"-"</span>));
00108 Operation OpMULTIPLIES(_multiplies,string(<span class="stringliteral">"*"</span>));
00109 <span class="comment">// We can use a protected divide function.</span>
00110 Operation OpDIVIDE( _divides, string(<span class="stringliteral">"/"</span>) );
00111
00112
00113 <span class="comment">// Now the functions as binary functions</span>
00114 Operation PLUS( string(<span class="stringliteral">"plus"</span>), _plus);
00115 Operation MINUS( string(<span class="stringliteral">"minus"</span>), _minus);
00116 Operation MULTIPLIES( string(<span class="stringliteral">"multiply"</span>), _multiplies);
00117 Operation DIVIDE( string(<span class="stringliteral">"divide"</span>), _divides);
00118
00119
00120 <span class="comment">// and some unary functions</span>
00121 Operation NEGATE( _negate,string(<span class="stringliteral">"-"</span>));
00122 Operation SIN ( sin, string(<span class="stringliteral">"sin"</span>));
00123 Operation COS ( cos, string(<span class="stringliteral">"cos"</span>));
00124
00125 <span class="comment">// Now we are ready to add the possible nodes to our initSequence (which is used by the eoDepthInitializer)</span>
00126
00127 <span class="comment">// so lets start with our variable</span>
00128 initSequence.push_back(varX);
00129
00130 <span class="comment">// followed by the constants 2, 4, 6</span>
00131 <span class="keywordflow">for</span>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=2; i &lt;= 6; i+=2)
00132 {
00133 <span class="keywordtype">char</span> text[255];
00134 sprintf(text, <span class="stringliteral">"%i"</span>, i);
00135 Operation op(i*1.0, text);
00136 initSequence.push_back( op );
00137 <span class="comment">// and we add the variable again (so we have get lots of variables);</span>
00138 initSequence.push_back( varX );
00139 }
00140
00141 <span class="comment">// next we add the unary functions</span>
00142
00143 initSequence.push_back( NEGATE );
00144 initSequence.push_back( SIN );
00145 initSequence.push_back( COS );
00146
00147 <span class="comment">// and the binary functions</span>
00148 initSequence.push_back( PLUS);
00149 initSequence.push_back( MINUS );
00150 initSequence.push_back( MULTIPLIES );
00151 initSequence.push_back( DIVIDE );
00152
00153 <span class="comment">// and the binary operators</span>
00154 initSequence.push_back( OpPLUS);
00155 initSequence.push_back( OpMINUS );
00156
00157 initSequence.push_back( OpMULTIPLIES );
00158 initSequence.push_back( OpDIVIDE );
00159
00160
00161 };
00162
00163
00164
00165 <span class="keyword">class </span>RegFitness: <span class="keyword">public</span> <a class="code" href="classeo_eval_func.html">eoEvalFunc</a>&lt; eoParseTree&lt;FitnessType, Node&gt; &gt;
00166 {
00167 <span class="keyword">public</span>:
00168
00169 <span class="keyword">typedef</span> <a class="code" href="classeo_parse_tree.html">eoParseTree&lt;FitnessType, Node&gt;</a> <a class="code" href="classeo_parse_tree.html">EoType</a>;
00170
00171 <span class="keywordtype">void</span> <a class="code" href="classeo_u_f.html#a1">operator()</a>(<a class="code" href="classeo_parse_tree.html">EoType</a> &amp;_eo)
00172 {
00173
00174 vector&lt; double &gt; input(1); <span class="comment">// the input variable(s)</span>
00175 <span class="keywordtype">double</span> output(0.);
00176 <span class="keywordtype">double</span> target;
00177 FitnessType fitness;
00178
00179
00180 <span class="keywordtype">float</span> x=0;
00181 <span class="keywordtype">double</span> fit=0;
00182 <span class="keywordflow">for</span>(x=-1; x &lt;= 1; x+=0.1)
00183 {
00184 input[0] = x;
00185 target = sextic_polynomial(x);
00186 _eo.apply(output,input);
00187
00188 fit += pow(target - output, 2);
00189 }
00190
00191 fitness[NORMAL] = fit;
00192
00193 fitness[SMALLESTSIZE] = _eo.size() / (1.0*parameter.MaxSize);
00194 _eo.<a class="code" href="class_e_o.html#a2">fitness</a>(fitness);
00195
00196 <span class="keywordflow">if</span> (fitness[NORMAL] &lt; best[NORMAL])
00197 {
00198 best[NORMAL] = fitness[NORMAL];
00199 tree=<span class="stringliteral">""</span>;
00200 _eo.apply(tree);
00201 }
00202
00203 }
00204
00205
00206
00207 RegFitness(<a class="code" href="classeo_value_param.html">eoValueParam&lt;unsigned&gt;</a> &amp;_generationCounter, vector&lt; Node &gt; &amp;initSequence, Parameters &amp;_parameter) : <a class="code" href="classeo_eval_func.html">eoEvalFunc</a>&lt;<a class="code" href="classeo_parse_tree.html">EoType</a>&gt;(), generationCounter(_generationCounter), parameter(_parameter)
00208 {
00209 init(initSequence);
00210 best[NORMAL] = 1000;
00211 tree= <span class="stringliteral">"not found"</span>;
00212 };
00213
00214 ~RegFitness()
00215 {
00216 cerr &lt;&lt; <span class="stringliteral">"Best Fitness= "</span> &lt;&lt; best[NORMAL] &lt;&lt; endl;
00217 cerr &lt;&lt; tree &lt;&lt; endl;
00218 };
00219
00220 <span class="keyword">private</span>:
00221 <a class="code" href="classeo_value_param.html">eoValueParam&lt;unsigned&gt;</a> &amp;generationCounter; <span class="comment">// so we know the current generation</span>
00222 Parameters &amp;parameter; <span class="comment">// the parameters</span>
00223 FitnessType best; <span class="comment">// the best found fitness</span>
00224 string tree;
00225 };
00226
00227 <span class="preprocessor">#endif</span>
00228 <span class="preprocessor"></span>
</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>