Commit bd18b1e4 authored by moh_lo's avatar moh_lo

Parser/lexer v0.7

parent 6d388b3a
......@@ -1987,7 +1987,7 @@ nNB_MIG=(int)yyattribute(2 - 2).dValue;
#line 1241 "C:\\repo\\src\\EaseaParse.y"
if (SymbolTable.find(yyattribute(1 - 1).pSymbol->sName)==NULL){
fprintf(stderr,"\n%s - Error line %d: Symbol \"%s\" not found.\n",sEZ_FILE_NAME,EASEALexer.yylineno,yyattribute(1 - 1).pSymbol->sName);
fprintf(stderr,"\n%s - Error line %d: Symbol \"%s\" not found.\n",sEZ_FILE_NAME,EASEALexer.yylineno,yyattribute(1 - 1).pSymbol->sName);
(*(YYSTYPE YYFAR*)yyvalptr).dValue = (SymbolTable.find(yyattribute(1 - 1).pSymbol->sName))->dValue;
......@@ -2095,8 +2095,13 @@ double CEASEAParser::divide(double a, double b)
void CEASEAParser::yysyntaxerror(){
fprintf(stderr,"%s \t Error line %d\n",sEZ_FILE_NAME,EASEALexer.yylineno);
#line 2100 "C:\\repo\\src\\EaseaParse.cpp"
#line 2104 "C:\\repo\\src\\EaseaParse.cpp"
void YYPARSENAME::yytables()
yyattribute_size = sizeof(YYSTYPE);
......@@ -109,6 +109,7 @@ public:
double assign(CSymbol* pIdentifier, double dValue);
double divide(double dDividend, double dDivisor);
void yysyntaxerror();
CSymbol* insert() const;
#line 115 "C:\\repo\\src\\EaseaParse.h"
EASEA implementation of the ONEMAX problem
Pierre COLLET (Pierre.Collet@Polytechnique.Fr)
\User classes :
GenomeClass { bool genes[10]; }
\GenomeClass::initialiser : // "initializer" is also accepted
for (int i=0;i<10;i++) Genome.genes[i]=tossCoin(.5)?1:0;
\GenomeClass::crossover :
int CrossoverPosition=random(0,10);
bool b;
for(int i=0;i<CrossoverPosition+1;i++)
{b=child1.genes[i]; child1.genes[i]=child2.genes[i]); child2.genes[i]=b;}
\GenomeClass::mutator : // Must hand back the number of mutations
int NbMut=0;
for (int i=0;i<10;i++)
if (tossCoin(pMutPerGene)){
return NbMut;
\GenomeClass::evaluator : // Returns the score
int Score=0;
for (int i=0; i<10;i++)
return Score;
\Default run parameters : // Please let the parameters appear in this order
Number of generations : 15 // NB_GEN
Mutation probability : 1 // MUT_PROB
Crossover probability : 1 // XOVER_PROB
Population size : 30 // POP_SIZE
Selection operator : Tournament // RouletteWheel, Deterministic, Ranking, Random
Offspring population size : 12 // 40%
Replacement strategy : Plus // Comma, SteadyState, Generational
Discarding operator : Worst // Best, Tournament, Parent, Random
Evaluator goal : Maximise // Minimise
Elitism : On // Off
This C:\WINDOWS\a_onemax\onemax.ez file was automatically created by GUIDE v0.1b
\User declarations:
#define SIZE 100
float pMutPerGene=0.1;
inline void swap(bool& a, bool& b)
{bool c=a; a=b; b=c;}
\User functions:
\Initialisation function:
\User classes:
bool x[SIZE];
for (int i=0;i<SIZE;i++) Genome.x[i]=tossCoin(.5)?1:0;
int CrossoverPosition=random(0,SIZE);
for(int i=0;i<CrossoverPosition+1;i++)
int NbMut=0;
for (int i=0;i<SIZE;i++)
if (tossCoin(pMutPerGene)){
return NbMut;
int Score=0;
for (int i=0; i<SIZE;i++)
return Score;
\At each new generation:
\Default run parameters:
// Variation operators:
Operators are called: Sequentially
Mutation probability: 0.3
Crossover probability: 0.8
// Evolution Engine:
Evaluator goal: Minimise
Number of generations: 2000
Evolutionary engine: Custom
Population size: 100
Elite: 10
Fertility: 80
Genitors selector: Tournament 2
Selected genitors: 100
Offspring size: 100
Reduce parents: Tournament 2
Surviving parents: 20
Reduce offspring: Tournament 2
Surviving offspring: 80
Final reduce: Tournament 2
Elitism: Strong
// Island model:
Number of islands: 5
Emigration policy: Move
Migrants selector: Tournament 2
Migrants destination: Neighbours
Migration frequency: 0.2
Number of emigrants: 3
Immigration replacement: Tournament 2
Immigration policy: Add
// EASEA.cpp
// C++ file generated by AESAE-EO Millennium Edition (v0.6)
#include <stdexcept> // runtime_error
#include <iostream> // cout
#include <strstream> // ostrstream, istrstream
#include <fstream>
#include <eo>
#include <other/eoExternalEO.h>
#include <other/eoExternalOpFunctions.h>
unsigned * pCurrentGeneration;
unsigned * pEZ_NB_GEN;
unsigned long EZ_NB_EVALUATIONS=0;
inline int random(int b1=0, int b2=1){
return rng.random(b2-b1)+b1;
inline double random(double b1=0, double b2=1){
return rng.uniform(b2-b1)+b1;
inline float random(float b1=0, float b2=1){
return rng.uniform(b2-b1)+b1;
struct EASEAGenome{
EASEAGenome::EASEAGenome() {
EASEAGenome(const EASEAGenome & orig) {
\GENOME_CTOR copy(orig);
virtual ~EASEAGenome() {
EASEAGenome& operator=(const EASEAGenome &);
virtual void copy(const EASEAGenome & c);
EASEAGenome& EASEAGenome::operator=(const EASEAGenome & arg) {
return *this;
void EASEAGenome::copy(const EASEAGenome& genome) {
if(&genome != this){
typedef eoExternalEO<\MINIMAXI, EASEAGenome> Chrom;
istream & operator>>(istream &is, struct EASEAGenome &genome) {
return is;
ostream & operator<<(ostream &os, const struct EASEAGenome &genome) {
return os;
int main_function(int argc, char **argv) {
// define a parser from the command-line arguments
eoParser parser(argc, argv);
// For each parameter, define Parameter, read it through the parser,
// and assign the value to the variable
eoValueParam<uint32> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
uint32 seed = seedParam.value();
// the parameter found in the .ez file is set as a default value
eoValueParam<unsigned int> popSizeParam(\POP_SIZE, "POP_SIZE", "Population size",'P');
parser.processParam( popSizeParam, "Evolution engine" );
EZ_POP_SIZE = popSizeParam.value();
eoValueParam<unsigned int> maxGenParam(\NB_GEN, "NB_GEN", "Maximum number of generations",'G');
parser.processParam( maxGenParam, "Stopping criterion" );
EZ_NB_GEN = maxGenParam.value();
pEZ_NB_GEN = & maxGenParam.value();
eoValueParam<double> pCrossParam(\XOVER_PROB, "XOVER_PROB", "Probability of Crossover", 'C');
parser.processParam( pCrossParam, "Genetic Operators" );
EZ_XOVER_PROB = pCrossParam.value();
eoValueParam<double> pMutParam(\MUT_PROB, "MUT_PROB", "Probability of Mutation", 'M');
parser.processParam( pMutParam, "Genetic Operators" );
EZ_MUT_PROB = pMutParam.value();
eoValueParam<unsigned int> freqSavParam(0, "freqSav", "Save population every freqSav generations (0=final only)",'f');
parser.processParam( freqSavParam, "Persistence" );
unsigned freqSav = freqSavParam.value();
eoValueParam<unsigned int> boolGnuplotParam(1, "gnuplot", "Interactive gnuplot display (0/1)",'g');
parser.processParam( boolGnuplotParam, "Persistence" );
unsigned boolGnuplot = boolGnuplotParam.value();
string str_status = "EASEA.status";
eoValueParam<string> statusParam(str_status.c_str(), "status","Status file",'S');
parser.processParam( statusParam, "Persistence" );
if (parser.userNeedsHelp()){
if (statusParam.value() != ""){
ofstream os(statusParam.value().c_str());
os << parser; // and you can use that file as parameter file
eoExternalInit<\MINIMAXI,EASEAGenome> ezInitialiser(&InitialiserFunction);
eoExternalMonOp<\MINIMAXI,EASEAGenome> ezMutation(&MutationFunction);
eoExternalQuadOp<\MINIMAXI,EASEAGenome> ezXOver(&CrossoverFunction);
eoExternalEvalFunc<\MINIMAXI,EASEAGenome> ezExtEval(&EvaluationFunction);
// ... to an object that counts the nb of actual evaluations
eoEvalFuncCounter<Chrom> ezEval(ezExtEval);
//reproductible random seed: if you don't change SEED above,
// you'll aways get the same result, NOT a random run
// Initialization of the population
eoPop<Chrom> pop(EZ_POP_SIZE, ezInitialiser);
// and evaluate it (STL syntax)
apply<Chrom>(ezEval, pop);
// Print best guy in intial population
cout << "Best individual in initial population:\n" << pop.best_element() << endl;
// The variation operators
// The operators are encapsulated into an eoTRansform object
eoDynSGATransform<Chrom> transform(ezXOver, EZ_XOVER_PROB, ezMutation, EZ_MUT_PROB);
// selection and replacement
eo\SELECTORSelect<Chrom> selectOne\SELECT_PRM;
eoSelectPerc<Chrom> select(selectOne,\REPL_PERC);
eoGenContinue<Chrom> continuator(EZ_NB_GEN);
// Declare a checkpoint (from a continuator)
eoCheckPoint<Chrom> checkpoint(continuator);
// Create a counter parameter
eoValueParam<unsigned> generationCounter(0, "Generation");
// Create an incrementor (which is an eoUpdater). Note that the
// Parameter's value is passed by reference, so every time the incrementer increments,
// the data in generationCounter will change.
eoIncrementor<unsigned> increment(generationCounter.value());
// Add it to the checkpoint, this will result in the counter being incremented every generation
// now some statistics on the population:
// Best fitness in population
eoBestFitnessStat<Chrom> bestStat;
eoAverageStat<Chrom> averageStat;
// Second moment stats: average and stdev
eoSecondMomentStats<Chrom> SecondStat;
// Add them to the checkpoint to get them called at the appropriate time
// The Stdout monitor will print parameters to the screen ...
eoStdoutMonitor monitor(false);
// when called by the checkpoint (i.e. at every generation)
// the monitor will output a series of parameters: add them
monitor.add(ezEval); // because now eval is an eoEvalFuncCounter!
// A file monitor: will print parameters to ... a File, yes, you got it!
eoFileMonitor fileMonitor("stats.xg", " ");
// interactive output: only if user wants it - hence have to use a pointer
eoMonitor *gnuMonitor;
if (boolGnuplot) // plot only if user requires
gnuMonitor = new eoGnuplot1DMonitor("best_average.xg");
else // but create file anyway
gnuMonitor = new eoFileMonitor("best_average.xg");
// the checkpoint mechanism can handle multiple monitors
// the fileMonitor can monitor parameters, too, but you must tell it!
// the fileMonitor can monitor parameters, too, but you must tell it!
eoState outState;
// Register the algorithm into the state (so it has something to save!!)
// and feed the state to state savers
// save state every freqSav generation
unsigned int freq = freqSav;
if (!freqSav) // only final generation
freq = EZ_NB_GEN+1; // so no save will take place before the end
eoCountedStateSaver stateSaver(freq, outState, "generation", true);
// Don't forget to add the saver to the checkpoint
// the algorithm
// Easy EA requires
// selection, transformation, eval, replacement, and stopping criterion
eoEasyEA<Chrom> gga(checkpoint, ezEval, select, transform, replace);
// Apply algo to pop
// print best final guy
cout << "\nBest individual in final population:\n" << pop.best_element() << endl;
// will have to do this more properly - cf eoFunctorStore
delete gnuMonitor;
return 0;
// A main that catches the exceptions
int main(int argc, char **argv)
#ifdef _MSC_VER
int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
// _CrtSetBreakAlloc(100);
main_function(argc, argv);
catch(exception& e)
cout << "Exception: " << e.what() << '\n';
return 1;
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment