📄 secondbitea.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>SecondBitEA.cpp</title><!-- Changed by: Marc Schoenauer, 29-Nov-2000 --></head><body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000"><a href="eoLesson3.html">Back to Lesson 3</a> - <a href="eoTutorial.html">Tutorialmain page </a>- <a href="eoTopDown.html">Algorithm-Based</a> - <a href="eoBottomUp.html">Component-Based</a>-<a href="eoProgramming.html">Programminghints</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">SecondBitEA.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.<p>For this particular program, as all new lines are concerned with programparameter input and information output, the <font color="#FF6600">fontcolor</font> for program-parameter-related sections will <font color="#FF6600">referto the section where the parameters are used</font> whereas the backgroundcolor will remain blue.<p><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">// SecondGA.cpp</font></tt><br><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt><br><tt><font color="#993300">//*</font></tt><br><tt><font color="#993300">// Same code than FirstBitEA as far as EvolutionaryComputation is concerned</font></tt><br><tt><font color="#993300">// but now you learn to enter the parametersin a more flexible way</font></tt><br><tt><font color="#993300">// and to twidle the output to your preferences!</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><b><tt><font color="#993300">#include <fstream></font></tt></b><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="evalfunc"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE ><tr><td><tt><font color="#CC0000">// a simple fitness function that computesthe number of ones of a bitstring</font></tt><br><b><tt><font color="#CC0000">#include "binary_value.h"</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">//-----------------------------------------------------------------------------</font></tt><br><tt><font color="#999900">// define your genotype and fitness types</font></tt><br><b><tt><font color="#999900">typedef eoBit<double> Indi;</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">// the main_function: nothing changed(!),except variable initialization</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">//-----------------------------------------------------------------------------</font></tt><br><tt><font color="#3366FF">// instead of having all values of usefulparameters as constants, read them:</font></tt><br><tt><font color="#3366FF">// either on the command line (--option=valueor -o=value)</font></tt><br><tt><font color="#3366FF">// or in a parameter file (same syntax, order independent, </font></tt><br><tt><font color="#3366FF">// # = usual comment character </font></tt><br><tt><font color="#3366FF">// or in the environment (TODO)</font></tt><p><tt><font color="#3366FF">// First define a parser from the command-linearguments</font></tt><br><a NAME="parser_declare"></a><b><tt><font color="#3366FF">eoParserparser(argc, argv);</font></tt></b><p><tt><font color="#3366FF">// For each parameter, define Parameter, readit through the parser,</font></tt><br><tt><font color="#3366FF">// and assign the value to the variable</font></tt><br><a NAME="random"></a><br><b><tt><font color="#990000">eoValueParam<uint32> seedParam(time(0),"seed", "Random number seed", 'S');</font></tt></b><br><a NAME="seed_process"></a><b><tt><font color="#990000">parser.processParam(seedParam );</font></tt></b><br><a NAME="seed_assign"></a><b><tt><font color="#990000">unsigned seed= seedParam.value();</font></tt></b><p><a NAME="_seed_declare"></a><tt><font color="#999900">// decriptionof genotype</font></tt><br><b><tt><font color="#999900">eoValueParam<unsigned int>& vecSizeParam(8,"vecSize", "Genotype size",'V');</font></tt></b><br><b><tt><font color="#999900">parser.processParam( vecSizeParam, "Representation");</font></tt></b><br><b><tt><font color="#999900">unsigned vecSize = vecSizeParam.value();</font></tt></b><p><tt><font color="#009900">// parameters for evolution engine</font></tt><br><b><tt><font color="#009900">eoValueParam<unsigned int>& popSizeParam(10,"popSize", "Population size",'P');</font></tt></b><br><b><tt><font color="#009900">parser.processParam( popSizeParam, "Evolutionengine" );</font></tt></b><br><b><tt><font color="#009900">unsigned popSize = popSizeParam.value();</font></tt></b><p><b><tt><font color="#009900">eoValueParam<unsigned int>& tSizeParam(10,"tSize", "Tournament size",'T');</font></tt></b><br><b><tt><font color="#009900">parser.processParam( seedParam );</font></tt></b><br><b><tt><font color="#009900">unsigned tSize = tSizeParam.value();</font></tt></b><br><a NAME="_load_name"></a><br><tt><font color="#3366FF"><b> </b>// init and stop</font></tt><br><b><tt><font color="#3366FF">eoValueParam<string> loadNameParam("","Load","A save file to restart from",'L');</font></tt></b><br><b><tt><font color="#3366FF">parser.processParam( loadNameParam, "Persistence");</font></tt></b><br><b><tt><font color="#3366FF">string loadName = loadNameParam.value();</font></tt></b><p><b><tt><font color="#3366FF">eoValueParam<unsigned int> maxGenParam(100,"maxGen", "Maximum number of generations",'G');</font></tt></b><br><b><tt><font color="#3366FF">parser.processParam( maxGenParam, "Stoppingcriterion" );</font></tt></b><br><b><tt><font color="#3366FF">unsigned maxGen = maxGenParam.value();</font></tt></b><p><b><tt><font color="#3366FF">eoValueParam<unsigned int> minGenParam(100,"minGen", "Minimum number of generations",'g');</font></tt></b><br><b><tt><font color="#3366FF">parser.processParam( minGenParam, "Stoppingcriterion" );</font></tt></b><br><b><tt><font color="#3366FF">unsigned minGen = minGenParam.value();</font></tt></b><p><b><tt><font color="#3366FF">eoValueParam<unsigned int> steadyGenParam(100,"steadyGen", "Number of generations with no improvement",'s');</font></tt></b><br><b><tt><font color="#3366FF">parser.processParam( steadyGenParam, "Stoppingcriterion" );</font></tt></b><br><b><tt><font color="#3366FF">unsigned steadyGen = steadyGenParam.value();</font></tt></b><p><tt><font color="#CC33CC">// operators probabilities at the algorithmlevel</font></tt><br><b><tt><font color="#CC33CC">eoValueParam<double> pCrossParam(0.6,"pCross", "Probability of Crossover", 'C'); </font></tt></b><br><b><tt><font color="#CC33CC">parser.processParam( pCrossParam, "GeneticOperators" );</font></tt></b><br><b><tt><font color="#CC33CC">double pCross = pCrossParam.value();</font></tt></b><p><b><tt><font color="#CC33CC">eoValueParam<double> pMutParam(0.1,"pMut", "Probability of Mutation", 'M');</font></tt></b><br><b><tt><font color="#CC33CC">parser.processParam( pMutParam, "GeneticOperators" );</font></tt></b><br><b><tt><font color="#CC33CC">double pMut = pMutParam.value();</font></tt></b><p><tt><font color="#CC33CC">// relative rates for crossovers</font></tt><br><b><tt><font color="#CC33CC">eoValueParam<double> onePointRateParam(1,"onePointRate", "Relative rate for one point crossover", '1');</font></tt></b><br><b><tt><font color="#CC33CC">parser.processParam( onePointRateParam,"Genetic Operators" );</font></tt></b><br><b><tt><font color="#CC33CC">double onePointRate = onePointRateParam.value();</font></tt></b><p><b><tt><font color="#CC33CC">eoValueParam<double> twoPointsRateParam(1,"twoPointRate", "Relative rate for two point crossover", '2');</font></tt></b><br><b><tt><font color="#CC33CC">parser.processParam( twoPointsRateParam,"Genetic Operators" );</font></tt></b><br><b><tt><font color="#CC33CC">double twoPointsRate = twoPointsRateParam.value();</font></tt></b><p><b><tt><font color="#CC33CC">eoValueParam<double> uRateParam(2, "uRate","Relative rate for uniform crossover", 'U');</font></tt></b><br><b><tt><font color="#CC33CC">parser.processParam( uRateParam, "GeneticOperators" );</font></tt></b><br><b><tt><font color="#CC33CC">double URate = uRateParam.value();</font></tt></b><p><tt><font color="#CC33CC">// relative rates and private parameters formutations;</font></tt><br><b><tt><font color="#CC33CC">eoValueParam<double> pMutPerBitParam(0.01,"pMutPerBit", "Probability of flipping 1 bit in bit-flip mutation", 'b');</font></tt></b><br><b><tt><font color="#CC33CC">parser.processParam( pMutPerBitParam,"Genetic Operators" );</font></tt></b><br><b><tt><font color="#CC33CC">double pMutPerBit = pMutPerBitParam.value();</font></tt></b><p><b><tt><font color="#CC33CC">eoValueParam<double> bitFlipRateParam(0.01,"bitFlipRate", "Relative rate for bit-flip mutation", 'B');</font></tt></b><br><b><tt><font color="#CC33CC">parser.processParam( bitFlipRateParam,"Genetic Operators" );</font></tt></b><br><b><tt><font color="#CC33CC">double bitFlipRate = bitFlipRateParam.value();</font></tt></b><p><b><tt><font color="#CC33CC">eoValueParam<double> oneBitRateParam(0.01,"oneBitRate", "Relative rate for deterministic bit-flip mutation", 'D');</font></tt></b><br><b><tt><font color="#CC33CC">parser.processParam( oneBitRateParam,"Genetic Operators" );</font></tt></b><br><b><tt><font color="#CC33CC">double oneBitRate = oneBitRateParam.value();</font></tt></b>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -