paradiseo/eo/tutorial/html/eoLesson2.html
2000-12-15 18:09:08 +00:00

355 lines
20 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.75 [en] (X11; U; Linux 2.2.17-21mdksmp i686) [Netscape]">
<title>Tutorial: Lesson 2</title>
<!-- Changed by: Marc Schoenauer, 29-Nov-2000 -->
</head>
<body text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000" background="beige009.jpg">
<a href="eoLesson1.html">Lesson 1</a> -
<a href="eoLesson3.html">Lesson
3</a> -
<a href="eoTutorial.html">Main page</a> -
<a href="eoTopDown.html">Top-Down</a>
- <a href="eoBottomUp.html">Bottom-up</a> - <a href="eoProgramming.html">Hints</a>
-<font size=+1> <b><font face="Arial,Helvetica"><a href="../../doc/html/index.html">EO
documentation</a></font></b></font>
<br>
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
<center>
<h1>
<font color="#FF0000">Tutorial Lesson 2: more encapsulations</font></h1></center>
In this lesson, the same Evolutionary Algorithm will be rewritten in a
much more general context.
<br>First, look at the <a href="#changes">changes</a> that have been done
to the algorithms. Then benefit from the new features by
<ul>
<li>
<a href="#minimize">minimizing</a> (and not only maximize) the fitness</li>
<li>
<a href="#combined_operators">combining</a> <font color="#CC33CC">several
operators</font> of the same type</li>
<li>
<a href="#combinedContinue">combining</a> <font color="#3366FF">several
stopping criteria</font></li>
<li>
use <font color="#009900"><a href="#evolution">alternate</a> selection/replacement</font>
engines, deviating from the pure generational GA</li>
</ul>
<p><br>Again, two basic algorithms are provided, namely <a href="FirstBitEA.html">FirstBitEA</a>
and <a href="FirstRealEA.html">FirstRealEA</a>.
<br><font color="#FF6600">To compile and run</font> them, go to the <font color="#FF6600">Lesson2
sub-directory</font> of the tutorial dir and simply type <b><tt><font color="#990000"><font size=+1>make</font></font></tt></b>.
Both examples should get compiled, and you can then run them by calling
their name from the system prompt.
<p>Note the slim difference in names, from <font color="#FF6600">GA</font>
to <font color="#FF6600">EA</font>: the behavior of these&nbsp; EAs is
almost identical to that of their GA counterpart, at least with the default
settings that are provided. But their potentialities for easy modifications
are much larger, both in terms of <b><font color="#CC33CC">variation operators</font></b>
and of <b><font color="#009900">evolution engine</font></b> (i.e. <font color="#009900">selection/replacement</font>
mechanism).&nbsp;
<hr WIDTH="100%"><a NAME="changes"></a><b><font color="#000099"><font size=+2>Changes</font></font></b>
<p>Browse through the code, and discover them one after the other:
<ul>
<li>
<font color="#000000">The</font><font color="#CC0000"> fitness function
</font><font color="#000000">now
lies in a </font><b><font color="#CC0000">separate file
</font></b><font color="#000000">(<a href="FirstBitEA.html#evalfunc">Bit</a>
- <a href="FirstRealEA.html#evalfunc">Real</a>). But, more important, its
argument is a <a href="binary_value.html">vector&lt;bool></a> or a <a href="real_value.html">vector&lt;double></a>,
and not an unknown type. This will allow to use the same file for any EO
object that is a sub-class of the corresponding STL vector class.</font></li>
<br>&nbsp;
<p>&nbsp;
<p><b><font color="#FF0000">Note:</font></b> <font color="#000000">Also,
a non-templatized fitness can be </font><b><font color="#FF6600">compiled
separately</font></b><font color="#000000"> (not done here) into an object
file once and for all (<a href="eoProgramming.html#templates">remember</a>
that templates forbid that).</font>
<br>&nbsp;
<li>
<a NAME="minimize"></a><font color="#000000">The </font><font color="#CC0000"><b>encapsulation
</b>of
the fitness </font><font color="#000000">(<a href="FirstBitEA.html#eval">Bit</a>
- <a href="FirstRealEA.html#eval">Real</a>) looks more complicated: you
have to declare 3 template arguments: the type of EO object it will be
applied to, the return type and the type of argument the function actually
requires.</font></li>
<br>&nbsp;
<p>&nbsp;
<br>&nbsp;
<br>&nbsp;
<p><b><font color="#FF0000">Note:</font></b> <font color="#000000">In the
previous files (<a href="FirstBitGA.html#eval">Bit</a> - <a href="FirstRealGA.html#eval">Real</a>)
, the last 2 types were deduced from the first (2nd argument = fitness
type of EO object, third = first).</font>
<br>&nbsp;
<li>
<font color="#000000">Both the above modifications makes it very easy to
</font><b><font color="#CC0000">minimize</font></b><font color="#000000">
rather than maximize a fitness function (see <a href="#Execrise1">Exercise
1)</a>.</font></li>
<br>&nbsp;
<li>
<font color="#000000">The</font><font color="#CC33CC"> initialization</font><font color="#000000">
of the population is now </font><b><font color="#CC33CC">encapsulated</font></b><font color="#000000">into
a </font><font color="#CC33CC"><b>separate initializer</b> </font><font color="#000000">(based
on a <a href="FirstBitEA.html#init">boolean generator</a> or a <a href="FirstRealEA.html#init">double-number
generator</a> -see <a href="../../doc/html/class_random_generator.html">random_generators.h</a>)
that is then used in the constructor of the population to build the individuals.
You can also use different initializers and call them in turn through the
call to <a href="../../doc/html/class_eopop.html#a2">pop.append()</a> function
(see <a href="#exercise2">Exercise 2</a>).</font></li>
<br>&nbsp;
<p>&nbsp;
<p><b><font color="#FF0000">Note</font><font color="#CC33CC">: </font></b><font color="#000000">Don't
forget to </font><b><font color="#CC0000">evaluate the population</font></b><font color="#000000">:
the eoPop has no idea of the eval function, so it has to be done from outside!!!</font>
<br>&nbsp;
<li>
<a NAME="combined_operators"></a><font color="#000000">You can now use
</font><font color="#CC33CC"><b>different
</b>crossover
</font><font color="#000000">and</font><font color="#CC33CC">
mutation
<b>operators</b></font><font color="#000000">in the same algorithm,
choosing among them according to
</font><b><font color="#FF6600">relative
rates.</font></b><font color="#CC33CC"> </font><font color="#000000">The
class </font><font color="#CC33CC"><b>eoPropCombinedxxxOp</b>,
</font><font color="#000000">where
xxx is either Mon (for mutations, of class <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/class_eomonop.html">eoMonOp</a></font></font></b></font><font color="#CC33CC">)</font><font color="#000000">
or Quad (for crossovers, of class <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/class_eoquadraticop.html">eoQuadOp</a></font></font></b>),
is derived from the corresponding eoxxxOp class. When applying the eoPropCombinedxxxOp,
one of the eoxxxOp it contains is chosen by a <a href="../../doc/html/class_eorng.html#a12">roulette
wheel,</a> according to their respective rates, and is applied to the arguments.
For more details on these classes, go to the <a href="eoOperators.html#crossover">top-down
corresponding pages</a>, or to their respective documentation pages.</font></li>
<ul>
<li>
<font color="#000000"><a href="FirstBitEA.html#operators">Bit</a></font></li>
<br><font color="#000000">Three </font><b><font color="#FF6600">crossover
operators</font></b><font color="#000000"> are available: the </font><font color="#FF6600">one-point</font><font color="#000000">
crossover is still there (class ), but now you also have the </font><font color="#FF6600">N-point</font><font color="#000000">
crossover </font><font color="#CC33CC">eoBinNxOver</font><font color="#000000">
(the&nbsp; number of points is 2 by default, but as always you can change
that in the constructor), and the </font><font color="#FF6600">Uniform</font><font color="#000000">
crossover </font><font color="#CC33CC">eoBinUxOver</font><font color="#000000">
(where you can eventually twidle the choice from one parent to the other
by providing a probability in the constructore - defaulted to 0.5, which
amounts to symmetrical choice).</font>
<br><font color="#000000">As for </font><b><font color="#FF6600">mutation
operators</font></b><font color="#000000">, apart from the </font><font color="#CC33CC">eoBinMutation</font><font color="#000000">
(standard bitstring mutation flipping one bit with a given probability)
you can also use the </font><font color="#CC33CC">eoDetBitFlip</font><font color="#000000">
that always filps the same number of bits (1 by default, but you can change
that in the constructor), randomly chosen in the bitstring. Even though
the average number of bits flipped is the same if the </font><font color="#CC33CC">eoBinMutation
</font><font color="#000000">is
used with a rate of 1/N (N is the bitstring length) </font><font color="#FF6600">the
behavior of these mutation can be very different</font><font color="#000000">
on many problems.</font>
<li>
<font color="#000000"><a href="FirstRealEA.html#operators">Real</a></font></li>
<br><font color="#000000">Two </font><b><font color="#FF6600">crossover
operators</font></b><font color="#000000"> are available: the </font><font color="#CC33CC">eoSegmentCrossover</font><font color="#000000">
chooses one point uniformly on the segment joining the parents, while the
</font><font color="#CC33CC">eoArithmeticCrossover</font><font color="#000000">
performs a segment crossover on each coordinate independently, which amount
to choosing the offspring uniformly in the hypercube whose diagonal is
the segment joining the parents.</font>
<br><font color="#000000">As for </font><b><font color="#FF6600">mutation
operators</font></b><font color="#000000">, apart from the </font><font color="#CC33CC">eoBinMutation</font><font color="#000000">
(standard bitstring mutation flipping one bit with a given probability)
you can also use the </font><font color="#CC33CC">eoDetBitFlip</font><font color="#000000">
that always filps the same number of bits (1 by default, but you can change
that in the constructor), randomly chosen in the bitstring. And last but
not least, the normal mutation eoNormMutation modifies all coordinates
with a Gaussian noise, with standard deviation passed in the constructor.</font></ul>
<b><font color="#FF0000">Note:</font></b> A third optional argument in
method <b><tt><font color="#660000">add</font></tt></b> is a boolean (defaulted
to false). When true, the actual rates for all operators are displayed
on the screen as percentages: you don't have to input rates that sum up
to 1, all rates are scaled anyway.
<p><a NAME="transform"></a><b><font color="#FF0000">Note:</font></b> The
operators have to be encapsulated into an <b><tt><font color="#CC33CC">eoTransform</font></tt></b>
object (<a href="FirstBitEA.html#transform">Bit</a> - <a href="FirstRealEA.html#transform">Real</a>)
to be passed to the <b><tt><font color="#FF6666">eoEasyEA </font></tt></b>algorithm.
The <b><tt><font color="#CC33CC">eoSGATransform</font></tt></b> is a simple
<b><tt><font color="#CC33CC">eoTransform</font></tt></b>
that does exactly the same thing than <b><tt><font color="#FF6666">eoSGA</font></tt></b>:
each pair from the selected parents undergoes the <font color="#CC33CC">crossover
operator with given probability</font>, and all individuals (after crossover
eventually) undergo <font color="#CC33CC">mutation with given probability</font>.
The arguments to the <b><tt><font color="#CC33CC">eoSGATransform</font></tt></b>
are an <b><tt><font color="#CC33CC">eoQuadOp</font></tt></b> with its probability
and an <b><tt><font color="#CC33CC">eoMonOp</font></tt></b> with the associated
probability.
<br>&nbsp;
<li>
<a NAME="combinedContinue"></a><font color="#000000">You can use </font><b><font color="#3366FF">combinations
</font></b><font color="#000000">of
several stopping criteria by using an object of the class </font><b><tt><font color="#3366FF">eoCombinedContinue
</font></tt></b><font color="#000000">(<a href="FirstBitEA.html#stop">Bit</a>
- <a href="FirstRealEA.html#stop">Real</a>). Initialize it with an object
of class </font><font color="#3366FF"><b><tt>eoContinue</tt></b>, </font><font color="#000000">and
</font><b><tt><font color="#3366FF">add</font></tt></b><font color="#000000">as
many of other such objects as you wish. And as an </font><b><tt><font color="#3366FF">eoCombinedContinue
</font></tt><font color="#FF6600">is
an</font><tt><font color="#3366FF">eoContinue</font></tt></b><font color="#000000">,
simply pass it to the algorithm (<a href="FirstBitEA.html#generation">Bit</a>
- <a href="FirstRealEA.html#generation">Real</a>).</font></li>
<br>&nbsp;
<li>
<a NAME="evolution"></a><font color="#000000">The</font><b><font color="#009900">
full selection/replacement mechanism </font></b><font color="#000000">is
now in place through the </font><font color="#FF6666"><b><tt>eoEasyEA</tt></b>
algorithm.</font></li>
<br>This means that you can use different <font color="#009900">selectors</font>.
which was already true in Lesson 1, but also different <font color="#009900">replacement
strategies</font> (see <a href="#Exercise3">Exercise 3</a>) whereas <font color="#009900">generational
replacement</font> was hard-coded in the algorithm <b><tt><font color="#FF6666">eoSGA</font></tt></b>
used in Lesson1.
<p>Beware that we have to encapsulate&nbsp; <font color="#000000">(<a href="FirstBitEA.html#select_encapsulate">Bit</a>
- <a href="FirstRealEA.html#select_encapsulate">Real</a>) </font>the <font color="#FF6600">eoDetTournament</font>,
which is of class <font color="#FF6600">eoSelectOne</font> (i.e. allows
to select one individual from a population, its <b><tt><font color="#660000">operator()</font></tt></b>
returning a single individual) into an object of the <font color="#FF6600">eoSelectPerc</font>
(perc stands for percentage) which allows to select a ... percentage of
a population (his <b><tt><font color="#660000">operator()</font></tt></b>&nbsp;
returns a population). This was done internally in the&nbsp; <a href="../../doc/html/class_eosga.html#a0">constructor
of eoSGA</a>&nbsp; - see lesson1.</ul>
<hr WIDTH="100%"><a NAME="Exercise1"></a><b><font size=+2><font color="#000099">Exercise
1: </font><font color="#FF0000">minimizing</font></font></b>
<br><font color="#000000">Modify the algorithm so that it minimizes the
fitness.</font>
<ul>
<li>
<font color="#000000">For the bitstring case, you only have to modify the
<a href="FirstBitEA.html#representation">declaration
of the representation</a>, using </font><b><tt><font color="#999900">eoMinimizingFitness</font></tt></b><font color="#000000">
instead of </font><b><tt><font color="#999900">double</font></tt></b><font color="#000000">.
But is that really all? Give it a try, look at the output, and do it right
the second time!!!</font></li>
<li>
<font color="#000000">For the real-valued problem, you also need to modify
the file <b><tt><a href="real_value.html">real_value.h</a></tt></b> so
that it returns the sum of squares instead of its inverse. And again there
is something else to modify...</font></li>
</ul>
<a NAME="exercise2"></a><b><font size=+2><font color="#000099">Exercise
2: </font><font color="#FF0000">initialization</font></font></b>
<br><font color="#000000">Use different initializers: for instance, on
the real-valued sphere function minimization, try to initialize half of
the population in [-2,-1] and the other half in [1,2], with and without
the segment and arithmetic crossovers (and for large values of VEC_SIZE,
the size of the vectors). Amazing, isn't it! Explain that result.</font>
<p><a NAME="Exercise3"></a><b><font size=+2><font color="#000099">Exercise
3:&nbsp; </font><font color="#FF0000">replacement</font></font></b>
<br><font color="#000000">You can now twiddle the number of offspring that
will be generated from the parents. But of course you need to adjust the
replacement to keep a constant population size.</font>
<ul>
<li>
<font color="#000000">To </font><font color="#FF6600">modify the number
of offspring</font><font color="#000000">, use the second argument of the
</font>encapsulator<font color="#000000">
(<a href="FirstBitEA.html#select_encapsulate">Bit</a> - <a href="FirstRealEA.html#select_encapsulate">Real</a>)
of the </font><font color="#009900">selector</font><font color="#000000">
of class </font><b><tt><font color="#009900">eoSelectOne</font></tt></b><font color="#000000">
into an eoSelectPerc object. For instance, try</font></li>
<br><b><tt><font color="#009900">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
eoSelectPerc&lt;Indi> select(selectOne,2.0)</font></tt></b>
<br><font color="#000000">to generate twice as many offspring as there
are parents.</font>
<li>
<font color="#000000">To </font><font color="#FF6600">keep a constant population
size</font><font color="#000000">, you can use either the </font><b><tt><font color="#009900">eoCommaReplacement</font></tt></b><font color="#000000">
class, or the </font><b><tt><font color="#009900">eoPlusReplacement</font></tt></b><font color="#000000">.
The former selects the best offspring to replace the parents, the latter
selects the best among parents+offspring. Of course you cannot use </font><b><tt><font color="#009900">eoCommaReplacement</font></tt></b><font color="#000000">
if you have less offspring than parents!</font></li>
<br>Now if you use <b><tt><font color="#009900">eoSelectRandom</font></tt></b>
as selector with a rate of
<i>lambda</i>, you end up with exactly the <i>(mu+lambda)</i>
or
<i>(mu,lambda)</i> strategies from <a href="intro.html#ES">Evolution
Strategies</a>.
<li>
<font color="#FF0000">Question</font><font color="#000000">: what do you
get if you have use a rate of 1/POP_SIZE for the selection, and an </font><b><tt><font color="#009900">eoPlusReplacement</font></tt></b><font color="#000000">
strategy? Yes, you get almost the replace_worst Steady-State GA, though
rather inefficient, as you sort the population at every generation, which
could be avoided - and will be in a later lesson).</font></li>
<li>
<font color="#FF0000">Homework</font><font color="#000000">: Write the
</font><b><tt><font color="#009900">eoCommaPlusReplacement</font></tt></b><font color="#000000">
that would start by taking the best of the offspring, and if some are still
missing to keep the population size constant, take the best of the parents.
Write the </font><b><tt><font color="#009900">eoConservativeReplacement</font></tt></b><font color="#000000">
that starts by taking a percentage of the parents (the best ones) and then
adds the best from the offspring. In both cases, </font><font color="#FF6600">send
use the code </font><font color="#000000">as we haven't done that yet (and
hence there is no solution available at the moment - Nov. 29 :-)</font></li>
</ul>
<font color="#FF0000">Remember</font><font color="#000000">: all solutions
are in the same sub-directory of the Tutorial dir than the examples (i.e.
here Lesson2), and are described <a href="NoWay.html">here</a>.</font>
<p>
<hr WIDTH="100%">
<br><b><font color="#000099"><font size=+2>Lessons learned:</font></font></b>
<ul>
<li>
How to write a <font color="#CC0000">fitness function</font> that only
needs a genotype, not a full individual. Moreover you can compile it separately.</li>
<br>How to <font color="#CC33CC">initialize</font> the population using
random generators
<li>
How to use other <font color="#009900">evolution engine</font> than the
simple generational GA.</li>
<li>
How to combine different objects of the same kind into a single object
that you can use like a simple basic object (<font color="#CC33CC">operators</font>
and <font color="#3366FF">stopping criteria</font> here).</li>
</ul>
<hr WIDTH="100%"><a href="eoLesson1.html">Lesson 1</a> -
<a href="eoLesson3.html">Lesson
3</a> -
<a href="eoTutorial.html">Main page</a> -
<a href="eoTopDown.html">Top-Down</a>
- <a href="eoBottomUp.html">Bottom-up</a> - <a href="eoProgramming.html">Hints</a>
- <b><font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/index.html">EO
documentation</a></font></font></b>
<br>
<hr>
<address>
<a href="mailto:Marc.Schoenauer@polytechnique.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>