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:
parent
bc1f453978
commit
c3aec878e5
1486 changed files with 342772 additions and 0 deletions
102
trunk/paradiseo-eo/doc/html/_bit_e_a_8cpp-source.html
Normal file
102
trunk/paradiseo-eo/doc/html/_bit_e_a_8cpp-source.html
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
<!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: BitEA.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 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_000003.html">tutorial</a> / <a class="el" href="dir_000006.html">Lesson4</a></div>
|
||||
<h1>BitEA.cpp</h1><div class="fragment"><pre class="fragment">00001 <span class="preprocessor">#include <iostream></span>
|
||||
00002
|
||||
00003 <span class="preprocessor">#include <ga/make_ga.h></span>
|
||||
00004 <span class="preprocessor">#include <apply.h></span>
|
||||
00005
|
||||
00006 <span class="comment">// EVAL</span>
|
||||
00007 <span class="preprocessor">#include "binary_value.h"</span>
|
||||
00008
|
||||
00009 <span class="comment">// GENERAL</span>
|
||||
00010 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
|
||||
00011
|
||||
00012 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[])
|
||||
00013 {
|
||||
00014
|
||||
00015 <span class="keywordflow">try</span>
|
||||
00016 {
|
||||
00017 <span class="comment">// REPRESENTATION</span>
|
||||
00018 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00019 <span class="comment">// define your genotype and fitness types</span>
|
||||
00020 <span class="keyword">typedef</span> <a class="code" href="classeo_bit.html">eoBit<double></a> <a class="code" href="struct_dummy.html">EOT</a>;
|
||||
00021
|
||||
00022 <span class="comment">// PARAMETRES</span>
|
||||
00023 eoParser parser(argc, argv); <span class="comment">// for user-parameter reading</span>
|
||||
00024
|
||||
00025 <span class="comment">// GENERAL</span>
|
||||
00026 <a class="code" href="classeo_state.html">eoState</a> state; <span class="comment">// keeps all things allocated</span>
|
||||
00027
|
||||
00030
|
||||
00031 <span class="comment">// EVAL</span>
|
||||
00032 <span class="comment">// The evaluation fn - encapsulated into an eval counter for output </span>
|
||||
00033 <a class="code" href="structeo_eval_func_ptr.html">eoEvalFuncPtr<EOT, double></a> mainEval( binary_value<EOT> );
|
||||
00034 <a class="code" href="classeo_eval_func_counter.html">eoEvalFuncCounter<EOT></a> eval(mainEval);
|
||||
00035
|
||||
00036 <span class="comment">// REPRESENTATION</span>
|
||||
00037 <span class="comment">// the genotype - through a genotype initializer</span>
|
||||
00038 <a class="code" href="classeo_init.html">eoInit<EOT></a>& init = make_genotype(parser, state, EOT());
|
||||
00039
|
||||
00040 <span class="comment">// if you want to do sharing, you'll need a distance.</span>
|
||||
00041 <span class="comment">// here Hamming distance </span>
|
||||
00042 <a class="code" href="classeo_hamming_distance.html">eoHammingDistance<EOT></a> dist;
|
||||
00043
|
||||
00044 <span class="comment">// OPERATORS</span>
|
||||
00045 <span class="comment">// Build the variation operator (any seq/prop construct)</span>
|
||||
00046 <a class="code" href="classeo_gen_op.html">eoGenOp<EOT></a>& op = make_op(parser, state, init);
|
||||
00047
|
||||
00048 <span class="comment">// GENERAL</span>
|
||||
00051 <span class="comment"></span>
|
||||
00052 <span class="comment">// initialize the population - and evaluate</span>
|
||||
00053 <span class="comment">// yes, this is representation indepedent once you have an eoInit</span>
|
||||
00054 <a class="code" href="classeo_pop.html">eoPop<EOT></a>& pop = make_pop(parser, state, init);
|
||||
00055
|
||||
00056 <span class="comment">// STOP</span>
|
||||
00057 <span class="comment">// stopping criteria</span>
|
||||
00058 <a class="code" href="classeo_continue.html">eoContinue<EOT></a> & term = make_continue(parser, state, eval);
|
||||
00059 <span class="comment">// output</span>
|
||||
00060 <a class="code" href="classeo_check_point.html">eoCheckPoint<EOT></a> & checkpoint = make_checkpoint(parser, state, eval, term);
|
||||
00061 <span class="comment">// GENERATION</span>
|
||||
00062 <span class="comment">// algorithm (need the operator!)</span>
|
||||
00063 <a class="code" href="classeo_algo.html">eoAlgo<EOT></a>& ga = make_algo_scalar(parser, state, eval, checkpoint, op, &dist);
|
||||
00064
|
||||
00067 <span class="comment">// PARAMETRES</span>
|
||||
00068 <span class="comment">// to be called AFTER all parameters have been read!!!</span>
|
||||
00069 make_help(parser);
|
||||
00070
|
||||
00073 <span class="comment">// EVAL</span>
|
||||
00074 <span class="comment">// evaluate intial population AFTER help and status in case it takes time</span>
|
||||
00075 apply<EOT>(eval, pop);
|
||||
00076 <span class="comment">// STOP</span>
|
||||
00077 <span class="comment">// print it out (sort witout modifying) </span>
|
||||
00078 cout << <span class="stringliteral">"Initial Population\n"</span>;
|
||||
00079 pop.<a class="code" href="classeo_pop.html#a17">sortedPrintOn</a>(cout);
|
||||
00080 cout << endl;
|
||||
00081
|
||||
00082 <span class="comment">// GENERATION</span>
|
||||
00083 run_ea(ga, pop); <span class="comment">// run the ga</span>
|
||||
00084 <span class="comment">// STOP</span>
|
||||
00085 <span class="comment">// print it out (sort witout modifying) </span>
|
||||
00086 cout << <span class="stringliteral">"Final Population\n"</span>;
|
||||
00087 pop.<a class="code" href="classeo_pop.html#a17">sortedPrintOn</a>(cout);
|
||||
00088 cout << endl;
|
||||
00089 <span class="comment">// GENERAL</span>
|
||||
00090 }
|
||||
00091 <span class="keywordflow">catch</span>(exception& e)
|
||||
00092 {
|
||||
00093 cout << e.what() << endl;
|
||||
00094 }
|
||||
00095 }
|
||||
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:34 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>
|
||||
95
trunk/paradiseo-eo/doc/html/_bounds_check_8cpp-source.html
Normal file
95
trunk/paradiseo-eo/doc/html/_bounds_check_8cpp-source.html
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
<!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: BoundsCheck.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 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_000007.html">contrib</a> / <a class="el" href="dir_000008.html">mathsym</a> / <a class="el" href="dir_000009.html">eval</a></div>
|
||||
<h1>BoundsCheck.cpp</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">/* </span>
|
||||
00002 <span class="comment"> * Copyright (C) 2005 Maarten Keijzer</span>
|
||||
00003 <span class="comment"> *</span>
|
||||
00004 <span class="comment"> * This program is free software; you can redistribute it and/or modify</span>
|
||||
00005 <span class="comment"> * it under the terms of version 2 of the GNU General Public License as </span>
|
||||
00006 <span class="comment"> * published by the Free Software Foundation. </span>
|
||||
00007 <span class="comment"> *</span>
|
||||
00008 <span class="comment"> * This program is distributed in the hope that it will be useful,</span>
|
||||
00009 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
|
||||
00010 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
|
||||
00011 <span class="comment"> * GNU General Public License for more details.</span>
|
||||
00012 <span class="comment"> *</span>
|
||||
00013 <span class="comment"> * You should have received a copy of the GNU General Public License</span>
|
||||
00014 <span class="comment"> * along with this program; if not, write to the Free Software</span>
|
||||
00015 <span class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
|
||||
00016 <span class="comment"> */</span>
|
||||
00017
|
||||
00018 <span class="preprocessor">#include <vector></span>
|
||||
00019
|
||||
00020 <span class="preprocessor">#include "BoundsCheck.h"</span>
|
||||
00021 <span class="preprocessor">#include <Sym.h></span>
|
||||
00022 <span class="preprocessor">#include <FunDef.h></span>
|
||||
00023 <span class="preprocessor">#include <sstream></span>
|
||||
00024
|
||||
00025 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
|
||||
00026
|
||||
00027 <span class="keyword">class </span>IntervalBoundsCheckImpl {
|
||||
00028 <span class="keyword">public</span> :
|
||||
00029 vector<Interval> bounds;
|
||||
00030 };
|
||||
00031
|
||||
00032 IntervalBoundsCheck::IntervalBoundsCheck(<span class="keyword">const</span> vector<double>& mins, <span class="keyword">const</span> vector<double>& maxes) {
|
||||
00033 pimpl = <span class="keyword">new</span> IntervalBoundsCheckImpl;
|
||||
00034 vector<Interval>& b = pimpl->bounds;
|
||||
00035
|
||||
00036 b.resize( mins.size());
|
||||
00037
|
||||
00038 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < b.size(); ++i) {
|
||||
00039 b[i] = Interval(mins[i], maxes[i]);
|
||||
00040 }
|
||||
00041
|
||||
00042 }
|
||||
00043
|
||||
00044 IntervalBoundsCheck::~IntervalBoundsCheck() { <span class="keyword">delete</span> pimpl; }
|
||||
00045 IntervalBoundsCheck::IntervalBoundsCheck(<span class="keyword">const</span> IntervalBoundsCheck& that) { pimpl = <span class="keyword">new</span> IntervalBoundsCheckImpl(*that.pimpl); }
|
||||
00046 IntervalBoundsCheck& IntervalBoundsCheck::operator=(<span class="keyword">const</span> IntervalBoundsCheck& that) { *pimpl = *that.pimpl; <span class="keywordflow">return</span> *<span class="keyword">this</span>; }
|
||||
00047
|
||||
00048 <span class="keywordtype">bool</span> IntervalBoundsCheck::in_bounds(<span class="keyword">const</span> Sym& sym)<span class="keyword"> const </span>{
|
||||
00049 Interval bounds;
|
||||
00050
|
||||
00051 <span class="keywordflow">try</span> {
|
||||
00052 bounds = eval(sym, pimpl->bounds);
|
||||
00053 <span class="keywordflow">if</span> (!valid(bounds)) <span class="keywordflow">return</span> <span class="keyword">false</span>;
|
||||
00054 } <span class="keywordflow">catch</span> (interval_error) {
|
||||
00055 <span class="keywordflow">return</span> <span class="keyword">false</span>;
|
||||
00056 }
|
||||
00057 <span class="keywordflow">return</span> <span class="keyword">true</span>;
|
||||
00058 }
|
||||
00059
|
||||
00060 std::string IntervalBoundsCheck::get_bounds(<span class="keyword">const</span> Sym& sym)<span class="keyword"> const </span>{
|
||||
00061
|
||||
00062 <span class="keywordflow">try</span> {
|
||||
00063 Interval bounds = eval(sym, pimpl->bounds);
|
||||
00064 <span class="keywordflow">if</span> (!valid(bounds)) <span class="keywordflow">return</span> <span class="stringliteral">"err"</span>;
|
||||
00065 ostringstream os;
|
||||
00066 os << bounds;
|
||||
00067 <span class="keywordflow">return</span> os.str();
|
||||
00068 } <span class="keywordflow">catch</span> (interval_error) {
|
||||
00069 <span class="keywordflow">return</span> <span class="stringliteral">"err"</span>;
|
||||
00070 }
|
||||
00071 }
|
||||
00072
|
||||
00073
|
||||
00074 std::pair<double, double> IntervalBoundsCheck::calc_bounds(<span class="keyword">const</span> Sym& sym)<span class="keyword"> const </span>{
|
||||
00075
|
||||
00076 Interval bounds = eval(sym, pimpl->bounds);
|
||||
00077 <span class="keywordflow">return</span> make_pair(bounds.lower(), bounds.upper());
|
||||
00078 }
|
||||
00079
|
||||
00080
|
||||
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:34 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>
|
||||
73
trunk/paradiseo-eo/doc/html/_bounds_check_8h-source.html
Normal file
73
trunk/paradiseo-eo/doc/html/_bounds_check_8h-source.html
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
<!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: BoundsCheck.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_000007.html">contrib</a> / <a class="el" href="dir_000008.html">mathsym</a> / <a class="el" href="dir_000009.html">eval</a></div>
|
||||
<h1>BoundsCheck.h</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">/* </span>
|
||||
00002 <span class="comment"> * Copyright (C) 2005 Maarten Keijzer</span>
|
||||
00003 <span class="comment"> *</span>
|
||||
00004 <span class="comment"> * This program is free software; you can redistribute it and/or modify</span>
|
||||
00005 <span class="comment"> * it under the terms of version 2 of the GNU General Public License as </span>
|
||||
00006 <span class="comment"> * published by the Free Software Foundation. </span>
|
||||
00007 <span class="comment"> *</span>
|
||||
00008 <span class="comment"> * This program is distributed in the hope that it will be useful,</span>
|
||||
00009 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
|
||||
00010 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
|
||||
00011 <span class="comment"> * GNU General Public License for more details.</span>
|
||||
00012 <span class="comment"> *</span>
|
||||
00013 <span class="comment"> * You should have received a copy of the GNU General Public License</span>
|
||||
00014 <span class="comment"> * along with this program; if not, write to the Free Software</span>
|
||||
00015 <span class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
|
||||
00016 <span class="comment"> */</span>
|
||||
00017
|
||||
00018 <span class="preprocessor">#ifndef BOUNDS_CHECK_H_</span>
|
||||
00019 <span class="preprocessor"></span><span class="preprocessor">#define BOUNDS_CHECK_H_</span>
|
||||
00020 <span class="preprocessor"></span>
|
||||
00021 <span class="preprocessor">#include <string></span>
|
||||
00022
|
||||
00023 <span class="keyword">class </span>IntervalBoundsCheckImpl;
|
||||
00024 <span class="keyword">class </span>Sym;
|
||||
00025
|
||||
00026 <span class="keyword">class </span>BoundsCheck {
|
||||
00027 <span class="keyword">public</span> :
|
||||
00028 <span class="keyword">virtual</span> ~BoundsCheck() {};
|
||||
00029 <span class="keyword">virtual</span> <span class="keywordtype">bool</span> in_bounds(<span class="keyword">const</span> Sym&) <span class="keyword">const </span>= 0;
|
||||
00030 <span class="keyword">virtual</span> std::string get_bounds(<span class="keyword">const</span> Sym&) <span class="keyword">const </span>= 0;
|
||||
00031 };
|
||||
00032
|
||||
00033 <span class="comment">// checks if a formula keeps within bounds using interval arithmetic</span>
|
||||
00034 <span class="keyword">class </span>IntervalBoundsCheck : <span class="keyword">public</span> BoundsCheck {
|
||||
00035
|
||||
00036 IntervalBoundsCheckImpl* pimpl;
|
||||
00037
|
||||
00038 <span class="keyword">public</span>:
|
||||
00039
|
||||
00040 IntervalBoundsCheck(<span class="keyword">const</span> std::vector<double>& minima, <span class="keyword">const</span> std::vector<double>& maxima);
|
||||
00041 ~IntervalBoundsCheck();
|
||||
00042 IntervalBoundsCheck(<span class="keyword">const</span> IntervalBoundsCheck&);
|
||||
00043 IntervalBoundsCheck& operator=(<span class="keyword">const</span> IntervalBoundsCheck&);
|
||||
00044
|
||||
00045 <span class="keywordtype">bool</span> in_bounds(<span class="keyword">const</span> Sym&) <span class="keyword">const</span>;
|
||||
00046 std::string get_bounds(<span class="keyword">const</span> Sym&) <span class="keyword">const</span>;
|
||||
00047
|
||||
00048 std::pair<double, double> calc_bounds(<span class="keyword">const</span> Sym&) <span class="keyword">const</span>;
|
||||
00049 };
|
||||
00050
|
||||
00051 <span class="keyword">class </span>NoBoundsCheck : <span class="keyword">public</span> BoundsCheck {
|
||||
00052 <span class="keywordtype">bool</span> in_bounds(<span class="keyword">const</span> Sym&)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="keyword">false</span>; }
|
||||
00053 std::string get_bounds(<span class="keyword">const</span> Sym&)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="stringliteral">""</span>; }
|
||||
00054 };
|
||||
00055
|
||||
00056 <span class="preprocessor">#endif</span>
|
||||
00057 <span class="preprocessor"></span>
|
||||
00058
|
||||
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:34 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>
|
||||
249
trunk/paradiseo-eo/doc/html/_c_m_a_params_8cpp-source.html
Normal file
249
trunk/paradiseo-eo/doc/html/_c_m_a_params_8cpp-source.html
Normal file
|
|
@ -0,0 +1,249 @@
|
|||
<!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: CMAParams.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 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_000000.html">src</a> / <a class="el" href="dir_000010.html">es</a></div>
|
||||
<h1>CMAParams.cpp</h1><div class="fragment"><pre class="fragment">00001
|
||||
00002 <span class="comment">/*</span>
|
||||
00003 <span class="comment"> * C++ification of Nikolaus Hansen's original C-source code for the</span>
|
||||
00004 <span class="comment"> * CMA-ES</span>
|
||||
00005 <span class="comment"> *</span>
|
||||
00006 <span class="comment"> * C++-ificiation performed by Maarten Keijzer (C) 2005. Licensed under</span>
|
||||
00007 <span class="comment"> * the LGPL. Original copyright of Nikolaus Hansen can be found below</span>
|
||||
00008 <span class="comment"> *</span>
|
||||
00009 <span class="comment"> *</span>
|
||||
00010 <span class="comment"> * </span>
|
||||
00011 <span class="comment"> */</span>
|
||||
00012
|
||||
00013 <span class="comment">/* --------------------------------------------------------- */</span>
|
||||
00014 <span class="comment">/* --------------------------------------------------------- */</span>
|
||||
00015 <span class="comment">/* --- File: cmaes.c -------- Author: Nikolaus Hansen --- */</span>
|
||||
00016 <span class="comment">/* --------------------------------------------------------- */</span>
|
||||
00017 <span class="comment">/*</span>
|
||||
00018 <span class="comment"> * CMA-ES for non-linear function minimization.</span>
|
||||
00019 <span class="comment"> *</span>
|
||||
00020 <span class="comment"> * Copyright (C) 1996, 2003 Nikolaus Hansen.</span>
|
||||
00021 <span class="comment"> * e-mail: hansen@bionik.tu-berlin.de</span>
|
||||
00022 <span class="comment"> *</span>
|
||||
00023 <span class="comment"> * This library is free software; you can redistribute it and/or</span>
|
||||
00024 <span class="comment"> * modify it under the terms of the GNU Lesser General Public</span>
|
||||
00025 <span class="comment"> * License as published by the Free Software Foundation; either</span>
|
||||
00026 <span class="comment"> * version 2.1 of the License, or (at your option) any later</span>
|
||||
00027 <span class="comment"> * version (see http://www.gnu.org/copyleft/lesser.html).</span>
|
||||
00028 <span class="comment"> *</span>
|
||||
00029 <span class="comment"> * This library is distributed in the hope that it will be useful,</span>
|
||||
00030 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
|
||||
00031 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</span>
|
||||
00032 <span class="comment"> * Lesser General Public License for more details.</span>
|
||||
00033 <span class="comment"> * </span>
|
||||
00034 <span class="comment"> * */</span>
|
||||
00035 <span class="comment">/* --- Changes : ---</span>
|
||||
00036 <span class="comment"> * 03/03/21: argument const double *rgFunVal of</span>
|
||||
00037 <span class="comment"> * cmaes_ReestimateDistribution() was treated incorrectly.</span>
|
||||
00038 <span class="comment"> * 03/03/29: restart via cmaes_resume_distribution() implemented.</span>
|
||||
00039 <span class="comment"> * 03/03/30: Always max std dev / largest axis is printed first.</span>
|
||||
00040 <span class="comment"> * 03/08/30: Damping is adjusted for large mueff.</span>
|
||||
00041 <span class="comment"> * 03/10/30: Damping is adjusted for large mueff always.</span>
|
||||
00042 <span class="comment"> * 04/04/22: Cumulation time and damping for step size adjusted.</span>
|
||||
00043 <span class="comment"> * No iniphase but conditional update of pc.</span>
|
||||
00044 <span class="comment"> * Version 2.23.</span>
|
||||
00045 <span class="comment"> * */</span>
|
||||
00046
|
||||
00047 <span class="preprocessor">#include <es/CMAParams.h></span>
|
||||
00048 <span class="preprocessor">#include <utils/eoParser.h></span>
|
||||
00049
|
||||
00050 <span class="preprocessor">#include <string></span>
|
||||
00051
|
||||
00052 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
|
||||
00053
|
||||
00054 <span class="keyword">namespace </span>eo {
|
||||
00055
|
||||
00056 CMAParams::CMAParams(eoParser& parser, <span class="keywordtype">unsigned</span> dimensionality) {
|
||||
00057
|
||||
00058 string section = <span class="stringliteral">"CMA parameters"</span>;
|
||||
00059
|
||||
00060 n = parser.createParam(dimensionality, <span class="stringliteral">"dimensionality"</span>, <span class="stringliteral">"Dimensionality (N) of the problem"</span>, <span class="charliteral">'N'</span>, section, dimensionality == 0).value();
|
||||
00061
|
||||
00062 maxgen = parser.createParam(
|
||||
00063 1000,
|
||||
00064 <span class="stringliteral">"max-gen"</span>,
|
||||
00065 <span class="stringliteral">"Maximum number of generations that the system will run (needed for damping)"</span>,
|
||||
00066 <span class="charliteral">'M'</span>,
|
||||
00067 section).value();
|
||||
00068
|
||||
00069
|
||||
00070 <span class="keywordflow">if</span> (n == 0) {
|
||||
00071 <span class="keywordflow">return</span>;
|
||||
00072 }
|
||||
00073
|
||||
00074 defaults(n, maxgen);
|
||||
00075
|
||||
00076 <span class="comment">/* handle lambda */</span>
|
||||
00077 lambda = parser.createParam(
|
||||
00078 lambda,
|
||||
00079 <span class="stringliteral">"lambda"</span>,
|
||||
00080 <span class="stringliteral">"Number of offspring"</span>,
|
||||
00081 <span class="charliteral">'l'</span>,
|
||||
00082 section).value();
|
||||
00083
|
||||
00084 <span class="keywordflow">if</span> (lambda < 2) {
|
||||
00085 lambda = 4+(int)(3*log((<span class="keywordtype">double</span>) n));
|
||||
00086 cerr << <span class="stringliteral">"Too small lambda specified, setting it to "</span> << lambda << endl;
|
||||
00087 }
|
||||
00088
|
||||
00089 <span class="comment">/* handle mu */</span>
|
||||
00090 mu = parser.createParam(
|
||||
00091 mu,
|
||||
00092 <span class="stringliteral">"mu"</span>,
|
||||
00093 <span class="stringliteral">"Population size"</span>,
|
||||
00094 <span class="charliteral">'m'</span>,
|
||||
00095 section).value();
|
||||
00096
|
||||
00097 <span class="keywordflow">if</span> (mu >= lambda) {
|
||||
00098 mu = lambda/2;
|
||||
00099 cerr << <span class="stringliteral">"Mu set larger/equal to lambda, setting it to "</span> << mu << endl;
|
||||
00100 }
|
||||
00101
|
||||
00102 <span class="comment">/* handle selection weights */</span>
|
||||
00103
|
||||
00104 <span class="keywordtype">int</span> weight_type = parser.createParam(
|
||||
00105 0,
|
||||
00106 <span class="stringliteral">"weighting"</span>,
|
||||
00107 <span class="stringliteral">"Weighting scheme (for 'selection'): 0 = logarithmic, 1 = equal, 2 = linear"</span>,
|
||||
00108 <span class="charliteral">'w'</span>,
|
||||
00109 section).value();
|
||||
00110
|
||||
00111 <span class="keywordflow">switch</span> (weight_type) {
|
||||
00112 <span class="keywordflow">case</span> 1:
|
||||
00113 {
|
||||
00114 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < weights.size(); ++i) {
|
||||
00115 weights[i] = mu - i;
|
||||
00116 }
|
||||
00117 }
|
||||
00118 <span class="keywordflow">case</span> 2:
|
||||
00119 {
|
||||
00120 weights = 1.;
|
||||
00121 }
|
||||
00122 <span class="keywordflow">default</span> :
|
||||
00123 {
|
||||
00124 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < weights.size(); ++i) {
|
||||
00125 weights[i] = log(mu+1.)-log(i+1.);
|
||||
00126 }
|
||||
00127 }
|
||||
00128
|
||||
00129 }
|
||||
00130
|
||||
00131 <span class="comment">/* Normalize weights and set mu_eff */</span>
|
||||
00132 <span class="keywordtype">double</span> sumw = weights.sum();
|
||||
00133 mueff = sumw * sumw / (weights * weights).sum();
|
||||
00134 weights /= sumw;
|
||||
00135
|
||||
00136
|
||||
00137 <span class="comment">/* most of the rest depends on mu_eff, so needs to be set again */</span>
|
||||
00138
|
||||
00139 <span class="comment">/* set the others using Nikolaus logic. If you want to tweak, you can parameterize over these defaults */</span>
|
||||
00140 mucov = mueff;
|
||||
00141 ccumsig = (mueff + 2.) / (n + mueff + 3.);
|
||||
00142 ccumcov = 4. / (n + 4);
|
||||
00143
|
||||
00144 <span class="keywordtype">double</span> t1 = 2. / ((n+1.4142)*(n+1.4142));
|
||||
00145 <span class="keywordtype">double</span> t2 = (2.*mucov-1.) / ((n+2.)*(n+2.)+mucov);
|
||||
00146 t2 = (t2 > 1) ? 1 : t2;
|
||||
00147 t2 = (1./mucov) * t1 + (1.-1./mucov) * t2;
|
||||
00148
|
||||
00149 ccov = t2;
|
||||
00150
|
||||
00151 damp = 1 + std::max(0.3,(1.-(<span class="keywordtype">double</span>)n/(<span class="keywordtype">double</span>)maxgen))
|
||||
00152 * (1+2*std::max(0.,sqrt((mueff-1.)/(n+1.))-1)) <span class="comment">/* limit sigma increase */</span>
|
||||
00153 / ccumsig;
|
||||
00154
|
||||
00155 vector<double> mins(1,0.0);
|
||||
00156 mins = parser.createParam(
|
||||
00157 mins,
|
||||
00158 <span class="stringliteral">"min-stdev"</span>,
|
||||
00159 <span class="stringliteral">"Array of minimum stdevs, last one will apply for all remaining axes"</span>,
|
||||
00160 0,
|
||||
00161 section).value();
|
||||
00162
|
||||
00163 <span class="keywordflow">if</span> (mins.size() > n) mins.resize(n);
|
||||
00164
|
||||
00165 <span class="keywordflow">if</span> (mins.size()) {
|
||||
00166 minStdevs = mins.back();
|
||||
00167 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < mins.size(); ++i) {
|
||||
00168 minStdevs[i] = mins[i];
|
||||
00169 }
|
||||
00170 }
|
||||
00171
|
||||
00172 vector<double> inits(1,0.3);
|
||||
00173 inits = parser.createParam(
|
||||
00174 inits,
|
||||
00175 <span class="stringliteral">"init-stdev"</span>,
|
||||
00176 <span class="stringliteral">"Array of initial stdevs, last one will apply for all remaining axes"</span>,
|
||||
00177 0,
|
||||
00178 section).value();
|
||||
00179
|
||||
00180 <span class="keywordflow">if</span> (inits.size() > n) inits.resize(n);
|
||||
00181
|
||||
00182 <span class="keywordflow">if</span> (inits.size()) {
|
||||
00183 initialStdevs = inits.back();
|
||||
00184 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < inits.size(); ++i) {
|
||||
00185 initialStdevs[i] = inits[i];
|
||||
00186 }
|
||||
00187 }
|
||||
00188
|
||||
00189 }
|
||||
00190
|
||||
00191 <span class="keywordtype">void</span> CMAParams::defaults(<span class="keywordtype">unsigned</span> n_, <span class="keywordtype">unsigned</span> maxgen_) {
|
||||
00192 n = n_;
|
||||
00193 maxgen = maxgen_;
|
||||
00194
|
||||
00195 lambda = 4+(int)(3*log((<span class="keywordtype">double</span>) n));
|
||||
00196 mu = lambda / 2;
|
||||
00197
|
||||
00198 weights.resize(mu);
|
||||
00199
|
||||
00200 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < weights.size(); ++i) {
|
||||
00201 weights[i] = log(mu+1.)-log(i+1.);
|
||||
00202 }
|
||||
00203
|
||||
00204 <span class="comment">/* Normalize weights and set mu_eff */</span>
|
||||
00205 <span class="keywordtype">double</span> sumw = weights.sum();
|
||||
00206 mueff = sumw * sumw / (weights * weights).sum();
|
||||
00207 weights /= sumw;
|
||||
00208
|
||||
00209 mucov = mueff;
|
||||
00210 ccumsig *= (mueff + 2.) / (n + mueff + 3.);
|
||||
00211 ccumcov = 4. / (n + 4);
|
||||
00212
|
||||
00213 <span class="keywordtype">double</span> t1 = 2. / ((n+1.4142)*(n+1.4142));
|
||||
00214 <span class="keywordtype">double</span> t2 = (2.*mucov-1.) / ((n+2.)*(n+2.)+mucov);
|
||||
00215 t2 = (t2 > 1) ? 1 : t2;
|
||||
00216 t2 = (1./mucov) * t1 + (1.-1./mucov) * t2;
|
||||
00217
|
||||
00218 ccov = t2;
|
||||
00219
|
||||
00220 damp = 1 + std::max(0.3,(1.-(<span class="keywordtype">double</span>)n/maxgen))
|
||||
00221 * (1+2*std::max(0.,sqrt((mueff-1.)/(n+1.))-1)) <span class="comment">/* limit sigma increase */</span>
|
||||
00222 / ccumsig;
|
||||
00223
|
||||
00224 minStdevs.resize(n);
|
||||
00225 minStdevs = 0.0;
|
||||
00226
|
||||
00227 initialStdevs.resize(n);
|
||||
00228 initialStdevs = 0.3;
|
||||
00229
|
||||
00230
|
||||
00231 }
|
||||
00232
|
||||
00233
|
||||
00234 }<span class="comment">// namespace eo</span>
|
||||
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:34 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>
|
||||
67
trunk/paradiseo-eo/doc/html/_c_m_a_params_8h-source.html
Normal file
67
trunk/paradiseo-eo/doc/html/_c_m_a_params_8h-source.html
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
<!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: CMAParams.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_000000.html">src</a> / <a class="el" href="dir_000010.html">es</a></div>
|
||||
<h1>CMAParams.h</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">/*</span>
|
||||
00002 <span class="comment"> * C++ification of Nikolaus Hansen's original C-source code for the</span>
|
||||
00003 <span class="comment"> * CMA-ES. </span>
|
||||
00004 <span class="comment"> *</span>
|
||||
00005 <span class="comment"> * Copyright (C) 1996, 2003, Nikolaus Hansen</span>
|
||||
00006 <span class="comment"> * (C) 2005, Maarten Keijzer</span>
|
||||
00007 <span class="comment"> *</span>
|
||||
00008 <span class="comment"> * License: LGPL </span>
|
||||
00009 <span class="comment"> * </span>
|
||||
00010 <span class="comment"> */</span>
|
||||
00011
|
||||
00012 <span class="preprocessor">#ifndef CMAPARAMS_H__</span>
|
||||
00013 <span class="preprocessor"></span><span class="preprocessor">#define CMAPARAMS_H__</span>
|
||||
00014 <span class="preprocessor"></span>
|
||||
00015 <span class="preprocessor">#include <valarray></span>
|
||||
00016
|
||||
00017 <span class="keyword">class </span><a class="code" href="classeo_parser.html">eoParser</a>;
|
||||
00018 <span class="keyword">namespace </span>eo {
|
||||
00019
|
||||
00020 <span class="keyword">class </span>CMAParams {
|
||||
00021
|
||||
00022 <span class="keyword">public</span>:
|
||||
00023
|
||||
00024 CMAParams() { <span class="comment">/* Call this and all values need to be set by hand */</span> }
|
||||
00025 CMAParams(<a class="code" href="classeo_parser.html">eoParser</a>& parser, <span class="keywordtype">unsigned</span> dimensionality = 0); <span class="comment">// 0 dimensionality -> user needs to set it</span>
|
||||
00026
|
||||
00027 <span class="keywordtype">void</span> defaults(<span class="keywordtype">unsigned</span> n_, <span class="keywordtype">unsigned</span> maxgen_); <span class="comment">/* apply all defaults using n and maxgen */</span>
|
||||
00028
|
||||
00029 <span class="keywordtype">unsigned</span> n;
|
||||
00030 <span class="keywordtype">unsigned</span> maxgen;
|
||||
00031
|
||||
00032 <span class="keywordtype">unsigned</span> lambda; <span class="comment">/* -> mu */</span>
|
||||
00033 <span class="keywordtype">unsigned</span> mu; <span class="comment">/* -> weights, lambda */</span>
|
||||
00034
|
||||
00035 std::valarray<double> weights; <span class="comment">/* <- mu, -> mueff -> mucov -> ccov */</span>
|
||||
00036 <span class="keywordtype">double</span> mueff; <span class="comment">/* <- weights */</span>
|
||||
00037
|
||||
00038 <span class="keywordtype">double</span> mucov;
|
||||
00039
|
||||
00040 <span class="keywordtype">double</span> damp; <span class="comment">/* <- ccumsig, maxeval, lambda */</span>
|
||||
00041 <span class="keywordtype">double</span> ccumsig; <span class="comment">/* -> damp, <- N */</span>
|
||||
00042 <span class="keywordtype">double</span> ccumcov;
|
||||
00043 <span class="keywordtype">double</span> ccov; <span class="comment">/* <- mucov, N */</span>
|
||||
00044
|
||||
00045 std::valarray<double> minStdevs; <span class="comment">/* Minimum standard deviations per coordinate (default = 0.0) */</span>
|
||||
00046 std::valarray<double> initialStdevs; <span class="comment">/* Initial standard deviations per coordinate (default = 0.3) */</span>
|
||||
00047 };
|
||||
00048
|
||||
00049 } <span class="comment">// namespace eo</span>
|
||||
00050
|
||||
00051 <span class="preprocessor">#endif</span>
|
||||
00052 <span class="preprocessor"></span>
|
||||
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:34 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>
|
||||
380
trunk/paradiseo-eo/doc/html/_c_m_a_state_8cpp-source.html
Normal file
380
trunk/paradiseo-eo/doc/html/_c_m_a_state_8cpp-source.html
Normal file
|
|
@ -0,0 +1,380 @@
|
|||
<!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: CMAState.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 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_000000.html">src</a> / <a class="el" href="dir_000010.html">es</a></div>
|
||||
<h1>CMAState.cpp</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">/*</span>
|
||||
00002 <span class="comment"> * C++ification of Nikolaus Hansen's original C-source code for the</span>
|
||||
00003 <span class="comment"> * CMA-ES</span>
|
||||
00004 <span class="comment"> *</span>
|
||||
00005 <span class="comment"> * C++-ificiation performed by Maarten Keijzer (C) 2005. Licensed under</span>
|
||||
00006 <span class="comment"> * the LGPL. Original copyright of Nikolaus Hansen can be found below</span>
|
||||
00007 <span class="comment"> *</span>
|
||||
00008 <span class="comment"> *</span>
|
||||
00009 <span class="comment"> * Some changes have been made to the original flow and logic of the</span>
|
||||
00010 <span class="comment"> * algorithm:</span>
|
||||
00011 <span class="comment"> *</span>
|
||||
00012 <span class="comment"> * - Numerical issues are now treated 'before' going into the eigenvector decomposition</span>
|
||||
00013 <span class="comment"> * (this was done out of convenience)</span>
|
||||
00014 <span class="comment"> * - dMaxSignifiKond (smallest x such that x == x + 1.0) replaced by</span>
|
||||
00015 <span class="comment"> * numeric_limits<double>::epsilon() (smallest x such that 1.0 != 1.0 + x)</span>
|
||||
00016 <span class="comment"> * </span>
|
||||
00017 <span class="comment"> * </span>
|
||||
00018 <span class="comment"> */</span>
|
||||
00019
|
||||
00020 <span class="comment">/* --------------------------------------------------------- */</span>
|
||||
00021 <span class="comment">/* --------------------------------------------------------- */</span>
|
||||
00022 <span class="comment">/* --- File: cmaes.c -------- Author: Nikolaus Hansen --- */</span>
|
||||
00023 <span class="comment">/* --------------------------------------------------------- */</span>
|
||||
00024 <span class="comment">/*</span>
|
||||
00025 <span class="comment"> * CMA-ES for non-linear function minimization.</span>
|
||||
00026 <span class="comment"> *</span>
|
||||
00027 <span class="comment"> * Copyright (C) 1996, 2003 Nikolaus Hansen.</span>
|
||||
00028 <span class="comment"> * e-mail: hansen@bionik.tu-berlin.de</span>
|
||||
00029 <span class="comment"> *</span>
|
||||
00030 <span class="comment"> * This library is free software; you can redistribute it and/or</span>
|
||||
00031 <span class="comment"> * modify it under the terms of the GNU Lesser General Public</span>
|
||||
00032 <span class="comment"> * License as published by the Free Software Foundation; either</span>
|
||||
00033 <span class="comment"> * version 2.1 of the License, or (at your option) any later</span>
|
||||
00034 <span class="comment"> * version (see http://www.gnu.org/copyleft/lesser.html).</span>
|
||||
00035 <span class="comment"> *</span>
|
||||
00036 <span class="comment"> * This library is distributed in the hope that it will be useful,</span>
|
||||
00037 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
|
||||
00038 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</span>
|
||||
00039 <span class="comment"> * Lesser General Public License for more details.</span>
|
||||
00040 <span class="comment"> * </span>
|
||||
00041 <span class="comment"> * */</span>
|
||||
00042 <span class="comment">/* --- Changes : ---</span>
|
||||
00043 <span class="comment"> * 03/03/21: argument const double *rgFunVal of</span>
|
||||
00044 <span class="comment"> * cmaes_ReestimateDistribution() was treated incorrectly.</span>
|
||||
00045 <span class="comment"> * 03/03/29: restart via cmaes_resume_distribution() implemented.</span>
|
||||
00046 <span class="comment"> * 03/03/30: Always max std dev / largest axis is printed first.</span>
|
||||
00047 <span class="comment"> * 03/08/30: Damping is adjusted for large mueff.</span>
|
||||
00048 <span class="comment"> * 03/10/30: Damping is adjusted for large mueff always.</span>
|
||||
00049 <span class="comment"> * 04/04/22: Cumulation time and damping for step size adjusted.</span>
|
||||
00050 <span class="comment"> * No iniphase but conditional update of pc.</span>
|
||||
00051 <span class="comment"> * Version 2.23.</span>
|
||||
00052 <span class="comment"> * */</span>
|
||||
00053
|
||||
00054 <span class="preprocessor">#include <valarray></span>
|
||||
00055 <span class="preprocessor">#include <limits></span>
|
||||
00056 <span class="preprocessor">#include <iostream></span>
|
||||
00057 <span class="preprocessor">#include <cassert></span>
|
||||
00058
|
||||
00059 <span class="preprocessor">#include <utils/eoRNG.h></span>
|
||||
00060
|
||||
00061 <span class="preprocessor">#include <es/CMAState.h></span>
|
||||
00062 <span class="preprocessor">#include <es/CMAParams.h></span>
|
||||
00063 <span class="preprocessor">#include <es/matrices.h></span>
|
||||
00064 <span class="preprocessor">#include <es/eig.h></span>
|
||||
00065
|
||||
00066 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
|
||||
00067
|
||||
00068 <span class="keyword">namespace </span>eo {
|
||||
00069
|
||||
00070 <span class="keyword">struct </span>CMAStateImpl {
|
||||
00071
|
||||
00072 CMAParams p;
|
||||
00073
|
||||
00074 lower_triangular_matrix C; <span class="comment">// Covariance matrix</span>
|
||||
00075 square_matrix B; <span class="comment">// Eigen vectors (in columns)</span>
|
||||
00076 valarray<double> d; <span class="comment">// eigen values (diagonal matrix)</span>
|
||||
00077 valarray<double> pc; <span class="comment">// Evolution path</span>
|
||||
00078 valarray<double> ps; <span class="comment">// Evolution path for stepsize;</span>
|
||||
00079
|
||||
00080 vector<double> mean; <span class="comment">// current mean to sample around</span>
|
||||
00081 <span class="keywordtype">double</span> sigma; <span class="comment">// global step size</span>
|
||||
00082
|
||||
00083 <span class="keywordtype">unsigned</span> gen;
|
||||
00084 vector<double> fitnessHistory;
|
||||
00085
|
||||
00086
|
||||
00087 CMAStateImpl(<span class="keyword">const</span> CMAParams& params_, <span class="keyword">const</span> vector<double>& m, <span class="keywordtype">double</span> sigma_) :
|
||||
00088 p(params_),
|
||||
00089 C(p.n), B(p.n), d(p.n), pc(p.n), ps(p.n), mean(m), sigma(sigma_),
|
||||
00090 gen(0), fitnessHistory(3)
|
||||
00091 {
|
||||
00092 <span class="keywordtype">double</span> trace = (p.initialStdevs * p.initialStdevs).sum();
|
||||
00093 <span class="comment">/* Initialize covariance structure */</span>
|
||||
00094 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < p.n; ++i)
|
||||
00095 {
|
||||
00096 B[i][i] = 1.;
|
||||
00097 d[i] = p.initialStdevs[i] * sqrt(p.n / trace);
|
||||
00098 C[i][i] = d[i] * d[i];
|
||||
00099 pc[i] = 0.;
|
||||
00100 ps[i] = 0.;
|
||||
00101 }
|
||||
00102
|
||||
00103 }
|
||||
00104
|
||||
00105 <span class="keywordtype">void</span> sample(vector<double>& v) {
|
||||
00106 <span class="keywordtype">unsigned</span> n = p.n;
|
||||
00107 v.resize(n);
|
||||
00108
|
||||
00109 vector<double> tmp(n);
|
||||
00110 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < n; ++i)
|
||||
00111 tmp[i] = d[i] * rng.<a class="code" href="classeo_rng.html#a7">normal</a>();
|
||||
00112
|
||||
00113 <span class="comment">/* add mutation (sigma * B * (D*z)) */</span>
|
||||
00114 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < n; ++i) {
|
||||
00115 <span class="keywordtype">double</span> sum = 0;
|
||||
00116 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j < n; ++j) {
|
||||
00117 sum += B[i][j] * tmp[j];
|
||||
00118 }
|
||||
00119 v[i] = mean[i] + sigma * sum;
|
||||
00120 }
|
||||
00121 }
|
||||
00122
|
||||
00123 <span class="keywordtype">void</span> reestimate(<span class="keyword">const</span> vector<<span class="keyword">const</span> vector<double>* >& pop, <span class="keywordtype">double</span> muBest, <span class="keywordtype">double</span> muWorst) {
|
||||
00124
|
||||
00125 assert(pop.size() == p.mu);
|
||||
00126
|
||||
00127 <span class="keywordtype">unsigned</span> n = p.n;
|
||||
00128
|
||||
00129 fitnessHistory[gen % fitnessHistory.size()] = muBest; <span class="comment">// needed for divergence check</span>
|
||||
00130
|
||||
00131 vector<double> oldmean = mean;
|
||||
00132 valarray<double> BDz(n);
|
||||
00133
|
||||
00134 <span class="comment">/* calculate xmean and rgBDz~N(0,C) */</span>
|
||||
00135 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < n; ++i) {
|
||||
00136 mean[i] = 0.;
|
||||
00137 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j < pop.size(); ++j) {
|
||||
00138 mean[i] += p.weights[j] * (*pop[j])[i];
|
||||
00139 }
|
||||
00140 BDz[i] = sqrt(p.mueff)*(mean[i] - oldmean[i])/sigma;
|
||||
00141 }
|
||||
00142
|
||||
00143 vector<double> tmp(n);
|
||||
00144 <span class="comment">/* calculate z := D^(-1) * B^(-1) * rgBDz into rgdTmp */</span>
|
||||
00145 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < n; ++i) {
|
||||
00146 <span class="keywordtype">double</span> sum = 0.0;
|
||||
00147 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j < n; ++j) {
|
||||
00148 sum += B[j][i] * BDz[j];
|
||||
00149 }
|
||||
00150 tmp[i] = sum / d[i];
|
||||
00151 }
|
||||
00152
|
||||
00153 <span class="comment">/* cumulation for sigma (ps) using B*z */</span>
|
||||
00154 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < n; ++i) {
|
||||
00155 <span class="keywordtype">double</span> sum = 0.0;
|
||||
00156 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j < n; ++j)
|
||||
00157 sum += B[i][j] * tmp[j];
|
||||
00158
|
||||
00159 ps[i] = (1. - p.ccumsig) * ps[i] + sqrt(p.ccumsig * (2. - p.ccumsig)) * sum;
|
||||
00160 }
|
||||
00161
|
||||
00162 <span class="comment">/* calculate norm(ps)^2 */</span>
|
||||
00163 <span class="keywordtype">double</span> psxps = (ps * ps).sum();
|
||||
00164
|
||||
00165
|
||||
00166 <span class="keywordtype">double</span> chiN = sqrt((<span class="keywordtype">double</span>) p.n) * (1. - 1./(4.*p.n) + 1./(21.*p.n*p.n));
|
||||
00167 <span class="comment">/* cumulation for covariance matrix (pc) using B*D*z~N(0,C) */</span>
|
||||
00168 <span class="keywordtype">double</span> hsig = sqrt(psxps) / sqrt(1. - pow(1.-p.ccumsig, 2.*gen)) / chiN < 1.5 + 1./(p.n-0.5);
|
||||
00169
|
||||
00170 pc = (1. - p.ccumcov) * pc + hsig * sqrt(p.ccumcov * (2. - p.ccumcov)) * BDz;
|
||||
00171
|
||||
00172 <span class="comment">/* stop initial phase (MK, this was not reachable in the org code, deleted) */</span>
|
||||
00173
|
||||
00174 <span class="comment">/* remove momentum in ps, if ps is large and fitness is getting worse */</span>
|
||||
00175
|
||||
00176 <span class="keywordflow">if</span> (gen >= fitnessHistory.size()) {
|
||||
00177
|
||||
00178 <span class="comment">// find direction from muBest and muWorst (muBest == muWorst handled seperately</span>
|
||||
00179 <span class="keywordtype">double</span> direction = muBest < muWorst? -1.0 : 1.0;
|
||||
00180
|
||||
00181 <span class="keywordtype">unsigned</span> now = gen % fitnessHistory.size();
|
||||
00182 <span class="keywordtype">unsigned</span> prev = (gen-1) % fitnessHistory.size();
|
||||
00183 <span class="keywordtype">unsigned</span> prevprev = (gen-2) % fitnessHistory.size();
|
||||
00184
|
||||
00185 <span class="keywordtype">bool</span> fitnessWorsens = (muBest == muWorst) || <span class="comment">// <- increase norm also when population has converged (this deviates from Hansen's scheme)</span>
|
||||
00186 ( (direction * fitnessHistory[now] < direction * fitnessHistory[prev])
|
||||
00187 &&
|
||||
00188 (direction * fitnessHistory[now] < direction * fitnessHistory[prevprev]));
|
||||
00189
|
||||
00190 <span class="keywordflow">if</span>(psxps/p.n > 1.5 + 10.*sqrt(2./p.n) && fitnessWorsens) {
|
||||
00191 <span class="keywordtype">double</span> tfac = sqrt((1 + std::max(0., log(psxps/p.n))) * p.n / psxps);
|
||||
00192 ps *= tfac;
|
||||
00193 psxps *= tfac*tfac;
|
||||
00194 }
|
||||
00195 }
|
||||
00196
|
||||
00197 <span class="comment">/* update of C */</span>
|
||||
00198 <span class="comment">/* Adapt_C(t); not used anymore */</span>
|
||||
00199 <span class="keywordflow">if</span> (p.ccov != 0.) {
|
||||
00200 <span class="comment">//flgEigensysIsUptodate = 0;</span>
|
||||
00201
|
||||
00202 <span class="comment">/* update covariance matrix */</span>
|
||||
00203 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < n; ++i) {
|
||||
00204 vector<double>::iterator c_row = C[i];
|
||||
00205 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j <= i; ++j) {
|
||||
00206 c_row[j] =
|
||||
00207 (1 - p.ccov) * c_row[j]
|
||||
00208 +
|
||||
00209 p.ccov * (1./p.mucov) * pc[i] * pc[j]
|
||||
00210 +
|
||||
00211 (1-hsig) * p.ccumcov * (2. - p.ccumcov) * c_row[j];
|
||||
00212
|
||||
00213 <span class="comment">/*C[i][j] = (1 - p.ccov) * C[i][j] </span>
|
||||
00214 <span class="comment"> + sp.ccov * (1./sp.mucov) </span>
|
||||
00215 <span class="comment"> * (rgpc[i] * rgpc[j] </span>
|
||||
00216 <span class="comment"> + (1-hsig)*sp.ccumcov*(2.-sp.ccumcov) * C[i][j]); */</span>
|
||||
00217 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> k = 0; k < p.mu; ++k) { <span class="comment">/* additional rank mu update */</span>
|
||||
00218 c_row[j] += p.ccov * (1-1./p.mucov) * p.weights[k]
|
||||
00219 * ( (*pop[k])[i] - oldmean[i])
|
||||
00220 * ( (*pop[k])[j] - oldmean[j])
|
||||
00221 / sigma / sigma;
|
||||
00222
|
||||
00223 <span class="comment">// * (rgrgx[index[k]][i] - rgxold[i]) </span>
|
||||
00224 <span class="comment">// * (rgrgx[index[k]][j] - rgxold[j])</span>
|
||||
00225 <span class="comment">// / sigma / sigma; </span>
|
||||
00226 }
|
||||
00227 }
|
||||
00228 }
|
||||
00229 }
|
||||
00230
|
||||
00231 <span class="comment">/* update of sigma */</span>
|
||||
00232 sigma *= exp(((sqrt(psxps)/chiN)-1.)/p.damp);
|
||||
00233 <span class="comment">/* calculate eigensystem, must be done by caller */</span>
|
||||
00234 <span class="comment">//cmaes_UpdateEigensystem(0);</span>
|
||||
00235
|
||||
00236
|
||||
00237 <span class="comment">/* treat minimal standard deviations and numeric problems </span>
|
||||
00238 <span class="comment"> * Note that in contrast with the original code, some numerical issues are treated *before* we </span>
|
||||
00239 <span class="comment"> * go into the eigenvalue calculation */</span>
|
||||
00240
|
||||
00241 treatNumericalIssues(muBest, muWorst);
|
||||
00242
|
||||
00243 gen++; <span class="comment">// increase generation</span>
|
||||
00244 }
|
||||
00245
|
||||
00246 <span class="keywordtype">void</span> treatNumericalIssues(<span class="keywordtype">double</span> best, <span class="keywordtype">double</span> worst) {
|
||||
00247
|
||||
00248 <span class="comment">/* treat stdevs */</span>
|
||||
00249 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < p.n; ++i) {
|
||||
00250 <span class="keywordflow">if</span> (sigma * sqrt(C[i][i]) < p.minStdevs[i]) {
|
||||
00251 <span class="comment">// increase stdev</span>
|
||||
00252 sigma *= exp(0.05+1./p.damp);
|
||||
00253 <span class="keywordflow">break</span>;
|
||||
00254 }
|
||||
00255 }
|
||||
00256
|
||||
00257 <span class="comment">/* treat convergence */</span>
|
||||
00258 <span class="keywordflow">if</span> (best == worst) {
|
||||
00259 sigma *= exp(0.2 + 1./p.damp);
|
||||
00260 }
|
||||
00261
|
||||
00262 <span class="comment">/* Jede Hauptachse i testen, ob x == x + 0.1 * sigma * rgD[i] * B[i] */</span>
|
||||
00263 <span class="comment">/* Test if all the means are not numerically out of whack with our coordinate system*/</span>
|
||||
00264 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> axis = 0; axis < p.n; ++axis) {
|
||||
00265 <span class="keywordtype">double</span> fac = 0.1 * sigma * d[axis];
|
||||
00266 <span class="keywordtype">unsigned</span> coord;
|
||||
00267 <span class="keywordflow">for</span> (coord = 0; coord < p.n; ++coord) {
|
||||
00268 <span class="keywordflow">if</span> (mean[coord] != mean[coord] + fac * B[coord][axis]) {
|
||||
00269 <span class="keywordflow">break</span>;
|
||||
00270 }
|
||||
00271 }
|
||||
00272
|
||||
00273 <span class="keywordflow">if</span> (coord == p.n) { <span class="comment">// means are way too big (little) for numerical accuraccy. Start rocking the craddle a bit more</span>
|
||||
00274 sigma *= exp(0.2+1./p.damp);
|
||||
00275 }
|
||||
00276
|
||||
00277 }
|
||||
00278
|
||||
00279 <span class="comment">/* Testen ob eine Komponente des Objektparameters festhaengt */</span>
|
||||
00280 <span class="comment">/* Correct issues with scale between objective values and covariances */</span>
|
||||
00281 <span class="keywordtype">bool</span> theresAnIssue = <span class="keyword">false</span>;
|
||||
00282
|
||||
00283 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < p.n; ++i) {
|
||||
00284 <span class="keywordflow">if</span> (mean[i] == mean[i] + 0.2 * sigma * sqrt(C[i][i])) {
|
||||
00285 C[i][i] *= (1. + p.ccov);
|
||||
00286 theresAnIssue = <span class="keyword">true</span>;
|
||||
00287 }
|
||||
00288 }
|
||||
00289
|
||||
00290 <span class="keywordflow">if</span> (theresAnIssue) {
|
||||
00291 sigma *= exp(0.05 + 1./p.damp);
|
||||
00292 }
|
||||
00293 }
|
||||
00294
|
||||
00295
|
||||
00296 <span class="keywordtype">bool</span> updateEigenSystem(<span class="keywordtype">unsigned</span> max_tries, <span class="keywordtype">unsigned</span> max_iters) {
|
||||
00297
|
||||
00298 <span class="keywordflow">if</span> (max_iters==0) max_iters = 30 * p.n;
|
||||
00299
|
||||
00300 <span class="keyword">static</span> <span class="keywordtype">double</span> lastGoodMinimumEigenValue = 1.0;
|
||||
00301
|
||||
00302 <span class="comment">/* Try to get a valid calculation */</span>
|
||||
00303 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> tries = 0; tries < max_tries; ++tries) {
|
||||
00304
|
||||
00305 <span class="keywordtype">unsigned</span> iters = eig( p.n, C, d, B, max_iters);
|
||||
00306 <span class="keywordflow">if</span> (iters < max_iters)
|
||||
00307 { <span class="comment">// all is well</span>
|
||||
00308
|
||||
00309 <span class="comment">/* find largest/smallest eigenvalues */</span>
|
||||
00310 <span class="keywordtype">double</span> minEV = d.min();
|
||||
00311 <span class="keywordtype">double</span> maxEV = d.max();
|
||||
00312
|
||||
00313 <span class="comment">/* (MK Original comment was) :Limit Condition of C to dMaxSignifKond+1 </span>
|
||||
00314 <span class="comment"> * replaced dMaxSignifKond with 1./numeric_limits<double>::epsilon()</span>
|
||||
00315 <span class="comment"> * */</span>
|
||||
00316 <span class="keywordflow">if</span> (maxEV * numeric_limits<double>::epsilon() > minEV) {
|
||||
00317 <span class="keywordtype">double</span> tmp = maxEV * numeric_limits<double>::epsilon() - minEV;
|
||||
00318 minEV += tmp;
|
||||
00319 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0;i<p.n;++i) {
|
||||
00320 C[i][i] += tmp;
|
||||
00321 d[i] += tmp;
|
||||
00322 }
|
||||
00323 } <span class="comment">/* if */</span>
|
||||
00324 lastGoodMinimumEigenValue = minEV;
|
||||
00325
|
||||
00326 d = sqrt(d);
|
||||
00327
|
||||
00328 <span class="comment">//flgEigensysIsUptodate = 1;</span>
|
||||
00329 <span class="comment">//genOfEigensysUpdate = gen; </span>
|
||||
00330 <span class="comment">//clockeigensum += clock() - clockeigenbegin;</span>
|
||||
00331 <span class="keywordflow">return</span> <span class="keyword">true</span>;
|
||||
00332 } <span class="comment">/* if cIterEig < ... */</span>
|
||||
00333
|
||||
00334 <span class="comment">// numerical problems, ignore them and try again</span>
|
||||
00335
|
||||
00336 <span class="comment">/* Addition des letzten minEW auf die Diagonale von C */</span>
|
||||
00337 <span class="comment">/* Add the last known good eigen value to the diagonal of C */</span>
|
||||
00338 <span class="keywordtype">double</span> summand = lastGoodMinimumEigenValue * exp((<span class="keywordtype">double</span>) tries);
|
||||
00339 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < p.n; ++i)
|
||||
00340 C[i][i] += summand;
|
||||
00341
|
||||
00342 } <span class="comment">/* for iEigenCalcVers */</span>
|
||||
00343
|
||||
00344 <span class="keywordflow">return</span> <span class="keyword">false</span>;
|
||||
00345
|
||||
00346 }
|
||||
00347
|
||||
00348
|
||||
00349 };
|
||||
00350
|
||||
00351 CMAState::CMAState(<span class="keyword">const</span> CMAParams& params, <span class="keyword">const</span> std::vector<double>& initial_point, <span class="keyword">const</span> <span class="keywordtype">double</span> initial_sigma)
|
||||
00352 : pimpl(new CMAStateImpl(params, initial_point, initial_sigma)) {}
|
||||
00353
|
||||
00354 CMAState::~CMAState() { <span class="keyword">delete</span> pimpl; }
|
||||
00355 CMAState::CMAState(<span class="keyword">const</span> CMAState& that) : pimpl(new CMAStateImpl(*that.pimpl )) {}
|
||||
00356 CMAState& CMAState::operator=(<span class="keyword">const</span> CMAState& that) { *pimpl = *that.pimpl; <span class="keywordflow">return</span> *<span class="keyword">this</span>; }
|
||||
00357
|
||||
00358 <span class="keywordtype">void</span> CMAState::sample(vector<double>& v)<span class="keyword"> const </span>{ pimpl->sample(v); }
|
||||
00359
|
||||
00360 <span class="keywordtype">void</span> CMAState::reestimate(<span class="keyword">const</span> vector<<span class="keyword">const</span> vector<double>* >& population, <span class="keywordtype">double</span> muBest, <span class="keywordtype">double</span> muWorst) { pimpl->reestimate(population, muBest, muWorst); }
|
||||
00361 <span class="keywordtype">bool</span> CMAState::updateEigenSystem(<span class="keywordtype">unsigned</span> max_tries, <span class="keywordtype">unsigned</span> max_iters) { <span class="keywordflow">return</span> pimpl->updateEigenSystem(max_tries, max_iters); }
|
||||
00362
|
||||
00363
|
||||
00364 } <span class="comment">// namespace eo</span>
|
||||
00365
|
||||
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:34 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>
|
||||
59
trunk/paradiseo-eo/doc/html/_c_m_a_state_8h-source.html
Normal file
59
trunk/paradiseo-eo/doc/html/_c_m_a_state_8h-source.html
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<!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: CMAState.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_000000.html">src</a> / <a class="el" href="dir_000010.html">es</a></div>
|
||||
<h1>CMAState.h</h1><div class="fragment"><pre class="fragment">00001
|
||||
00002 <span class="comment">/*</span>
|
||||
00003 <span class="comment"> * C++ification of Nikolaus Hansen's original C-source code for the</span>
|
||||
00004 <span class="comment"> * CMA-ES. </span>
|
||||
00005 <span class="comment"> *</span>
|
||||
00006 <span class="comment"> * Copyright (C) 1996, 2003, Nikolaus Hansen</span>
|
||||
00007 <span class="comment"> * (C) 2005, Maarten Keijzer</span>
|
||||
00008 <span class="comment"> *</span>
|
||||
00009 <span class="comment"> * License: LGPL (see source file)</span>
|
||||
00010 <span class="comment"> * </span>
|
||||
00011 <span class="comment"> */</span>
|
||||
00012
|
||||
00013 <span class="preprocessor">#ifndef CMASTATE_H_</span>
|
||||
00014 <span class="preprocessor"></span><span class="preprocessor">#define CMASTATE_H_</span>
|
||||
00015 <span class="preprocessor"></span>
|
||||
00016 <span class="preprocessor">#include <vector></span>
|
||||
00017 <span class="preprocessor">#include <valarray></span>
|
||||
00018
|
||||
00019 <span class="keyword">namespace </span>eo {
|
||||
00020
|
||||
00021
|
||||
00022 <span class="keyword">class </span>CMAStateImpl;
|
||||
00023 <span class="keyword">class </span>CMAParams;
|
||||
00024 <span class="keyword">class </span>CMAState {
|
||||
00025
|
||||
00026 CMAStateImpl* pimpl; <span class="comment">/* pointer to implementation, hidden in source file */</span>
|
||||
00027
|
||||
00028 <span class="keyword">public</span>:
|
||||
00029
|
||||
00030 CMAState(<span class="keyword">const</span> CMAParams&, <span class="keyword">const</span> std::vector<double>& initial_point, <span class="keyword">const</span> <span class="keywordtype">double</span> initial_sigma = 1.0);
|
||||
00031 ~CMAState();
|
||||
00032 CMAState(<span class="keyword">const</span> CMAState&);
|
||||
00033 CMAState& operator=(<span class="keyword">const</span> CMAState&);
|
||||
00034
|
||||
00051 <span class="keywordtype">void</span> sample(std::vector<double>& v) <span class="keyword">const</span>;
|
||||
00052
|
||||
00063 <span class="keywordtype">void</span> reestimate(<span class="keyword">const</span> std::vector<<span class="keyword">const</span> std::vector<double>* >& sorted_population, <span class="keywordtype">double</span> muBest, <span class="keywordtype">double</span> muWorst);
|
||||
00064
|
||||
00074 <span class="keywordtype">bool</span> updateEigenSystem(<span class="keywordtype">unsigned</span> max_tries = 1, <span class="keywordtype">unsigned</span> max_iters = 0);
|
||||
00075 };
|
||||
00076
|
||||
00077 } <span class="comment">// namespace eo</span>
|
||||
00078
|
||||
00079 <span class="preprocessor">#endif</span>
|
||||
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:34 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>
|
||||
148
trunk/paradiseo-eo/doc/html/_dataset_8cpp-source.html
Normal file
148
trunk/paradiseo-eo/doc/html/_dataset_8cpp-source.html
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
<!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: Dataset.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 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_000007.html">contrib</a> / <a class="el" href="dir_000008.html">mathsym</a> / <a class="el" href="dir_000012.html">regression</a></div>
|
||||
<h1>Dataset.cpp</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">/* </span>
|
||||
00002 <span class="comment"> * Copyright (C) 2005 Maarten Keijzer</span>
|
||||
00003 <span class="comment"> *</span>
|
||||
00004 <span class="comment"> * This program is free software; you can redistribute it and/or modify</span>
|
||||
00005 <span class="comment"> * it under the terms of version 2 of the GNU General Public License as </span>
|
||||
00006 <span class="comment"> * published by the Free Software Foundation. </span>
|
||||
00007 <span class="comment"> *</span>
|
||||
00008 <span class="comment"> * This program is distributed in the hope that it will be useful,</span>
|
||||
00009 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
|
||||
00010 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
|
||||
00011 <span class="comment"> * GNU General Public License for more details.</span>
|
||||
00012 <span class="comment"> *</span>
|
||||
00013 <span class="comment"> * You should have received a copy of the GNU General Public License</span>
|
||||
00014 <span class="comment"> * along with this program; if not, write to the Free Software</span>
|
||||
00015 <span class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
|
||||
00016 <span class="comment"> */</span>
|
||||
00017
|
||||
00018 <span class="preprocessor">#include "Dataset.h"</span>
|
||||
00019 <span class="preprocessor">#include <fstream></span>
|
||||
00020 <span class="preprocessor">#include <sstream></span>
|
||||
00021
|
||||
00022 <span class="preprocessor">#include <iostream></span>
|
||||
00023
|
||||
00024 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
|
||||
00025
|
||||
00026 <span class="keyword">class </span>DataSetImpl {
|
||||
00027 <span class="keyword">public</span>:
|
||||
00028 vector< vector<double> > inputs;
|
||||
00029 vector<double> targets;
|
||||
00030
|
||||
00031 <span class="keywordtype">void</span> read_data(vector<string> strings) {
|
||||
00032 <span class="comment">// find the number of inputs</span>
|
||||
00033
|
||||
00034 istringstream cnt(strings[0]);
|
||||
00035 <span class="keywordtype">unsigned</span> n = 0;
|
||||
00036 <span class="keywordflow">for</span> (;;) {
|
||||
00037 string s;
|
||||
00038 cnt >> s;
|
||||
00039 <span class="keywordflow">if</span> (!cnt) <span class="keywordflow">break</span>;
|
||||
00040 ++n;
|
||||
00041 }
|
||||
00042
|
||||
00043 inputs.resize(strings.size(), vector<double>(n-1));
|
||||
00044 targets.resize(strings.size());
|
||||
00045
|
||||
00046 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < strings.size(); ++i) {
|
||||
00047 istringstream is(strings[i]);
|
||||
00048 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j < n; ++j) {
|
||||
00049
|
||||
00050 <span class="keywordflow">if</span> (!is) {
|
||||
00051 cerr << <span class="stringliteral">"Too few targets in record "</span> << i << endl;
|
||||
00052 exit(1);
|
||||
00053 }
|
||||
00054
|
||||
00055 <span class="keywordflow">if</span> (j < n-1) {
|
||||
00056 is >> inputs[i][j];
|
||||
00057 } <span class="keywordflow">else</span> {
|
||||
00058 is >> targets[i];
|
||||
00059 }
|
||||
00060
|
||||
00061 }
|
||||
00062 }
|
||||
00063
|
||||
00064 }
|
||||
00065
|
||||
00066 };
|
||||
00067
|
||||
00068 Dataset::Dataset() { pimpl = <span class="keyword">new</span> DataSetImpl; }
|
||||
00069 Dataset::~Dataset() { <span class="keyword">delete</span> pimpl; }
|
||||
00070 Dataset::Dataset(<span class="keyword">const</span> Dataset& that) { pimpl = <span class="keyword">new</span> DataSetImpl(*that.pimpl); }
|
||||
00071 Dataset& Dataset::operator=(<span class="keyword">const</span> Dataset& that) { *pimpl = *that.pimpl; <span class="keywordflow">return</span> *<span class="keyword">this</span>; }
|
||||
00072
|
||||
00073 <span class="keywordtype">unsigned</span> Dataset::n_records()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> pimpl->targets.size(); }
|
||||
00074 <span class="keywordtype">unsigned</span> Dataset::n_fields()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> pimpl->inputs[0].size(); }
|
||||
00075 <span class="keyword">const</span> std::vector<double>& Dataset::get_inputs(<span class="keywordtype">unsigned</span> record)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> pimpl->inputs[record]; }
|
||||
00076 <span class="keywordtype">double</span> Dataset::get_target(<span class="keywordtype">unsigned</span> record)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> pimpl->targets[record]; }
|
||||
00077
|
||||
00078 <span class="keywordtype">double</span> error(string errstr);
|
||||
00079
|
||||
00080 <span class="keywordtype">void</span> Dataset::load_data(std::string filename) {
|
||||
00081 vector<string> strings; <span class="comment">// first load it in strings</span>
|
||||
00082
|
||||
00083 ifstream is(filename.c_str());
|
||||
00084
|
||||
00085 <span class="keywordflow">for</span>(;;) {
|
||||
00086 string s;
|
||||
00087 getline(is, s);
|
||||
00088 <span class="keywordflow">if</span> (!is) <span class="keywordflow">break</span>;
|
||||
00089
|
||||
00090 <span class="keywordflow">if</span> (s[0] == <span class="charliteral">'#'</span>) <span class="keywordflow">continue</span>; <span class="comment">// comment, skip</span>
|
||||
00091
|
||||
00092 strings.push_back(s);
|
||||
00093 }
|
||||
00094
|
||||
00095 is.close();
|
||||
00096
|
||||
00097 <span class="keywordflow">if</span> (strings.size() == 0) {
|
||||
00098 error(<span class="stringliteral">"No data could be loaded"</span>);
|
||||
00099 }
|
||||
00100
|
||||
00101 pimpl->read_data(strings);
|
||||
00102
|
||||
00103 }
|
||||
00104
|
||||
00105 std::vector<double> Dataset::input_minima()<span class="keyword"> const </span>{
|
||||
00106 vector<vector<double> >& in = pimpl->inputs;
|
||||
00107
|
||||
00108 vector<double> mn(in[0].size(), 1e+50);
|
||||
00109 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < in.size(); ++i) {
|
||||
00110 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j < in[i].size(); ++j) {
|
||||
00111 mn[j] = std::min(mn[j], in[i][j]);
|
||||
00112 }
|
||||
00113 }
|
||||
00114
|
||||
00115 <span class="keywordflow">return</span> mn;
|
||||
00116 }
|
||||
00117
|
||||
00118 vector<double> Dataset::input_maxima()<span class="keyword"> const </span>{
|
||||
00119 vector<vector<double> >& in = pimpl->inputs;
|
||||
00120
|
||||
00121 vector<double> mx(in[0].size(), -1e+50);
|
||||
00122 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < in.size(); ++i) {
|
||||
00123 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j < in[i].size(); ++j) {
|
||||
00124 mx[j] = std::max(mx[j], in[i][j]);
|
||||
00125 }
|
||||
00126 }
|
||||
00127
|
||||
00128 <span class="keywordflow">return</span> mx;
|
||||
00129 }
|
||||
00130
|
||||
00131
|
||||
00132
|
||||
00133
|
||||
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:34 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>
|
||||
66
trunk/paradiseo-eo/doc/html/_dataset_8h-source.html
Normal file
66
trunk/paradiseo-eo/doc/html/_dataset_8h-source.html
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
<!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: Dataset.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_000007.html">contrib</a> / <a class="el" href="dir_000008.html">mathsym</a> / <a class="el" href="dir_000012.html">regression</a></div>
|
||||
<h1>Dataset.h</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">/* </span>
|
||||
00002 <span class="comment"> * Copyright (C) 2005 Maarten Keijzer</span>
|
||||
00003 <span class="comment"> *</span>
|
||||
00004 <span class="comment"> * This program is free software; you can redistribute it and/or modify</span>
|
||||
00005 <span class="comment"> * it under the terms of version 2 of the GNU General Public License as </span>
|
||||
00006 <span class="comment"> * published by the Free Software Foundation. </span>
|
||||
00007 <span class="comment"> *</span>
|
||||
00008 <span class="comment"> * This program is distributed in the hope that it will be useful,</span>
|
||||
00009 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
|
||||
00010 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
|
||||
00011 <span class="comment"> * GNU General Public License for more details.</span>
|
||||
00012 <span class="comment"> *</span>
|
||||
00013 <span class="comment"> * You should have received a copy of the GNU General Public License</span>
|
||||
00014 <span class="comment"> * along with this program; if not, write to the Free Software</span>
|
||||
00015 <span class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
|
||||
00016 <span class="comment"> */</span>
|
||||
00017
|
||||
00018 <span class="preprocessor">#ifndef DATASET_H_</span>
|
||||
00019 <span class="preprocessor"></span><span class="preprocessor">#define DATASET_H_</span>
|
||||
00020 <span class="preprocessor"></span>
|
||||
00021 <span class="preprocessor">#include <string></span>
|
||||
00022 <span class="preprocessor">#include <vector></span>
|
||||
00023
|
||||
00024 <span class="keyword">class </span>DataSetImpl;
|
||||
00025
|
||||
00026 <span class="keyword">class </span>Dataset {
|
||||
00027
|
||||
00028 DataSetImpl* pimpl;
|
||||
00029
|
||||
00030 Dataset& operator=(<span class="keyword">const</span> Dataset&); <span class="comment">// cannot assign</span>
|
||||
00031 <span class="keyword">public</span>:
|
||||
00032
|
||||
00033 Dataset();
|
||||
00034 ~Dataset();
|
||||
00035 Dataset(<span class="keyword">const</span> Dataset&);
|
||||
00036
|
||||
00037 <span class="keywordtype">void</span> load_data(std::string filename);
|
||||
00038
|
||||
00039 <span class="keywordtype">unsigned</span> n_records() <span class="keyword">const</span>;
|
||||
00040 <span class="keywordtype">unsigned</span> n_fields() <span class="keyword">const</span>;
|
||||
00041
|
||||
00042 <span class="keyword">const</span> std::vector<double>& get_inputs(<span class="keywordtype">unsigned</span> record) <span class="keyword">const</span>;
|
||||
00043 <span class="keywordtype">double</span> get_target(<span class="keywordtype">unsigned</span> record) <span class="keyword">const</span>;
|
||||
00044
|
||||
00045 std::vector<double> input_minima() <span class="keyword">const</span>;
|
||||
00046 std::vector<double> input_maxima() <span class="keyword">const</span>;
|
||||
00047
|
||||
00048 };
|
||||
00049
|
||||
00050 <span class="preprocessor">#endif</span>
|
||||
00051 <span class="preprocessor"></span>
|
||||
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:34 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>
|
||||
125
trunk/paradiseo-eo/doc/html/_e_o_8h-source.html
Normal file
125
trunk/paradiseo-eo/doc/html/_e_o_8h-source.html
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
<!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: EO.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_000000.html">src</a></div>
|
||||
<h1>EO.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">// EO.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 <span class="preprocessor">#ifndef EO_H</span>
|
||||
00026 <span class="preprocessor"></span><span class="preprocessor">#define EO_H</span>
|
||||
00027 <span class="preprocessor"></span>
|
||||
00028 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00029
|
||||
00030 <span class="preprocessor">#include <stdexcept></span> <span class="comment">// std::runtime_error</span>
|
||||
00031 <span class="preprocessor">#include <eoObject.h></span> <span class="comment">// eoObject</span>
|
||||
00032 <span class="preprocessor">#include <eoPersistent.h></span> <span class="comment">// eoPersistent</span>
|
||||
00033
|
||||
00034 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
<a name="l00044"></a><a class="code" href="class_e_o.html">00044</a> <span class="comment"></span><span class="keyword">template</span><<span class="keyword">class</span> F> <span class="keyword">class </span><a class="code" href="class_e_o.html">EO</a>: <span class="keyword">public</span> <a class="code" href="classeo_object.html">eoObject</a>, <span class="keyword">public</span> <a class="code" href="classeo_persistent.html">eoPersistent</a>
|
||||
00045 {
|
||||
00046 <span class="keyword">public</span>:
|
||||
00047 <span class="keyword">typedef</span> F Fitness;
|
||||
00048
|
||||
<a name="l00054"></a><a class="code" href="class_e_o.html#a0">00054</a> <a class="code" href="class_e_o.html#a0">EO</a>(): repFitness(Fitness()), invalidFitness(true) {}
|
||||
00055
|
||||
<a name="l00057"></a><a class="code" href="class_e_o.html#a1">00057</a> <span class="keyword">virtual</span> <a class="code" href="class_e_o.html#a1">~EO</a>() {};
|
||||
00058
|
||||
<a name="l00060"></a><a class="code" href="class_e_o.html#a2">00060</a> Fitness <a class="code" href="class_e_o.html#a2">fitness</a>()<span class="keyword"> const </span>{
|
||||
00061 <span class="keywordflow">if</span> (<a class="code" href="class_e_o.html#a5">invalid</a>())
|
||||
00062 <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"invalid fitness"</span>);
|
||||
00063 <span class="keywordflow">return</span> repFitness;
|
||||
00064 }
|
||||
00065
|
||||
00066 <span class="comment">// Set fitness as invalid.</span>
|
||||
00067 <span class="keywordtype">void</span> invalidate() { invalidFitness = <span class="keyword">true</span>; }
|
||||
00068
|
||||
<a name="l00072"></a><a class="code" href="class_e_o.html#a4">00072</a> <span class="keywordtype">void</span> <a class="code" href="class_e_o.html#a2">fitness</a>(<span class="keyword">const</span> Fitness& _fitness)
|
||||
00073 {
|
||||
00074 repFitness = _fitness;
|
||||
00075 invalidFitness = <span class="keyword">false</span>;
|
||||
00076 }
|
||||
00077
|
||||
<a name="l00081"></a><a class="code" href="class_e_o.html#a5">00081</a> <span class="keywordtype">bool</span> <a class="code" href="class_e_o.html#a5">invalid</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> invalidFitness; }
|
||||
00082
|
||||
<a name="l00086"></a><a class="code" href="class_e_o.html#a6">00086</a> <span class="keywordtype">bool</span> <a class="code" href="class_e_o.html#a6">operator<</a>(<span class="keyword">const</span> <a class="code" href="class_e_o.html">EO</a>& _eo2)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <a class="code" href="class_e_o.html#a2">fitness</a>() < _eo2.<a class="code" href="class_e_o.html#a2">fitness</a>(); }
|
||||
00087 <span class="keywordtype">bool</span> operator>(<span class="keyword">const</span> <a class="code" href="class_e_o.html">EO</a>& _eo2)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> !(<a class="code" href="class_e_o.html#a2">fitness</a>() <= _eo2.<a class="code" href="class_e_o.html#a2">fitness</a>()); }
|
||||
00088
|
||||
00090
|
||||
00091
|
||||
<a name="l00095"></a><a class="code" href="class_e_o.html#z10_0">00095</a> <span class="keyword">virtual</span> std::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">"EO"</span>; }
|
||||
00096
|
||||
<a name="l00105"></a><a class="code" href="class_e_o.html#z10_1">00105</a> <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="class_e_o.html#z10_1">readFrom</a>(std::istream& _is) {
|
||||
00106
|
||||
00107 <span class="comment">// the new version of the reafFrom function.</span>
|
||||
00108 <span class="comment">// It can distinguish between valid and invalid fitness values. </span>
|
||||
00109 std::string fitness_str;
|
||||
00110 <span class="keywordtype">int</span> pos = _is.tellg();
|
||||
00111 _is >> fitness_str;
|
||||
00112
|
||||
00113 <span class="keywordflow">if</span> (fitness_str == <span class="stringliteral">"INVALID"</span>)
|
||||
00114 {
|
||||
00115 invalidFitness = <span class="keyword">true</span>;
|
||||
00116 }
|
||||
00117 <span class="keywordflow">else</span>
|
||||
00118 {
|
||||
00119 invalidFitness = <span class="keyword">false</span>;
|
||||
00120 _is.seekg(pos); <span class="comment">// rewind</span>
|
||||
00121 _is >> repFitness;
|
||||
00122 }
|
||||
00123 }
|
||||
00124
|
||||
<a name="l00129"></a><a class="code" href="class_e_o.html#z10_2">00129</a> <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="class_e_o.html#z10_2">printOn</a>(std::ostream& _os)<span class="keyword"> const </span>{
|
||||
00130
|
||||
00131
|
||||
00132 <span class="comment">// the latest version of the code. Very similar to the old code</span>
|
||||
00133 <span class="keywordflow">if</span> (<a class="code" href="class_e_o.html#a5">invalid</a>()) {
|
||||
00134 _os << <span class="stringliteral">"INVALID "</span>;
|
||||
00135 }
|
||||
00136 <span class="keywordflow">else</span>
|
||||
00137 {
|
||||
00138 _os << repFitness << <span class="charliteral">' '</span>;
|
||||
00139 }
|
||||
00140
|
||||
00141 }
|
||||
00142
|
||||
00144
|
||||
00145 <span class="keyword">private</span>:
|
||||
00146 Fitness repFitness; <span class="comment">// value of fitness for this chromosome</span>
|
||||
00147 <span class="keywordtype">bool</span> invalidFitness; <span class="comment">// true if the value of fitness is invalid</span>
|
||||
00148 };
|
||||
00149
|
||||
00150 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00151
|
||||
00152 <span class="preprocessor">#endif</span>
|
||||
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:34 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>
|
||||
139
trunk/paradiseo-eo/doc/html/_e_s_e_a_8cpp-source.html
Normal file
139
trunk/paradiseo-eo/doc/html/_e_s_e_a_8cpp-source.html
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
<!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: ESEA.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 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_000003.html">tutorial</a> / <a class="el" href="dir_000006.html">Lesson4</a></div>
|
||||
<h1>ESEA.cpp</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">// Program to test several EO-ES features</span>
|
||||
00002
|
||||
00003 <span class="preprocessor">#ifdef _MSC_VER</span>
|
||||
00004 <span class="preprocessor"></span><span class="preprocessor">#pragma warning(disable:4786)</span>
|
||||
00005 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
|
||||
00006 <span class="preprocessor"></span>
|
||||
00007 <span class="preprocessor">#include <algorithm></span>
|
||||
00008 <span class="preprocessor">#include <string></span>
|
||||
00009 <span class="preprocessor">#include <iostream></span>
|
||||
00010 <span class="preprocessor">#include <iterator></span>
|
||||
00011 <span class="preprocessor">#include <stdexcept></span>
|
||||
00012 <span class="preprocessor">#include <time.h></span>
|
||||
00013
|
||||
00014 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
|
||||
00015
|
||||
00016 <span class="preprocessor">#include <eo></span>
|
||||
00017
|
||||
00018 <span class="comment">// representation specific</span>
|
||||
00019 <span class="preprocessor">#include <es/make_es.h></span>
|
||||
00020
|
||||
00021 <span class="preprocessor">#include "real_value.h"</span> <span class="comment">// the sphere fitness</span>
|
||||
00022
|
||||
00023 <span class="comment">// Now the main </span>
|
||||
00025 <span class="comment"></span><span class="keyword">typedef</span> <a class="code" href="classeo_scalar_fitness.html">eoMinimizingFitness</a> FitT;
|
||||
00026
|
||||
00027 <span class="keyword">template</span> <<span class="keyword">class</span> EOT>
|
||||
00028 <span class="keywordtype">void</span> runAlgorithm(<a class="code" href="struct_dummy.html">EOT</a>, eoParser& _parser, <a class="code" href="classeo_state.html">eoState</a>& _state);
|
||||
00029
|
||||
00030 <span class="keywordtype">int</span> main_function(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])
|
||||
00031 {
|
||||
00032 <span class="comment">// Create the command-line parser</span>
|
||||
00033 eoParser parser(argc, argv); <span class="comment">// for user-parameter reading</span>
|
||||
00034
|
||||
00035 <a class="code" href="classeo_state.html">eoState</a> state; <span class="comment">// keeps all things allocated</span>
|
||||
00036
|
||||
00037
|
||||
00038 <a class="code" href="classeo_value_param.html">eoValueParam<bool></a>& simpleParam = parser.createParam(<span class="keyword">true</span>, <span class="stringliteral">"Isotropic"</span>, <span class="stringliteral">"Isotropic self-adaptive mutation"</span>, <span class="charliteral">'i'</span>, <span class="stringliteral">"ES mutation"</span>);
|
||||
00039 <a class="code" href="classeo_value_param.html">eoValueParam<bool></a>& stdevsParam = parser.createParam(<span class="keyword">false</span>, <span class="stringliteral">"Stdev"</span>, <span class="stringliteral">"One self-adaptive stDev per variable"</span>, <span class="charliteral">'s'</span>, <span class="stringliteral">"ES mutation"</span>);
|
||||
00040 <a class="code" href="classeo_value_param.html">eoValueParam<bool></a>& corrParam = parser.createParam(<span class="keyword">false</span>, <span class="stringliteral">"Correl"</span>, <span class="stringliteral">"Use correlated mutations"</span>, <span class="charliteral">'c'</span>, <span class="stringliteral">"ES mutation"</span>);
|
||||
00041
|
||||
00042 <span class="comment">// Run the appropriate algorithm</span>
|
||||
00043 <span class="keywordflow">if</span> (simpleParam.<a class="code" href="classeo_value_param.html#a2">value</a>() == <span class="keyword">false</span>)
|
||||
00044 {
|
||||
00045 cout << <span class="stringliteral">"Using eoReal"</span> << endl;
|
||||
00046 runAlgorithm(<a class="code" href="classeo_real.html">eoReal<FitT></a>(), parser, state);
|
||||
00047 }
|
||||
00048 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (stdevsParam.<a class="code" href="classeo_value_param.html#a2">value</a>() == <span class="keyword">false</span>)
|
||||
00049 {
|
||||
00050 cout << <span class="stringliteral">"Using eoEsSimple"</span> << endl;
|
||||
00051 runAlgorithm(<a class="code" href="classeo_es_simple.html">eoEsSimple<FitT></a>(), parser, state);
|
||||
00052 }
|
||||
00053 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (corrParam.<a class="code" href="classeo_value_param.html#a2">value</a>() == <span class="keyword">false</span>)
|
||||
00054 {
|
||||
00055 cout << <span class="stringliteral">"Using eoEsStdev"</span> << endl;
|
||||
00056 runAlgorithm(<a class="code" href="classeo_es_stdev.html">eoEsStdev<FitT></a>(), parser, state);
|
||||
00057 }
|
||||
00058 <span class="keywordflow">else</span>
|
||||
00059 {
|
||||
00060 cout << <span class="stringliteral">"Using eoEsFull"</span> << endl;
|
||||
00061 runAlgorithm(<a class="code" href="classeo_es_full.html">eoEsFull<FitT></a>(), parser, state);
|
||||
00062 }
|
||||
00063
|
||||
00064 <span class="keywordflow">return</span> 0;
|
||||
00065 }
|
||||
00066
|
||||
00067 <span class="comment">// A main that catches the exceptions</span>
|
||||
00068
|
||||
00069 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
|
||||
00070 {
|
||||
00071 <span class="keywordflow">try</span>
|
||||
00072 {
|
||||
00073 main_function(argc, argv);
|
||||
00074 }
|
||||
00075 <span class="keywordflow">catch</span>(exception& e)
|
||||
00076 {
|
||||
00077 cout << <span class="stringliteral">"Exception: "</span> << e.what() << <span class="charliteral">'\n'</span>;
|
||||
00078 }
|
||||
00079
|
||||
00080 <span class="keywordflow">return</span> 1;
|
||||
00081 }
|
||||
00082
|
||||
00087 <span class="keyword">template</span> <<span class="keyword">class</span> EOT>
|
||||
00088 <span class="keywordtype">void</span> runAlgorithm(<a class="code" href="struct_dummy.html">EOT</a>, eoParser& _parser, <a class="code" href="classeo_state.html">eoState</a>& _state)
|
||||
00089 {
|
||||
00090 <span class="keyword">typedef</span> <span class="keyword">typename</span> EOT::Fitness FitT;
|
||||
00091
|
||||
00094
|
||||
00095 <span class="comment">// The evaluation fn - encapsulated into an eval counter for output </span>
|
||||
00096 <a class="code" href="structeo_eval_func_ptr.html">eoEvalFuncPtr<EOT, double, const std::vector<double></a>&>
|
||||
00097 mainEval( real_value );
|
||||
00098 <a class="code" href="classeo_eval_func_counter.html">eoEvalFuncCounter<EOT></a> eval(mainEval);
|
||||
00099
|
||||
00100 <span class="comment">// the genotype - through a genotype initializer</span>
|
||||
00101 <a class="code" href="classeo_real_init_bounded.html">eoRealInitBounded<EOT></a>& init = make_genotype(_parser, _state, <a class="code" href="struct_dummy.html">EOT</a>());
|
||||
00102
|
||||
00103 <span class="comment">// Build the variation operator (any seq/prop construct)</span>
|
||||
00104 <a class="code" href="classeo_gen_op.html">eoGenOp<EOT></a>& op = make_op(_parser, _state, init);
|
||||
00105
|
||||
00108
|
||||
00109 <span class="comment">// initialize the population - and evaluate</span>
|
||||
00110 <span class="comment">// yes, this is representation indepedent once you have an eoInit</span>
|
||||
00111 <a class="code" href="classeo_pop.html">eoPop<EOT></a>& pop = make_pop(_parser, _state, init);
|
||||
00112 apply<EOT>(eval, pop);
|
||||
00113
|
||||
00114 <span class="comment">// stopping criteria</span>
|
||||
00115 <a class="code" href="classeo_continue.html">eoContinue<EOT></a> & term = make_continue(_parser, _state, eval);
|
||||
00116 <span class="comment">// output</span>
|
||||
00117 <a class="code" href="classeo_check_point.html">eoCheckPoint<EOT></a> & checkpoint = make_checkpoint(_parser, _state, eval, term);
|
||||
00118 <span class="comment">// algorithm (need the operator!)</span>
|
||||
00119 <a class="code" href="classeo_algo.html">eoAlgo<EOT></a>& ga = make_algo_scalar(_parser, _state, eval, checkpoint, op);
|
||||
00120
|
||||
00123 <span class="comment">// to be called AFTER all parameters have been read!!!</span>
|
||||
00124 make_help(_parser);
|
||||
00125
|
||||
00128 cout << <span class="stringliteral">"Initial Population\n"</span>;
|
||||
00129 pop.<a class="code" href="classeo_pop.html#a17">sortedPrintOn</a>(cout);
|
||||
00130 cout << endl;
|
||||
00131
|
||||
00132 run_ea(ga, pop); <span class="comment">// run the ga</span>
|
||||
00133
|
||||
00134 cout << <span class="stringliteral">"Final Population\n"</span>;
|
||||
00135 pop.<a class="code" href="classeo_pop.html#a17">sortedPrintOn</a>(cout);
|
||||
00136 cout << endl;
|
||||
00137 }
|
||||
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:39 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>
|
||||
355
trunk/paradiseo-eo/doc/html/_error_measure_8cpp-source.html
Normal file
355
trunk/paradiseo-eo/doc/html/_error_measure_8cpp-source.html
Normal file
|
|
@ -0,0 +1,355 @@
|
|||
<!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: ErrorMeasure.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 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_000007.html">contrib</a> / <a class="el" href="dir_000008.html">mathsym</a> / <a class="el" href="dir_000012.html">regression</a></div>
|
||||
<h1>ErrorMeasure.cpp</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">/* </span>
|
||||
00002 <span class="comment"> * Copyright (C) 2005 Maarten Keijzer</span>
|
||||
00003 <span class="comment"> *</span>
|
||||
00004 <span class="comment"> * This program is free software; you can redistribute it and/or modify</span>
|
||||
00005 <span class="comment"> * it under the terms of version 2 of the GNU General Public License as </span>
|
||||
00006 <span class="comment"> * published by the Free Software Foundation. </span>
|
||||
00007 <span class="comment"> *</span>
|
||||
00008 <span class="comment"> * This program is distributed in the hope that it will be useful,</span>
|
||||
00009 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
|
||||
00010 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
|
||||
00011 <span class="comment"> * GNU General Public License for more details.</span>
|
||||
00012 <span class="comment"> *</span>
|
||||
00013 <span class="comment"> * You should have received a copy of the GNU General Public License</span>
|
||||
00014 <span class="comment"> * along with this program; if not, write to the Free Software</span>
|
||||
00015 <span class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
|
||||
00016 <span class="comment"> */</span>
|
||||
00017
|
||||
00018
|
||||
00019 <span class="preprocessor">#include <vector></span>
|
||||
00020 <span class="preprocessor">#include <valarray></span>
|
||||
00021
|
||||
00022 <span class="preprocessor">#include "MultiFunction.h"</span>
|
||||
00023
|
||||
00024 <span class="preprocessor">#include "ErrorMeasure.h"</span>
|
||||
00025 <span class="preprocessor">#include "Dataset.h"</span>
|
||||
00026 <span class="preprocessor">#include "Sym.h"</span>
|
||||
00027 <span class="preprocessor">#include "FunDef.h"</span>
|
||||
00028 <span class="preprocessor">#include "sym_compile.h"</span>
|
||||
00029 <span class="preprocessor">#include "TargetInfo.h"</span>
|
||||
00030 <span class="preprocessor">#include "stats.h"</span>
|
||||
00031
|
||||
00032 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
|
||||
00033
|
||||
00034 <span class="preprocessor">#ifdef INTERVAL_DEBUG</span>
|
||||
00035 <span class="preprocessor"></span>
|
||||
00036 <span class="preprocessor">#include <BoundsCheck.h></span>
|
||||
00037 <span class="preprocessor">#include <FunDef.h></span>
|
||||
00038
|
||||
00039 vector<double> none;
|
||||
00040 IntervalBoundsCheck bounds(none, none);
|
||||
00041
|
||||
00042 <span class="preprocessor">#endif</span>
|
||||
00043 <span class="preprocessor"></span>
|
||||
00044
|
||||
00045
|
||||
00046 <span class="keyword">static</span> <span class="keywordtype">double</span> not_a_number = atof(<span class="stringliteral">"nan"</span>);
|
||||
00047
|
||||
00048 <span class="keyword">class </span>ErrorMeasureImpl {
|
||||
00049 <span class="keyword">public</span>:
|
||||
00050 <span class="keyword">const</span> Dataset& data;
|
||||
00051 TargetInfo train_info;
|
||||
00052
|
||||
00053 ErrorMeasure::measure measure;
|
||||
00054
|
||||
00055 Scaling no_scaling;
|
||||
00056
|
||||
00057 ErrorMeasureImpl(<span class="keyword">const</span> Dataset& d, <span class="keywordtype">double</span> t_p, ErrorMeasure::measure m) : data(d), measure(m) {
|
||||
00058
|
||||
00059 <span class="preprocessor">#ifdef INTERVAL_DEBUG</span>
|
||||
00060 <span class="preprocessor"></span> bounds = IntervalBoundsCheck(d.input_minima(), d.input_maxima());
|
||||
00061 <span class="preprocessor">#endif</span>
|
||||
00062 <span class="preprocessor"></span>
|
||||
00063 <span class="keywordtype">unsigned</span> nrecords = d.n_records();
|
||||
00064 <span class="keywordtype">unsigned</span> cases = unsigned(t_p * nrecords);
|
||||
00065
|
||||
00066 valarray<double> t(cases);
|
||||
00067
|
||||
00068 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < cases; ++i) {
|
||||
00069 t[i] = data.get_target(i);
|
||||
00070 }
|
||||
00071
|
||||
00072 train_info = TargetInfo(t);
|
||||
00073 no_scaling = Scaling(<span class="keyword">new</span> NoScaling);
|
||||
00074 }
|
||||
00075
|
||||
00076 ErrorMeasure::result eval(<span class="keyword">const</span> valarray<double>& y) {
|
||||
00077
|
||||
00078 ErrorMeasure::result result;
|
||||
00079 result.scaling = no_scaling;
|
||||
00080
|
||||
00081
|
||||
00082 <span class="keywordflow">switch</span>(measure) {
|
||||
00083 <span class="keywordflow">case</span> ErrorMeasure::mean_squared:
|
||||
00084 result.error = pow(train_info.targets() - y, 2.0).sum() / y.size();
|
||||
00085 <span class="keywordflow">return</span> result;
|
||||
00086 <span class="keywordflow">case</span> ErrorMeasure::absolute:
|
||||
00087 result.error = abs(train_info.targets() - y).sum() / y.size();
|
||||
00088 <span class="keywordflow">return</span> result;
|
||||
00089 <span class="keywordflow">case</span> ErrorMeasure::mean_squared_scaled:
|
||||
00090 result.scaling = ols(y, train_info);
|
||||
00091 result.error = pow(train_info.targets() - result.scaling->transform(y), 2.0).sum() / y.size();
|
||||
00092 <span class="keywordflow">return</span> result;
|
||||
00093 <span class="keywordflow">default</span>:
|
||||
00094 cerr << <span class="stringliteral">"Unknown measure encountered: "</span> << measure << <span class="stringliteral">" "</span> << __FILE__ << <span class="stringliteral">" "</span> << __LINE__ << endl;
|
||||
00095 }
|
||||
00096
|
||||
00097 <span class="keywordflow">return</span> result;
|
||||
00098 }
|
||||
00099
|
||||
00100 <span class="keywordtype">unsigned</span> train_cases()<span class="keyword"> const </span>{
|
||||
00101 <span class="keywordflow">return</span> train_info.targets().size();
|
||||
00102 }
|
||||
00103
|
||||
00104 vector<ErrorMeasure::result> multi_function_eval(<span class="keyword">const</span> vector<Sym>& pop) {
|
||||
00105
|
||||
00106 <span class="keywordflow">if</span> (pop.size() == 0) <span class="keywordflow">return</span> vector<ErrorMeasure::result>();
|
||||
00107
|
||||
00108 multi_function all = compile(pop);
|
||||
00109 <span class="comment">//MultiFunction all(pop);</span>
|
||||
00110 std::vector<double> y(pop.size());
|
||||
00111
|
||||
00112 Scaling noScaling = Scaling(<span class="keyword">new</span> NoScaling);
|
||||
00113
|
||||
00114 <span class="keyword">const</span> std::valarray<double>& t = train_info.targets();
|
||||
00115
|
||||
00116 cout << <span class="stringliteral">"Population size "</span> << pop.size() << endl;
|
||||
00117
|
||||
00118 <span class="keywordflow">if</span> (measure == ErrorMeasure::mean_squared_scaled) {
|
||||
00119 std::vector<Var> var(pop.size());
|
||||
00120 std::vector<Cov> cov(pop.size());
|
||||
00121
|
||||
00122 Var vart;
|
||||
00123
|
||||
00124 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < t.size(); ++i) {
|
||||
00125 vart.update(t[i]);
|
||||
00126
|
||||
00127 all(&data.get_inputs(i)[0], &y[0]); <span class="comment">// evalutate</span>
|
||||
00128 <span class="comment">//all(data.get_inputs(i), y); // evalutate</span>
|
||||
00129
|
||||
00130 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j < pop.size(); ++j) {
|
||||
00131 var[j].update(y[j]);
|
||||
00132 cov[j].update(y[j], t[i]);
|
||||
00133 }
|
||||
00134 }
|
||||
00135
|
||||
00136 std::vector<ErrorMeasure::result> result(pop.size());
|
||||
00137
|
||||
00138 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < pop.size(); ++i) {
|
||||
00139
|
||||
00140 <span class="comment">// calculate scaling</span>
|
||||
00141 <span class="keywordtype">double</span> b = cov[i].get_cov() / var[i].get_var();
|
||||
00142
|
||||
00143 <span class="keywordflow">if</span> (!finite(b)) {
|
||||
00144 result[i].scaling = noScaling;
|
||||
00145 result[i].error = vart.get_var(); <span class="comment">// largest error</span>
|
||||
00146 <span class="keywordflow">continue</span>;
|
||||
00147 }
|
||||
00148
|
||||
00149 <span class="keywordtype">double</span> a = vart.get_mean() - b * var[i].get_mean();
|
||||
00150
|
||||
00151 result[i].scaling = Scaling( <span class="keyword">new</span> LinearScaling(a,b));
|
||||
00152
|
||||
00153 <span class="comment">// calculate error</span>
|
||||
00154 <span class="keywordtype">double</span> c = cov[i].get_cov();
|
||||
00155 c *= c;
|
||||
00156
|
||||
00157 <span class="keywordtype">double</span> err = vart.get_var() - c / var[i].get_var();
|
||||
00158 result[i].error = err;
|
||||
00159 <span class="keywordflow">if</span> (!finite(err)) {
|
||||
00160 <span class="comment">//cout << pop[i] << endl;</span>
|
||||
00161 cout << <span class="stringliteral">"b "</span> << b << endl;
|
||||
00162 cout << <span class="stringliteral">"var t "</span> << vart.get_var() << endl;
|
||||
00163 cout << <span class="stringliteral">"var i "</span> << var[i].get_var() << endl;
|
||||
00164 cout << <span class="stringliteral">"cov "</span> << cov[i].get_cov() << endl;
|
||||
00165
|
||||
00166 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j < t.size(); ++j) {
|
||||
00167 all(&data.get_inputs(i)[0], &y[0]); <span class="comment">// evalutate</span>
|
||||
00168 <span class="comment">//all(data.get_inputs(j), y); // evalutate</span>
|
||||
00169
|
||||
00170 cout << y[i] << <span class="charliteral">' '</span> << ::eval(pop[i], data.get_inputs(j)) << endl;
|
||||
00171 }
|
||||
00172
|
||||
00173 exit(1);
|
||||
00174 }
|
||||
00175 }
|
||||
00176
|
||||
00177 <span class="keywordflow">return</span> result;
|
||||
00178 }
|
||||
00179
|
||||
00180
|
||||
00181 std::vector<double> err(pop.size());
|
||||
00182
|
||||
00183 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < train_cases(); ++i) {
|
||||
00184 <span class="comment">// evaluate</span>
|
||||
00185 all(&data.get_inputs(i)[0], &y[0]);
|
||||
00186 <span class="comment">//all(data.get_inputs(i), y);</span>
|
||||
00187
|
||||
00188 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j < pop.size(); ++j) {
|
||||
00189 <span class="keywordtype">double</span> diff = y[j] - t[i];
|
||||
00190 <span class="keywordflow">if</span> (measure == ErrorMeasure::mean_squared) { <span class="comment">// branch prediction will probably solve this inefficiency</span>
|
||||
00191 err[j] += diff * diff;
|
||||
00192 } <span class="keywordflow">else</span> {
|
||||
00193 err[j] += fabs(diff);
|
||||
00194 }
|
||||
00195
|
||||
00196 }
|
||||
00197
|
||||
00198 }
|
||||
00199
|
||||
00200 std::vector<ErrorMeasure::result> result(pop.size());
|
||||
00201
|
||||
00202 <span class="keywordtype">double</span> n = train_cases();
|
||||
00203 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < pop.size(); ++i) {
|
||||
00204 result[i].error = err[i] / n;
|
||||
00205 result[i].scaling = noScaling;
|
||||
00206 }
|
||||
00207
|
||||
00208 <span class="keywordflow">return</span> result;
|
||||
00209
|
||||
00210 }
|
||||
00211
|
||||
00212 vector<ErrorMeasure::result> single_function_eval(<span class="keyword">const</span> vector<Sym> & pop) {
|
||||
00213
|
||||
00214 vector<single_function> funcs(pop.size());
|
||||
00215 compile(pop, funcs); <span class="comment">// get one function pointer for each individual</span>
|
||||
00216
|
||||
00217 valarray<double> y(train_cases());
|
||||
00218 vector<ErrorMeasure::result> result(pop.size());
|
||||
00219 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < funcs.size(); ++i) {
|
||||
00220 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j < train_cases(); ++j) {
|
||||
00221 y[j] = funcs[i](&data.get_inputs(j)[0]);
|
||||
00222 }
|
||||
00223
|
||||
00224 <span class="preprocessor">#ifdef INTERVAL_DEBUG</span>
|
||||
00225 <span class="preprocessor"></span> <span class="comment">//cout << "eval func " << i << " " << pop[i] << endl;</span>
|
||||
00226 pair<double, double> b = bounds.calc_bounds(pop[i]);
|
||||
00227
|
||||
00228 <span class="comment">// check if y is in bounds</span>
|
||||
00229 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j < y.size(); ++j) {
|
||||
00230 <span class="keywordflow">if</span> (y[j] < b.first -1e-4 || y[j] > b.second + 1e-4 || !finite(y[j])) {
|
||||
00231 cout << <span class="stringliteral">"Error "</span> << y[j] << <span class="stringliteral">" not in "</span> << b.first << <span class="charliteral">' '</span> << b.second << endl;
|
||||
00232 cout << <span class="stringliteral">"Function "</span> << pop[i] << endl;
|
||||
00233 exit(1);
|
||||
00234 }
|
||||
00235 }
|
||||
00236 <span class="preprocessor">#endif</span>
|
||||
00237 <span class="preprocessor"></span>
|
||||
00238 result[i] = eval(y);
|
||||
00239 }
|
||||
00240
|
||||
00241 <span class="keywordflow">return</span> result;
|
||||
00242 }
|
||||
00243
|
||||
00244 vector<ErrorMeasure::result> calc_error(<span class="keyword">const</span> vector<Sym>& pop) {
|
||||
00245
|
||||
00246 <span class="comment">// first declone</span>
|
||||
00247 <span class="preprocessor">#if USE_TR1</span>
|
||||
00248 <span class="preprocessor"></span> <span class="keyword">typedef</span> std::tr1::unordered_map<Sym, unsigned, HashSym> HashMap;
|
||||
00249 <span class="preprocessor">#else</span>
|
||||
00250 <span class="preprocessor"></span> <span class="keyword">typedef</span> hash_map<Sym, unsigned, HashSym> HashMap;
|
||||
00251 <span class="preprocessor">#endif </span>
|
||||
00252 <span class="preprocessor"></span> HashMap clone_map;
|
||||
00253 vector<Sym> decloned;
|
||||
00254 decloned.reserve(pop.size());
|
||||
00255
|
||||
00256 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < pop.size(); ++i) {
|
||||
00257 HashMap::iterator it = clone_map.find(pop[i]);
|
||||
00258
|
||||
00259 <span class="keywordflow">if</span> (it == clone_map.end()) { <span class="comment">// new</span>
|
||||
00260 clone_map[ pop[i] ] = decloned.size();
|
||||
00261 decloned.push_back(pop[i]);
|
||||
00262 }
|
||||
00263
|
||||
00264 }
|
||||
00265
|
||||
00266 <span class="comment">// evaluate </span>
|
||||
00267 vector<ErrorMeasure::result> dresult;
|
||||
00268 <span class="comment">// currently we can only accumulate simple measures such as absolute and mean_squared</span>
|
||||
00269 <span class="keywordflow">switch</span>(measure) {
|
||||
00270 <span class="keywordflow">case</span> ErrorMeasure::mean_squared:
|
||||
00271 <span class="keywordflow">case</span> ErrorMeasure::absolute:
|
||||
00272 dresult = multi_function_eval(decloned);
|
||||
00273 <span class="keywordflow">break</span>;
|
||||
00274 <span class="keywordflow">case</span> ErrorMeasure::mean_squared_scaled:
|
||||
00275 dresult = multi_function_eval(decloned);
|
||||
00276 <span class="keywordflow">break</span>;
|
||||
00277 }
|
||||
00278
|
||||
00279 vector<ErrorMeasure::result> result(pop.size());
|
||||
00280 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < result.size(); ++i) {
|
||||
00281 result[i] = dresult[ clone_map[pop[i]] ];
|
||||
00282 }
|
||||
00283
|
||||
00284 <span class="keywordflow">return</span> result;
|
||||
00285 }
|
||||
00286
|
||||
00287 };
|
||||
00288
|
||||
00289 ErrorMeasure::result::result() {
|
||||
00290 error = 0.0;
|
||||
00291 scaling = Scaling(0);
|
||||
00292 }
|
||||
00293
|
||||
00294 <span class="keywordtype">bool</span> ErrorMeasure::result::valid()<span class="keyword"> const </span>{
|
||||
00295 <span class="keywordflow">return</span> isfinite(error);
|
||||
00296 }
|
||||
00297
|
||||
00298 ErrorMeasure::ErrorMeasure(<span class="keyword">const</span> Dataset& data, <span class="keywordtype">double</span> train_perc, measure meas) {
|
||||
00299 pimpl = <span class="keyword">new</span> ErrorMeasureImpl(data, train_perc, meas);
|
||||
00300 }
|
||||
00301
|
||||
00302 ErrorMeasure::~ErrorMeasure() { <span class="keyword">delete</span> pimpl; }
|
||||
00303 ErrorMeasure::ErrorMeasure(<span class="keyword">const</span> ErrorMeasure& that) { pimpl = <span class="keyword">new</span> ErrorMeasureImpl(*that.pimpl); }
|
||||
00304
|
||||
00305
|
||||
00306 ErrorMeasure::result ErrorMeasure::calc_error(Sym sym) {
|
||||
00307
|
||||
00308 single_function f = compile(sym);
|
||||
00309
|
||||
00310 valarray<double> y(pimpl->train_cases());
|
||||
00311
|
||||
00312 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < y.size(); ++i) {
|
||||
00313
|
||||
00314 y[i] = f(&pimpl->data.get_inputs(i)[0]);
|
||||
00315
|
||||
00316 <span class="keywordflow">if</span> (!finite(y[i])) {
|
||||
00317 result res;
|
||||
00318 res.scaling = Scaling(<span class="keyword">new</span> NoScaling);
|
||||
00319 res.error = not_a_number;
|
||||
00320 <span class="keywordflow">return</span> res;
|
||||
00321 }
|
||||
00322 }
|
||||
00323
|
||||
00324 <span class="keywordflow">return</span> pimpl->eval(y);
|
||||
00325 }
|
||||
00326
|
||||
00327 vector<ErrorMeasure::result> ErrorMeasure::calc_error(<span class="keyword">const</span> vector<Sym>& syms) {
|
||||
00328 <span class="keywordflow">return</span> pimpl->calc_error(syms);
|
||||
00329
|
||||
00330 }
|
||||
00331
|
||||
00332 <span class="keywordtype">double</span> ErrorMeasure::worst_performance()<span class="keyword"> const </span>{
|
||||
00333
|
||||
00334 <span class="keywordflow">if</span> (pimpl->measure == mean_squared_scaled) {
|
||||
00335 <span class="keywordflow">return</span> pimpl->train_info.tvar();
|
||||
00336 }
|
||||
00337
|
||||
00338 <span class="keywordflow">return</span> 1e+20;
|
||||
00339 }
|
||||
00340
|
||||
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:39 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>
|
||||
76
trunk/paradiseo-eo/doc/html/_error_measure_8h-source.html
Normal file
76
trunk/paradiseo-eo/doc/html/_error_measure_8h-source.html
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
<!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: ErrorMeasure.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_000007.html">contrib</a> / <a class="el" href="dir_000008.html">mathsym</a> / <a class="el" href="dir_000012.html">regression</a></div>
|
||||
<h1>ErrorMeasure.h</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">/* </span>
|
||||
00002 <span class="comment"> * Copyright (C) 2005 Maarten Keijzer</span>
|
||||
00003 <span class="comment"> *</span>
|
||||
00004 <span class="comment"> * This program is free software; you can redistribute it and/or modify</span>
|
||||
00005 <span class="comment"> * it under the terms of version 2 of the GNU General Public License as </span>
|
||||
00006 <span class="comment"> * published by the Free Software Foundation. </span>
|
||||
00007 <span class="comment"> *</span>
|
||||
00008 <span class="comment"> * This program is distributed in the hope that it will be useful,</span>
|
||||
00009 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
|
||||
00010 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
|
||||
00011 <span class="comment"> * GNU General Public License for more details.</span>
|
||||
00012 <span class="comment"> *</span>
|
||||
00013 <span class="comment"> * You should have received a copy of the GNU General Public License</span>
|
||||
00014 <span class="comment"> * along with this program; if not, write to the Free Software</span>
|
||||
00015 <span class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
|
||||
00016 <span class="comment"> */</span>
|
||||
00017
|
||||
00018 <span class="preprocessor">#ifndef ERROR_MEASURE_H</span>
|
||||
00019 <span class="preprocessor"></span><span class="preprocessor">#define ERROR_MEASURE_H</span>
|
||||
00020 <span class="preprocessor"></span>
|
||||
00021 <span class="preprocessor">#include "Scaling.h"</span>
|
||||
00022
|
||||
00023 <span class="keyword">class </span>ErrorMeasureImpl;
|
||||
00024 <span class="keyword">class </span>Sym;
|
||||
00025 <span class="keyword">class </span>Dataset;
|
||||
00026
|
||||
00027 <span class="keyword">class </span>ErrorMeasure {
|
||||
00028
|
||||
00029 ErrorMeasureImpl* pimpl;
|
||||
00030
|
||||
00031 <span class="keyword">public</span> :
|
||||
00032
|
||||
00033 <span class="keyword">enum</span> measure {
|
||||
00034 absolute,
|
||||
00035 mean_squared,
|
||||
00036 mean_squared_scaled,
|
||||
00037 };
|
||||
00038
|
||||
00039 <span class="keyword">struct </span>result {
|
||||
00040 <span class="keywordtype">double</span> error;
|
||||
00041 Scaling scaling;
|
||||
00042
|
||||
00043 result();
|
||||
00044 <span class="keywordtype">bool</span> valid() <span class="keyword">const</span>;
|
||||
00045 };
|
||||
00046
|
||||
00047 ErrorMeasure(<span class="keyword">const</span> Dataset& data, <span class="keywordtype">double</span> train_perc, measure meas = mean_squared);
|
||||
00048
|
||||
00049 ~ErrorMeasure();
|
||||
00050 ErrorMeasure(<span class="keyword">const</span> ErrorMeasure& that);
|
||||
00051 ErrorMeasure& operator=(<span class="keyword">const</span> ErrorMeasure& that);
|
||||
00052
|
||||
00053 result calc_error(Sym sym);
|
||||
00054
|
||||
00055 std::vector<result> calc_error(<span class="keyword">const</span> std::vector<Sym>& sym);
|
||||
00056
|
||||
00057 <span class="keywordtype">double</span> worst_performance() <span class="keyword">const</span>;
|
||||
00058 };
|
||||
00059
|
||||
00060 <span class="preprocessor">#endif</span>
|
||||
00061 <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
|
||||
<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>
|
||||
194
trunk/paradiseo-eo/doc/html/_first_bit_e_a_8cpp-source.html
Normal file
194
trunk/paradiseo-eo/doc/html/_first_bit_e_a_8cpp-source.html
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
<!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: FirstBitEA.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 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_000003.html">tutorial</a> / <a class="el" href="dir_000004.html">Lesson2</a></div>
|
||||
<h1>FirstBitEA.cpp</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00002 <span class="comment">// FirstBitEA.cpp</span>
|
||||
00003 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00004 <span class="comment">//*</span>
|
||||
00005 <span class="comment">// Still an instance of a VERY simple Bitstring Genetic Algorithm</span>
|
||||
00006 <span class="comment">// (see FirstBitGA.cpp) but now with Breeder - and Combined Ops</span>
|
||||
00007 <span class="comment">//</span>
|
||||
00008 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00009 <span class="preprocessor">#ifdef HAVE_CONFIG_H</span>
|
||||
00010 <span class="preprocessor"></span><span class="preprocessor">#include <config.h></span>
|
||||
00011 <span class="preprocessor">#endif</span>
|
||||
00012 <span class="preprocessor"></span>
|
||||
00013 <span class="comment">// standard includes</span>
|
||||
00014 <span class="preprocessor">#include <stdexcept></span> <span class="comment">// runtime_error</span>
|
||||
00015 <span class="preprocessor">#include <iostream></span> <span class="comment">// cout</span>
|
||||
00016
|
||||
00017 <span class="comment">// the general include for eo</span>
|
||||
00018 <span class="preprocessor">#include <eo></span>
|
||||
00019 <span class="preprocessor">#include <ga.h></span>
|
||||
00020
|
||||
00021 <span class="comment">// Use functions from namespace std</span>
|
||||
00022 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
|
||||
00023
|
||||
00024 <span class="comment">// REPRESENTATION</span>
|
||||
00025 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00026 <span class="comment">// define your individuals</span>
|
||||
00027 <span class="keyword">typedef</span> <a class="code" href="classeo_bit.html">eoBit<double></a> <a class="code" href="classeo_real.html">Indi</a>; <span class="comment">// A bitstring with fitness double</span>
|
||||
00028
|
||||
00029 <span class="comment">// EVALFUNC</span>
|
||||
00030 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00031 <span class="comment">// a simple fitness function that computes the number of ones of a bitstring</span>
|
||||
00032 <span class="comment">// Now in a separate file, and declared as binary_value(const vector<bool> &)</span>
|
||||
00033
|
||||
00034 <span class="preprocessor">#include "binary_value.h"</span>
|
||||
00035
|
||||
00036 <span class="comment">// GENERAL</span>
|
||||
00037 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00038
|
||||
00039 <span class="keywordtype">void</span> main_function(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
|
||||
00040 {
|
||||
00041 <span class="comment">// PARAMETRES</span>
|
||||
00042 <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>
|
||||
00043 <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>
|
||||
00044 <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>
|
||||
00045 <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>
|
||||
00046
|
||||
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">unsigned</span> <span class="keywordtype">int</span> MIN_GEN = 10; <span class="comment">// Minimum number of generation before ...</span>
|
||||
00049 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> STEADY_GEN = 50; <span class="comment">// stop after STEADY_GEN gen. without improvelent</span>
|
||||
00050
|
||||
00051 <span class="keyword">const</span> <span class="keywordtype">double</span> P_CROSS = 0.8; <span class="comment">// Crossover probability</span>
|
||||
00052 <span class="keyword">const</span> <span class="keywordtype">double</span> P_MUT = 1.0; <span class="comment">// mutation probability</span>
|
||||
00053
|
||||
00054 <span class="keyword">const</span> <span class="keywordtype">double</span> P_MUT_PER_BIT = 0.01; <span class="comment">// internal probability for bit-flip mutation</span>
|
||||
00055 <span class="comment">// some parameters for chosing among different operators</span>
|
||||
00056 <span class="keyword">const</span> <span class="keywordtype">double</span> onePointRate = 0.5; <span class="comment">// rate for 1-pt Xover</span>
|
||||
00057 <span class="keyword">const</span> <span class="keywordtype">double</span> twoPointsRate = 0.5; <span class="comment">// rate for 2-pt Xover</span>
|
||||
00058 <span class="keyword">const</span> <span class="keywordtype">double</span> URate = 0.5; <span class="comment">// rate for Uniform Xover</span>
|
||||
00059 <span class="keyword">const</span> <span class="keywordtype">double</span> bitFlipRate = 0.5; <span class="comment">// rate for bit-flip mutation</span>
|
||||
00060 <span class="keyword">const</span> <span class="keywordtype">double</span> oneBitRate = 0.5; <span class="comment">// rate for one-bit mutation</span>
|
||||
00061
|
||||
00062 <span class="comment">// GENERAL</span>
|
||||
00064 <span class="comment"></span> <span class="comment">// Random seed</span>
|
||||
00066 <span class="comment"></span> <span class="comment">//reproducible random seed: if you don't change SEED above,</span>
|
||||
00067 <span class="comment">// you'll aways get the same result, NOT a random run</span>
|
||||
00068 rng.<a class="code" href="classeo_rng.html#a2">reseed</a>(SEED);
|
||||
00069
|
||||
00070 <span class="comment">// EVAL</span>
|
||||
00072 <span class="comment"></span> <span class="comment">// Fitness function</span>
|
||||
00074 <span class="comment"></span> <span class="comment">// Evaluation: from a plain C++ fn to an EvalFunc Object</span>
|
||||
00075 <span class="comment">// you need to give the full description of the function</span>
|
||||
00076 <a class="code" href="structeo_eval_func_ptr.html">eoEvalFuncPtr<Indi, double, const vector<bool></a>& > eval( binary_value );
|
||||
00077
|
||||
00078 <span class="comment">// INIT</span>
|
||||
00080 <span class="comment"></span> <span class="comment">// Initilisation of population</span>
|
||||
00082 <span class="comment"></span>
|
||||
00083 <span class="comment">// based on boolean_generator class (see utils/eoRndGenerators.h)</span>
|
||||
00084 <a class="code" href="classeo_uniform_generator.html">eoUniformGenerator<bool></a> uGen;
|
||||
00085 <a class="code" href="classeo_init_fixed_length.html">eoInitFixedLength<Indi></a> random(VEC_SIZE, uGen);
|
||||
00086 <span class="comment">// Initialization of the population</span>
|
||||
00087 <a class="code" href="classeo_pop.html">eoPop<Indi></a> pop(POP_SIZE, random);
|
||||
00088
|
||||
00089 <span class="comment">// and evaluate it in one loop</span>
|
||||
00090 apply<Indi>(eval, pop); <span class="comment">// STL syntax</span>
|
||||
00091
|
||||
00092 <span class="comment">// OUTPUT</span>
|
||||
00093 <span class="comment">// sort pop before printing it!</span>
|
||||
00094 pop.sort();
|
||||
00095 <span class="comment">// Print (sorted) intial population (raw printout)</span>
|
||||
00096 cout << <span class="stringliteral">"Initial Population"</span> << endl;
|
||||
00097 cout << pop;
|
||||
00098
|
||||
00099 <span class="comment">// ENGINE</span>
|
||||
00101 <span class="comment"></span> <span class="comment">// selection and replacement</span>
|
||||
00103 <span class="comment"></span><span class="comment">// SELECT</span>
|
||||
00104 <span class="comment">// The robust tournament selection</span>
|
||||
00105 <a class="code" href="classeo_det_tournament_select.html">eoDetTournamentSelect<Indi></a> selectOne(T_SIZE); <span class="comment">// T_SIZE in [2,POP_SIZE]</span>
|
||||
00106 <span class="comment">// is now encapsulated in a eoSelectPerc (entage)</span>
|
||||
00107 <a class="code" href="classeo_select_perc.html">eoSelectPerc<Indi></a> select(selectOne);<span class="comment">// by default rate==1</span>
|
||||
00108
|
||||
00109 <span class="comment">// REPLACE</span>
|
||||
00110 <span class="comment">// And we now have the full slection/replacement - though with</span>
|
||||
00111 <span class="comment">// no replacement (== generational replacement) at the moment :-)</span>
|
||||
00112 <a class="code" href="classeo_generational_replacement.html">eoGenerationalReplacement<Indi></a> replace;
|
||||
00113
|
||||
00114 <span class="comment">// OPERATORS</span>
|
||||
00116 <span class="comment"></span> <span class="comment">// The variation operators</span>
|
||||
00118 <span class="comment"></span><span class="comment">// CROSSOVER</span>
|
||||
00119 <span class="comment">// 1-point crossover for bitstring</span>
|
||||
00120 eo1PtBitXover<Indi> xover1;
|
||||
00121 <span class="comment">// uniform crossover for bitstring</span>
|
||||
00122 <a class="code" href="classeo_u_bit_xover.html">eoUBitXover<Indi></a> xoverU;
|
||||
00123 <span class="comment">// 2-pots xover</span>
|
||||
00124 <a class="code" href="classeo_n_pts_bit_xover.html">eoNPtsBitXover<Indi></a> xover2(2);
|
||||
00125 <span class="comment">// Combine them with relative rates</span>
|
||||
00126 <a class="code" href="classeo_prop_combined_quad_op.html">eoPropCombinedQuadOp<Indi></a> xover(xover1, onePointRate);
|
||||
00127 xover.add(xoverU, URate);
|
||||
00128 xover.add(xover2, twoPointsRate, <span class="keyword">true</span>);
|
||||
00129
|
||||
00130 <span class="comment">// MUTATION</span>
|
||||
00131 <span class="comment">// standard bit-flip mutation for bitstring</span>
|
||||
00132 <a class="code" href="classeo_bit_mutation.html">eoBitMutation<Indi></a> mutationBitFlip(P_MUT_PER_BIT);
|
||||
00133 <span class="comment">// mutate exactly 1 bit per individual</span>
|
||||
00134 <a class="code" href="classeo_det_bit_flip.html">eoDetBitFlip<Indi></a> mutationOneBit;
|
||||
00135 <span class="comment">// Combine them with relative rates</span>
|
||||
00136 <a class="code" href="classeo_prop_combined_mon_op.html">eoPropCombinedMonOp<Indi></a> mutation(mutationBitFlip, bitFlipRate);
|
||||
00137 mutation.add(mutationOneBit, oneBitRate, <span class="keyword">true</span>);
|
||||
00138
|
||||
00139 <span class="comment">// The operators are encapsulated into an eoTRansform object</span>
|
||||
00140 <a class="code" href="classeo_s_g_a_transform.html">eoSGATransform<Indi></a> transform(xover, P_CROSS, mutation, P_MUT);
|
||||
00141
|
||||
00142 <span class="comment">// STOP</span>
|
||||
00143 <span class="comment">// CHECKPOINT</span>
|
||||
00145 <span class="comment"></span> <span class="comment">// termination conditions: use more than one</span>
|
||||
00147 <span class="comment"></span> <span class="comment">// stop after MAX_GEN generations</span>
|
||||
00148 <a class="code" href="classeo_gen_continue.html">eoGenContinue<Indi></a> genCont(MAX_GEN);
|
||||
00149 <span class="comment">// do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement</span>
|
||||
00150 <a class="code" href="classeo_steady_fit_continue.html">eoSteadyFitContinue<Indi></a> steadyCont(MIN_GEN, STEADY_GEN);
|
||||
00151 <span class="comment">// stop when fitness reaches a target (here VEC_SIZE)</span>
|
||||
00152 <a class="code" href="classeo_fit_continue.html">eoFitContinue<Indi></a> fitCont(VEC_SIZE);
|
||||
00153 <span class="comment">// do stop when one of the above says so</span>
|
||||
00154 <a class="code" href="classeo_combined_continue.html">eoCombinedContinue<Indi></a> continuator(genCont);
|
||||
00155 continuator.add(steadyCont);
|
||||
00156 continuator.add(fitCont);
|
||||
00157
|
||||
00158 <span class="comment">// GENERATION</span>
|
||||
00160 <span class="comment"></span> <span class="comment">// the algorithm</span>
|
||||
00162 <span class="comment"></span>
|
||||
00163 <span class="comment">// Easy EA requires</span>
|
||||
00164 <span class="comment">// selection, transformation, eval, replacement, and stopping criterion</span>
|
||||
00165 <a class="code" href="classeo_easy_e_a.html">eoEasyEA<Indi></a> gga(continuator, eval, select, transform, replace);
|
||||
00166
|
||||
00167 <span class="comment">// Apply algo to pop - that's it!</span>
|
||||
00168 cout << <span class="stringliteral">"\n Here we go\n\n"</span>;
|
||||
00169 gga(pop);
|
||||
00170
|
||||
00171 <span class="comment">// OUTPUT</span>
|
||||
00172 <span class="comment">// Print (sorted) intial population</span>
|
||||
00173 pop.sort();
|
||||
00174 cout << <span class="stringliteral">"FINAL Population\n"</span> << pop << endl;
|
||||
00175 <span class="comment">// GENERAL</span>
|
||||
00176 }
|
||||
00177
|
||||
00178 <span class="comment">// A main that catches the exceptions</span>
|
||||
00179
|
||||
00180 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
|
||||
00181 {
|
||||
00182
|
||||
00183 <span class="keywordflow">try</span>
|
||||
00184 {
|
||||
00185 main_function(argc, argv);
|
||||
00186 }
|
||||
00187 <span class="keywordflow">catch</span>(exception& e)
|
||||
00188 {
|
||||
00189 cout << <span class="stringliteral">"Exception: "</span> << e.what() << <span class="charliteral">'\n'</span>;
|
||||
00190 }
|
||||
00191
|
||||
00192 <span class="keywordflow">return</span> 1;
|
||||
00193 }
|
||||
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:39 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>
|
||||
168
trunk/paradiseo-eo/doc/html/_first_bit_g_a_8cpp-source.html
Normal file
168
trunk/paradiseo-eo/doc/html/_first_bit_g_a_8cpp-source.html
Normal file
|
|
@ -0,0 +1,168 @@
|
|||
<!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: FirstBitGA.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 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_000003.html">tutorial</a> / <a class="el" href="dir_000019.html">Lesson1</a></div>
|
||||
<h1>FirstBitGA.cpp</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00002 <span class="comment">// FirstBitGA.cpp</span>
|
||||
00003 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00004 <span class="comment">//*</span>
|
||||
00005 <span class="comment">// An instance of a VERY simple Bitstring Genetic Algorithm</span>
|
||||
00006 <span class="comment">//</span>
|
||||
00007 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00008
|
||||
00009 <span class="preprocessor">#ifdef HAVE_CONFIG_H</span>
|
||||
00010 <span class="preprocessor"></span><span class="preprocessor">#include <config.h></span>
|
||||
00011 <span class="preprocessor">#endif</span>
|
||||
00012 <span class="preprocessor"></span>
|
||||
00013 <span class="preprocessor">#include <stdexcept></span>
|
||||
00014 <span class="preprocessor">#include <iostream></span>
|
||||
00015
|
||||
00016 <span class="preprocessor">#include <eo></span>
|
||||
00017 <span class="preprocessor">#include <ga.h></span>
|
||||
00018
|
||||
00019 <span class="comment">// Use functions from namespace std</span>
|
||||
00020 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
|
||||
00021
|
||||
00022 <span class="comment">// REPRESENTATION</span>
|
||||
00023 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00024 <span class="comment">// define your individuals</span>
|
||||
00025 <span class="keyword">typedef</span> <a class="code" href="classeo_bit.html">eoBit<double></a> <a class="code" href="classeo_real.html">Indi</a>; <span class="comment">// A bitstring with fitness double</span>
|
||||
00026
|
||||
00027 <span class="comment">// EVAL</span>
|
||||
00028 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00029 <span class="comment">// a simple fitness function that computes the number of ones of a bitstring</span>
|
||||
00030 <span class="comment">// @param _indi A biststring individual</span>
|
||||
00031
|
||||
00032 <span class="keywordtype">double</span> binary_value(<span class="keyword">const</span> Indi & _indi)
|
||||
00033 {
|
||||
00034 <span class="keywordtype">double</span> sum = 0;
|
||||
00035 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < _indi.size(); i++)
|
||||
00036 sum += _indi[i];
|
||||
00037 <span class="keywordflow">return</span> sum;
|
||||
00038 }
|
||||
00039 <span class="comment">// GENERAL</span>
|
||||
00040 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00041 <span class="keywordtype">void</span> main_function(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
|
||||
00042 {
|
||||
00043 <span class="comment">// PARAMETRES</span>
|
||||
00044 <span class="comment">// all parameters are hard-coded!</span>
|
||||
00045 <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>
|
||||
00046 <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>
|
||||
00047 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> VEC_SIZE = 16; <span class="comment">// Number of bits in genotypes</span>
|
||||
00048 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> POP_SIZE = 100; <span class="comment">// Size of population</span>
|
||||
00049 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> MAX_GEN = 400; <span class="comment">// Maximum number of generation before STOP</span>
|
||||
00050 <span class="keyword">const</span> <span class="keywordtype">float</span> CROSS_RATE = 0.8; <span class="comment">// Crossover rate</span>
|
||||
00051 <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>
|
||||
00052 <span class="keyword">const</span> <span class="keywordtype">float</span> MUT_RATE = 1.0; <span class="comment">// mutation rate</span>
|
||||
00053
|
||||
00054 <span class="comment">// GENERAL</span>
|
||||
00056 <span class="comment"></span> <span class="comment">// Random seed</span>
|
||||
00058 <span class="comment"></span> <span class="comment">//reproducible random seed: if you don't change SEED above,</span>
|
||||
00059 <span class="comment">// you'll aways get the same result, NOT a random run</span>
|
||||
00060 rng.<a class="code" href="classeo_rng.html#a2">reseed</a>(SEED);
|
||||
00061
|
||||
00062 <span class="comment">// EVAL</span>
|
||||
00064 <span class="comment"></span> <span class="comment">// Fitness function</span>
|
||||
00066 <span class="comment"></span> <span class="comment">// Evaluation: from a plain C++ fn to an EvalFunc Object</span>
|
||||
00067 <a class="code" href="structeo_eval_func_ptr.html">eoEvalFuncPtr<Indi></a> eval( binary_value );
|
||||
00068
|
||||
00069 <span class="comment">// INIT</span>
|
||||
00071 <span class="comment"></span> <span class="comment">// Initilisation of population</span>
|
||||
00073 <span class="comment"></span>
|
||||
00074 <span class="comment">// declare the population</span>
|
||||
00075 <a class="code" href="classeo_pop.html">eoPop<Indi></a> pop;
|
||||
00076 <span class="comment">// fill it!</span>
|
||||
00077 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> igeno=0; igeno<POP_SIZE; igeno++)
|
||||
00078 {
|
||||
00079 Indi v; <span class="comment">// void individual, to be filled</span>
|
||||
00080 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> ivar=0; ivar<VEC_SIZE; ivar++)
|
||||
00081 {
|
||||
00082 <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>
|
||||
00083 v.push_back(r); <span class="comment">// append that random value to v</span>
|
||||
00084 }
|
||||
00085 eval(v); <span class="comment">// evaluate it</span>
|
||||
00086 pop.push_back(v); <span class="comment">// and put it in the population</span>
|
||||
00087 }
|
||||
00088
|
||||
00089 <span class="comment">// OUTPUT</span>
|
||||
00090 <span class="comment">// sort pop before printing it!</span>
|
||||
00091 pop.<a class="code" href="classeo_pop.html#a5">sort</a>();
|
||||
00092 <span class="comment">// Print (sorted) intial population (raw printout)</span>
|
||||
00093 cout << <span class="stringliteral">"Initial Population"</span> << endl;
|
||||
00094 cout << pop;
|
||||
00095 <span class="comment">// shuffle - this is a test</span>
|
||||
00096 pop.<a class="code" href="classeo_pop.html#a7">shuffle</a>();
|
||||
00097 <span class="comment">// Print (sorted) intial population (raw printout)</span>
|
||||
00098 cout << <span class="stringliteral">"Shuffled Population"</span> << endl;
|
||||
00099 cout << pop;
|
||||
00100
|
||||
00101 <span class="comment">// ENGINE</span>
|
||||
00103 <span class="comment"></span> <span class="comment">// selection and replacement</span>
|
||||
00105 <span class="comment"></span><span class="comment">// SELECT</span>
|
||||
00106 <span class="comment">// The robust tournament selection</span>
|
||||
00107 <a class="code" href="classeo_det_tournament_select.html">eoDetTournamentSelect<Indi></a> select(T_SIZE); <span class="comment">// T_SIZE in [2,POP_SIZE]</span>
|
||||
00108
|
||||
00109 <span class="comment">// REPLACE</span>
|
||||
00110 <span class="comment">// The simple GA evolution engine uses generational replacement</span>
|
||||
00111 <span class="comment">// so no replacement procedure is needed</span>
|
||||
00112
|
||||
00113 <span class="comment">// OPERATORS</span>
|
||||
00115 <span class="comment"></span> <span class="comment">// The variation operators</span>
|
||||
00117 <span class="comment"></span><span class="comment">// CROSSOVER</span>
|
||||
00118 <span class="comment">// 1-point crossover for bitstring</span>
|
||||
00119 eo1PtBitXover<Indi> xover;
|
||||
00120 <span class="comment">// MUTATION</span>
|
||||
00121 <span class="comment">// standard bit-flip mutation for bitstring</span>
|
||||
00122 <a class="code" href="classeo_bit_mutation.html">eoBitMutation<Indi></a> mutation(P_MUT_PER_BIT);
|
||||
00123
|
||||
00124 <span class="comment">// STOP</span>
|
||||
00125 <span class="comment">// CHECKPOINT</span>
|
||||
00127 <span class="comment"></span> <span class="comment">// termination condition</span>
|
||||
00129 <span class="comment"></span> <span class="comment">// stop after MAX_GEN generations</span>
|
||||
00130 <a class="code" href="classeo_gen_continue.html">eoGenContinue<Indi></a> continuator(MAX_GEN);
|
||||
00131
|
||||
00132 <span class="comment">// GENERATION</span>
|
||||
00134 <span class="comment"></span> <span class="comment">// the algorithm</span>
|
||||
00136 <span class="comment"></span> <span class="comment">// standard Generational GA requires as parameters</span>
|
||||
00137 <span class="comment">// selection, evaluation, crossover and mutation, stopping criterion</span>
|
||||
00138
|
||||
00139
|
||||
00140 <a class="code" href="classeo_s_g_a.html">eoSGA<Indi></a> gga(select, xover, CROSS_RATE, mutation, MUT_RATE,
|
||||
00141 eval, continuator);
|
||||
00142
|
||||
00143 <span class="comment">// Apply algo to pop - that's it!</span>
|
||||
00144 gga(pop);
|
||||
00145
|
||||
00146 <span class="comment">// OUTPUT</span>
|
||||
00147 <span class="comment">// Print (sorted) intial population</span>
|
||||
00148 pop.<a class="code" href="classeo_pop.html#a5">sort</a>();
|
||||
00149 cout << <span class="stringliteral">"FINAL Population\n"</span> << pop << endl;
|
||||
00150 <span class="comment">// GENERAL</span>
|
||||
00151 }
|
||||
00152 <span class="comment">// A main that catches the exceptions</span>
|
||||
00153
|
||||
00154 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
|
||||
00155 {
|
||||
00156
|
||||
00157 <span class="keywordflow">try</span>
|
||||
00158 {
|
||||
00159 main_function(argc, argv);
|
||||
00160 }
|
||||
00161 <span class="keywordflow">catch</span>(exception& e)
|
||||
00162 {
|
||||
00163 cout << <span class="stringliteral">"Exception: "</span> << e.what() << <span class="charliteral">'\n'</span>;
|
||||
00164 }
|
||||
00165
|
||||
00166 <span class="keywordflow">return</span> 1;
|
||||
00167 }
|
||||
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:39 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>
|
||||
193
trunk/paradiseo-eo/doc/html/_first_real_e_a_8cpp-source.html
Normal file
193
trunk/paradiseo-eo/doc/html/_first_real_e_a_8cpp-source.html
Normal file
|
|
@ -0,0 +1,193 @@
|
|||
<!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: FirstRealEA.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 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_000003.html">tutorial</a> / <a class="el" href="dir_000004.html">Lesson2</a></div>
|
||||
<h1>FirstRealEA.cpp</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00002 <span class="comment">// FirstRealEA.cpp</span>
|
||||
00003 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00004 <span class="comment">//*</span>
|
||||
00005 <span class="comment">// Still an instance of a VERY simple Real-coded Genetic Algorithm</span>
|
||||
00006 <span class="comment">// (see FirstBitGA.cpp) but now with Breeder - and Combined Ops</span>
|
||||
00007 <span class="comment">//</span>
|
||||
00008 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00009 <span class="preprocessor">#ifdef HAVE_CONFIG_H</span>
|
||||
00010 <span class="preprocessor"></span><span class="preprocessor">#include <config.h></span>
|
||||
00011 <span class="preprocessor">#endif</span>
|
||||
00012 <span class="preprocessor"></span>
|
||||
00013 <span class="comment">// standard includes</span>
|
||||
00014 <span class="preprocessor">#include <stdexcept></span> <span class="comment">// runtime_error</span>
|
||||
00015 <span class="preprocessor">#include <iostream></span> <span class="comment">// cout</span>
|
||||
00016
|
||||
00017 <span class="comment">// the general include for eo</span>
|
||||
00018 <span class="preprocessor">#include <eo></span>
|
||||
00019 <span class="preprocessor">#include <es.h></span>
|
||||
00020
|
||||
00021 <span class="comment">// REPRESENTATION</span>
|
||||
00022 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00023 <span class="comment">// define your individuals</span>
|
||||
00024 <span class="keyword">typedef</span> <a class="code" href="classeo_real.html">eoReal<double></a> <a class="code" href="classeo_real.html">Indi</a>;
|
||||
00025
|
||||
00026 <span class="comment">// Use functions from namespace std</span>
|
||||
00027 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
|
||||
00028
|
||||
00029 <span class="comment">// EVALFUNC</span>
|
||||
00030 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00031 <span class="comment">// a simple fitness function that computes the euclidian norm of a real vector</span>
|
||||
00032 <span class="comment">// Now in a separate file, and declared as binary_value(const vector<bool> &)</span>
|
||||
00033
|
||||
00034 <span class="preprocessor">#include "real_value.h"</span>
|
||||
00035
|
||||
00036 <span class="comment">// GENERAL</span>
|
||||
00037 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00038
|
||||
00039 <span class="keywordtype">void</span> main_function(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
|
||||
00040 {
|
||||
00041 <span class="comment">// PARAMETRES</span>
|
||||
00042 <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>
|
||||
00043 <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>
|
||||
00044 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> VEC_SIZE = 8; <span class="comment">// Number of object variables in genotypes</span>
|
||||
00045 <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>
|
||||
00046
|
||||
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">unsigned</span> <span class="keywordtype">int</span> MIN_GEN = 10; <span class="comment">// Minimum number of generation before ...</span>
|
||||
00049 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> STEADY_GEN = 50; <span class="comment">// stop after STEADY_GEN gen. without improvelent</span>
|
||||
00050
|
||||
00051 <span class="keyword">const</span> <span class="keywordtype">float</span> P_CROSS = 0.8; <span class="comment">// Crossover probability</span>
|
||||
00052 <span class="keyword">const</span> <span class="keywordtype">float</span> P_MUT = 0.5; <span class="comment">// mutation probability</span>
|
||||
00053
|
||||
00054 <span class="keyword">const</span> <span class="keywordtype">double</span> EPSILON = 0.01; <span class="comment">// range for real uniform mutation</span>
|
||||
00055 <span class="keywordtype">double</span> SIGMA = 0.3; <span class="comment">// std dev. for normal mutation</span>
|
||||
00056 <span class="comment">// some parameters for chosing among different operators</span>
|
||||
00057 <span class="keyword">const</span> <span class="keywordtype">double</span> hypercubeRate = 0.5; <span class="comment">// relative weight for hypercube Xover</span>
|
||||
00058 <span class="keyword">const</span> <span class="keywordtype">double</span> segmentRate = 0.5; <span class="comment">// relative weight for segment Xover</span>
|
||||
00059 <span class="keyword">const</span> <span class="keywordtype">double</span> uniformMutRate = 0.5; <span class="comment">// relative weight for uniform mutation</span>
|
||||
00060 <span class="keyword">const</span> <span class="keywordtype">double</span> detMutRate = 0.5; <span class="comment">// relative weight for det-uniform mutation</span>
|
||||
00061 <span class="keyword">const</span> <span class="keywordtype">double</span> normalMutRate = 0.5; <span class="comment">// relative weight for normal mutation</span>
|
||||
00062
|
||||
00063 <span class="comment">// GENERAL</span>
|
||||
00065 <span class="comment"></span> <span class="comment">// Random seed</span>
|
||||
00067 <span class="comment"></span> <span class="comment">//reproducible random seed: if you don't change SEED above,</span>
|
||||
00068 <span class="comment">// you'll aways get the same result, NOT a random run</span>
|
||||
00069 rng.<a class="code" href="classeo_rng.html#a2">reseed</a>(SEED);
|
||||
00070
|
||||
00071 <span class="comment">// EVAL</span>
|
||||
00073 <span class="comment"></span> <span class="comment">// Fitness function</span>
|
||||
00075 <span class="comment"></span> <span class="comment">// Evaluation: from a plain C++ fn to an EvalFunc Object</span>
|
||||
00076 <span class="comment">// you need to give the full description of the function</span>
|
||||
00077 <a class="code" href="structeo_eval_func_ptr.html">eoEvalFuncPtr<Indi, double, const vector<double></a>& > eval( real_value );
|
||||
00078
|
||||
00079 <span class="comment">// INIT</span>
|
||||
00081 <span class="comment"></span> <span class="comment">// Initilisation of population</span>
|
||||
00083 <span class="comment"></span> <span class="comment">// based on a uniform generator</span>
|
||||
00084 <a class="code" href="classeo_uniform_generator.html">eoUniformGenerator<double></a> uGen(-1.0, 1.0);
|
||||
00085 <a class="code" href="classeo_init_fixed_length.html">eoInitFixedLength<Indi></a> random(VEC_SIZE, uGen);
|
||||
00086 <span class="comment">// Initialization of the population</span>
|
||||
00087 <a class="code" href="classeo_pop.html">eoPop<Indi></a> pop(POP_SIZE, random);
|
||||
00088
|
||||
00089 <span class="comment">// and evaluate it in one loop</span>
|
||||
00090 apply<Indi>(eval, pop); <span class="comment">// STL syntax</span>
|
||||
00091
|
||||
00092 <span class="comment">// OUTPUT</span>
|
||||
00093 <span class="comment">// sort pop before printing it!</span>
|
||||
00094 pop.sort();
|
||||
00095 <span class="comment">// Print (sorted) intial population (raw printout)</span>
|
||||
00096 cout << <span class="stringliteral">"Initial Population"</span> << endl;
|
||||
00097 cout << pop;
|
||||
00098
|
||||
00099 <span class="comment">// ENGINE</span>
|
||||
00101 <span class="comment"></span> <span class="comment">// selection and replacement</span>
|
||||
00103 <span class="comment"></span><span class="comment">// SELECT</span>
|
||||
00104 <span class="comment">// The robust tournament selection</span>
|
||||
00105 <a class="code" href="classeo_det_tournament_select.html">eoDetTournamentSelect<Indi></a> selectOne(T_SIZE);
|
||||
00106 <span class="comment">// is now encapsulated in a eoSelectPerc (entage)</span>
|
||||
00107 <a class="code" href="classeo_select_perc.html">eoSelectPerc<Indi></a> select(selectOne);<span class="comment">// by default rate==1</span>
|
||||
00108
|
||||
00109 <span class="comment">// REPLACE</span>
|
||||
00110 <span class="comment">// And we now have the full slection/replacement - though with</span>
|
||||
00111 <span class="comment">// no replacement (== generational replacement) at the moment :-)</span>
|
||||
00112 <a class="code" href="classeo_generational_replacement.html">eoGenerationalReplacement<Indi></a> replace;
|
||||
00113
|
||||
00114 <span class="comment">// OPERATORS</span>
|
||||
00116 <span class="comment"></span> <span class="comment">// The variation operators</span>
|
||||
00118 <span class="comment"></span><span class="comment">// CROSSOVER</span>
|
||||
00119 <span class="comment">// uniform chooce on segment made by the parents</span>
|
||||
00120 <a class="code" href="classeo_segment_crossover.html">eoSegmentCrossover<Indi></a> xoverS;
|
||||
00121 <span class="comment">// uniform choice in hypercube built by the parents</span>
|
||||
00122 eoHypercubeCrossover<Indi> xoverA;
|
||||
00123 <span class="comment">// Combine them with relative weights</span>
|
||||
00124 <a class="code" href="classeo_prop_combined_quad_op.html">eoPropCombinedQuadOp<Indi></a> xover(xoverS, segmentRate);
|
||||
00125 xover.add(xoverA, hypercubeRate, <span class="keyword">true</span>);
|
||||
00126
|
||||
00127 <span class="comment">// MUTATION</span>
|
||||
00128 <span class="comment">// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]</span>
|
||||
00129 <a class="code" href="classeo_uniform_mutation.html">eoUniformMutation<Indi></a> mutationU(EPSILON);
|
||||
00130 <span class="comment">// k (=1) coordinates of parents are uniformly modified</span>
|
||||
00131 <a class="code" href="classeo_det_uniform_mutation.html">eoDetUniformMutation<Indi></a> mutationD(EPSILON);
|
||||
00132 <span class="comment">// all coordinates of parents are normally modified (stDev SIGMA)</span>
|
||||
00133 <a class="code" href="classeo_normal_mutation.html">eoNormalMutation<Indi></a> mutationN(SIGMA);
|
||||
00134 <span class="comment">// Combine them with relative weights</span>
|
||||
00135 <a class="code" href="classeo_prop_combined_mon_op.html">eoPropCombinedMonOp<Indi></a> mutation(mutationU, uniformMutRate);
|
||||
00136 mutation.add(mutationD, detMutRate);
|
||||
00137 mutation.add(mutationN, normalMutRate, <span class="keyword">true</span>);
|
||||
00138
|
||||
00139 <span class="comment">// STOP</span>
|
||||
00140 <span class="comment">// CHECKPOINT</span>
|
||||
00142 <span class="comment"></span> <span class="comment">// termination conditions: use more than one</span>
|
||||
00144 <span class="comment"></span> <span class="comment">// stop after MAX_GEN generations</span>
|
||||
00145 <a class="code" href="classeo_gen_continue.html">eoGenContinue<Indi></a> genCont(MAX_GEN);
|
||||
00146 <span class="comment">// do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement</span>
|
||||
00147 <a class="code" href="classeo_steady_fit_continue.html">eoSteadyFitContinue<Indi></a> steadyCont(MIN_GEN, STEADY_GEN);
|
||||
00148 <span class="comment">// stop when fitness reaches a target (here VEC_SIZE)</span>
|
||||
00149 <a class="code" href="classeo_fit_continue.html">eoFitContinue<Indi></a> fitCont(0);
|
||||
00150 <span class="comment">// do stop when one of the above says so</span>
|
||||
00151 <a class="code" href="classeo_combined_continue.html">eoCombinedContinue<Indi></a> continuator(genCont);
|
||||
00152 continuator.add(steadyCont);
|
||||
00153 continuator.add(fitCont);
|
||||
00154
|
||||
00155 <span class="comment">// The operators are encapsulated into an eoTRansform object</span>
|
||||
00156 <a class="code" href="classeo_s_g_a_transform.html">eoSGATransform<Indi></a> transform(xover, P_CROSS, mutation, P_MUT);
|
||||
00157
|
||||
00158 <span class="comment">// GENERATION</span>
|
||||
00160 <span class="comment"></span> <span class="comment">// the algorithm</span>
|
||||
00162 <span class="comment"></span>
|
||||
00163 <span class="comment">// Easy EA requires</span>
|
||||
00164 <span class="comment">// selection, transformation, eval, replacement, and stopping criterion</span>
|
||||
00165 <a class="code" href="classeo_easy_e_a.html">eoEasyEA<Indi></a> gga(continuator, eval, select, transform, replace);
|
||||
00166
|
||||
00167 <span class="comment">// Apply algo to pop - that's it!</span>
|
||||
00168 cout << <span class="stringliteral">"\n Here we go\n\n"</span>;
|
||||
00169 gga(pop);
|
||||
00170
|
||||
00171 <span class="comment">// OUTPUT</span>
|
||||
00172 <span class="comment">// Print (sorted) intial population</span>
|
||||
00173 pop.sort();
|
||||
00174 cout << <span class="stringliteral">"FINAL Population\n"</span> << pop << endl;
|
||||
00175 <span class="comment">// GENERAL</span>
|
||||
00176 }
|
||||
00177
|
||||
00178 <span class="comment">// A main that catches the exceptions</span>
|
||||
00179
|
||||
00180 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
|
||||
00181 {
|
||||
00182 <span class="keywordflow">try</span>
|
||||
00183 {
|
||||
00184 main_function(argc, argv);
|
||||
00185 }
|
||||
00186 <span class="keywordflow">catch</span>(exception& e)
|
||||
00187 {
|
||||
00188 cout << <span class="stringliteral">"Exception: "</span> << e.what() << <span class="charliteral">'\n'</span>;
|
||||
00189 }
|
||||
00190
|
||||
00191 <span class="keywordflow">return</span> 1;
|
||||
00192 }
|
||||
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:39 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>
|
||||
163
trunk/paradiseo-eo/doc/html/_first_real_g_a_8cpp-source.html
Normal file
163
trunk/paradiseo-eo/doc/html/_first_real_g_a_8cpp-source.html
Normal file
|
|
@ -0,0 +1,163 @@
|
|||
<!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: FirstRealGA.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 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_000003.html">tutorial</a> / <a class="el" href="dir_000019.html">Lesson1</a></div>
|
||||
<h1>FirstRealGA.cpp</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00002 <span class="comment">// FirstRealGA.cpp</span>
|
||||
00003 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00004 <span class="comment">//*</span>
|
||||
00005 <span class="comment">// An instance of a VERY simple Real-coded Genetic Algorithm</span>
|
||||
00006 <span class="comment">//</span>
|
||||
00007 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00008 <span class="preprocessor">#ifdef HAVE_CONFIG_H</span>
|
||||
00009 <span class="preprocessor"></span><span class="preprocessor">#include <config.h></span>
|
||||
00010 <span class="preprocessor">#endif</span>
|
||||
00011 <span class="preprocessor"></span>
|
||||
00012 <span class="preprocessor">#include <stdexcept></span>
|
||||
00013 <span class="preprocessor">#include <iostream></span>
|
||||
00014 <span class="preprocessor">#include <sstream></span>
|
||||
00015
|
||||
00016 <span class="preprocessor">#include <eo></span>
|
||||
00017 <span class="preprocessor">#include <es.h></span>
|
||||
00018
|
||||
00019 <span class="comment">// Use functions from namespace std</span>
|
||||
00020 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
|
||||
00021
|
||||
00022 <span class="comment">// REPRESENTATION</span>
|
||||
00023 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00024 <span class="comment">// define your individuals</span>
|
||||
00025 <span class="keyword">typedef</span> <a class="code" href="classeo_real.html">eoReal<double></a> <a class="code" href="classeo_real.html">Indi</a>;
|
||||
00026
|
||||
00027 <span class="comment">// EVAL</span>
|
||||
00028 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00029 <span class="comment">// a simple fitness function that computes the euclidian norm of a real vector</span>
|
||||
00030 <span class="comment">// @param _indi A real-valued individual</span>
|
||||
00031
|
||||
00032 <span class="keywordtype">double</span> real_value(<span class="keyword">const</span> Indi & _indi)
|
||||
00033 {
|
||||
00034 <span class="keywordtype">double</span> sum = 0;
|
||||
00035 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < _indi.size(); i++)
|
||||
00036 sum += _indi[i]*_indi[i];
|
||||
00037 <span class="keywordflow">return</span> (-sum); <span class="comment">// maximizing only</span>
|
||||
00038 }
|
||||
00039 <span class="comment">// GENERAL</span>
|
||||
00040 <span class="comment">//-----------------------------------------------------------------------------</span>
|
||||
00041 <span class="keywordtype">void</span> main_function(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
|
||||
00042 {
|
||||
00043 <span class="comment">// PARAMETRES</span>
|
||||
00044 <span class="comment">// all parameters are hard-coded!</span>
|
||||
00045 <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>
|
||||
00046 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> VEC_SIZE = 8; <span class="comment">// Number of object variables in genotypes</span>
|
||||
00047 <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>
|
||||
00048 <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>
|
||||
00049 <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>
|
||||
00050 <span class="keyword">const</span> <span class="keywordtype">float</span> CROSS_RATE = 0.8; <span class="comment">// Crossover rate</span>
|
||||
00051 <span class="keyword">const</span> <span class="keywordtype">double</span> EPSILON = 0.01; <span class="comment">// range for real uniform mutation</span>
|
||||
00052 <span class="keyword">const</span> <span class="keywordtype">float</span> MUT_RATE = 0.5; <span class="comment">// mutation rate</span>
|
||||
00053
|
||||
00054 <span class="comment">// GENERAL</span>
|
||||
00056 <span class="comment"></span> <span class="comment">// Random seed</span>
|
||||
00058 <span class="comment"></span> <span class="comment">//reproducible random seed: if you don't change SEED above,</span>
|
||||
00059 <span class="comment">// you'll aways get the same result, NOT a random run</span>
|
||||
00060 rng.<a class="code" href="classeo_rng.html#a2">reseed</a>(SEED);
|
||||
00061
|
||||
00062 <span class="comment">// EVAL</span>
|
||||
00064 <span class="comment"></span> <span class="comment">// Fitness function</span>
|
||||
00066 <span class="comment"></span> <span class="comment">// Evaluation: from a plain C++ fn to an EvalFunc Object</span>
|
||||
00067 <a class="code" href="structeo_eval_func_ptr.html">eoEvalFuncPtr<Indi></a> eval( real_value );
|
||||
00068
|
||||
00069 <span class="comment">// INIT</span>
|
||||
00071 <span class="comment"></span> <span class="comment">// Initilisation of population</span>
|
||||
00073 <span class="comment"></span>
|
||||
00074 <span class="comment">// declare the population</span>
|
||||
00075 <a class="code" href="classeo_pop.html">eoPop<Indi></a> pop;
|
||||
00076 <span class="comment">// fill it!</span>
|
||||
00077 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> igeno=0; igeno<POP_SIZE; igeno++)
|
||||
00078 {
|
||||
00079 Indi v; <span class="comment">// void individual, to be filled</span>
|
||||
00080 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> ivar=0; ivar<VEC_SIZE; ivar++)
|
||||
00081 {
|
||||
00082 <span class="keywordtype">double</span> r = 2*rng.<a class="code" href="classeo_rng.html#a4">uniform</a>() - 1; <span class="comment">// new value, random in [-1,1)</span>
|
||||
00083 v.push_back(r); <span class="comment">// append that random value to v</span>
|
||||
00084 }
|
||||
00085 eval(v); <span class="comment">// evaluate it</span>
|
||||
00086 pop.push_back(v); <span class="comment">// and put it in the population</span>
|
||||
00087 }
|
||||
00088
|
||||
00089 <span class="comment">// OUTPUT</span>
|
||||
00090 <span class="comment">// sort pop before printing it!</span>
|
||||
00091 pop.<a class="code" href="classeo_pop.html#a5">sort</a>();
|
||||
00092 <span class="comment">// Print (sorted) intial population (raw printout)</span>
|
||||
00093 cout << <span class="stringliteral">"Initial Population"</span> << endl;
|
||||
00094 cout << pop;
|
||||
00095
|
||||
00096 <span class="comment">// ENGINE</span>
|
||||
00098 <span class="comment"></span> <span class="comment">// selection and replacement</span>
|
||||
00100 <span class="comment"></span><span class="comment">// SELECT</span>
|
||||
00101 <span class="comment">// The robust tournament selection</span>
|
||||
00102 <a class="code" href="classeo_det_tournament_select.html">eoDetTournamentSelect<Indi></a> select(T_SIZE); <span class="comment">// T_SIZE in [2,POP_SIZE]</span>
|
||||
00103
|
||||
00104 <span class="comment">// REPLACE</span>
|
||||
00105 <span class="comment">// eoSGA uses generational replacement by default</span>
|
||||
00106 <span class="comment">// so no replacement procedure has to be given</span>
|
||||
00107
|
||||
00108 <span class="comment">// OPERATORS</span>
|
||||
00110 <span class="comment"></span> <span class="comment">// The variation operators</span>
|
||||
00112 <span class="comment"></span><span class="comment">// CROSSOVER</span>
|
||||
00113 <span class="comment">// offspring(i) is a linear combination of parent(i)</span>
|
||||
00114 <a class="code" href="classeo_segment_crossover.html">eoSegmentCrossover<Indi></a> xover;
|
||||
00115 <span class="comment">// MUTATION</span>
|
||||
00116 <span class="comment">// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]</span>
|
||||
00117 <a class="code" href="classeo_uniform_mutation.html">eoUniformMutation<Indi></a> mutation(EPSILON);
|
||||
00118
|
||||
00119 <span class="comment">// STOP</span>
|
||||
00120 <span class="comment">// CHECKPOINT</span>
|
||||
00122 <span class="comment"></span> <span class="comment">// termination condition</span>
|
||||
00124 <span class="comment"></span> <span class="comment">// stop after MAX_GEN generations</span>
|
||||
00125 <a class="code" href="classeo_gen_continue.html">eoGenContinue<Indi></a> continuator(MAX_GEN);
|
||||
00126
|
||||
00127 <span class="comment">// GENERATION</span>
|
||||
00129 <span class="comment"></span> <span class="comment">// the algorithm</span>
|
||||
00131 <span class="comment"></span> <span class="comment">// standard Generational GA requires</span>
|
||||
00132 <span class="comment">// selection, evaluation, crossover and mutation, stopping criterion</span>
|
||||
00133
|
||||
00134
|
||||
00135 <a class="code" href="classeo_s_g_a.html">eoSGA<Indi></a> gga(select, xover, CROSS_RATE, mutation, MUT_RATE,
|
||||
00136 eval, continuator);
|
||||
00137
|
||||
00138 <span class="comment">// Apply algo to pop - that's it!</span>
|
||||
00139 gga(pop);
|
||||
00140
|
||||
00141 <span class="comment">// OUTPUT</span>
|
||||
00142 <span class="comment">// Print (sorted) intial population</span>
|
||||
00143 pop.<a class="code" href="classeo_pop.html#a5">sort</a>();
|
||||
00144 cout << <span class="stringliteral">"FINAL Population\n"</span> << pop << endl;
|
||||
00145 <span class="comment">// GENERAL</span>
|
||||
00146 }
|
||||
00147
|
||||
00148 <span class="comment">// A main that catches the exceptions</span>
|
||||
00149
|
||||
00150 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
|
||||
00151 {
|
||||
00152 <span class="keywordflow">try</span>
|
||||
00153 {
|
||||
00154 main_function(argc, argv);
|
||||
00155 }
|
||||
00156 <span class="keywordflow">catch</span>(exception& e)
|
||||
00157 {
|
||||
00158 cout << <span class="stringliteral">"Exception: "</span> << e.what() << <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
|
||||
<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>
|
||||
905
trunk/paradiseo-eo/doc/html/_fun_def_8cpp-source.html
Normal file
905
trunk/paradiseo-eo/doc/html/_fun_def_8cpp-source.html
Normal file
|
|
@ -0,0 +1,905 @@
|
|||
<!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: FunDef.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 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_000007.html">contrib</a> / <a class="el" href="dir_000008.html">mathsym</a> / <a class="el" href="dir_000022.html">fun</a></div>
|
||||
<h1>FunDef.cpp</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">/* </span>
|
||||
00002 <span class="comment"> * Copyright (C) 2005 Maarten Keijzer</span>
|
||||
00003 <span class="comment"> *</span>
|
||||
00004 <span class="comment"> * This program is free software; you can redistribute it and/or modify</span>
|
||||
00005 <span class="comment"> * it under the terms of version 2 of the GNU General Public License as </span>
|
||||
00006 <span class="comment"> * published by the Free Software Foundation. </span>
|
||||
00007 <span class="comment"> *</span>
|
||||
00008 <span class="comment"> * This program is distributed in the hope that it will be useful,</span>
|
||||
00009 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
|
||||
00010 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
|
||||
00011 <span class="comment"> * GNU General Public License for more details.</span>
|
||||
00012 <span class="comment"> *</span>
|
||||
00013 <span class="comment"> * You should have received a copy of the GNU General Public License</span>
|
||||
00014 <span class="comment"> * along with this program; if not, write to the Free Software</span>
|
||||
00015 <span class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
|
||||
00016 <span class="comment"> */</span>
|
||||
00017
|
||||
00018
|
||||
00019 <span class="preprocessor">#include <sstream></span>
|
||||
00020 <span class="preprocessor">#include "Sym.h"</span>
|
||||
00021 <span class="preprocessor">#include "FunDef.h"</span>
|
||||
00022 <span class="preprocessor">#include <LanguageTable.h></span>
|
||||
00023
|
||||
00024 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
|
||||
00025 <span class="keyword">using</span> <span class="keyword">namespace </span>boost::numeric;
|
||||
00026
|
||||
00027 vector<const FunDef*> language;
|
||||
00028
|
||||
00029 token_t add_function(FunDef* function) {
|
||||
00030 language.push_back(function);
|
||||
00031 <span class="keywordflow">return</span> token_t(language.size()-1);
|
||||
00032 }
|
||||
00033
|
||||
00034 <span class="keyword">const</span> FunDef& get_element(token_t token) { <span class="keywordflow">return</span> *language[token]; }
|
||||
00035
|
||||
00036 <span class="comment">/* Printing */</span>
|
||||
00037
|
||||
00038 string c_print(<span class="keyword">const</span> Sym& sym) {
|
||||
00039 <span class="keywordflow">return</span> c_print(sym, vector<string>());
|
||||
00040 }
|
||||
00041
|
||||
00042 string c_print(<span class="keyword">const</span> Sym& sym, <span class="keyword">const</span> vector<string>& vars) {
|
||||
00043 <span class="keyword">const</span> SymVec& args = sym.args();
|
||||
00044 vector<string> names(args.size());
|
||||
00045 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < args.size(); ++i) {
|
||||
00046 names[i] = c_print(args[i], vars);
|
||||
00047 }
|
||||
00048 <span class="keywordflow">return</span> language[sym.token()]->c_print(names, vars);
|
||||
00049 }
|
||||
00050
|
||||
00051 <span class="comment">/* Evaluation */</span>
|
||||
00052
|
||||
00053
|
||||
00054 <span class="keywordtype">double</span> eval(<span class="keyword">const</span> Sym& sym, <span class="keyword">const</span> std::vector<double>& inputs) {
|
||||
00055 <span class="keywordflow">return</span> language[sym.token()]->eval(sym.args(), inputs);
|
||||
00056 }
|
||||
00057
|
||||
00058
|
||||
00059 <span class="comment">/* Interval Logic */</span>
|
||||
00060 Interval eval(<span class="keyword">const</span> Sym& sym, <span class="keyword">const</span> vector<Interval>& inputs) {
|
||||
00061 <span class="keyword">const</span> SymVec& args = sym.args();
|
||||
00062 vector<Interval> interv(args.size());
|
||||
00063 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < args.size(); ++i) {
|
||||
00064 interv[i] = eval(args[i], inputs);
|
||||
00065
|
||||
00066 <span class="keywordflow">if</span> (!valid(interv[i])) <span class="keywordflow">throw</span> interval_error();
|
||||
00067 }
|
||||
00068 <span class="keywordflow">return</span> language[sym.token()]->eval(interv, inputs);
|
||||
00069 }
|
||||
00070
|
||||
00071 <span class="comment">/* */</span>
|
||||
00072 <span class="keywordtype">void</span> add_function_to_table(LanguageTable& table, token_t token) {
|
||||
00073 <span class="keyword">const</span> FunDef& fundef = *language[token];
|
||||
00074
|
||||
00075 <span class="keywordflow">if</span> (fundef.has_varargs() == <span class="keyword">false</span>) {
|
||||
00076 table.add_function(token, fundef.min_arity());
|
||||
00077 } <span class="keywordflow">else</span> { <span class="comment">// sum or prod (or min or max)</span>
|
||||
00078 table.add_function(token, 2);
|
||||
00079 }
|
||||
00080 }
|
||||
00081
|
||||
00082
|
||||
00083 <span class="comment">// by default it is eager</span>
|
||||
00084 <span class="keywordtype">double</span> FunDef::eval(<span class="keyword">const</span> SymVec& args, <span class="keyword">const</span> vector<double>& inputs)<span class="keyword"> const </span>{
|
||||
00085 vector<double> values(args.size());
|
||||
00086 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < args.size(); ++i) {
|
||||
00087 values[i] = ::eval(args[i], inputs);
|
||||
00088 }
|
||||
00089
|
||||
00090 <span class="keywordflow">return</span> eval(values, inputs);
|
||||
00091 }
|
||||
00092
|
||||
00093 <span class="comment">/* Variable Handling */</span>
|
||||
00094 FunDef* make_var(<span class="keywordtype">int</span> idx); <span class="comment">// defined in FunDefs.h</span>
|
||||
00095 <span class="keyword">static</span> vector<token_t> var_token;
|
||||
00096
|
||||
00097 Sym SymVar(<span class="keywordtype">unsigned</span> idx) {
|
||||
00098 <span class="keywordflow">if</span> (var_token.size() <= idx) {
|
||||
00099 <span class="comment">// it is new</span>
|
||||
00100 var_token.resize(idx+1, token_t(-1));
|
||||
00101 var_token[idx] = add_function( make_var(idx) );
|
||||
00102 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (var_token[idx] == token_t(-1)) {
|
||||
00103 var_token[idx] = add_function( make_var(idx) );
|
||||
00104 }
|
||||
00105 <span class="keywordflow">return</span> Sym(var_token[idx]);
|
||||
00106 }
|
||||
00107
|
||||
00108
|
||||
00109 <span class="comment">/* Constant Handling */</span>
|
||||
00110
|
||||
00111 <span class="keyword">struct </span>HashDouble{
|
||||
00112 size_t operator()(<span class="keywordtype">double</span> val)<span class="keyword"> const </span>{
|
||||
00113 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> h = 0;
|
||||
00114 <span class="keywordtype">char</span>* s = (<span class="keywordtype">char</span>*)&val;
|
||||
00115 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0 ; i<<span class="keyword">sizeof</span>(double); ++i)
|
||||
00116 h = 5*h + s[i];
|
||||
00117 <span class="keywordflow">return</span> size_t(h);
|
||||
00118 }
|
||||
00119 };
|
||||
00120
|
||||
00121 <span class="preprocessor">#if USE_TR1</span>
|
||||
00122 <span class="preprocessor"></span><span class="keyword">typedef</span> std::tr1::unordered_map<double, token_t> DoubleSet;
|
||||
00123 <span class="keyword">typedef</span> std::tr1::unordered_map<Sym, token_t> LambdaSet;
|
||||
00124 <span class="preprocessor">#else</span>
|
||||
00125 <span class="preprocessor"></span><span class="keyword">typedef</span> hash_map<double, token_t, HashDouble> DoubleSet;
|
||||
00126 <span class="keyword">typedef</span> hash_map<Sym, token_t, HashSym> LambdaSet;
|
||||
00127 <span class="preprocessor">#endif</span>
|
||||
00128 <span class="preprocessor"></span>
|
||||
00129 <span class="keyword">static</span> DoubleSet doubleSet; <span class="comment">// for quick checking if a constant already exists</span>
|
||||
00130 <span class="keyword">static</span> vector<double> token_value;
|
||||
00131
|
||||
00132 <span class="keyword">static</span> LambdaSet lambdaSet;
|
||||
00133 <span class="keyword">static</span> vector<Sym> token_lambda;
|
||||
00134
|
||||
00135 <span class="keyword">static</span> std::vector<token_t> free_list;
|
||||
00136
|
||||
00137 <span class="keywordtype">void</span> delete_val(token_t token) { <span class="comment">// clean up the information about this value</span>
|
||||
00138
|
||||
00139 <span class="keywordflow">if</span> (is_constant(token)) {
|
||||
00140 <span class="comment">//cout << "Deleting constant token " << token << endl;</span>
|
||||
00141 <span class="keywordtype">double</span> value = token_value[token];
|
||||
00142 doubleSet.erase(value);
|
||||
00143
|
||||
00144 <span class="keyword">delete</span> language[token];
|
||||
00145 language[token] = 0;
|
||||
00146 free_list.push_back(token);
|
||||
00147 }
|
||||
00148 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (is_lambda(token)) {
|
||||
00149 <span class="comment">//cout << "Deleting lambda token " << token << endl;</span>
|
||||
00150
|
||||
00151 Sym expression = token_lambda[token];
|
||||
00152 lambdaSet.erase(expression);
|
||||
00153
|
||||
00154 <span class="keyword">delete</span> language[token];
|
||||
00155 language[token] = 0;
|
||||
00156 free_list.push_back(token);
|
||||
00157 }
|
||||
00158 }
|
||||
00159
|
||||
00160
|
||||
00161 FunDef* make_const(<span class="keywordtype">double</span> value);
|
||||
00162
|
||||
00163 <span class="keywordtype">void</span> extend_free_list();
|
||||
00164
|
||||
00165 Sym SymConst(<span class="keywordtype">double</span> value) {
|
||||
00166
|
||||
00167 DoubleSet::iterator it = doubleSet.find(value);
|
||||
00168
|
||||
00169 <span class="keywordflow">if</span> (it != doubleSet.end()) {
|
||||
00170 <span class="keywordflow">return</span> Sym(it->second); <span class="comment">// already exists</span>
|
||||
00171 }
|
||||
00172
|
||||
00173
|
||||
00174 <span class="keywordflow">if</span> (free_list.empty()) { <span class="comment">// make space for tokens;</span>
|
||||
00175 extend_free_list();
|
||||
00176 }
|
||||
00177
|
||||
00178 token_t token = free_list.back();
|
||||
00179 free_list.pop_back();
|
||||
00180 <span class="comment">//cout << "Creating constant with token " << token << endl; </span>
|
||||
00181 assert(language[token] == 0);
|
||||
00182
|
||||
00183 language[token] = make_const(value);
|
||||
00184
|
||||
00185 doubleSet[value] = token;
|
||||
00186 <span class="keywordflow">if</span> (token_value.size() < token) token_value.resize(token+1);
|
||||
00187 token_value[token] = value;
|
||||
00188
|
||||
00189 <span class="keywordflow">return</span> Sym(token);
|
||||
00190 }
|
||||
00191
|
||||
00192 <span class="comment">/* LanguageTable depends on this one, XXX move somewhere safe.*/</span>
|
||||
00193 <span class="preprocessor">#include <utils/eoRNG.h></span>
|
||||
00194 <span class="keyword">extern</span> Sym default_const() { <span class="keywordflow">return</span> SymConst(rng.<a class="code" href="classeo_rng.html#a7">normal</a>()); }
|
||||
00195
|
||||
00196 <span class="comment">/* The functions */</span>
|
||||
00197 <span class="keyword">namespace </span>{
|
||||
00198
|
||||
00199 <span class="keyword">class </span>Var : <span class="keyword">public</span> FunDef {
|
||||
00200 <span class="keyword">public</span> :
|
||||
00201 <span class="keywordtype">unsigned</span> idx;
|
||||
00202 string default_str;
|
||||
00203
|
||||
00204 Var(<span class="keywordtype">unsigned</span> _idx) : idx(_idx) {
|
||||
00205 ostringstream os;
|
||||
00206 os << <span class="stringliteral">"x["</span> << idx << <span class="charliteral">']'</span>; <span class="comment">// CompiledCode expects this form</span>
|
||||
00207 default_str = os.str();
|
||||
00208 }
|
||||
00209
|
||||
00210 <span class="keywordtype">double</span> eval(<span class="keyword">const</span> vector<double>& _, <span class="keyword">const</span> vector<double>& inputs)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> inputs[idx]; }
|
||||
00211 <span class="keywordtype">double</span> eval(<span class="keyword">const</span> SymVec& _, <span class="keyword">const</span> vector<double>& inputs)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> inputs[idx]; }
|
||||
00212 string c_print(<span class="keyword">const</span> vector<string>& _, <span class="keyword">const</span> vector<string>& names)<span class="keyword"> const </span>{
|
||||
00213 <span class="keywordflow">if</span> (names.empty()) {
|
||||
00214 <span class="keywordflow">return</span> default_str;
|
||||
00215 }
|
||||
00216 <span class="keywordflow">return</span> names[idx];
|
||||
00217 }
|
||||
00218
|
||||
00219 Interval eval(<span class="keyword">const</span> vector<Interval>& _, <span class="keyword">const</span> vector<Interval>& inputs)<span class="keyword"> const </span>{
|
||||
00220 <span class="keywordflow">return</span> inputs[idx];
|
||||
00221 }
|
||||
00222
|
||||
00223 <span class="keywordtype">unsigned</span> min_arity()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> 0; }
|
||||
00224
|
||||
00225 string name()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="stringliteral">"var"</span>; }
|
||||
00226
|
||||
00227 };
|
||||
00228
|
||||
00229 <span class="keyword">class </span>Const : <span class="keyword">public</span> FunDef {
|
||||
00230 <span class="keyword">public</span>:
|
||||
00231 <span class="keywordtype">double</span> value;
|
||||
00232 string value_str;
|
||||
00233
|
||||
00234 Const(<span class="keywordtype">double</span> _value) : value(_value) {
|
||||
00235 ostringstream os;
|
||||
00236 os.precision(17);
|
||||
00237 os.setf(ios::showpoint);
|
||||
00238 os << <span class="charliteral">'('</span> << value << <span class="charliteral">')'</span>;
|
||||
00239 value_str = os.str();
|
||||
00240 }
|
||||
00241
|
||||
00242
|
||||
00243 <span class="keywordtype">double</span> eval(<span class="keyword">const</span> vector<double>& _, <span class="keyword">const</span> vector<double>& inputs)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> value; }
|
||||
00244 <span class="keywordtype">double</span> eval(<span class="keyword">const</span> SymVec& _, <span class="keyword">const</span> vector<double>& inputs)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> value; }
|
||||
00245 string c_print(<span class="keyword">const</span> vector<string>& _, <span class="keyword">const</span> vector<string>& names)<span class="keyword"> const </span>{
|
||||
00246 <span class="keywordflow">return</span> value_str;
|
||||
00247 }
|
||||
00248
|
||||
00249 Interval eval(<span class="keyword">const</span> vector<Interval>& _, <span class="keyword">const</span> vector<Interval>& inputs)<span class="keyword"> const </span>{
|
||||
00250 <span class="comment">// Profil/Bias seems to have a problem with 0 * inf when the Interval is exact zero (fpe)</span>
|
||||
00251 <span class="comment">//if (value == 0.0) return Interval(-BiasEpsilon,BiasEpsilon);</span>
|
||||
00252 <span class="keywordflow">return</span> Interval(value);
|
||||
00253 }
|
||||
00254
|
||||
00255 <span class="keywordtype">unsigned</span> min_arity()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> 0; }
|
||||
00256
|
||||
00257 string name()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="stringliteral">"parameter"</span>; }
|
||||
00258 };
|
||||
00259
|
||||
00260 } <span class="comment">// namespace </span>
|
||||
00261
|
||||
00262 <span class="keywordtype">void</span> get_constants(Sym sym, vector<double>& ret) {
|
||||
00263 token_t token = sym.token();
|
||||
00264 <span class="keywordflow">if</span> (is_constant(token)) {
|
||||
00265 <span class="keywordtype">double</span> val = static_cast<const Const*>(language[token])->value;
|
||||
00266 ret.push_back(val);
|
||||
00267 }
|
||||
00268
|
||||
00269 <span class="keyword">const</span> SymVec& args = sym.args();
|
||||
00270 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < args.size(); ++i) {
|
||||
00271 get_constants(args[i], ret);
|
||||
00272 }
|
||||
00273
|
||||
00274 }
|
||||
00275
|
||||
00276 <span class="keywordtype">double</span> get_constant_value(token_t token) {
|
||||
00277 <span class="keywordflow">return</span> static_cast<const Const*>(language[token])->value;
|
||||
00278 }
|
||||
00279
|
||||
00281 vector<double> get_constants(Sym sym) {
|
||||
00282 vector<double> retval;
|
||||
00283 get_constants(sym, retval);
|
||||
00284 <span class="keywordflow">return</span> retval;
|
||||
00285 }
|
||||
00286
|
||||
00289 Sym set_constants(Sym sym, vector<double>::const_iterator& it) {
|
||||
00290
|
||||
00291 token_t token = sym.token();
|
||||
00292 <span class="keywordflow">if</span> (is_constant(token)) {
|
||||
00293 <span class="keywordflow">return</span> SymConst(*it++);
|
||||
00294 }
|
||||
00295
|
||||
00296 SymVec args = sym.args();
|
||||
00297 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < args.size(); ++i) {
|
||||
00298 args[i] = set_constants(args[i], it);
|
||||
00299 }
|
||||
00300
|
||||
00301 <span class="keywordflow">return</span> Sym(token, args);
|
||||
00302 }
|
||||
00303
|
||||
00304 Sym set_constants(Sym sym, <span class="keyword">const</span> vector<double>& constants) {
|
||||
00305 vector<double>::const_iterator it = constants.begin();
|
||||
00306 <span class="keywordflow">return</span> set_constants(sym, it);
|
||||
00307 }
|
||||
00308
|
||||
00309 <span class="comment">// Get functions out, excluding Const and Var</span>
|
||||
00310 vector<const FunDef*> get_defined_functions() {
|
||||
00311 vector<const FunDef*> res;
|
||||
00312 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < language.size(); ++i) {
|
||||
00313 res.push_back(language[i]);
|
||||
00314
|
||||
00315 <span class="keywordflow">if</span> (is_constant(i) || is_variable(i)) {
|
||||
00316 res.back() = 0; <span class="comment">// erase</span>
|
||||
00317 }
|
||||
00318 }
|
||||
00319
|
||||
00320 <span class="keywordflow">return</span> res;
|
||||
00321 }
|
||||
00322
|
||||
00323 FunDef* make_var(<span class="keywordtype">int</span> idx) { <span class="keywordflow">return</span> <span class="keyword">new</span> Var(idx); }
|
||||
00324 FunDef* make_const(<span class="keywordtype">double</span> value) { <span class="keywordflow">return</span> <span class="keyword">new</span> Const(value); }
|
||||
00325
|
||||
00326 <span class="keywordtype">bool</span> is_constant(token_t token) {
|
||||
00327 <span class="keyword">const</span> Const* cnst = dynamic_cast<const Const*>( language[token] );
|
||||
00328 <span class="keywordflow">return</span> cnst != 0;
|
||||
00329 }
|
||||
00330
|
||||
00331 <span class="keywordtype">bool</span> is_variable(token_t token) {
|
||||
00332 <span class="keyword">const</span> Var* var = dynamic_cast<const Var*>( language[token] );
|
||||
00333 <span class="keywordflow">return</span> var != 0;
|
||||
00334 }
|
||||
00335
|
||||
00336 <span class="keywordtype">unsigned</span> get_variable_index(token_t token) {
|
||||
00337 <span class="keyword">const</span> Var* var = static_cast<const Var*>( language[token] );
|
||||
00338 <span class="keywordflow">return</span> var->idx;
|
||||
00339 }
|
||||
00340
|
||||
00341 <span class="keyword">namespace </span>{
|
||||
00342 <span class="keyword">class </span>Lambda : <span class="keyword">public</span> FunDef {
|
||||
00343 <span class="keyword">public</span>:
|
||||
00344 Sym expression;
|
||||
00345 <span class="keywordtype">int</span> arity;
|
||||
00346
|
||||
00347 Lambda(Sym expr, <span class="keywordtype">int</span> arity_) : expression(expr), arity(arity_) {}
|
||||
00348
|
||||
00349 <span class="keywordtype">double</span> eval(<span class="keyword">const</span> vector<double>& vals, <span class="keyword">const</span> vector<double>& _)<span class="keyword"> const </span>{
|
||||
00350 return ::eval(expression, vals);
|
||||
00351 }
|
||||
00352
|
||||
00353 string c_print(<span class="keyword">const</span> vector<string>& args, <span class="keyword">const</span> vector<string>& _)<span class="keyword"> const </span>{
|
||||
00354 <span class="keywordflow">return</span> string(<span class="stringliteral">"/*f*/"</span>) + ::c_print(expression, args) + string(<span class="stringliteral">"/*eof*/"</span>);
|
||||
00355 }
|
||||
00356
|
||||
00357 Interval eval(<span class="keyword">const</span> vector<Interval>& args, <span class="keyword">const</span> vector<Interval>& _)<span class="keyword"> const </span>{
|
||||
00358 return ::eval(expression, args);
|
||||
00359 }
|
||||
00360
|
||||
00361 <span class="keywordtype">unsigned</span> min_arity()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> arity; }
|
||||
00362 string name()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="stringliteral">"F"</span>; }
|
||||
00363
|
||||
00364 };
|
||||
00365 Sym normalize(Sym sym, SymVec& args) {
|
||||
00366 <span class="comment">// check if it's a variable</span>
|
||||
00367 token_t token = sym.token();
|
||||
00368 <span class="keyword">const</span> Var* var = dynamic_cast< const Var*>(language[token]);
|
||||
00369
|
||||
00370 <span class="keywordflow">if</span> (var != 0) {
|
||||
00371 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < args.size(); ++i) {
|
||||
00372 <span class="keywordflow">if</span> (sym == args[i]) {
|
||||
00373 <span class="keywordflow">return</span> SymVar(i); <span class="comment">// replace with reference to arg</span>
|
||||
00374 }
|
||||
00375 }
|
||||
00376
|
||||
00377 <span class="comment">// not replaced, add it</span>
|
||||
00378 args.push_back(sym);
|
||||
00379 <span class="keywordflow">return</span> SymVar(args.size()-1);
|
||||
00380
|
||||
00381 }
|
||||
00382
|
||||
00383 SymVec a = sym.args();
|
||||
00384 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < a.size(); ++i) {
|
||||
00385 a[i] = normalize(a[i], args);
|
||||
00386 }
|
||||
00387
|
||||
00388 <span class="keywordflow">return</span> Sym(token, a);
|
||||
00389 }
|
||||
00390 }
|
||||
00391
|
||||
00392 <span class="keywordtype">bool</span> is_lambda(token_t token) {
|
||||
00393 <span class="keyword">const</span> Lambda* lambda = dynamic_cast<const Lambda*>( language[token]);
|
||||
00394 <span class="keywordflow">return</span> lambda != 0;
|
||||
00395 }
|
||||
00396
|
||||
00397 std::ostream& print_list(Sym sym, ostream& os) {
|
||||
00398 os << sym.token() << <span class="charliteral">' '</span>;
|
||||
00399
|
||||
00400 <span class="keyword">const</span> SymVec& args = sym.args();
|
||||
00401 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < args.size(); ++i) {
|
||||
00402 print_list(args[i], os);
|
||||
00403 }
|
||||
00404 <span class="keywordflow">return</span> os;
|
||||
00405 }
|
||||
00406
|
||||
00407 token_t new_lambda(Sym sym, <span class="keywordtype">int</span> arity) {
|
||||
00408 <span class="comment">// check if already present</span>
|
||||
00409
|
||||
00410 LambdaSet::iterator it = lambdaSet.find(sym);
|
||||
00411 <span class="keywordflow">if</span> (it != lambdaSet.end()) {
|
||||
00412 <span class="keywordflow">return</span> it->second;
|
||||
00413 }
|
||||
00414
|
||||
00415
|
||||
00416 <span class="comment">// new, insert</span>
|
||||
00417 Lambda* lambda = <span class="keyword">new</span> Lambda(sym, arity);
|
||||
00418
|
||||
00419 <span class="keywordflow">if</span> (free_list.empty()) {
|
||||
00420 extend_free_list();
|
||||
00421 }
|
||||
00422
|
||||
00423 token_t token = free_list.back();
|
||||
00424 free_list.pop_back();
|
||||
00425 language[token] = lambda;
|
||||
00426
|
||||
00427 lambdaSet[sym] = token;
|
||||
00428 <span class="keywordflow">if</span> (token_lambda.size() <= token) token_lambda.resize(token+1);
|
||||
00429 token_lambda[token] = sym;
|
||||
00430
|
||||
00431 <span class="keywordflow">return</span> token;
|
||||
00432 }
|
||||
00433
|
||||
00434 <span class="comment">/* Compression */</span>
|
||||
00435 <span class="keyword">typedef</span> hash_map<Sym, unsigned, HashSym> OccMap;
|
||||
00436
|
||||
00437 <span class="keywordtype">void</span> count_occurances(Sym sym, OccMap& occ) {
|
||||
00438 occ[sym]++;
|
||||
00439 <span class="keyword">const</span> SymVec& args = sym.args();
|
||||
00440 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < args.size(); ++i) {
|
||||
00441 count_occurances(args[i], occ);
|
||||
00442 }
|
||||
00443 }
|
||||
00444
|
||||
00445 Sym create_lambda(Sym sym, OccMap& occ, <span class="keywordtype">unsigned</span> nvars, vector<Sym>& args) {
|
||||
00446 <span class="keywordtype">unsigned</span> o = occ[sym];
|
||||
00447 <span class="keywordtype">unsigned</span> sz = sym.size();
|
||||
00448
|
||||
00449 <span class="keywordflow">if</span> (o * sz > o + sz + nvars || is_variable(sym.token()) ) {
|
||||
00450 <span class="comment">// check if it's already present</span>
|
||||
00451 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < args.size(); ++i) {
|
||||
00452 <span class="keywordflow">if</span> (args[i] == sym) {
|
||||
00453 <span class="keywordflow">return</span> SymVar(i);
|
||||
00454 }
|
||||
00455 }
|
||||
00456 <span class="comment">// push_back</span>
|
||||
00457 args.push_back(sym);
|
||||
00458 <span class="keywordflow">return</span> SymVar(args.size()-1);
|
||||
00459 }
|
||||
00460
|
||||
00461 SymVec sym_args = sym.args();
|
||||
00462 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < sym_args.size(); ++i) {
|
||||
00463 sym_args[i] = create_lambda(sym_args[i], occ, nvars, args);
|
||||
00464 }
|
||||
00465
|
||||
00466 <span class="keywordflow">return</span> Sym(sym.token(), sym_args);
|
||||
00467
|
||||
00468 }
|
||||
00469
|
||||
00470 Sym compress(Sym sym) {
|
||||
00471 OccMap occ(sym.size());
|
||||
00472 count_occurances(sym, occ);
|
||||
00473
|
||||
00474 <span class="keywordtype">unsigned</span> nvars = 0;
|
||||
00475 <span class="keywordflow">for</span> (OccMap::iterator it = occ.begin(); it != occ.end(); ++it) {
|
||||
00476 <span class="keywordflow">if</span> (is_variable(it->first.token())) nvars++;
|
||||
00477 }
|
||||
00478
|
||||
00479 SymVec args;
|
||||
00480 Sym body = create_lambda(sym, occ, nvars, args);
|
||||
00481
|
||||
00482
|
||||
00483 <span class="keywordflow">if</span> (body.size() < sym.size()) {
|
||||
00484 <span class="comment">// see if the body can be compressed some more</span>
|
||||
00485 body = compress(body);
|
||||
00486
|
||||
00487 token_t token = new_lambda(body, args.size());
|
||||
00488
|
||||
00489 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < args.size(); ++i) {
|
||||
00490 args[i] = compress(args[i]);
|
||||
00491 }
|
||||
00492
|
||||
00493 Sym result = Sym(token, args);
|
||||
00494 <span class="keywordflow">return</span> compress(result); <span class="comment">// see if it can be compressed some more</span>
|
||||
00495 }
|
||||
00496
|
||||
00497 <span class="keywordflow">return</span> sym;
|
||||
00498 }
|
||||
00499
|
||||
00500 Sym SymLambda(Sym expr) { <span class="keywordflow">return</span> compress(expr); }
|
||||
00501
|
||||
00502 Sym expand(Sym expr, <span class="keyword">const</span> SymVec& args) {
|
||||
00503
|
||||
00504 <span class="keyword">const</span> Var* var = dynamic_cast<const Var*>( language[expr.token()] );
|
||||
00505 <span class="keywordflow">if</span> (var != 0) {
|
||||
00506 <span class="keywordflow">return</span> args[var->idx];
|
||||
00507 }
|
||||
00508
|
||||
00509 SymVec expr_args = expr.args();
|
||||
00510 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < expr_args.size(); ++i) {
|
||||
00511 expr_args[i] = expand(expr_args[i], args);
|
||||
00512 }
|
||||
00513
|
||||
00514 <span class="keywordflow">return</span> Sym(expr.token(), expr_args);
|
||||
00515 }
|
||||
00516
|
||||
00517 Sym SymUnlambda(Sym sym) {
|
||||
00518 Sym retval = sym;
|
||||
00519 <span class="keyword">const</span> Lambda* lambda = dynamic_cast<const Lambda*>( language[sym.token()] );
|
||||
00520 <span class="keywordflow">if</span> (lambda != 0) {
|
||||
00521 retval = expand(lambda->expression, sym.args());
|
||||
00522 }
|
||||
00523
|
||||
00524 <span class="keywordflow">return</span> retval;
|
||||
00525 }
|
||||
00526
|
||||
00527 Sym expand_all(Sym sym) {
|
||||
00528 SymVec args = sym.args();
|
||||
00529 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < args.size(); ++i) {
|
||||
00530 args[i] = expand_all(args[i]);
|
||||
00531 }
|
||||
00532
|
||||
00533 Sym nw = SymUnlambda( Sym(sym.token(), args) );
|
||||
00534
|
||||
00535 <span class="keywordflow">if</span> (nw != sym) {
|
||||
00536 nw = expand_all(nw);
|
||||
00537 }
|
||||
00538
|
||||
00539 <span class="keywordflow">return</span> nw;
|
||||
00540 }
|
||||
00541
|
||||
00542 <span class="keyword">namespace </span>{
|
||||
00543
|
||||
00544 <span class="keyword">class </span>Sum : <span class="keyword">public</span> FunDef {
|
||||
00545
|
||||
00546 <span class="keyword">public</span> :
|
||||
00547
|
||||
00548 <span class="keywordtype">double</span> eval(<span class="keyword">const</span> vector<double>& vals, <span class="keyword">const</span> vector<double>& _)<span class="keyword"> const </span>{
|
||||
00549 <span class="keywordtype">double</span> res = 0;
|
||||
00550 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < vals.size(); ++i) res += vals[i];
|
||||
00551 <span class="keywordflow">return</span> res;
|
||||
00552 }
|
||||
00553
|
||||
00554 string c_print(<span class="keyword">const</span> vector<string>& args, <span class="keyword">const</span> vector<string>& _)<span class="keyword"> const </span>{
|
||||
00555 <span class="keywordflow">if</span> (args.empty()) { <span class="keywordflow">return</span> <span class="stringliteral">"0.0"</span>; }
|
||||
00556
|
||||
00557 ostringstream os;
|
||||
00558 os << <span class="stringliteral">"("</span> << args[0];
|
||||
00559 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 1; i < args.size(); ++i) {
|
||||
00560 os << <span class="stringliteral">"+"</span> << args[i];
|
||||
00561 }
|
||||
00562 os << <span class="stringliteral">")"</span>;
|
||||
00563 <span class="keywordflow">return</span> os.str();
|
||||
00564 }
|
||||
00565
|
||||
00566 Interval eval(<span class="keyword">const</span> vector<Interval>& args, <span class="keyword">const</span> vector<Interval>& inputs)<span class="keyword"> const </span>{
|
||||
00567 Interval interv(0.0);
|
||||
00568 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < args.size(); ++i) {
|
||||
00569 interv += args[i];
|
||||
00570 }
|
||||
00571 <span class="keywordflow">return</span> interv;
|
||||
00572 }
|
||||
00573
|
||||
00574 <span class="keywordtype">unsigned</span> min_arity()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> 0; }
|
||||
00575 <span class="keywordtype">bool</span> has_varargs()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="keyword">true</span>; }
|
||||
00576
|
||||
00577 string name()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="stringliteral">"sum"</span>; }
|
||||
00578 };
|
||||
00579
|
||||
00580
|
||||
00581 <span class="keyword">class </span>Prod : <span class="keyword">public</span> FunDef {
|
||||
00582
|
||||
00583 <span class="keyword">public</span> :
|
||||
00584
|
||||
00585 <span class="keywordtype">double</span> eval(<span class="keyword">const</span> vector<double>& vals, <span class="keyword">const</span> vector<double>& _)<span class="keyword"> const </span>{
|
||||
00586 <span class="keywordtype">double</span> res = 1;
|
||||
00587 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < vals.size(); ++i) res *= vals[i];
|
||||
00588 <span class="keywordflow">return</span> res;
|
||||
00589 }
|
||||
00590
|
||||
00591 string c_print(<span class="keyword">const</span> vector<string>& args, <span class="keyword">const</span> vector<string>& _)<span class="keyword"> const </span>{
|
||||
00592 <span class="keywordflow">if</span> (args.empty()) { <span class="keywordflow">return</span> <span class="stringliteral">"1.0"</span>; }
|
||||
00593
|
||||
00594 ostringstream os;
|
||||
00595 os << <span class="stringliteral">"("</span> << args[0];
|
||||
00596 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 1; i < args.size(); ++i) {
|
||||
00597 os << <span class="stringliteral">"*"</span> << args[i];
|
||||
00598 }
|
||||
00599 os << <span class="stringliteral">")"</span>;
|
||||
00600
|
||||
00601 <span class="keywordflow">return</span> os.str();
|
||||
00602 }
|
||||
00603
|
||||
00604 Interval eval(<span class="keyword">const</span> vector<Interval>& args, <span class="keyword">const</span> vector<Interval>& inputs)<span class="keyword"> const </span>{
|
||||
00605 Interval interv(1.0);
|
||||
00606 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < args.size(); ++i) {
|
||||
00607 interv *= args[i];
|
||||
00608 }
|
||||
00609 <span class="keywordflow">return</span> interv;
|
||||
00610 }
|
||||
00611
|
||||
00612 <span class="keywordtype">unsigned</span> min_arity()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> 0; }
|
||||
00613 <span class="keywordtype">bool</span> has_varargs()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="keyword">true</span>; }
|
||||
00614
|
||||
00615 string name()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="stringliteral">"prod"</span>; }
|
||||
00616 };
|
||||
00617
|
||||
00618
|
||||
00619 <span class="keyword">class </span>Power : <span class="keyword">public</span> FunDef {
|
||||
00620 <span class="keyword">public</span> :
|
||||
00621 <span class="keywordtype">double</span> eval(<span class="keyword">const</span> vector<double>& vals, <span class="keyword">const</span> vector<double>& _)<span class="keyword"> const </span>{
|
||||
00622 <span class="keywordflow">return</span> pow(vals[0], vals[1]);
|
||||
00623 }
|
||||
00624
|
||||
00625 string c_print(<span class="keyword">const</span> vector<string>& args, <span class="keyword">const</span> vector<string>& _)<span class="keyword"> const </span>{
|
||||
00626 <span class="keywordflow">return</span> <span class="stringliteral">"pow("</span> + args[0] + <span class="charliteral">','</span> + args[1] + <span class="charliteral">')'</span>;
|
||||
00627 }
|
||||
00628
|
||||
00629 Interval eval(<span class="keyword">const</span> vector<Interval>& args, <span class="keyword">const</span> vector<Interval>& _)<span class="keyword"> const </span>{
|
||||
00630 Interval first = args[0];
|
||||
00631 Interval second = args[1];
|
||||
00632 Interval lg = log(first);
|
||||
00633 <span class="keywordflow">if</span> (!valid(lg)) <span class="keywordflow">throw</span> interval_error();
|
||||
00634 <span class="keywordflow">return</span> exp(second * lg);
|
||||
00635 }
|
||||
00636
|
||||
00637 <span class="keywordtype">unsigned</span> min_arity()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> 2; }
|
||||
00638
|
||||
00639 string name()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="stringliteral">"pow"</span>; }
|
||||
00640 };
|
||||
00641
|
||||
00642 <span class="keyword">class </span>IsNeg : <span class="keyword">public</span> FunDef {
|
||||
00643
|
||||
00644 <span class="keyword">public</span>:
|
||||
00645 <span class="keywordtype">double</span> eval(<span class="keyword">const</span> vector<double>& vals, <span class="keyword">const</span> vector<double>& _)<span class="keyword"> const </span>{
|
||||
00646 <span class="keywordflow">if</span> (vals[0] < 0.0) <span class="keywordflow">return</span> vals[1];
|
||||
00647 <span class="keywordflow">return</span> vals[2];
|
||||
00648 }
|
||||
00649
|
||||
00650 <span class="keywordtype">double</span> eval(<span class="keyword">const</span> Sym& sym, <span class="keyword">const</span> vector<double>& inputs)<span class="keyword"> const </span>{
|
||||
00651 <span class="keyword">const</span> SymVec& args = sym.args();
|
||||
00652 <span class="keywordtype">double</span> arg0 = ::eval(args[0], inputs);
|
||||
00653 <span class="keywordflow">if</span> (arg0 < 0.0) {
|
||||
00654 return ::eval(args[1], inputs);
|
||||
00655 }
|
||||
00656 return ::eval(args[2], inputs);
|
||||
00657 }
|
||||
00658
|
||||
00659 string c_print(<span class="keyword">const</span> vector<string>& args, <span class="keyword">const</span> vector<string>& _)<span class="keyword"> const </span>{
|
||||
00660 <span class="keywordflow">return</span> <span class="stringliteral">"(("</span> + args[0] + <span class="stringliteral">"<0.0)?"</span> + args[1] + <span class="stringliteral">":"</span> + args[2]+<span class="stringliteral">")"</span>;
|
||||
00661 }
|
||||
00662
|
||||
00663 Interval eval(<span class="keyword">const</span> vector<Interval>& args, <span class="keyword">const</span> vector<Interval>& _)<span class="keyword"> const </span>{
|
||||
00664 Interval a0 = args[0];
|
||||
00665 <span class="keywordflow">if</span> (a0.upper() < 0.0) <span class="keywordflow">return</span> args[1];
|
||||
00666 <span class="keywordflow">if</span> (a0.lower() >= 0.0) <span class="keywordflow">return</span> args[2];
|
||||
00667
|
||||
00668 <span class="keywordflow">return</span> Interval( std::min(args[1].lower(), args[2].lower()), std::max(args[1].upper(), args[2].upper()));
|
||||
00669 }
|
||||
00670
|
||||
00671 <span class="keywordtype">unsigned</span> min_arity()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> 3; }
|
||||
00672
|
||||
00673 string name()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="stringliteral">"ifltz"</span>; }
|
||||
00674 };
|
||||
00675
|
||||
00676 <span class="keyword">template</span> <<span class="keyword">typename</span> Func>
|
||||
00677 <span class="keyword">class </span>Unary : <span class="keyword">public</span> FunDef {
|
||||
00678
|
||||
00679 Func un;
|
||||
00680
|
||||
00681 <span class="keywordtype">double</span> eval(<span class="keyword">const</span> vector<double>& vals, <span class="keyword">const</span> vector<double>& _)<span class="keyword"> const </span>{
|
||||
00682 <span class="keywordflow">return</span> un(vals[0]);
|
||||
00683 }
|
||||
00684
|
||||
00685 string c_print(<span class="keyword">const</span> vector<string>& args, <span class="keyword">const</span> vector<string>& _)<span class="keyword"> const </span>{
|
||||
00686 <span class="keywordflow">return</span> un(args[0]);
|
||||
00687 }
|
||||
00688
|
||||
00689 Interval eval(<span class="keyword">const</span> vector<Interval>& args, <span class="keyword">const</span> vector<Interval>& _)<span class="keyword"> const </span>{
|
||||
00690 <span class="keywordflow">return</span> un(args[0]);
|
||||
00691 }
|
||||
00692
|
||||
00693 <span class="keywordtype">unsigned</span> min_arity()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> 1; }
|
||||
00694
|
||||
00695 string name()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> un.name(); }
|
||||
00696
|
||||
00697 };
|
||||
00698
|
||||
00699 <span class="keyword">struct </span>Inv {
|
||||
00700 <span class="keywordtype">double</span> operator()(<span class="keywordtype">double</span> val)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> 1.0 / val; }
|
||||
00701 string operator()(string v)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="stringliteral">"(1./"</span> + v + <span class="stringliteral">")"</span>; }
|
||||
00702 Interval operator()(Interval v)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> 1.0 / v; }
|
||||
00703
|
||||
00704 string name()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="stringliteral">"inv"</span>; }
|
||||
00705 };
|
||||
00706
|
||||
00707 <span class="keyword">struct </span>Min {
|
||||
00708 <span class="keywordtype">double</span> operator()(<span class="keywordtype">double</span> val)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> -val; }
|
||||
00709 string operator()(string v)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="stringliteral">"(-"</span> + v + <span class="stringliteral">")"</span>; }
|
||||
00710 Interval operator()(Interval v)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> -v; }
|
||||
00711
|
||||
00712 string name()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="stringliteral">"min"</span>; }
|
||||
00713 };
|
||||
00714
|
||||
00715 } <span class="comment">// namespace</span>
|
||||
00716
|
||||
00717 string prototypes = <span class="stringliteral">"double pow(double, double);"</span>;
|
||||
00718 string get_prototypes() { <span class="keywordflow">return</span> prototypes; }
|
||||
00719 <span class="keywordtype">unsigned</span> add_prototype(string str) { prototypes += string(<span class="stringliteral">"double "</span>) + str + <span class="stringliteral">"(double);"</span>; <span class="keywordflow">return</span> prototypes.size(); }
|
||||
00720
|
||||
00721 token_t add_function(FunDef* function, token_t where) {
|
||||
00722 <span class="keywordflow">if</span> (language.size() <= where) language.resize(where+1);
|
||||
00723 language[where] = function;
|
||||
00724 <span class="keywordflow">return</span> 0;
|
||||
00725 }
|
||||
00726
|
||||
00727 <span class="keyword">namespace </span>{
|
||||
00728
|
||||
00729 <span class="preprocessor">#define FUNCDEF(funcname) struct funcname##_struct { \</span>
|
||||
00730 <span class="preprocessor"> double operator()(double val) const { return funcname(val); }\</span>
|
||||
00731 <span class="preprocessor"> string operator()(string val) const { return string(#funcname) + '(' + val + ')'; }\</span>
|
||||
00732 <span class="preprocessor"> Interval operator()(Interval val) const { return funcname(val); }\</span>
|
||||
00733 <span class="preprocessor"> string name() const { return string(#funcname); }\</span>
|
||||
00734 <span class="preprocessor">};\</span>
|
||||
00735 <span class="preprocessor">static const token_t funcname##_token_static = add_function( new Unary<funcname##_struct>, funcname##_token);\</span>
|
||||
00736 <span class="preprocessor">unsigned funcname##_size = add_prototype(#funcname);</span>
|
||||
00737 <span class="preprocessor"></span>
|
||||
00738 <span class="keyword">static</span> token_t ssum_token = add_function( <span class="keyword">new</span> Sum , sum_token);
|
||||
00739 <span class="keyword">static</span> token_t sprod_token = add_function( <span class="keyword">new</span> Prod, prod_token);
|
||||
00740 <span class="keyword">static</span> token_t sinv_token = add_function( <span class="keyword">new</span> Unary<Inv>, inv_token);
|
||||
00741 <span class="keyword">static</span> token_t smin_token = add_function( <span class="keyword">new</span> Unary<Min>, min_token);
|
||||
00742 <span class="keyword">static</span> token_t spow_token = add_function( <span class="keyword">new</span> Power, pow_token);
|
||||
00743 <span class="keyword">static</span> token_t sifltz_token = add_function( <span class="keyword">new</span> IsNeg, ifltz_token);
|
||||
00744
|
||||
00745 FUNCDEF(sin);
|
||||
00746 FUNCDEF(cos);
|
||||
00747 FUNCDEF(tan);
|
||||
00748 FUNCDEF(asin);
|
||||
00749 FUNCDEF(acos);
|
||||
00750 FUNCDEF(atan);
|
||||
00751
|
||||
00752 FUNCDEF(sinh);
|
||||
00753 FUNCDEF(cosh);
|
||||
00754 FUNCDEF(tanh);
|
||||
00755 FUNCDEF(asinh);
|
||||
00756 FUNCDEF(acosh);
|
||||
00757 FUNCDEF(atanh);
|
||||
00758
|
||||
00759 FUNCDEF(exp);
|
||||
00760 FUNCDEF(log);
|
||||
00761 } <span class="comment">// namespace</span>
|
||||
00762
|
||||
00763 <span class="keywordtype">double</span> sqr(<span class="keywordtype">double</span> x) { <span class="keywordflow">return</span> x*x; }
|
||||
00764
|
||||
00765 <span class="keyword">namespace </span>{
|
||||
00766 FUNCDEF(sqr);
|
||||
00767 FUNCDEF(sqrt);
|
||||
00768
|
||||
00769 <span class="keyword">const</span> <span class="keywordtype">int</span> buildInFunctionOffset = language.size();
|
||||
00770 } <span class="comment">// namespace </span>
|
||||
00771
|
||||
00772 <span class="keywordtype">void</span> add_tokens() {
|
||||
00773 <span class="keywordtype">unsigned</span> sz = language.size();
|
||||
00774 language.resize(sz + sz+1); <span class="comment">// double</span>
|
||||
00775
|
||||
00776 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = sz; i < language.size(); ++i) {
|
||||
00777 free_list.push_back(i);
|
||||
00778 }
|
||||
00779 }
|
||||
00780
|
||||
00781 <span class="keywordtype">void</span> extend_free_list() {
|
||||
00782 <span class="comment">// first check if we can clean up unused tokens;</span>
|
||||
00783 <span class="keyword">const</span> vector<unsigned>& refcount = Sym::token_refcount();
|
||||
00784 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = buildInFunctionOffset; i < refcount.size(); ++i) {
|
||||
00785 <span class="keywordflow">if</span> (language[i] == 0) <span class="keywordflow">continue</span>;
|
||||
00786
|
||||
00787 <span class="keywordtype">bool</span> c = is_constant(i);
|
||||
00788 <span class="keywordtype">bool</span> l = is_lambda(i);
|
||||
00789
|
||||
00790 <span class="keywordflow">if</span> (refcount[i] == 0 && (c || l)) {
|
||||
00791
|
||||
00792 <span class="keywordflow">if</span> (c) {
|
||||
00793 doubleSet.erase(token_value[i]);
|
||||
00794 }
|
||||
00795
|
||||
00796 <span class="keywordflow">if</span> (l) {
|
||||
00797 lambdaSet.erase(token_lambda[i]);
|
||||
00798 }
|
||||
00799
|
||||
00800 <span class="keyword">delete</span> language[i];
|
||||
00801 language[i] = 0;
|
||||
00802 free_list.push_back(i);
|
||||
00803 }
|
||||
00804 }
|
||||
00805
|
||||
00806 <span class="comment">// if still empty, add new tokens</span>
|
||||
00807 <span class="keywordflow">if</span> (free_list.empty()) {
|
||||
00808 add_tokens();
|
||||
00809 }
|
||||
00810 }
|
||||
00811
|
||||
00812
|
||||
00813 <span class="comment">/* Serialization */</span>
|
||||
00814 <span class="keywordtype">void</span> write_raw(ostream& os, <span class="keyword">const</span> Sym& sym) {
|
||||
00815 token_t token = sym.token();
|
||||
00816 <span class="keyword">const</span> SymVec& args = sym.args();
|
||||
00817
|
||||
00818 <span class="keywordflow">if</span> (is_constant(token)) {
|
||||
00819 os << <span class="stringliteral">"c"</span> << language[token]->c_print(vector<string>(), vector<string>());
|
||||
00820 } <span class="keywordflow">else</span> {
|
||||
00821
|
||||
00822 <span class="keyword">const</span> Var* var = dynamic_cast<const Var*>( language[token] );
|
||||
00823
|
||||
00824 <span class="keywordflow">if</span> (var != 0) {
|
||||
00825 os << <span class="stringliteral">"v"</span> << var->idx;
|
||||
00826 } <span class="keywordflow">else</span> {
|
||||
00827 os << <span class="stringliteral">"f"</span> << token << <span class="charliteral">' '</span> << args.size();
|
||||
00828 }
|
||||
00829 }
|
||||
00830
|
||||
00831 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < args.size(); ++i) {
|
||||
00832 write_raw(os, args[i]);
|
||||
00833 }
|
||||
00834 }
|
||||
00835
|
||||
00836 string write_raw(<span class="keyword">const</span> Sym& sym) {
|
||||
00837
|
||||
00838 ostringstream os;
|
||||
00839 write_raw(os, sym);
|
||||
00840
|
||||
00841 <span class="keywordflow">return</span> os.str();
|
||||
00842 }
|
||||
00843
|
||||
00844 Sym read_raw(istream& is) {
|
||||
00845 <span class="keywordtype">char</span> <span class="keywordtype">id</span> = is.get();
|
||||
00846
|
||||
00847 <span class="keywordflow">switch</span> (id) {
|
||||
00848 <span class="keywordflow">case</span> <span class="charliteral">'c'</span> :
|
||||
00849 {
|
||||
00850 <span class="keywordtype">double</span> val;
|
||||
00851 is.get(); <span class="comment">// skip '('</span>
|
||||
00852 is >> val;
|
||||
00853 is.get(); <span class="comment">// skip ')'</span>
|
||||
00854 <span class="keywordflow">return</span> SymConst(val);
|
||||
00855 }
|
||||
00856 <span class="keywordflow">case</span> <span class="charliteral">'v'</span> :
|
||||
00857 {
|
||||
00858 <span class="keywordtype">unsigned</span> idx;
|
||||
00859 is >> idx;
|
||||
00860 <span class="keywordflow">return</span> SymVar(idx);
|
||||
00861 }
|
||||
00862 <span class="keywordflow">case</span> <span class="charliteral">'f'</span> :
|
||||
00863 {
|
||||
00864 token_t token;
|
||||
00865 <span class="keywordtype">unsigned</span> arity;
|
||||
00866 is >> token;
|
||||
00867 is >> arity;
|
||||
00868 SymVec args(arity);
|
||||
00869 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < arity; ++i) {
|
||||
00870 args[i] = read_raw(is);
|
||||
00871 }
|
||||
00872
|
||||
00873 <span class="keywordflow">return</span> Sym(token, args);
|
||||
00874 }
|
||||
00875 <span class="keywordflow">default</span> : {
|
||||
00876 cerr << <span class="stringliteral">"Character = "</span> << <span class="keywordtype">id</span> << <span class="stringliteral">" Could not read formula from stream"</span> << endl;
|
||||
00877 exit(1);
|
||||
00878 }
|
||||
00879
|
||||
00880 }
|
||||
00881
|
||||
00882 <span class="keywordflow">return</span> Sym();
|
||||
00883 }
|
||||
00884
|
||||
00885 Sym read_raw(string str) {
|
||||
00886 istringstream is(str);
|
||||
00887 <span class="keywordflow">return</span> read_raw(is);
|
||||
00888 }
|
||||
00889
|
||||
00890 <span class="keywordtype">void</span> read_raw(istream& is, Sym& sym) {
|
||||
00891 sym = read_raw(is);
|
||||
00892 }
|
||||
00893
|
||||
</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>
|
||||
171
trunk/paradiseo-eo/doc/html/_fun_def_8h-source.html
Normal file
171
trunk/paradiseo-eo/doc/html/_fun_def_8h-source.html
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
<!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: FunDef.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_000007.html">contrib</a> / <a class="el" href="dir_000008.html">mathsym</a> / <a class="el" href="dir_000022.html">fun</a></div>
|
||||
<h1>FunDef.h</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">/* </span>
|
||||
00002 <span class="comment"> * Copyright (C) 2005 Maarten Keijzer</span>
|
||||
00003 <span class="comment"> *</span>
|
||||
00004 <span class="comment"> * This program is free software; you can redistribute it and/or modify</span>
|
||||
00005 <span class="comment"> * it under the terms of version 2 of the GNU General Public License as </span>
|
||||
00006 <span class="comment"> * published by the Free Software Foundation. </span>
|
||||
00007 <span class="comment"> *</span>
|
||||
00008 <span class="comment"> * This program is distributed in the hope that it will be useful,</span>
|
||||
00009 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
|
||||
00010 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
|
||||
00011 <span class="comment"> * GNU General Public License for more details.</span>
|
||||
00012 <span class="comment"> *</span>
|
||||
00013 <span class="comment"> * You should have received a copy of the GNU General Public License</span>
|
||||
00014 <span class="comment"> * along with this program; if not, write to the Free Software</span>
|
||||
00015 <span class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
|
||||
00016 <span class="comment"> */</span>
|
||||
00017
|
||||
00018 <span class="preprocessor">#ifndef FUNCTION_DEF_H_</span>
|
||||
00019 <span class="preprocessor"></span><span class="preprocessor">#define FUNCTION_DEF_H_</span>
|
||||
00020 <span class="preprocessor"></span>
|
||||
00021 <span class="preprocessor">#include <string></span>
|
||||
00022 <span class="preprocessor">#include <vector></span>
|
||||
00023 <span class="preprocessor">#include <memory></span>
|
||||
00024 <span class="preprocessor">#include <iostream></span>
|
||||
00025
|
||||
00026 <span class="preprocessor">#include "Sym.h"</span>
|
||||
00027 <span class="preprocessor">#include "Interval.h"</span>
|
||||
00028
|
||||
00029 <span class="keyword">class </span>FunDef {
|
||||
00030 <span class="keyword">public</span>:
|
||||
00031
|
||||
00032 <span class="keyword">virtual</span> ~FunDef() {}
|
||||
00033
|
||||
00034 <span class="comment">// (possibly) lazy evaluation function, default implementation calls 'eager' eval</span>
|
||||
00035 <span class="keyword">virtual</span> <span class="keywordtype">double</span> eval(<span class="keyword">const</span> SymVec& args, <span class="keyword">const</span> std::vector<double>& inputs) <span class="keyword">const</span>;
|
||||
00036
|
||||
00037 <span class="comment">// eager evaluation function </span>
|
||||
00038 <span class="keyword">virtual</span> <span class="keywordtype">double</span> eval(<span class="keyword">const</span> std::vector<double>& args, <span class="keyword">const</span> std::vector<double>& inputs) <span class="keyword">const </span>= 0;
|
||||
00039
|
||||
00040 <span class="comment">// interval evaluation</span>
|
||||
00041 <span class="keyword">virtual</span> Interval eval(<span class="keyword">const</span> std::vector<Interval>& args, <span class="keyword">const</span> std::vector<Interval>& inputs) <span class="keyword">const </span>= 0;
|
||||
00042
|
||||
00043 <span class="comment">// prints 'c' like code</span>
|
||||
00044 <span class="keyword">virtual</span> std::string c_print(<span class="keyword">const</span> std::vector<std::string>& names, <span class="keyword">const</span> std::vector<std::string>& names) <span class="keyword">const </span>= 0;
|
||||
00045
|
||||
00046 <span class="keyword">virtual</span> <span class="keywordtype">unsigned</span> min_arity() <span class="keyword">const </span>= 0;
|
||||
00047 <span class="keyword">virtual</span> <span class="keywordtype">bool</span> has_varargs()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="keyword">false</span>; } <span class="comment">// sum, prod, min, max are variable arity</span>
|
||||
00048
|
||||
00049 <span class="keyword">virtual</span> std::string name() <span class="keyword">const </span>= 0;
|
||||
00050
|
||||
00051 <span class="keyword">protected</span>:
|
||||
00052
|
||||
00053 };
|
||||
00054
|
||||
00056 <span class="keyword">extern</span> std::vector<const FunDef*> get_defined_functions();
|
||||
00057
|
||||
00059 <span class="keyword">extern</span> <span class="keyword">const</span> FunDef& get_element(token_t token);
|
||||
00060
|
||||
00062 <span class="keyword">extern</span> <span class="keywordtype">double</span> eval(<span class="keyword">const</span> Sym& sym, <span class="keyword">const</span> std::vector<double>& inputs);
|
||||
00063
|
||||
00065 <span class="keyword">extern</span> Interval eval(<span class="keyword">const</span> Sym& sym, <span class="keyword">const</span> std::vector<Interval>& inputs);
|
||||
00066
|
||||
00068 <span class="keyword">extern</span> std::string c_print(<span class="keyword">const</span> Sym& sym);
|
||||
00069
|
||||
00071 <span class="keyword">extern</span> std::string c_print(<span class="keyword">const</span> Sym& sym, <span class="keyword">const</span> std::vector<std::string>& var_names);
|
||||
00072
|
||||
00074 <span class="keyword">inline</span> std::ostream& operator<<(std::ostream& os, <span class="keyword">const</span> Sym& sym) { <span class="keywordflow">return</span> os << c_print(sym); }
|
||||
00075
|
||||
00076 <span class="comment">/* Support for Ephemeral Random Constants (ERC) */</span>
|
||||
00077
|
||||
00079 <span class="keyword">extern</span> Sym SymConst(<span class="keywordtype">double</span> value);
|
||||
00081 <span class="keyword">extern</span> Sym SymVar(<span class="keywordtype">unsigned</span> idx);
|
||||
00082
|
||||
00093 <span class="keyword">extern</span> Sym SymLambda(Sym expression);
|
||||
00094
|
||||
00095 <span class="keyword">extern</span> Sym SymUnlambda(Sym sym);
|
||||
00096
|
||||
00098 <span class="keyword">extern</span> Sym expand_all(Sym sym);
|
||||
00099 <span class="keyword">extern</span> Sym compress(Sym sym);
|
||||
00100
|
||||
00102 std::vector<double> get_constants(Sym sym);
|
||||
00103
|
||||
00106 Sym set_constants(Sym sym, <span class="keyword">const</span> std::vector<double>& constants);
|
||||
00107
|
||||
00109 <span class="keyword">extern</span> <span class="keywordtype">bool</span> is_constant(token_t token);
|
||||
00110 <span class="keyword">extern</span> <span class="keywordtype">double</span> get_constant_value(token_t token);
|
||||
00112 <span class="keyword">extern</span> <span class="keywordtype">bool</span> is_variable(token_t token);
|
||||
00113 <span class="keyword">extern</span> <span class="keywordtype">unsigned</span> get_variable_index(token_t token);
|
||||
00114
|
||||
00116 <span class="keyword">extern</span> <span class="keywordtype">bool</span> is_lambda(token_t token);
|
||||
00117
|
||||
00118
|
||||
00120 <span class="keyword">extern</span> Sym simplify(Sym sym);
|
||||
00121
|
||||
00125 <span class="keyword">extern</span> Sym differentiate(Sym sym, token_t dx);
|
||||
00126 <span class="keyword">struct </span>differentiation_error{}; <span class="comment">// thrown in case of ifltz</span>
|
||||
00127
|
||||
00128 <span class="comment">/* Add function to the language table (and take a guess at the arity) */</span>
|
||||
00129 <span class="keyword">class </span>LanguageTable;
|
||||
00130 <span class="keyword">extern</span> <span class="keywordtype">void</span> add_function_to_table(LanguageTable& table, token_t token);
|
||||
00131
|
||||
00132 <span class="keyword">enum</span> {
|
||||
00133 sum_token,
|
||||
00134 prod_token,
|
||||
00135 inv_token,
|
||||
00136 min_token,
|
||||
00137 pow_token,
|
||||
00138 ifltz_token,
|
||||
00139 sin_token, cos_token, tan_token,
|
||||
00140 asin_token, acos_token, atan_token,
|
||||
00141 sinh_token, cosh_token, tanh_token,
|
||||
00142 acosh_token, asinh_token, atanh_token,
|
||||
00143 exp_token, log_token,
|
||||
00144 sqr_token, sqrt_token
|
||||
00145 };
|
||||
00146
|
||||
00147 <span class="comment">/* Defition of function overloads: for example, this define the function 'Sym sin(Sym)' */</span>
|
||||
00148
|
||||
00149 <span class="preprocessor">#define HEADERFUNC(name) inline Sym name(Sym arg) { return Sym(name##_token, arg); }</span>
|
||||
00150 <span class="preprocessor"></span>
|
||||
00151 <span class="comment">/* This defines the tokens: sin_token, cos_token, etc. */</span>
|
||||
00152 HEADERFUNC(inv);
|
||||
00153 HEADERFUNC(sin);
|
||||
00154 HEADERFUNC(cos);
|
||||
00155 HEADERFUNC(tan);
|
||||
00156 HEADERFUNC(asin);
|
||||
00157 HEADERFUNC(acos);
|
||||
00158 HEADERFUNC(atan);
|
||||
00159
|
||||
00160 HEADERFUNC(sinh);
|
||||
00161 HEADERFUNC(cosh);
|
||||
00162 HEADERFUNC(tanh);
|
||||
00163 HEADERFUNC(asinh);
|
||||
00164 HEADERFUNC(acosh);
|
||||
00165 HEADERFUNC(atanh);
|
||||
00166
|
||||
00167 HEADERFUNC(exp);
|
||||
00168 HEADERFUNC(log);
|
||||
00169
|
||||
00170 HEADERFUNC(sqr);
|
||||
00171 HEADERFUNC(sqrt);
|
||||
00172
|
||||
00173 <span class="comment">/* Get the prototype functions out, this is needed for compilation */</span>
|
||||
00174 <span class="keyword">extern</span> std::string get_prototypes();
|
||||
00175
|
||||
00176 <span class="comment">// reading and writing in internal format, no parser for symbolic functions implemented yet</span>
|
||||
00177 <span class="keyword">extern</span> std::string write_raw(<span class="keyword">const</span> Sym& sym);
|
||||
00178 <span class="keyword">extern</span> <span class="keywordtype">void</span> write_raw(std::ostream& os, <span class="keyword">const</span> Sym& sym);
|
||||
00179 <span class="keyword">extern</span> Sym read_raw(std::string str);
|
||||
00180 <span class="keyword">extern</span> Sym read_raw(std::istream& is);
|
||||
00181 <span class="keyword">extern</span> <span class="keywordtype">void</span> read_raw(std::istream& is, Sym& sym);
|
||||
00182
|
||||
00183 <span class="preprocessor">#include "SymOps.h"</span>
|
||||
00184
|
||||
00185 <span class="preprocessor">#endif</span>
|
||||
00186 <span class="preprocessor"></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>
|
||||
130
trunk/paradiseo-eo/doc/html/_interval_8h-source.html
Normal file
130
trunk/paradiseo-eo/doc/html/_interval_8h-source.html
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
<!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: Interval.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_000007.html">contrib</a> / <a class="el" href="dir_000008.html">mathsym</a> / <a class="el" href="dir_000009.html">eval</a></div>
|
||||
<h1>Interval.h</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">/* </span>
|
||||
00002 <span class="comment"> * Copyright (C) 2005 Maarten Keijzer</span>
|
||||
00003 <span class="comment"> *</span>
|
||||
00004 <span class="comment"> * This program is free software; you can redistribute it and/or modify</span>
|
||||
00005 <span class="comment"> * it under the terms of version 2 of the GNU General Public License as </span>
|
||||
00006 <span class="comment"> * published by the Free Software Foundation. </span>
|
||||
00007 <span class="comment"> *</span>
|
||||
00008 <span class="comment"> * This program is distributed in the hope that it will be useful,</span>
|
||||
00009 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
|
||||
00010 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
|
||||
00011 <span class="comment"> * GNU General Public License for more details.</span>
|
||||
00012 <span class="comment"> *</span>
|
||||
00013 <span class="comment"> * You should have received a copy of the GNU General Public License</span>
|
||||
00014 <span class="comment"> * along with this program; if not, write to the Free Software</span>
|
||||
00015 <span class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
|
||||
00016 <span class="comment"> */</span>
|
||||
00017
|
||||
00018 <span class="preprocessor">#ifndef INTERVAL__H__</span>
|
||||
00019 <span class="preprocessor"></span><span class="preprocessor">#define INTERVAL__H__</span>
|
||||
00020 <span class="preprocessor"></span>
|
||||
00021 <span class="preprocessor">#include <boost/numeric/interval.hpp></span>
|
||||
00022 <span class="preprocessor">#include <iostream></span>
|
||||
00023 <span class="preprocessor">#include <limits></span>
|
||||
00024
|
||||
00025
|
||||
00026 <span class="keyword">typedef</span> boost::numeric::interval_lib::rounded_transc_exact<double> RoundingTransc;
|
||||
00027 <span class="keyword">typedef</span> boost::numeric::interval_lib::save_state<RoundingTransc> Rounding;
|
||||
00028 <span class="keyword">typedef</span> boost::numeric::interval_lib::checking_base<double> Checking;
|
||||
00029 <span class="keyword">typedef</span> boost::numeric::interval_lib::policies<Rounding,Checking> Policy;
|
||||
00030 <span class="keyword">typedef</span> boost::numeric::interval<double, Policy> Interval;
|
||||
00031
|
||||
00032 <span class="keyword">struct </span>interval_error{};
|
||||
00033
|
||||
00034 <span class="keyword">inline</span> <span class="keywordtype">bool</span> valid(<span class="keyword">const</span> Interval& val) {
|
||||
00035 <span class="keywordflow">if</span> (!finite(val.lower()) || !finite(val.upper())) <span class="keywordflow">return</span> <span class="keyword">false</span>;
|
||||
00036
|
||||
00037 <span class="keywordflow">return</span> val.lower() > -1e10 && val.upper() < 1e10;
|
||||
00038 }
|
||||
00039
|
||||
00040 <span class="keyword">inline</span> Interval sqrt(<span class="keyword">const</span> Interval& val) {
|
||||
00041 <span class="keywordflow">if</span> (val.lower() < 0.0) {
|
||||
00042 <span class="keywordflow">return</span> Interval::whole();
|
||||
00043 }
|
||||
00044
|
||||
00045 <span class="keywordflow">return</span> boost::numeric::sqrt(val);
|
||||
00046 }
|
||||
00047
|
||||
00048 <span class="keyword">inline</span> Interval sqr(<span class="keyword">const</span> Interval& val) {
|
||||
00049 <span class="keywordflow">return</span> square(val);
|
||||
00050 }
|
||||
00051
|
||||
00052 <span class="keyword">inline</span> Interval acos(<span class="keyword">const</span> Interval& val) {
|
||||
00053 <span class="keywordflow">if</span> (val.lower() < 1.0 || val.upper() > 1.0) {
|
||||
00054 <span class="keywordflow">return</span> Interval::whole();
|
||||
00055 }
|
||||
00056
|
||||
00057 <span class="keywordflow">return</span> boost::numeric::acos(val);
|
||||
00058 }
|
||||
00059
|
||||
00060 <span class="keyword">inline</span> Interval asin(<span class="keyword">const</span> Interval& val) {
|
||||
00061 <span class="keywordflow">if</span> (val.lower() < 1.0 || val.upper() > 1.0) {
|
||||
00062 <span class="keywordflow">return</span> Interval::whole();
|
||||
00063 }
|
||||
00064
|
||||
00065 <span class="keywordflow">return</span> boost::numeric::asin(val);
|
||||
00066 }
|
||||
00067
|
||||
00068 <span class="keyword">inline</span> Interval acosh(<span class="keyword">const</span> Interval& val) {
|
||||
00069 <span class="keywordflow">if</span> (val.lower() < 1.0) <span class="keywordflow">return</span> Interval::whole();
|
||||
00070 <span class="keywordflow">return</span> boost::numeric::acosh(val);
|
||||
00071 }
|
||||
00072
|
||||
00073 <span class="keyword">inline</span>
|
||||
00074 std::ostream& operator<<(std::ostream& os, <span class="keyword">const</span> Interval& val) {
|
||||
00075 os << <span class="charliteral">'['</span> << val.lower() << <span class="stringliteral">", "</span> << val.upper() << <span class="charliteral">']'</span>;
|
||||
00076 <span class="keywordflow">return</span> os;
|
||||
00077 }
|
||||
00078
|
||||
00079 <span class="preprocessor">#ifdef TEST_INTERVAL</span>
|
||||
00080 <span class="preprocessor"></span><span class="keyword">using</span> <span class="keyword">namespace </span>std;
|
||||
00081 <span class="keyword">using</span> <span class="keyword">namespace </span>boost::numeric;
|
||||
00082
|
||||
00083 <span class="keywordtype">int</span> main() {
|
||||
00084 Interval a(0, 10);
|
||||
00085 Interval b(-1.5, 2);
|
||||
00086 cout << <span class="stringliteral">"a = "</span> << a << endl;
|
||||
00087 cout << <span class="stringliteral">"b = "</span> << b << endl;
|
||||
00088 cout << <span class="stringliteral">"a + b = "</span> << a + b << endl;
|
||||
00089 cout << <span class="stringliteral">"a - b = "</span> << a - b << endl;
|
||||
00090 cout << <span class="stringliteral">"b - a = "</span> << b - a << endl;
|
||||
00091 cout << <span class="stringliteral">"-a = "</span> << -a << endl;
|
||||
00092 cout << <span class="stringliteral">"a * b = "</span> << a * b << endl;
|
||||
00093 cout << <span class="stringliteral">"b/(a+1) = "</span> << b / (a + 1.0) << endl;
|
||||
00094 cout << <span class="stringliteral">"b * a = "</span> << b * a << endl;
|
||||
00095
|
||||
00096 cout << <span class="stringliteral">"b / a = "</span> << b/a << endl;
|
||||
00097
|
||||
00098 cout << <span class="stringliteral">"cos(a) = "</span> << cos(a) << endl;
|
||||
00099 cout << <span class="stringliteral">"cos(b) = "</span> << cos(b) << endl;
|
||||
00100
|
||||
00101 cout << <span class="stringliteral">"log(b) = "</span> << log(b) << endl;
|
||||
00102
|
||||
00103 cout << <span class="stringliteral">"sqrt(b) = "</span> << sqrt(b) << endl;
|
||||
00104 cout << <span class="stringliteral">"sqrt(a) = "</span> << sqrt(a) << endl;
|
||||
00105 cout << <span class="stringliteral">"sqr(b) = "</span> << sqr(b) << endl;
|
||||
00106
|
||||
00107 cout << <span class="stringliteral">"exp(b*a)= "</span> << exp(b*a) << endl;
|
||||
00108
|
||||
00109 cout << <span class="stringliteral">"atan(a) = "</span> << atan(a) << endl;
|
||||
00110 cout << <span class="stringliteral">"cosh(b) = "</span> << cosh(b) << endl;
|
||||
00111
|
||||
00112 }
|
||||
00113 <span class="preprocessor">#endif</span>
|
||||
00114 <span class="preprocessor"></span>
|
||||
00115 <span class="preprocessor">#endif</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>
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue