Added the normal mutation - and the 1/5 mutation in FirstRealEA in Lesson2
This commit is contained in:
parent
51f3b56fd2
commit
deace62e55
2 changed files with 278 additions and 286 deletions
|
|
@ -2,7 +2,7 @@
|
|||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="cpp2html Marc Schoenauer">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdk i686) [Netscape]">
|
||||
<title>FirstRealEA.cpp</title>
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
|
|
@ -15,343 +15,330 @@ documentation</a></font>
|
|||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">FirstRealEA.cpp</font></h1></center>
|
||||
Click on the figure to see the corresponding code.<br>
|
||||
In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a><br>
|
||||
The actual code is in boldface and the comment in normal face.
|
||||
<br><img SRC="EA_tutorial.jpg" USEMAP="#Map" >
|
||||
<map NAME="Map">
|
||||
<!-- Init -->
|
||||
<area SHAPE="rect" HREF="#init" COORDS="14,31,135,70">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150">
|
||||
<!-- main loop -->
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150">
|
||||
<area SHAPE="rect" HREF="#output" COORDS="280,45,480,70">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150">
|
||||
<area SHAPE="rect" HREF="#select" COORDS="495,110,615,150">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230">
|
||||
<area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287">
|
||||
<area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310">
|
||||
<area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230">
|
||||
<!-- Center of loop -->
|
||||
<area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260">
|
||||
<!-- 4 bottom lines -->
|
||||
<area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370">
|
||||
<area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423">
|
||||
<area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450">
|
||||
<area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450">
|
||||
<area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378">
|
||||
</map>
|
||||
<br>
|
||||
<A NAME="start"></a>
|
||||
Click on the figure to see the corresponding code.
|
||||
<br>In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a>
|
||||
<br>The actual code is in boldface and the comment in normal face.
|
||||
<br><img SRC="EA_tutorial.jpg" USEMAP="#Map" ><map NAME="Map"><!-- Init --><area SHAPE="rect" HREF="#init" COORDS="14,31,135,70"><area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150"><!-- main loop --><area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150"><area SHAPE="rect" HREF="#output" COORDS="280,45,480,70"><area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150"><area SHAPE="rect" HREF="#select" COORDS="495,110,615,150"><area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230"><area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287"><area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305"><area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310"><area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310"><area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230"><!-- Center of loop --><area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260"><!-- 4 bottom lines --><area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370"><area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370"><area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397"><area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423"><area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450"><area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450"><area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450"><area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378"></map>
|
||||
<br><a NAME="start"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr NOSAVE>
|
||||
<td NOSAVE><tt><font color="#993300">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// FirstRealEA.cpp<br>
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
//*<br>
|
||||
// Still an instance of a VERY simple Real-coded Genetic Algorithm <br>
|
||||
// (see FirstBitGA.cpp) but now with Breeder - and Combined Ops<br>
|
||||
//<br>
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// standard includes<br>
|
||||
<b>#include <stdexcept> </b>// runtime_error <br>
|
||||
<b>#include <iostream> </b>// cout<br>
|
||||
<b>#include <strstream> </b>// ostrstream, istrstream<br>
|
||||
// the general include for eo<br>
|
||||
<b>#include <eo></b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
<td NOSAVE><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#993300">// FirstRealEA.cpp</font></tt>
|
||||
<br><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#993300">//*</font></tt>
|
||||
<br><tt><font color="#993300">// Still an instance of a VERY simple Real-coded
|
||||
Genetic Algorithm </font></tt>
|
||||
<br><tt><font color="#993300">// (see FirstBitGA.cpp) but now with
|
||||
Breeder - and Combined Ops</font></tt>
|
||||
<br><tt><font color="#993300">//</font></tt>
|
||||
<br><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#993300">// standard includes</font></tt>
|
||||
<br><tt><font color="#993300"><b>#include <stdexcept> </b>// runtime_error </font></tt>
|
||||
<br><tt><font color="#993300"><b>#include <iostream>
|
||||
</b>// cout</font></tt>
|
||||
<br><tt><font color="#993300"><b>#include <strstream> </b>// ostrstream,
|
||||
istrstream</font></tt>
|
||||
<br><tt><font color="#993300">// the general include for eo</font></tt>
|
||||
<br><b><tt><font color="#993300">#include <eo></font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="representation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
|
||||
<a NAME="representation"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#999900">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#999900">// define your individuals</font></tt>
|
||||
<br><b><tt><font color="#999900">typedef eoReal<double> Indi; </font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="evalfunc"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#CC0000">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#CC0000">// a simple fitness function that computes
|
||||
the euclidian norm of a real vector</font></tt>
|
||||
<br><tt><font color="#CC0000">// Now in a separate file, and declared as
|
||||
binary_value(const vector<bool> &)</font></tt>
|
||||
<br><b><tt><font color="#CC0000">#include "real_value.h"</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><b><tt><font color="#993300">void main_function(int argc, char **argv)</font></tt></b>
|
||||
<br><b><tt><font color="#993300">{</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="parametres"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#3366FF"><b> const unsigned int SEED = 42; </b>//
|
||||
seed for random number generator</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> const unsigned int T_SIZE = 3; </b>//
|
||||
size for tournament selection</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> const unsigned int VEC_SIZE = 8;
|
||||
</b>// Number of object variables in genotypes</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> const unsigned int POP_SIZE = 20;
|
||||
</b>// Size of population</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> const unsigned int MAX_GEN = 500;
|
||||
</b>// Maximum number of generation before STOP</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> const unsigned int MIN_GEN = 10;
|
||||
</b>// Minimum number of generation before ...</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> const unsigned int STEADY_GEN =
|
||||
50; </b>// stop after STEADY_GEN gen. without improvelent</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> const float P_CROSS = 0.8; </b>//
|
||||
Crossover probability</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> const float P_MUT = 0.5; </b>//
|
||||
mutation probability</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> const double EPSILON = 0.01; </b>//
|
||||
range for real uniform mutation</font></tt>
|
||||
<br><tt><font color="#3366FF"> <b>double
|
||||
SIGMA = 0.3; </b> // std dev.
|
||||
for normal mutation</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// some parameters for chosing
|
||||
among different operators</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> const double segmentRate = 0.5;
|
||||
</b>// relative weight for 1-pt Xover</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> const double arithmeticRate = 0.5;
|
||||
</b>// relative weight for 2-pt Xover</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> const double uniformMutRate = 0.5;
|
||||
</b>// relative weight for bit-flip mutation</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> const double detMutRate = 0.5;
|
||||
</b>// relative weight for one-bit mutation</font></tt>
|
||||
<br><tt><font color="#3366FF"> <b>const double normalMutRate = 0.5;</b>
|
||||
// relative weight for normal mutation</font></tt></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#993300"><b> </b>//////////////////////////</font></tt>
|
||||
<br><tt><font color="#993300"><b> </b>// Random seed</font></tt>
|
||||
<br><tt><font color="#993300"><b> </b>//////////////////////////</font></tt>
|
||||
<br><tt><font color="#993300"><b> </b>//reproducible random seed:
|
||||
if you don't change SEED above, </font></tt>
|
||||
<br><tt><font color="#993300"><b> </b>// you'll aways get the same
|
||||
result, NOT a random run</font></tt>
|
||||
<br><b><tt><font color="#993300"> rng.reseed(SEED);</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="eval"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#CC0000"><b> </b>/////////////////////////////</font></tt>
|
||||
<br><tt><font color="#CC0000"><b> </b>// Fitness function</font></tt>
|
||||
<br><tt><font color="#CC0000"><b> </b>////////////////////////////</font></tt>
|
||||
<br><tt><font color="#CC0000"><b> </b>// Evaluation: from a plain
|
||||
C++ fn to an EvalFunc Object</font></tt>
|
||||
<br><tt><font color="#CC0000"><b> </b>// you need to give the full
|
||||
description of the function</font></tt>
|
||||
<br><b><tt><font color="#CC0000"> eoEvalFuncPtr<Indi, double, const
|
||||
vector<double>& > eval( real_value );</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="init"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#993399"><b> </b>////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>// Initilisation of population</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>// based on a uniform generator</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoInitFixedLength<Indi, uniform_generator<double>
|
||||
></font></tt></b>
|
||||
<br><b><tt><font color="#993399">
|
||||
random(VEC_SIZE, uniform_generator<double>(-1.0, 1.0));</font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// Initialization of
|
||||
the population</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoPop<Indi> pop(POP_SIZE, random);</font></tt></b>
|
||||
<p><tt><font color="#993399"><b> </b>// and evaluate it in one loop</font></tt>
|
||||
<br><tt><font color="#993399"><b> apply<Indi>(eval, pop); </b>//
|
||||
STL syntax</font></tt></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="output"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#3366FF"><b> </b>// sort pop before printing
|
||||
it!</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> pop.sort();</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// Print (sorted) intial population
|
||||
(raw printout)</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> cout << "Initial Population"
|
||||
<< endl;</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> cout << pop;</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="engine"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#009900"><b> </b>/////////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#009900"><b> </b>// selection and replacement</font></tt>
|
||||
<br><tt><font color="#009900"><b> </b>////////////////////////////////////</font></tt></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="select"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#009900"><b> </b>// The robust tournament selection</font></tt>
|
||||
<br><b><tt><font color="#009900"> eoDetTournamentSelect<Indi> selectOne(T_SIZE);</font></tt></b>
|
||||
<br><a NAME="select_encapsulate"></a><tt><font color="#009900"><b> </b>//
|
||||
is now encapsulated in a eoSelectPerc (entage)</font></tt>
|
||||
<br><tt><font color="#009900"><b> eoSelectPerc<Indi> select(selectOne);</b>//
|
||||
by default rate==1</font></tt></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="replace"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#009900"><b> </b>// And we now have the full
|
||||
slection/replacement - though with </font></tt>
|
||||
<br><tt><font color="#009900"><b> </b>// no replacement (== generational
|
||||
replacement) at the moment :-)</font></tt>
|
||||
<br><b><tt><font color="#009900"> eoNoReplacement<Indi> replace; </font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="operators"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#993399"><b> </b>//////////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>// The variation operators</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>//////////////////////////////////////</font></tt></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="crossover"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#993399"><b> </b>// uniform chooce on segment
|
||||
made by the parents</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoSegmentCrossover<Indi> xoverS;</font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// uniform choice in hypercube
|
||||
built by the parents</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoArithmeticCrossover<Indi> xoverA;</font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// Combine them with relative
|
||||
weights</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoPropCombinedQuadOp<Indi> xover(xoverS,
|
||||
segmentRate);</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> xover.add(xoverA, arithmeticRate,
|
||||
true);</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="mutation"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#999900">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// define your individuals<br>
|
||||
<b>typedef eoReal<double> Indi; </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
<br><tt><font color="#993399"><b> </b>// offspring(i) uniformly chosen
|
||||
in [parent(i)-epsilon, parent(i)+epsilon]</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoUniformMutation<Indi>
|
||||
mutationU(EPSILON); </font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// k (=1) coordinates of parents
|
||||
are uniformly modified</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoDetUniformMutation<Indi>
|
||||
mutationD(EPSILON); </font></tt></b>
|
||||
<br><tt><font color="#993399"> // all coordinates of parents are normally
|
||||
modified (stDev SIGMA)</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoNormalMutation<Indi>
|
||||
mutationN(SIGMA); </font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// Combine them with relative
|
||||
weights</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoPropCombinedMonOp<Indi> mutation(mutationU,
|
||||
uniformMutRate);</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> mutation.add(mutationD, detMutRate);</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> mutation.add(mutationN, normalMutRate,
|
||||
true);</font></tt></b>
|
||||
<p><tt><font color="#993399"><b> </b>// The operators are encapsulated
|
||||
into an eoTRansform object</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoSGATransform<Indi> transform(xover,
|
||||
P_CROSS, mutation, P_MUT);</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="evalfunc"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<a NAME="stop"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#CC0000">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// a simple fitness function that computes the euclidian norm of a real vector<br>
|
||||
// Now in a separate file, and declared as binary_value(const vector<bool> &)<br>
|
||||
<b>#include "real_value.h"</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
<td></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<a NAME="checkpoint"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
<b>void main_function(int argc, char **argv)</b><br>
|
||||
<b>{</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
<td><tt><font color="#3366FF"><b> </b>//////////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// termination conditions: use
|
||||
more than one</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>/////////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// stop after MAX_GEN generations</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoGenContinue<Indi> genCont(MAX_GEN);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// do MIN_GEN gen., then stop
|
||||
after STEADY_GEN gen. without improvement</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoSteadyFitContinue<Indi> steadyCont(MIN_GEN,
|
||||
STEADY_GEN);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// stop when fitness reaches
|
||||
a target (here VEC_SIZE)</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoFitContinue<Indi> fitCont(0);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// do stop when one of the above
|
||||
says so</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoCombinedContinue<Indi> continuator(genCont);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> continuator.add(steadyCont);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> continuator.add(fitCont);</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="parametres"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<a NAME="generation"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> const unsigned int SEED = 42; </b>// seed for random number generator<br>
|
||||
<b> const unsigned int T_SIZE = 3; </b>// size for tournament selection<br>
|
||||
<b> const unsigned int VEC_SIZE = 8; </b>// Number of object variables in genotypes<br>
|
||||
<b> const unsigned int POP_SIZE = 20; </b>// Size of population<br>
|
||||
<b> const unsigned int MAX_GEN = 500; </b>// Maximum number of generation before STOP<br>
|
||||
<b> const unsigned int MIN_GEN = 10; </b>// Minimum number of generation before ...<br>
|
||||
<b> const unsigned int STEADY_GEN = 50; </b>// stop after STEADY_GEN gen. without improvelent<br>
|
||||
<b> const float P_CROSS = 0.8; </b>// Crossover probability<br>
|
||||
<b> const float P_MUT = 0.5; </b>// mutation probability<br>
|
||||
<b> const double EPSILON = 0.01; </b>// range for real uniform mutation<br>
|
||||
<b> </b>// some parameters for chosing among different operators<br>
|
||||
<b> const double segmentRate = 0.5; </b>// rate for 1-pt Xover<br>
|
||||
<b> const double arithmeticRate = 0.5; </b>// rate for 2-pt Xover<br>
|
||||
<b> const double uniformMutRate = 0.5; </b>// rate for bit-flip mutation<br>
|
||||
<b> const double detMutRate = 0.5; </b>// rate for one-bit mutation<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
<td><tt><font color="#FF6666"><b> </b>/////////////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#FF6666"><b> </b>// the algorithm</font></tt>
|
||||
<br><tt><font color="#FF6666"><b> </b>////////////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#FF6666"><b> </b>// Easy EA requires </font></tt>
|
||||
<br><tt><font color="#FF6666"><b> </b>// selection, transformation,
|
||||
eval, replacement, and stopping criterion</font></tt>
|
||||
<br><b><tt><font color="#FF6666"> eoEasyEA<Indi> gga(continuator,
|
||||
eval, select, transform, replace);</font></tt></b>
|
||||
<br><tt><font color="#FF6666"><b> </b>// Apply algo to pop - that's
|
||||
it!</font></tt>
|
||||
<br><b><tt><font color="#FF6666"> cout << "\n
|
||||
Here we go\n\n";</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666"> gga(pop);</font></tt></b>
|
||||
<br> </td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<a NAME="output"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
<b> </b>//////////////////////////<br>
|
||||
<b> </b>// Random seed<br>
|
||||
<b> </b>//////////////////////////<br>
|
||||
<b> </b>//reproducible random seed: if you don't change SEED above, <br>
|
||||
<b> </b>// you'll aways get the same result, NOT a random run<br>
|
||||
<b> rng.reseed(SEED);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
<td><tt><font color="#3366FF"><b> </b>// Print (sorted) intial population</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> pop.sort();</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> cout << "FINAL Population\n"
|
||||
<< pop << endl;</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="eval"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<a NAME="general"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#CC0000">
|
||||
<b> </b>/////////////////////////////<br>
|
||||
<b> </b>// Fitness function<br>
|
||||
<b> </b>////////////////////////////<br>
|
||||
<b> </b>// Evaluation: from a plain C++ fn to an EvalFunc Object<br>
|
||||
<b> </b>// you need to give the full description of the function<br>
|
||||
<b> eoEvalFuncPtr<Indi, double, const vector<double>& > eval( real_value );</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="init"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>////////////////////////////////<br>
|
||||
<b> </b>// Initilisation of population<br>
|
||||
<b> </b>////////////////////////////////<br>
|
||||
<b> </b>// based on a uniform generator<br>
|
||||
<b> eoInitFixedLength<Indi, uniform_generator<double> ></b><br>
|
||||
<b> random(VEC_SIZE, uniform_generator<double>(-1.0, 1.0));</b><br>
|
||||
<b> </b>// Initialization of the population<br>
|
||||
<b> eoPop<Indi> pop(POP_SIZE, random);</b><br>
|
||||
<b> </b><br>
|
||||
<b> </b>// and evaluate it in one loop<br>
|
||||
<b> apply<Indi>(eval, pop); </b>// STL syntax<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>// sort pop before printing it!<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> </b>// Print (sorted) intial population (raw printout)<br>
|
||||
<b> cout << "Initial Population" << endl;</b><br>
|
||||
<b> cout << pop;</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="engine"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// selection and replacement<br>
|
||||
<b> </b>////////////////////////////////////<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="select"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>// The robust tournament selection<br>
|
||||
<b> eoDetTournamentSelect<Indi> selectOne(T_SIZE);</b><br>
|
||||
<a NAME="select_encapsulate"></a>
|
||||
<b> </b>// is now encapsulated in a eoSelectPerc (entage)<br>
|
||||
<b> eoSelectPerc<Indi> select(selectOne);</b>// by default rate==1<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="replace"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>// And we now have the full slection/replacement - though with <br>
|
||||
<b> </b>// no replacement (== generational replacement) at the moment :-)<br>
|
||||
<b> eoNoReplacement<Indi> replace; </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="operators"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// The variation operators<br>
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="crossover"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>// uniform chooce on segment made by the parents<br>
|
||||
<b> eoSegmentCrossover<Indi> xoverS;</b><br>
|
||||
<b> </b>// uniform choice in hypercube built by the parents<br>
|
||||
<b> eoArithmeticCrossover<Indi> xoverA;</b><br>
|
||||
<b> </b>// Combine them with relative rates<br>
|
||||
<b> eoPropCombinedQuadOp<Indi> xover(xoverS, segmentRate);</b><br>
|
||||
<b> xover.add(xoverA, arithmeticRate, true);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="mutation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b><br>
|
||||
<b> </b>// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]<br>
|
||||
<b> eoUniformMutation<Indi> mutationU(EPSILON); </b><br>
|
||||
<b> </b>// k (=1) coordinates of parents are uniformly modified<br>
|
||||
<b> eoDetUniformMutation<Indi> mutationD(EPSILON); </b><br>
|
||||
<b> </b>// Combine them with relative rates<br>
|
||||
<b> eoPropCombinedMonOp<Indi> mutation(mutationU, uniformMutRate);</b><br>
|
||||
<b> mutation.add(mutationD, detMutRate, true);</b><br>
|
||||
<b> </b><br>
|
||||
<b> </b>// The operators are encapsulated into an eoTRansform object<br>
|
||||
<b> eoSGATransform<Indi> transform(xover, P_CROSS, mutation, P_MUT);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="stop"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="checkpoint"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// termination conditions: use more than one<br>
|
||||
<b> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// stop after MAX_GEN generations<br>
|
||||
<b> eoGenContinue<Indi> genCont(MAX_GEN);</b><br>
|
||||
<b> </b>// do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement<br>
|
||||
<b> eoSteadyFitContinue<Indi> steadyCont(MIN_GEN, STEADY_GEN);</b><br>
|
||||
<b> </b>// stop when fitness reaches a target (here VEC_SIZE)<br>
|
||||
<b> eoFitContinue<Indi> fitCont(0);</b><br>
|
||||
<b> </b>// do stop when one of the above says so<br>
|
||||
<b> eoCombinedContinue<Indi> continuator(genCont);</b><br>
|
||||
<b> continuator.add(steadyCont);</b><br>
|
||||
<b> continuator.add(fitCont);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="generation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#FF6666">
|
||||
<b> </b>/////////////////////////////////////////<br>
|
||||
<b> </b>// the algorithm<br>
|
||||
<b> </b>////////////////////////////////////////<br>
|
||||
<b> </b>// Easy EA requires <br>
|
||||
<b> </b>// selection, transformation, eval, replacement, and stopping criterion<br>
|
||||
<b> eoEasyEA<Indi> gga(continuator, eval, select, transform, replace);</b><br>
|
||||
<b> </b>// Apply algo to pop - that's it!<br>
|
||||
<b> cout << "\n Here we go\n\n";</b><br>
|
||||
<b> gga(pop);</b><br>
|
||||
<b> </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>// Print (sorted) intial population<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> cout << "FINAL Population\n" << pop << endl;</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
<b>}</b><br>
|
||||
// A main that catches the exceptions<br>
|
||||
<b>int main(int argc, char **argv)</b><br>
|
||||
<b>{</b><br>
|
||||
<b>#ifdef _MSC_VER</b><br>
|
||||
<b> </b>// rng.reseed(42);<br>
|
||||
<b> int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</b><br>
|
||||
<b> flag |= _CRTDBG_LEAK_CHECK_DF;</b><br>
|
||||
<b> _CrtSetDbgFlag(flag);</b><br>
|
||||
// _CrtSetBreakAlloc(100);<br>
|
||||
<b>#endif</b><br>
|
||||
<b> try</b><br>
|
||||
<b> {</b><br>
|
||||
<b> main_function(argc, argv);</b><br>
|
||||
<b> }</b><br>
|
||||
<b> catch(exception& e)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> cout << "Exception: " << e.what() << '\n';</b><br>
|
||||
<b> }</b><br>
|
||||
<b> return 1;</b><br>
|
||||
<b>}</b><br>
|
||||
</font></font></font></td>
|
||||
<td><b><tt><font color="#993300">}</font></tt></b>
|
||||
<br><tt><font color="#993300">// A main that catches the exceptions</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">#ifdef _MSC_VER</font></tt></b>
|
||||
<br><tt><font color="#993300"><b> </b>// rng.reseed(42);</font></tt>
|
||||
<br><b><tt><font color="#993300"> int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> flag
|
||||
|= _CRTDBG_LEAK_CHECK_DF;</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> _CrtSetDbgFlag(flag);</font></tt></b>
|
||||
<br><tt><font color="#993300">// _CrtSetBreakAlloc(100);</font></tt>
|
||||
<br><b><tt><font color="#993300">#endif</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> try</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> {</font></tt></b>
|
||||
<br><b><tt><font color="#993300">
|
||||
main_function(argc, argv);</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 << "Exception: " << e.what() << '\n';</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> }</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> return 1;</font></tt></b>
|
||||
<br><b><tt><font color="#993300">}</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<hr WIDTH="100%"><a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="../../doc/html/index.html">EO
|
||||
|
|
@ -359,8 +346,8 @@ documentation</a></font>
|
|||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last
|
||||
modified: Wed Nov 29 07:38:36 2000
|
||||
<!-- hhmts end -->
|
||||
|
||||
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last modified: Wed Nov
|
||||
29 07:38:36 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
|||
Reference in a new issue