312 lines
18 KiB
HTML
312 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.76 [en] (X11; U; Linux 2.2.17-21mdksmp i686) [Netscape]">
|
|
<title>FirstBitGA.html</title>
|
|
<!-- Changed by: Marc Schoenauer, 29-Nov-2000 -->
|
|
</head>
|
|
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
|
<a href="eoLesson1.html">Back to Lesson 1</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
|
|
documentation</a></font>
|
|
<br>
|
|
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
|
<center>
|
|
<h1>
|
|
<font color="#FF0000">Code for FirstBitGA</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>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
|
<tr NOSAVE>
|
|
<td NOSAVE><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
|
|
<br><tt><font color="#993300">// FirstBitGA.cpp</font></tt>
|
|
<br><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
|
|
<br><tt><font color="#993300">//*</font></tt>
|
|
<br><tt><font color="#993300">// An instance of a VERY simple Bitstring
|
|
Genetic Algorithm</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 >
|
|
<tr>
|
|
<td><b><tt><font color="#999900">#include <ga.h></font></tt></b><br>
|
|
<tt><font color="#999900">//-----------------------------------------------------------------------------</font></tt>
|
|
<br><tt><font color="#999900">// define your individuals</font></tt>
|
|
<br><tt><font color="#999900"><b>typedef eoBit<double> Indi;
|
|
</b>// A bitstring with fitness double</font></tt></td>
|
|
</tr>
|
|
</table>
|
|
|
|
<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 number of ones of a bitstring</font></tt>
|
|
<br><tt><font color="#CC0000">// @param _indi A biststring individual</font></tt>
|
|
<br><a NAME="evalfunc"></a><b><tt><font color="#CC0000">double binary_value(const
|
|
Indi & _indi)</font></tt></b>
|
|
<br><b><tt><font color="#CC0000">{</font></tt></b>
|
|
<br><b><tt><font color="#CC0000"> double sum = 0;</font></tt></b>
|
|
<br><b><tt><font color="#CC0000"> for (unsigned i = 0; i < _indi.size();
|
|
i++)</font></tt></b>
|
|
<br><b><tt><font color="#CC0000"> sum += _indi[i];</font></tt></b>
|
|
<br><b><tt><font color="#CC0000"> return sum;</font></tt></b>
|
|
<br><b><tt><font color="#CC0000">}</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> </b>// all parameters are hard-coded!</font></tt>
|
|
<br><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 bits 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 = 100;
|
|
</b>// Maximum number of generation before STOP</font></tt>
|
|
<br><tt><font color="#3366FF"><b> const float CROSS_RATE = 0.8;
|
|
</b>// Crossover rate</font></tt>
|
|
<br><tt><font color="#3366FF"><b> const double P_MUT_PER_BIT = 0.01;
|
|
</b>// probability of bit-flip mutation</font></tt>
|
|
<br><tt><font color="#3366FF"><b> const float MUT_RATE = 1.0;
|
|
</b>// mutation rate</font></tt></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="general"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
|
<tr>
|
|
<td><a NAME="random"></a><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><b><tt><font color="#CC0000"> eoEvalFuncPtr<Indi> eval(
|
|
binary_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>// declare the population</font></tt>
|
|
<br><b><tt><font color="#993399"> eoPop<Indi> pop;</font></tt></b>
|
|
<br><tt><font color="#993399"><b> </b>// fill it!</font></tt>
|
|
<br><b><tt><font color="#993399"> for (unsigned int igeno=0; igeno<POP_SIZE;
|
|
igeno++)</font></tt></b>
|
|
<br><b><tt><font color="#993399"> {</font></tt></b>
|
|
<br><tt><font color="#993399"><b>
|
|
Indi v;
|
|
</b>// void individual, to be filled</font></tt>
|
|
<br><b><tt><font color="#993399">
|
|
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)</font></tt></b>
|
|
<br><b><tt><font color="#993399">
|
|
{</font></tt></b>
|
|
<br><tt><font color="#993399"><b>
|
|
bool r = rng.flip(); </b>// new value, random in {0,1}</font></tt>
|
|
<br><tt><font color="#993399"><b>
|
|
v.push_back(r); </b>//
|
|
append that random value to v</font></tt>
|
|
<br><b><tt><font color="#993399">
|
|
}</font></tt></b>
|
|
<br><tt><font color="#993399"><b>
|
|
eval(v);
|
|
</b>// evaluate it</font></tt>
|
|
<br><b><tt><font color="#993399">
|
|
pop.push_back(v);
|
|
</font></tt></b><tt><font color="#993399">// and put it in the population</font></tt>
|
|
<br><b><tt><font color="#993399"> }</font></tt></b></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><tt><font color="#009900"><b> eoDetTournamentSelect<Indi> select(T_SIZE);
|
|
</b>// T_SIZE in [2,POP_SIZE]</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>// The simple GA evolution engine
|
|
uses generational replacement</font></tt>
|
|
<br><tt><font color="#009900"><b> </b>// so no replacement procedure
|
|
is needed</font></tt></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="stop"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
|
<tr>
|
|
<td></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>// 1-point crossover for bitstring</font></tt>
|
|
<br><b><tt><font color="#993399"> eo1PtBitXover<Indi> xover;</font></tt></b></td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="mutation"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
|
<tr>
|
|
<td>
|
|
<br><tt><font color="#993399"><b> </b>// standard bit-flip mutation
|
|
for bitstring</font></tt>
|
|
<br><b><tt><font color="#993399"> eoBitMutation<Indi> mutation(P_MUT_PER_BIT);</font></tt></b></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"><b> </b>//////////////////////////////////////</font></tt>
|
|
<br><tt><font color="#3366FF"><b> </b>// termination condition</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> continuator(MAX_GEN);</font></tt></b>
|
|
<br> </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>/////////////////////////////////////////</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>// standard Generational GA
|
|
requires as parameters</font></tt>
|
|
<br><tt><font color="#FF6666"><b> </b>// selection, evaluation, crossover
|
|
and mutation, stopping criterion</font></tt>
|
|
<p><b><tt><font color="#FF6666"> eoSGA<Indi> gga(select, xover,
|
|
CROSS_RATE, mutation, MUT_RATE, </font></tt></b>
|
|
<br><b><tt><font color="#FF6666">
|
|
eval, continuator);</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"> gga(pop);</font></tt></b>
|
|
<br> </td>
|
|
</tr>
|
|
</table>
|
|
<a NAME="final_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</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="main"></a>
|
|
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
|
<tr>
|
|
<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="eoLesson1.html">Back to Lesson 1</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
|
|
documentation</a></font>
|
|
<hr>
|
|
<address>
|
|
<a href="mailto:Marc.Schoenauer@inria.fr">Marc Schoenauer</a></address>
|
|
|
|
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last modified: Sun Nov
|
|
19 08:31:26 2000<!-- hhmts end -->
|
|
</body>
|
|
</html>
|