git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@40 331e1502-861f-0410-8da2-ba01fb791d7f
254 lines
16 KiB
HTML
254 lines
16 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: gprop.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 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 Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Class List</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="namespacemembers.html">Namespace Members</a> | <a class="qindex" href="functions.html">Class Members</a> | <a class="qindex" href="pages.html">Related Pages</a> | <span class="search"><u>S</u>earch for <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> / <a class="el" href="dir_000023.html">gprop</a></div>
|
|
<h1>gprop.h</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-</span>
|
|
00002
|
|
00003 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00004 <span class="comment">// gprop.h</span>
|
|
00005 <span class="comment">// (c) GeNeura Team 1998</span>
|
|
00006 <span class="comment">/*</span>
|
|
00007 <span class="comment"> This library is free software; you can redistribute it and/or</span>
|
|
00008 <span class="comment"> modify it under the terms of the GNU Lesser General Public</span>
|
|
00009 <span class="comment"> License as published by the Free Software Foundation; either</span>
|
|
00010 <span class="comment"> version 2 of the License, or (at your option) any later version.</span>
|
|
00011 <span class="comment"></span>
|
|
00012 <span class="comment"> This library is distributed in the hope that it will be useful,</span>
|
|
00013 <span class="comment"> but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
|
|
00014 <span class="comment"> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</span>
|
|
00015 <span class="comment"> Lesser General Public License for more details.</span>
|
|
00016 <span class="comment"></span>
|
|
00017 <span class="comment"> You should have received a copy of the GNU Lesser General Public</span>
|
|
00018 <span class="comment"> License along with this library; if not, write to the Free Software</span>
|
|
00019 <span class="comment"> Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
|
|
00020 <span class="comment"></span>
|
|
00021 <span class="comment"> Contact: todos@geneura.ugr.es, http://geneura.ugr.es</span>
|
|
00022 <span class="comment"> */</span>
|
|
00023 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00024
|
|
00025
|
|
00026 <span class="preprocessor">#ifndef gprop_h</span>
|
|
00027 <span class="preprocessor"></span><span class="preprocessor">#define gprop_h</span>
|
|
00028 <span class="preprocessor"></span>
|
|
00029 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00030
|
|
00031 <span class="preprocessor">#include <iostream></span> <span class="comment">// istream ostream</span>
|
|
00032 <span class="preprocessor">#include <iomanip></span> <span class="comment">// setprecision</span>
|
|
00033 <span class="preprocessor">#include <string></span> <span class="comment">// string</span>
|
|
00034 <span class="preprocessor">#include <EO.h></span> <span class="comment">// EO</span>
|
|
00035 <span class="preprocessor">#include <eoOp.h></span> <span class="comment">// eoMonOp eoQuadraticOp</span>
|
|
00036 <span class="preprocessor">#include <eoInit.h></span> <span class="comment">// eoInit</span>
|
|
00037 <span class="preprocessor">#include <utils/rnd_generators.h></span> <span class="comment">// normal_generator</span>
|
|
00038 <span class="preprocessor">#include "mlp.h"</span> <span class="comment">// mlp::net mlp::set</span>
|
|
00039 <span class="preprocessor">#include "qp.h"</span> <span class="comment">// qp::set</span>
|
|
00040 <span class="preprocessor">#include "mse.h"</span> <span class="comment">// mse::error</span>
|
|
00041
|
|
00042 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00043 <span class="comment">// phenotype</span>
|
|
00044 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00045
|
|
00046 <span class="keyword">struct </span>phenotype
|
|
00047 {
|
|
00048 <span class="keywordtype">int</span> trn_ok, val_ok, tst_ok;
|
|
00049 <span class="keywordtype">double</span> mse_error;
|
|
00050
|
|
00051 <span class="keyword">static</span> <span class="keywordtype">int</span> trn_max, val_max, tst_max;
|
|
00052
|
|
00053 <span class="keyword">friend</span> <span class="keywordtype">bool</span> operator<(<span class="keyword">const</span> phenotype& a, <span class="keyword">const</span> phenotype& b)
|
|
00054 {
|
|
00055 <span class="keywordflow">return</span> a.val_ok < b.val_ok || !(b.val_ok < a.val_ok) && b.mse_error < a.mse_error;
|
|
00056 }
|
|
00057
|
|
00058 <span class="keyword">friend</span> <span class="keywordtype">bool</span> operator==(<span class="keyword">const</span> phenotype& a, <span class="keyword">const</span> phenotype& b)
|
|
00059 {
|
|
00060 <span class="keywordflow">return</span> a.val_ok == b.val_ok && b.mse_error == a.mse_error;
|
|
00061 }
|
|
00062
|
|
00063 <span class="keyword">friend</span> <span class="keywordtype">bool</span> operator>=(<span class="keyword">const</span> phenotype& a, <span class="keyword">const</span> phenotype& b)
|
|
00064 {
|
|
00065 <span class="keywordflow">return</span> !(a < b);
|
|
00066 }
|
|
00067
|
|
00068 <span class="keyword">friend</span> <span class="keywordtype">bool</span> operator>(<span class="keyword">const</span> phenotype& a, <span class="keyword">const</span> phenotype& b)
|
|
00069 {
|
|
00070 <span class="keywordflow">return</span> (!(a == b)) && (!(a < b));
|
|
00071 }
|
|
00072
|
|
00073
|
|
00074
|
|
00075 <span class="keyword">friend</span> ostream& operator<<(ostream& os, <span class="keyword">const</span> phenotype& p)
|
|
00076 {
|
|
00077 <span class="keywordflow">return</span> os << p.trn_ok << <span class="stringliteral">"/"</span> << p.trn_max << <span class="stringliteral">" "</span>
|
|
00078 << p.val_ok << <span class="stringliteral">"/"</span> << p.val_max << <span class="stringliteral">" "</span>
|
|
00079 << p.tst_ok << <span class="stringliteral">"/"</span> << p.tst_max << <span class="stringliteral">" "</span>
|
|
00080 << p.mse_error;
|
|
00081 }
|
|
00082
|
|
00083 <span class="keyword">friend</span> istream& operator>>(istream& is, phenotype& p)
|
|
00084 {
|
|
00085 <span class="keywordflow">return</span> is; <span class="comment">// complete me</span>
|
|
00086 }
|
|
00087 };
|
|
00088
|
|
00089
|
|
00090 <span class="keywordtype">int</span> phenotype::trn_max = 0, phenotype::val_max = 0, phenotype::tst_max = 0;
|
|
00091
|
|
00092 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00093 <span class="comment">// genotype</span>
|
|
00094 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00095 <span class="preprocessor">#ifndef GPROP_GENOTYPE</span>
|
|
00096 <span class="preprocessor"></span><span class="preprocessor">#define GPROP_GENOTYPE mlp::net</span>
|
|
00097 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
|
|
00098 <span class="preprocessor"></span>
|
|
00099 <span class="keyword">typedef</span> GPROP_GENOTYPE genotype;
|
|
00100
|
|
00101 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00102 <span class="comment">// Chrom</span>
|
|
00103 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00104
|
|
00105 <span class="keyword">extern</span> <span class="keywordtype">unsigned</span> in, out, hidden;
|
|
00106
|
|
00107 <span class="keyword">class </span>Chrom: <span class="keyword">public</span> <a class="code" href="class_e_o.html">EO</a><phenotype>, <span class="keyword">public</span> genotype
|
|
00108 {
|
|
00109 <span class="keyword">public</span>:
|
|
00110 Chrom(): genotype(in, out, vector<unsigned>(hidden < 1 ? 0 : 1, hidden)) {}
|
|
00111
|
|
00112 string <a class="code" href="class_e_o.html#z10_0">className</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="stringliteral">"Chrom"</span>; }
|
|
00113
|
|
00114 <span class="keywordtype">void</span> <a class="code" href="class_e_o.html#z10_2">printOn</a> (ostream& os)<span class="keyword"> const </span>
|
|
00115 <span class="keyword"> </span>{
|
|
00116 os << setprecision(3) << static_cast<genotype>(*this) << <span class="stringliteral">" \t"</span>
|
|
00117 << <a class="code" href="class_e_o.html#a2">fitness</a>();
|
|
00118 <span class="comment">// os << fitness();</span>
|
|
00119 }
|
|
00120
|
|
00121 <span class="keywordtype">void</span> <a class="code" href="class_e_o.html#z10_1">readFrom</a> (istream& is)
|
|
00122 {
|
|
00123 invalidate(); <span class="comment">// complete me</span>
|
|
00124 }
|
|
00125 };
|
|
00126
|
|
00127 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00128 <span class="comment">// eoChromInit</span>
|
|
00129 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00130
|
|
00131 <span class="keyword">class </span>eoInitChrom: <span class="keyword">public</span> <a class="code" href="classeo_init.html">eoInit</a><Chrom>
|
|
00132 {
|
|
00133 <span class="keyword">public</span>:
|
|
00134 <span class="keywordtype">void</span> <a class="code" href="classeo_u_f.html#a1">operator()</a>(Chrom& chrom)
|
|
00135 {
|
|
00136 chrom.reset();
|
|
00137 chrom.invalidate();
|
|
00138 }
|
|
00139 };
|
|
00140
|
|
00141 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00142 <span class="comment">// global variables</span>
|
|
00143 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00144
|
|
00145 mlp::set *trn_set = 0, *val_set = 0, *tst_set = 0;
|
|
00146
|
|
00147 <span class="keywordtype">void</span> gprop_use_datasets(mlp::set *trn, mlp::set *val, mlp::set *tst) {
|
|
00148 trn_set = trn;
|
|
00149 val_set = val;
|
|
00150 tst_set = tst;
|
|
00151 }
|
|
00152
|
|
00153 <span class="keywordtype">void</span> ensure_datasets_initialized() {
|
|
00154 <span class="keywordflow">if</span> (!trn_set) {
|
|
00155 cerr << <span class="stringliteral">"trn_set is not initialized. Must call gprop_use_datasets before training\n"</span>;
|
|
00156 cerr.flush();
|
|
00157 abort();
|
|
00158 }
|
|
00159 }
|
|
00160
|
|
00161 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00162 <span class="comment">// eoChromMutation</span>
|
|
00163 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00164
|
|
00165 <span class="keyword">class </span>eoChromMutation: <span class="keyword">public</span> <a class="code" href="classeo_mon_op.html">eoMonOp</a><Chrom>
|
|
00166 {
|
|
00167 <span class="keyword">public</span>:
|
|
00168 <span class="keywordtype">bool</span> <a class="code" href="classeo_u_f.html#a1">operator()</a>(Chrom& chrom)
|
|
00169 {
|
|
00170 mse::net tmp(chrom);
|
|
00171 tmp.train(*trn_set, 10, 0, 0.001);
|
|
00172 <span class="keywordflow">return</span> <span class="keyword">true</span>;
|
|
00173 }
|
|
00174 };
|
|
00175
|
|
00176 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00177 <span class="comment">// eoChromXover</span>
|
|
00178 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00179
|
|
00180 <span class="keyword">class </span>eoChromXover: <span class="keyword">public</span> <a class="code" href="classeo_quad_op.html">eoQuadOp</a><Chrom>
|
|
00181 {
|
|
00182 <span class="keyword">public</span>:
|
|
00183 <span class="keywordtype">bool</span> <a class="code" href="classeo_b_f.html#a1">operator()</a>(Chrom& chrom1, Chrom& chrom2)
|
|
00184 {
|
|
00185 chrom1.normalize();
|
|
00186 chrom2.desaturate();
|
|
00187
|
|
00188 mse::net tmp1(chrom1), tmp2(chrom2);
|
|
00189 ensure_datasets_initialized();
|
|
00190 tmp1.train(*trn_set, 100, 0, 0.001);
|
|
00191 tmp2.train(*trn_set, 100, 0, 0.001);
|
|
00192
|
|
00193 <span class="keywordflow">return</span> <span class="keyword">true</span>;
|
|
00194 }
|
|
00195 };
|
|
00196
|
|
00197 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00198 <span class="comment">// eoChromEvaluator</span>
|
|
00199 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00200
|
|
00201 <span class="keywordtype">int</span> correct(<span class="keyword">const</span> mlp::net& net, <span class="keyword">const</span> mlp::set& set)
|
|
00202 {
|
|
00203 <span class="keywordtype">int</span> sum = 0;
|
|
00204
|
|
00205 <span class="keywordflow">for</span> (mlp::set::const_iterator s = set.begin(); s != set.end(); ++s)
|
|
00206 {
|
|
00207 <span class="keywordtype">unsigned</span> partial = 0;
|
|
00208
|
|
00209 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < s->output.size(); ++i)
|
|
00210 <span class="keywordflow">if</span> (s->output[i] < 0.5 && net(s->input)[i] < 0.5 ||
|
|
00211 s->output[i] > 0.5 && net(s->input)[i] > 0.5)
|
|
00212 ++partial;
|
|
00213
|
|
00214 <span class="keywordflow">if</span> (partial == s->output.size())
|
|
00215 ++sum;
|
|
00216 }
|
|
00217
|
|
00218 <span class="keywordflow">return</span> sum;
|
|
00219 }
|
|
00220
|
|
00221 phenotype eoChromEvaluator(<span class="keyword">const</span> Chrom& chrom)
|
|
00222 {
|
|
00223 phenotype p;
|
|
00224 ensure_datasets_initialized();
|
|
00225 p.trn_ok = correct(chrom, *trn_set);
|
|
00226 p.val_ok = correct(chrom, *val_set);
|
|
00227 p.tst_ok = correct(chrom, *tst_set);
|
|
00228 p.mse_error = mse::error(chrom, *val_set);
|
|
00229
|
|
00230 <span class="keywordflow">return</span> p;
|
|
00231 };
|
|
00232
|
|
00233 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00234
|
|
00235 <span class="preprocessor">#endif // gprop_h</span>
|
|
00236 <span class="preprocessor"></span>
|
|
00237 <span class="comment">// Local Variables: </span>
|
|
00238 <span class="comment">// mode:C++ </span>
|
|
00239 <span class="comment">// End:</span>
|
|
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:40 2006 for EO by
|
|
<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>
|