360 lines
18 KiB
HTML
360 lines
18 KiB
HTML
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<meta name="GENERATOR" content="Mozilla/4.78 [en] (X11; U; Linux 2.4.7-10 i686) [Netscape]">
|
|
<title>Templates/OneMaxEA.cpp</title>
|
|
</head>
|
|
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
|
<a href="eoLesson5.html">Back to Lesson 5</a> - <a href="eoTutorial.html">Tutorial
|
|
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
|
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
|
documentation</a></font>
|
|
<br>
|
|
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
|
<center>
|
|
<h1>
|
|
<font color="#FF0000">Templates/OneMaxEA.cpp</font></h1></center>
|
|
The places where you have to put some code are on <b><font color="#FF6666">pink
|
|
background</font></b>.. Only the the <a href="eoTutorial.html#colors">character
|
|
colors have the usual meaning</a>.
|
|
<br><a NAME="start"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr NOSAVE>
|
|
<td NOSAVE><tt><font color="#993300">/** -*- mode: c++; c-indent-level:
|
|
4; c++-member-init-indent: 8; comment-column: 35; -*-</font></tt>
|
|
<br><tt><font color="#993300">The above line is usefulin Emacs-like editors</font></tt>
|
|
<br><tt><font color="#993300">*/</font></tt>
|
|
<br><tt><font color="#993300">/*</font></tt>
|
|
<br><tt><font color="#993300">Template for creating a new representation
|
|
in EO</font></tt>
|
|
<br><tt><font color="#993300">================================================</font></tt>
|
|
<br><tt><font color="#993300">This is the template main file.</font></tt>
|
|
<br><tt><font color="#993300">It includes all other files that have been
|
|
generated by the script create.sh</font></tt>
|
|
<br><tt><font color="#993300">so it is the only file to compile.</font></tt>
|
|
<br><tt><font color="#993300">In case you want to build up a separate library
|
|
for your new Evolving Object,</font></tt>
|
|
<br><tt><font color="#993300">you'll need some work - follow what's done
|
|
in the src/ga dir, used in the</font></tt>
|
|
<br><tt><font color="#993300">main file BitEA in tutorial/Lesson4 dir.</font></tt>
|
|
<br><tt><font color="#993300">Or you can wait until we do it :-)</font></tt>
|
|
<br><tt><font color="#993300">*/</font></tt>
|
|
<br><tt><font color="#993300">// Miscilaneous include and declaration </font></tt>
|
|
<br><b><tt><font color="#993300">#include <iostream></font></tt></b>
|
|
<br><b><tt><font color="#993300">using namespace std;</font></tt></b>
|
|
<br><tt><font color="#993300">// eo general include</font></tt>
|
|
<br><b><tt><font color="#993300">#include "eo"</font></tt></b>
|
|
<br><tt><font color="#993300">// the real bounds (not yet in general eo
|
|
include)</font></tt>
|
|
<br><b><tt><font color="#993300">#include "utils/eoRealVectorBounds.h"</font></tt></b>
|
|
<br><tt><font color="#993300">// include here whatever specific files for
|
|
your representation</font></tt>
|
|
<br><tt><font color="#993300">// Basically, this should include at least
|
|
the following</font></tt>
|
|
<br><tt><font color="#993300">/** definition of representation: </font></tt>
|
|
<br><tt><font color="#993300">* class eoOneMax MUST derive from EO<FitT>
|
|
for some fitness</font></tt>
|
|
<br><tt><font color="#993300">*/</font></tt></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="representation"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><b><tt><font color="#999900">#include "eoOneMax.h"</font></tt></b>
|
|
<br><tt><font color="#999900">/** definition of initilizqtion: </font></tt>
|
|
<br><tt><font color="#999900">* class eoOneMaxInit MUST derive from eoInit<eoOneMax></font></tt>
|
|
<br><tt><font color="#999900">*/</font></tt></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="init"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><b><tt><font color="#993399">#include "eoOneMaxInit.h"</font></tt></b>
|
|
<br><tt><font color="#993399">/** definition of evaluation: </font></tt>
|
|
<br><tt><font color="#993399">* class eoOneMaxEvalFunc MUST derive from
|
|
eoEvalFunc<eoOneMax></font></tt>
|
|
<br><tt><font color="#993399">* and should test for validity before doing
|
|
any computation</font></tt>
|
|
<br><tt><font color="#993399">* see tutorial/Templates/evalFunc.tmpl</font></tt>
|
|
<br><tt><font color="#993399">*/</font></tt></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="fitness"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><b><tt><font color="#CC0000">#include "eoOneMaxEvalFunc.h"</font></tt></b>
|
|
<br><tt><font color="#CC0000">// GENOTYPE eoOneMax ***MUST***
|
|
be templatized over the fitness</font></tt></td>
|
|
</tr>
|
|
</table>
|
|
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#CC0000">// START fitness type: double or eoMaximizingFitness
|
|
if you are maximizing</font></tt>
|
|
<br><tt><font color="#CC0000">//
|
|
eoMinimizingFitness if you are minimizing</font></tt>
|
|
<br><tt><font color="#CC0000"><b>typedef eoMinimizingFitness MyFitT ; </b>//
|
|
type of fitness </font></tt>
|
|
<br><tt><font color="#CC0000">// END fitness type</font></tt></td>
|
|
</tr>
|
|
</table>
|
|
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="representation"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#999900">// Then define your EO objects using that
|
|
fitness type</font></tt>
|
|
<br><tt><font color="#999900"><b>typedef eoOneMax<MyFitT> Indi; </b>
|
|
// ***MUST*** derive from EO </font></tt></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="init"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#993399">// create an initializer</font></tt>
|
|
<br><b><tt><font color="#993399">#include "make_genotype_OneMax.h"</font></tt></b>
|
|
<br><b><tt><font color="#993399">eoInit<Indi> & make_genotype(eoParser&
|
|
_parser, eoState&_state, Indi _eo)</font></tt></b>
|
|
<br><b><tt><font color="#993399">{</font></tt></b>
|
|
<br><b><tt><font color="#993399"> return do_make_genotype(_parser,
|
|
_state, _eo);</font></tt></b>
|
|
<br><b><tt><font color="#993399">} </font></tt></b></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="operators"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#993399">// and the variation operaotrs</font></tt>
|
|
<br><b><tt><font color="#993399">#include "make_op_OneMax.h"</font></tt></b>
|
|
<br><b><tt><font color="#993399">eoGenOp<Indi>& make_op(eoParser&
|
|
_parser, eoState& _state, eoInit<Indi>& _init)</font></tt></b>
|
|
<br><b><tt><font color="#993399">{</font></tt></b>
|
|
<br><b><tt><font color="#993399"> return do_make_op(_parser, _state,
|
|
_init);</font></tt></b>
|
|
<br><b><tt><font color="#993399">}</font></tt></b></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="init"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#993399">// Use existing modules to define representation
|
|
independent routines</font></tt>
|
|
<br><tt><font color="#993399">// These are parser-based definitions of
|
|
objects</font></tt>
|
|
<br><tt><font color="#993399">// how to initialize the population </font></tt>
|
|
<br><tt><font color="#993399">// it IS representation independent if an
|
|
eoInit is given</font></tt>
|
|
<br><b><tt><font color="#993399">#include <do/make_pop.h></font></tt></b>
|
|
<br><b><tt><font color="#993399">eoPop<Indi >& make_pop(eoParser&
|
|
_parser, eoState& _state, eoInit<Indi> & _init)</font></tt></b>
|
|
<br><b><tt><font color="#993399">{</font></tt></b>
|
|
<br><b><tt><font color="#993399"> return do_make_pop(_parser, _state,
|
|
_init);</font></tt></b>
|
|
<br><b><tt><font color="#993399">}</font></tt></b></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="stop"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#3366FF">// the stopping criterion</font></tt>
|
|
<br><b><tt><font color="#3366FF">#include <do/make_continue.h></font></tt></b>
|
|
<br><b><tt><font color="#3366FF">eoContinue<Indi>& make_continue(eoParser&
|
|
_parser, eoState& _state, eoEvalFuncCounter<Indi> & _eval)</font></tt></b>
|
|
<br><b><tt><font color="#3366FF">{</font></tt></b>
|
|
<br><b><tt><font color="#3366FF"> return do_make_continue(_parser,
|
|
_state, _eval);</font></tt></b>
|
|
<br><b><tt><font color="#3366FF">}</font></tt></b></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="stat"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#3366FF">// outputs (stats, population dumps, ...)</font></tt>
|
|
<br><b><tt><font color="#3366FF">#include <do/make_checkpoint.h></font></tt></b>
|
|
<br><b><tt><font color="#3366FF">eoCheckPoint<Indi>& make_checkpoint(eoParser&
|
|
_parser, eoState& _state, eoEvalFuncCounter<Indi>& _eval, eoContinue<Indi>&
|
|
_continue) </font></tt></b>
|
|
<br><b><tt><font color="#3366FF">{</font></tt></b>
|
|
<br><b><tt><font color="#3366FF"> return do_make_checkpoint(_parser,
|
|
_state, _eval, _continue);</font></tt></b>
|
|
<br><b><tt><font color="#3366FF">}</font></tt></b></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="engine"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#009900">// evolution engine (selection and replacement)</font></tt>
|
|
<br><b><tt><font color="#009900">#include <do/make_algo_scalar.h></font></tt></b>
|
|
<br><b><tt><font color="#009900">eoAlgo<Indi>& make_algo_scalar(eoParser&
|
|
_parser, eoState& _state, eoEvalFunc<Indi>& _eval, eoContinue<Indi>&
|
|
_continue, eoGenOp<Indi>& _op)</font></tt></b>
|
|
<br><b><tt><font color="#009900">{</font></tt></b>
|
|
<br><b><tt><font color="#009900"> return do_make_algo_scalar(_parser,
|
|
_state, _eval, _continue, _op);</font></tt></b>
|
|
<br><b><tt><font color="#009900">}</font></tt></b></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="general"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#993300">// simple call to the algo. stays there for
|
|
consistency reasons </font></tt>
|
|
<br><tt><font color="#993300">// no template for that one</font></tt>
|
|
<br><b><tt><font color="#993300">#include <do/make_run.h></font></tt></b>
|
|
<br><tt><font color="#993300">// the instanciating fitnesses</font></tt>
|
|
<br><b><tt><font color="#993300">#include <eoScalarFitness.h></font></tt></b>
|
|
<br><b><tt><font color="#993300">void run_ea(eoAlgo<Indi>& _ga,
|
|
eoPop<Indi>& _pop)</font></tt></b>
|
|
<br><b><tt><font color="#993300">{</font></tt></b>
|
|
<br><b><tt><font color="#993300"> do_run(_ga, _pop);</font></tt></b>
|
|
<br><b><tt><font color="#993300">}</font></tt></b>
|
|
<br><tt><font color="#993300">// checks for help demand, and writes the
|
|
status file</font></tt>
|
|
<br><tt><font color="#993300">// and make_help; in libutils</font></tt>
|
|
<br><b><tt><font color="#993300">void make_help(eoParser & _parser);</font></tt></b>
|
|
<br><tt><font color="#993300">// now use all of the above, + representation
|
|
dependent things</font></tt>
|
|
<br><b><tt><font color="#993300">int main(int argc, char* argv[])</font></tt></b>
|
|
<br><b><tt><font color="#993300">{</font></tt></b>
|
|
<br><b><tt><font color="#993300"> try</font></tt></b>
|
|
<br><b><tt><font color="#993300"> {</font></tt></b>
|
|
<br><tt><font color="#993300"><b> eoParser parser(argc, argv); </b>
|
|
// for user-parameter reading</font></tt>
|
|
<br><tt><font color="#993300"><b> eoState state; </b>
|
|
// keeps all things allocated</font></tt></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="eval"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#CC0000"><b> </b> //
|
|
The fitness</font></tt>
|
|
<br><tt><font color="#CC0000"><b> </b> //////////////</font></tt>
|
|
<br><tt><font color="#CC0000"><b> eoOneMaxEvalFunc<Indi>
|
|
plainEval</b> /* (varType _anyVariable) */;<b>;</b></font></tt>
|
|
<br><tt><font color="#CC0000"><b> </b> // turn that object
|
|
into an evaluation counter</font></tt>
|
|
<br><b><tt><font color="#CC0000"> eoEvalFuncCounter<Indi>
|
|
eval(plainEval);</font></tt></b></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="representation"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#999900"><b> </b> // the genotype - through a
|
|
genotype initializer</font></tt>
|
|
<br><b><tt><font color="#999900"> eoInit<Indi>& init = make_genotype(parser,
|
|
state, Indi());</font></tt></b></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="operators"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#993399"><b> </b> // Build the variation operator
|
|
(any seq/prop construct)</font></tt>
|
|
<br><b><tt><font color="#993399"> eoGenOp<Indi>& op = make_op(parser,
|
|
state, init);</font></tt></b>
|
|
<br><tt><font color="#993399"><b> </b> //// Now the representation-independent
|
|
things </font></tt>
|
|
<br><tt><font color="#993399"><b> </b> //</font></tt>
|
|
<br><tt><font color="#993399"><b> </b> // YOU SHOULD NOT NEED TO MODIFY
|
|
ANYTHING BEYOND THIS POINT</font></tt>
|
|
<br><tt><font color="#993399"><b> </b> // unless you want to add specific
|
|
statistics to the checkpoint</font></tt>
|
|
<br><tt><font color="#993399"><b> </b> //////////////////////////////////////////////</font></tt></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="init"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#993399"><b> </b> // initialize the population</font></tt>
|
|
<br><tt><font color="#993399"><b> </b> // yes, this is representation
|
|
indepedent once you have an eoInit</font></tt>
|
|
<br><b><tt><font color="#993399"> eoPop<Indi>& pop
|
|
= make_pop(parser, state, init);</font></tt></b></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="stop"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#3366FF"><b> </b> // stopping criteria</font></tt>
|
|
<br><b><tt><font color="#3366FF"> eoContinue<Indi> & term =
|
|
make_continue(parser, state, eval);</font></tt></b></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="output"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#3366FF"><b> </b> // output</font></tt>
|
|
<br><b><tt><font color="#3366FF"> eoCheckPoint<Indi> & checkpoint
|
|
= make_checkpoint(parser, state, eval, term);</font></tt></b></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="general"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#993300"><b> </b> // algorithm (need the operator!)</font></tt>
|
|
<br><b><tt><font color="#993300"> eoAlgo<Indi>& ga = make_algo_scalar(parser,
|
|
state, eval, checkpoint, op);</font></tt></b>
|
|
<br><tt><font color="#993300"><b> </b> ///// End of construction of
|
|
the algorithm</font></tt>
|
|
<br><tt><font color="#993300"><b> </b> /////////////////////////////////////////</font></tt>
|
|
<br><tt><font color="#993300"><b> </b> // to be called AFTER all parameters
|
|
have been read!!!</font></tt>
|
|
<br><b><tt><font color="#993300"> make_help(parser);</font></tt></b>
|
|
<br><tt><font color="#993300"><b> </b> //// GO</font></tt>
|
|
<br><tt><font color="#993300"><b> </b> ///////</font></tt></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="eval"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#CC0000"><b> </b> // evaluate intial population
|
|
AFTER help and status in case it takes time</font></tt>
|
|
<br><b><tt><font color="#CC0000"> apply<Indi>(eval, pop);</font></tt></b>
|
|
<br><tt><font color="#CC0000"><b> </b> // if you want to print it
|
|
out</font></tt>
|
|
<br><tt><font color="#CC0000">// cout << "Initial
|
|
Population\n";</font></tt>
|
|
<br><tt><font color="#CC0000">// pop.sortedPrintOn(cout);</font></tt>
|
|
<br><tt><font color="#CC0000">// cout << endl;</font></tt></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="general"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCCCCC" NOSAVE >
|
|
<tr>
|
|
<td><tt><font color="#993300"><b> run_ea(ga, pop); </b>// run the
|
|
ga</font></tt>
|
|
<br><b><tt><font color="#993300"> cout << "Final Population\n";</font></tt></b>
|
|
<br><b><tt><font color="#993300"> pop.sortedPrintOn(cout);</font></tt></b>
|
|
<br><b><tt><font color="#993300"> cout << endl;</font></tt></b>
|
|
<br><b><tt><font color="#993300"> }</font></tt></b>
|
|
<br><b><tt><font color="#993300"> catch(exception& e)</font></tt></b>
|
|
<br><b><tt><font color="#993300"> {</font></tt></b>
|
|
<br><b><tt><font color="#993300"> cout <<
|
|
e.what() << endl;</font></tt></b>
|
|
<br><b><tt><font color="#993300"> }</font></tt></b>
|
|
<br><b><tt><font color="#993300"> return 0;</font></tt></b>
|
|
<br><b><tt><font color="#993300">}</font></tt></b></td>
|
|
</tr>
|
|
</table>
|
|
|
|
<hr WIDTH="100%"><a href="eoLesson5.html">Back to Lesson 5</a> - <a href="eoTutorial.html">Tutorial
|
|
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
|
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
|
documentation</a></font>
|
|
<hr>
|
|
<address>
|
|
<a href="mailto:marc.schoenauer@inria.fr">Marc Schoenauer</a></address>
|
|
|
|
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last modified: Sat May
|
|
4 07:37:41 2002<!-- hhmts end -->
|
|
</body>
|
|
</html>
|