📄 firstrealea.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-21mdk i686) [Netscape]"> <title>FirstRealEA.cpp</title></head><body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000"><a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorialmain page </a>- <a href="eoTopDown.html">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Basedpage</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="../../doc/html/index.html">EOdocumentation</a></font><br><hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- --><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><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">// 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 ><tr><td><tt><font color="#999900"><b>#include <es.h></b></font></tt><br><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 computesthe euclidian norm of a real vector</font></tt><br><tt><font color="#CC0000">// Now in a separate file, and declared asbinary_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>doubleSIGMA = 0.3; </b> // std dev.for normal mutation</font></tt><br><tt><font color="#3366FF"><b> </b>// some parameters for chosingamong different operators</font></tt><br><tt><font color="#3366FF"><b> const double hypercubeRate = 0.5; </b>// relative weight for hypercube Xover</font></tt><br><tt><font color="#3366FF"><b> const double segmentRate = 0.5;</b>// relative weight for segment Xover</font></tt><br><tt><font color="#3366FF"><b> const double uniformMutRate = 0.5;</b>// relative weight for uniform mutation</font></tt><br><tt><font color="#3366FF"><b> const double detMutRate = 0.5; </b>// relative weight for det-uniform 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 sameresult, 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 plainC++ fn to an EvalFunc Object</font></tt><br><tt><font color="#CC0000"><b> </b>// you need to give the fulldescription of the function</font></tt><br><b><tt><font color="#CC0000"> eoEvalFuncPtr<Indi, double, constvector<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 ofthe 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 printingit!</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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -