paradiseo/eo/tutorial/html/eoLesson1.html

440 lines
23 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.76 [en] (X11; U; Linux 2.2.17-21mdksmp i686) [Netscape]">
<title>Tutorial: Lesson 1</title>
</head>
<body text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000" background="beige009.jpg">
<a href="eoLesson2.html">Lesson 2</a> -
<a href="eoTutorial.html">Tutorial
main page</a> -
<a href="eoTopDown.html">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based</a>
- <a href="eoProgramming.html">Programming hints</a> -<font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/index.html">EO
documentation</a></font></font>
<br>
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
<center>
<h1>
<font color="#FF0000">Tutorial: Lesson 1</font></h1></center>
This lesson will let you
<ul>
<li>
<a href="#run">run</a> your first Evolutionary Algorithm written within
EO Library, choosing between evolving <font color="#999900">bitstrings</font>,
or evolving <font color="#999900">vectors of real numbers</font>,</li>
<li>
<a href="#browse">browse</a> through the code of these algorithms, or</li>
<li>
follow the <a href="#tour">guided tour</a>.</li>
</ul>
Later you will be asked to
<ul>
<li>
<a href="#exercise1">write</a> your own <font color="#990000">fitness function</font>,</li>
<li>
<a href="#exercise2">check</a> that EO let you separate the <font color="#999900">representation</font>
from the <font color="#009900">evolution engine</font>.</li>
<li>
<a href="#exercise3">use different kinds</a> of <font color="#009900">selection
procedures</font> in the framework of a generational GA <font color="#009900">evolution
engine</font>,</li>
</ul>
<h3>
<hr WIDTH="100%"><a NAME="run"></a><font color="#000099">I want to run
an Evolutionary Algorithm
<b>now</b></font></h3>
You can choose to run a standard <a href="FirstBitGA.html">bitstring Genetic
Algorithm</a> (as defined in Goldberg's book) or a standard <a href="FirstRealGA.html">real-valued
genetic algorithm</a>, as proposed in Micahlewicz's book.
<p>If you have not already done what was recommended in the <a href="eoTutorial.html#install">Tutorial
main page</a> , do it <b><font color="#FF6600">NOW</font></b>. Then go
to the Lesson1 sub-dir of the tutorial dir, and simply type at the system
prompt
<p><tt>(myname@myhost) EOdir/Tutorial/Lesson1 % <font color="#FF6666">FirstRealGA</font></tt>
<br>or
<br><tt>(myname@myhost) EOdir/Tutorial/Lesson1 % <font color="#FF6666">FirstBitGA</font></tt>
<p>and something should happen.
<h3>
<font color="#000099">What is happening?</font></h3>
At the moment, the <font color="#FF6600">FirstBitGA</font> maximizes the
number of ones in the bitstring (also calls the <font color="#FF6600">OneMaxfunction</font>,
whose solution is, as you can guess,
<font color="#FF6600">11111111</font>),
and the <font color="#FF6600">FirstRealGA</font> is maximizing (the default
action for EO is to maximize) the inverse of the sum (i.e. minimizing the
sum) of the square of its variables (also called the <font color="#FF6600">sphere
function</font>, whose solution is ... <font color="#FF6600">all zeroes</font>).
<p>And what you see on the screen when running one of these two programs
is, in each case, the initial and final population of an Evolutionary run,
one individual per line, its fitness first, then the number of items (bits
or real numbers) of the genotype, and the genotype itself. The final population
hopefully contains the solution in the discrete case, and is close to it
in the continuous case.
<br>
<hr WIDTH="100%"><a NAME="browse"></a><b><font color="#000099"><font size=+1>Browsing
the code:</font></font></b>
<p>Now you need to take a look at the program codes, either by browsing
alone through the&nbsp; sources for <a href="FirstBitGA.html">FirstBitGA</a>
and <a href="FirstRealGA.html">FirstRealGA</a>, or by following the guided
tour below. You might prefer to go directly to the <a href="#exercise1">exercises</a>.
<h3>
<hr WIDTH="100%"><a NAME="tour"></a><font color="#000099">Guided tour:</font></h3>
<ul>
<li>
<b><font color="#993300">General includes:</font></b><font color="#000000">Like
all C-like code, the file starts with include directives (<a href="FirstBitGA.html#start">Bit</a>
- <a href="FirstRealGA.html#start">Real</a>). Apart from standard includes,
the specific file to include is </font><b><font color="#FF6600">eo</font></b><font color="#000000">:
this is a file that contains the list of the all important representation-independent
EO files.</font></li>
<br>&nbsp;
<li>
<b><font color="#999900">Representation</font></b><font color="#000000">:
you then have to declare the type of individuals you will be handling.
All evolution-related objects you will need are templatized w.r.t. the
type of individuals.</font></li>
<ul>
<li>
<a href="FirstBitGA.html#representation">Bit</a> You first need to include
the representation-dependent file <b><tt><font color="#993300"><font size=+1>ga.h</font></font></tt></b>,
containing the bitstring representation and operators. More details about
<a href="eoRepresentation.html#bitstring">what is available for bitstrings
here</a>.<br>
You then say that you will be handling <b><font face="Arial,Helvetica"><a href="../../doc/html/classeo_bit.html">Bitstring
genotypes</a></font></b>, whose fitness is a double. This makes Indi derive
from the <a href="eoProgramming.html#STL">STL class</a> <font color="#993300">vector&lt;bool></font></li>
<li>
<a href="FirstRealGA.html#representation">Real</a> You first need to include
the representation-dependent file <b><tt><font color="#993300"><font size=+1>es.h</font></font></tt></b>,
containing the bitstring representation and operators. More details about
<a href="eoRepresentation.html#real">what is available for real-values
here</a>.<br>
You then say that you will be handling <b><font face="Arial,Helvetica"><a href="../../doc/html/classeo_real.html">Real-valued
genotypes</a></font></b>, whose fitness is a double. This makes Indi derive
from the <a href="eoProgramming.html#STL">STL class</a> <font color="#993300">vector&lt;double></font></li>
<br>&nbsp;</ul>
<li>
<b><font color="#CC0000">Fitness function:</font></b><font color="#000000">
the code for the fitness function is included in the file. It must take
as argument a reference to an individual (at the moment).</font></li>
<ul>
<li>
<a href="FirstBitGA.html#evalfunc">Bit</a> This function simply computes
the number of ones of the bitstring (it's called the OneMax function).
The optimum is of course the all-ones bitstring.</li>
<li>
<a href="FirstRealGA.html#evalfunc">Real</a> This function simply computes
the inverse of the sum of the squares of all variables (also called the
sphere function). The optimum is of course the all-zeroes vector.</li>
<br>&nbsp;</ul>
<li>
<a NAME="parametres"></a><b><font color="#3366FF">Parameters</font></b><font color="#000000">:
all parameters of the algorithm are declared here (<a href="FirstBitGA.html#parametres">Bit</a>
- <a href="FirstRealGA.html#parametres">Real</a>), and their values and
assigned. Of course, this means that you will need to recompile to change
these values - see Lesson 3 to get rid of that heavy requirement.</font></li>
<br>&nbsp;
<li>
<b><font color="#990000">Random seeding:</font></b><font color="#000000">
Random numbers play an important role in Evolutionary Algorithms. See in
<a href="eoProgramming.html#random">EO
programming hints</a> more details about how this is simulated in EO -
but as far as you are concerned now, remember that the </font><font color="#660000">global
Random Number Generator</font><font color="#000000"> is called </font><b><tt><font color="#660000"><font size=+1>rng</font></font></tt></b><font color="#000000">
and should be used everywhere you need a realization of a random variable
of known law. Moreover, this RNG requires a </font><b><tt><font color="#660000"><font size=+1>seed</font></font></tt></b><font color="#000000">,
which is set here (<a href="FirstBitGA.html#random">Bit</a> - <a href="FirstRealGA.html#random">Real</a>):
every time you run the algorithm with the </font><font color="#FF6600">same
seed</font><font color="#000000">, you will get the </font><font color="#FF6600">same
result</font><font color="#000000">. Hence, to test the robustness of your
algorithm, you should run it with different seeds. This is rather time
consuming in the present programs, so we suggest that you wait until Lesson
3 to do so.</font></li>
<br>&nbsp;
<li>
<b><font color="#CC0000">Fitness function encapsulation: </font></b><font color="#000000">EO
is based on the notion of <a href="eoProgramming.html#functors">functors</a>
- hence you now need to encapsulate your fitness function into a functor
object. This is what is done here (<a href="FirstBitGA.html#eval">Bit</a>
- <a href="FirstRealGA.html#eval">Real</a>).</font></li>
<br>&nbsp;
<li>
<b><font color="#CC33CC">Initialization</font></b><font color="#000000">:
to initialize the population, first declare an empty object of class </font><b><tt><font color="#990000">eoPop&lt;Indi></font></tt></b><font color="#000000">,
which is basically an <a href="eoProgramming.html#STL">STL</a> </font><b><tt><font color="#990000">vector&lt;Indi></font></tt></b><font color="#000000">,
then fill it with Indi's.</font> And remember that
<b><tt><font color="#990000">v.push_back</font></tt></b>
simply appends its argument at the end of <a href="eoProgramming.html#STL">STL</a>
vector <b><tt><font color="#993300">v.</font></tt></b></li>
<ul>
<li>
<a href="FirstBitGA.html#init">Bit</a> <b><tt><font color="#990000">rng.flip()</font></tt></b>
return a <font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_rng.html">random
boolean</a></font></font></li>
<li>
<a href="FirstRealGA.html#init">Real</a> <b><tt><font color="#990000">rng.uniform()</font></tt></b>
returns a <font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_rng.html">real
value uniformly drawn in [0,1].</a></font></font></li>
<br>&nbsp;</ul>
<li>
<b><font color="#3366FF">Output</font></b><font color="#000000">: take
a snapshot at the initial population (<a href="FirstBitGA.html#output">Bit</a>
- <a href="FirstRealGA.html#output">Real</a>). Sort it first, so the best
individuals are first, and display it. Note that an eoPop has a </font><b><tt><font color="#990000">&lt;&lt;</font></tt></b><font color="#000000">
method, which means that a simple </font><b><tt><font color="#990000">os
&lt;&lt; pop </font></tt></b><font color="#000000">streams the </font><b><tt><font color="#990000">pop</font></tt></b><font color="#000000">
onto the ostream </font><b><tt><font color="#990000">os</font></tt></b><font color="#000000">.
This is true for </font><font color="#FF6600">all objects of of class </font><font color="#000000"><b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_printable.html">eoPrintable</a></font></font></b>
(most EO objects) through the method </font><b><tt><font color="#990000">printOn</font></tt></b><font color="#000000">
(which is then called by the </font><b><tt><font color="#990000">&lt;&lt;</font></tt></b><font color="#000000">
operator).</font></li>
<br>&nbsp;
<li>
<b><font color="#009900">Evolution engine:</font></b><font color="#000000">
The selection/replacement mechanism (<a href="FirstBitGA.html#engine">Bit</a>
- <a href="FirstRealGA.html#engine">Real</a>) is a simple generational
GA here: a simple selector, and a generational replacement. The <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_det_tournament_select.html">eoDetTournamentSelect</a></font></font></b>
has been chosen as a robust selection, and the generational replacement
(all parents are replaced by the offspring) is hard-coded in the eoSGA
<a href="#algorithm">algorithm</a>.</font></li>
<br>&nbsp;
<li>
<font color="#CC33CC"><b>Variation operators</b>:</font><font color="#000000">
in the simple algorithm considered here, individuals undergo </font><font color="#CC33CC">crossover</font><font color="#000000">
and </font><font color="#CC33CC">mutation</font><font color="#000000">.
In EO, these operators are (<a href="eoProgramming.html#functors">functor</a>)
objects of class</font> <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_quad_op.html">eoQuadOp</a></font></font></b>
(binary operator that modifies both its arguments) and <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_mon_op.html">eoMonOp</a></font></font></b>
(unary operator).&nbsp; These operators are applied in turn to all selected
parents, according to user-defined probabilities.&nbsp; These probabilities
are defined with all other <a href="#parametres">parameters</a>, and will
be passed to the <b><tt><font color="#FF6666"><font size=+1>eoSGA </font></font></tt></b><a href="#parametres">algorithm</a><font color="#000000">.&nbsp;
For more details on these classes, go to the <a href="eoOperators.html#crossover">algorithm-based
corresponding pages</a>, or to their respective documentation pages.</font></li>
<br>&nbsp;
<ul>
<li>
<a href="FirstBitGA.html#operators">Bit</a> The crossover <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo1_pt_bit_crossover.html">eo1PtBitXover</a></font></font></b>
is the standard <font color="#CC33CC">1-point crossover</font>, and <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_bit_mutation.html">eoBitMutation</a></font></font></b>
is the standard <font color="#FF6600">bit-flip mutation</font> that randomly
flips all bits with a given probability <b><tt>P_MUT_PER_BIT.</tt></b></li>
<br><b><font color="#FF0000">Warning</font></b>: the <b><tt>P_MUT_PER_BIT</tt></b>
probability is an <font color="#FF6600">internal parameter</font> of the
<b><tt><font color="#CC33CC"><font size=+1>eoBinMutation</font></font></tt></b>,
it is <b><font color="#FF6600">NOT</font></b> the probability of mutation
at the individual level. EO corrects what can be viewed as an inconsistency
in Holland's original work, further used in Goldberg's book by separating
the probability of mutation for each individual (independent of the type
of mutation that will be applied) from the probability of flipping each
bit, which is specific of the bit-flip mutation.&nbsp; Hence, to run the
same algorithm as Goldberg's SGA, the mutation probability (at individual
level) is 1, and the probability of flipping each bit is <b><tt>P_MUT_PER_BIT.</tt></b>
<li>
<a href="FirstRealGA.html#operators">Real</a> The crossover <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_segment_crossover.html">eoSegmentCrossover</a></font></font></b>
is the standard <font color="#CC33CC">segment crossover</font> for real-valued
vectors, that chooses a point randomly on the segment between both parents
(also termed <font color="#CC33CC">BLX-0</font>). <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_uniform_mutation.html">eoUniformMutation</a></font></font></b>
is the <font color="#CC33CC">uniform mutation</font> for real-valued vectors
that chooses a new value for each variable uniformly on an interval centered
on the parent value. The width of the interval is an <font color="#FF6600">internal
parameter</font> of the object, here called <b><tt>EPSILON</tt></b>.</li>
<br>&nbsp;</ul>
<li>
<b><font color="#3366FF">Stopping criterion:</font></b><font color="#000000">
Specify a </font><font color="#3366FF">maximum number of generations</font><font color="#000000">
to run (<a href="FirstBitGA.html#stop">Bit</a> - <a href="FirstRealGA.html#stop">Real</a>):
the simplest of all stopping criteria at the moment, using an object of
a sub-class of class </font><b><font face="Arial,Helvetica"><font color="#3366FF"><font size=+1><a href="../../doc/html/classeo_continue.html">eoContinue</a></font></font></font></b><font color="#000000">.</font></li>
<br>&nbsp;
<li>
<a NAME="algorithm"></a><b><font color="#FF6666">The algorithm: </font></b><font color="#000000">the
simple algorithm that is used here, called&nbsp; </font><b><tt><font color="#FF6666"><font size=+1>eoSGA
</font></font></tt></b><font color="#000000">requires
as parameters a </font><font color="#FF6600">selector</font><font color="#000000">,
a </font><font color="#FF6600">crossover</font><font color="#000000"> and
the associated </font><font color="#FF6600">crossover rate</font><font color="#000000">,
a </font><font color="#FF6600">mutation</font><font color="#000000"> and
the associated </font><font color="#FF6600">mutation rate,</font><font color="#000000">
and a </font><font color="#FF6600">stopping criterion</font><font color="#000000">.
Take a look at the corresponding
<a href="eoSGA.html#constructor">constructor</a>
of the class </font><b><tt><font color="#FF6666"><font size=+1>eoSGA</font></font></tt></b><font color="#000000">:
it only initializes its <a href="eoSGA.html#parametres">private data</a>
with the parameters. Now look at the <a href="eoSGA.html#generation">operator()</a>
method - the one that is called in the code for <a href="FirstBitGA.html#generation">FirstBitGA</a>
or <a href="FirstRealGA.html#generation">FirstRealGA</a> - and you'll find
out that is is as simple as it sounds.</font></li>
<br>&nbsp;
<li>
<b><font color="#3366FF">Output</font></b><font color="#000000">: After
running the algorithm, output the sorted final population (<a href="FirstBitGA.html#final_output">Bit</a>
- <a href="FirstRealGA.html#final_output">Real</a>) - and look at the best
individual: this is the result of the algorithm.</font></li>
<br>&nbsp;
<li>
<b><font color="#990000">Main body:</font></b><font color="#000000"> for
technical reasons (intercepting the exceptions), we need a main like this
one (<a href="FirstBitGA.html#main">Bit</a> - <a href="FirstRealGA.html#main">Real</a>).,
and you should not touch it unless you know what you are doing. Simply
note that this main calls the function main_function, which we have been
discussing up to now!</font></li>
</ul>
<h3>
<hr WIDTH="100%"><a NAME="exercise1"></a><font color="#000099">Exercise
1: maximize your own function</font></h3>
This is very easy - if your search space is that of bitstring or of unbounded
real numbers.
<ul>
<li>
Go to the tutorial directory, and <font color="#FF6600">copy</font> the
program you want to modify onto <b><tt><font color="#663300">mytest.cpp.</font></tt></b></li>
<li>
<font color="#FF6600">Edit</font> <b><tt><font color="#990000">mytest.cpp</font></tt></b>
with any text editor:</li>
<li>
<font color="#FF6600">Modify</font> the fitness function itself (<a href="FirstBitGA.html#evalfunc">binary_value</a>
in <font color="#FF6666"><b><tt>FirstBitGA</tt></b><font face="Courier New,Courier">,</font></font><a href="FirstRealGA.html#evalfunc">real_value</a>
in&nbsp; <font color="#FF6666"><b><tt>FirstRealGA</tt></b><font face="Courier New,Courier">)</font></font></li>
<li>
<font color="#FF6600">Compile</font> the program by typing <b><tt><font color="#990000">make
mytest</font></tt></b> at system prompt</li>
<li>
<font color="#FF6600">Run</font> the new program by entering the command
<b><tt><font color="#990000">mytest</font></tt></b>
at system prompt.</li>
</ul>
<h3>
<hr WIDTH="100%"><a NAME="exercise2"></a><font color="#000099">Exercise
2: check the differences between both programs</font></h3>
Go and take a look at the code for these programs <font color="#000000">(<a href="FirstBitGA.html">Bit</a>
- <a href="FirstRealGA.html">Real</a>)</font>. Use the symbolic representation
of an Evolutionary Algorithm (you should understand that figure now, otherwise
go <a href="eoIntro.html">there</a> and come back) to understand how each
part of the EA is coded. Try to spot the differences between both codes:
there are not so many!
<br>After you've tried that alone, take a look at the <a href="Firstmerge.html">solution</a>
:-)
<h3>
<hr WIDTH="100%"><a NAME="exercise3"></a><font color="#000099">Exercise
3: change the selection procedure</font></h3>
This is rather straightforward ... if you know what other types of selection
are available!
<br>At the moment, let's only consider only the following simple ones:
<ul>
<li>
You already know the <font color="#FF6600">tournament selection</font></li>
<br><font color="#FF0000">Syntax:&nbsp; </font><tt><font color="#009900"><b>eoDetTournamentSelect&lt;Indi>
select(T_SIZE);&nbsp;&nbsp; </b>// T_SIZE in [2,POP_SIZE)</font></tt>
<li>
Try the well-known <font color="#FF6600">roulette wheel</font></li>
<br>&nbsp;<font color="#FF0000">Syntax:&nbsp;</font>&nbsp;&nbsp; <b><tt><font color="#009900">eoProportionalSelect&lt;Indi>
select;</font></tt></b>
<li>
Or the <font color="#FF6600">stochastic binary tournament</font></li>
<br><font color="#FF0000">Syntax:&nbsp; </font><b><tt><font color="#009900">eoStochTournamentSelect&lt;Indi>
select(RATE);&nbsp;</font></tt></b><tt><font color="#009900">&nbsp;&nbsp;&nbsp;
// RATE in ]0.5,1]</font></tt>
<li>
and of course the <font color="#FF6600">random</font> selection should
give bad results!</li>
<br><font color="#FF0000">Syntax:&nbsp; </font><b><tt><font color="#009900">eoRandomSelect&lt;Indi>
select;</font></tt></b></ul>
Note that all these classes of eoObjects are derived from the abstract
class
<b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/classeo_select_one.html">eoSelectOne.</a></font></font></b>
To find out exactly how each procedure selects the individuals, read the
corresponding <a href="eoEngine.html#selection">component-based page</a>.
<p>
<hr WIDTH="100%"><b><font color="#000099"><font size=+2>Lessons learned:</font></font></b>
<ul>
<li>
in EO, all actions are performed by <a href="eoProgramming.html#functors">functor
objects</a> (this section is the last time in this tutorial that there
is a direct link to the <a href="eoProgramming.html">EO Programming hints</a>
page - though the link at top and bottom of all pages will remain there).</li>
<li>
in EO, all object you will usually need to manipulate are <a href="eoProgramming.html#templates">templatized</a><b><font color="#FF6600">
w.r.t. the type of the individual</font></b> you are handling.</li>
<li>
The type of the individual is itself <a href="eoProgramming.html#templates">templatized</a><b><font color="#FF6600">
w.r.t. the type of fitness</font></b> (double by default).</li>
<li>
In EO (actually, in EC!) <font color="#CC33CC">initialization and variation</font>
operators are <font color="#999900">representation</font>-dependent, while
the <font color="#009900">evolution engine</font> is <font color="#999900">representation</font>-independent
(well, like any rule, this one does have some exceptions).</li>
<li>
Changing the <font color="#990000">fitness function</font>, or the <font color="#009900">selection
procedure</font> inside the generational GA evolution engine is straightforward.</li>
<li>
remember, all <font color="#FF6600">solutions</font> to exercises are in
the same sub-dir of dir Tutorial than the lesson itself (see <a href="NoWay.html">here</a>).</li>
</ul>
<hr WIDTH="100%"><a href="eoLesson2.html">Lesson 2</a> -
<a href="eoTutorial.html">Tutorial
main page</a> -
<a href="eoTopDown.html">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based</a>
- <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/index.html">EO
documentation</a></font></font>
<br>
<hr>
<address>
<a href="mailto:Marc.Schoenauer@inria.fr">Marc Schoenauer</a></address>
<br><!-- Created: Fri Nov 3 18:49:12 CET 2000 --><!-- hhmts start -->Last
modified: Fri Nov 3 18:49:12 CET 2000<!-- hhmts end -->
</body>
</html>