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);
exit(1);
}
(*(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);
exit(-1);
}
#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"
......
/*_________________________________________________________
onemax.ez
EASEA implementation of the ONEMAX problem
Pierre COLLET (Pierre.Collet@Polytechnique.Fr)
17/01/01
__________________________________________________________*/
\User classes :
GenomeClass { bool genes[10]; }
\end
\GenomeClass::initialiser : // "initializer" is also accepted
for (int i=0;i<10;i++) Genome.genes[i]=tossCoin(.5)?1:0;
\end
\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;}
\end
\GenomeClass::mutator : // Must hand back the number of mutations
int NbMut=0;
for (int i=0;i<10;i++)
if (tossCoin(pMutPerGene)){
NbMut++;
Genome.genes[i]=Genome.genes[i]?0:1;
}
return NbMut;
\end
\GenomeClass::evaluator : // Returns the score
int Score=0;
for (int i=0; i<10;i++)
Score+=(int)Genome.genes[i];
return Score;
\end
\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
\end
/*_________________________________________________________
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;}
\end
\User functions:
\end
\Initialisation function:
\end
\User classes:
GenomeClass
{
bool x[SIZE];
}
\end
\GenomeClass::initialiser:
for (int i=0;i<SIZE;i++) Genome.x[i]=tossCoin(.5)?1:0;
\end
\GenomeClass::crossover:
int CrossoverPosition=random(0,SIZE);
for(int i=0;i<CrossoverPosition+1;i++)
swap(child1.x[i],child2.x[i]);
\end
\GenomeClass::mutator:
int NbMut=0;
for (int i=0;i<SIZE;i++)
if (tossCoin(pMutPerGene)){
NbMut++;
Genome.x[i]=Genome.x[i]?0:1;
}
return NbMut;
\end
\GenomeClass::evaluator:
int Score=0;
for (int i=0; i<SIZE;i++)
Score+=(int)Genome.x[i];
return Score;
\end
\GenomeClass::display:
\end
\At each new generation:
\end
\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
\end
\TEMPLATE_START//********************************************
//
// 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;
double EZ_MUT_PROB, EZ_XOVER_PROB, EZ_REPL_PERC=0;
int EZ_NB_GEN, EZ_POP_SIZE;
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;
}
\ANALYSE_PARAMETERS
\INSERT_USER_DECLARATIONS
\INSERT_INITIALISATION_FUNCTION \ANALYSE_USER_CLASSES
\INSERT_USER_CLASSES
struct EASEAGenome{
public:
EASEAGenome::EASEAGenome() {
\GENOME_CTOR }
EASEAGenome(const EASEAGenome & orig) {
\GENOME_CTOR copy(orig);
}
virtual ~EASEAGenome() {
\GENOME_DTOR }
EASEAGenome& operator=(const EASEAGenome &);
virtual void copy(const EASEAGenome & c);
\INSERT_GENOME};
EASEAGenome& EASEAGenome::operator=(const EASEAGenome & arg) {
copy(arg);
return *this;
}
void EASEAGenome::copy(const EASEAGenome& genome) {
if(&genome != this){
\GENOME_DTOR
\COPY_CTOR }
}
typedef eoExternalEO<\MINIMAXI, EASEAGenome> Chrom;
istream & operator>>(istream &is, struct EASEAGenome &genome) {
\READ
return is;
}
ostream & operator<<(ostream &os, const struct EASEAGenome &genome) {
\WRITE
return os;
}
\INSERT_USER_FUNCTIONS
\INSERT_INITIALISER
\INSERT_CROSSOVER
\INSERT_MUTATOR
\INSERT_EVALUATOR
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()){
parser.printHelp(cout);
exit(1);
}
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
rng.reseed(seed);
\INSERT_INIT_FCT_CALL
// 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);
eo\REPLACEMENTReplacement<Chrom> \ELITIST_REPLACE_NAMEeplace\DISCARD_PRM;
\ELITISM
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
checkpoint.add(increment);
// 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
checkpoint.add(bestStat);
checkpoint.add(averageStat);
checkpoint.add(SecondStat);
// The Stdout monitor will print parameters to the screen ...
eoStdoutMonitor monitor(false);
// when called by the checkpoint (i.e. at every generation)
checkpoint.add(monitor);
// the monitor will output a series of parameters: add them
monitor.add(generationCounter);
monitor.add(ezEval); // because now eval is an eoEvalFuncCounter!
monitor.add(bestStat);
monitor.add(SecondStat);
// 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
checkpoint.add(fileMonitor);
checkpoint.add(*gnuMonitor);
// the fileMonitor can monitor parameters, too, but you must tell it!
fileMonitor.add(generationCounter);
fileMonitor.add(bestStat);
fileMonitor.add(SecondStat);
// the fileMonitor can monitor parameters, too, but you must tell it!
gnuMonitor->add(ezEval);
gnuMonitor->add(bestStat);
gnuMonitor->add(averageStat);
eoState outState;
// Register the algorithm into the state (so it has something to save!!)
outState.registerObject(parser);
outState.registerObject(pop);
outState.registerObject(rng);
// 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
checkpoint.add(stateSaver);
/////////////////////////////////////////
// the algorithm
////////////////////////////////////////
// Easy EA requires
// selection, transformation, eval, replacement, and stopping criterion
eoEasyEA<Chrom> gga(checkpoint, ezEval, select, transform, replace);
// Apply algo to pop
gga(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);
flag |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(flag);
// _CrtSetBreakAlloc(100);
#endif
try
{
main_function(argc, argv);
}
catch(exception& e)
{
cout << "Exception: " << e.what() << '\n';
}
return 1;
}
\TEMPLATE_END
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