Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Arnaud Kress
easea
Commits
bd18b1e4
Commit
bd18b1e4
authored
Nov 06, 2008
by
moh_lo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Parser/lexer v0.7
parent
6d388b3a
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
772 additions
and
2 deletions
+772
-2
EaseaParse.cpp
EaseaParse.cpp
+7
-2
EaseaParse.h
EaseaParse.h
+1
-0
myonemax.ez
myonemax.ez
+55
-0
onemax.ez
onemax.ez
+100
-0
tpl/EO.tpl
tpl/EO.tpl
+290
-0
tpl/GALib.tpl
tpl/GALib.tpl
+319
-0
No files found.
EaseaParse.cpp
View file @
bd18b1e4
...
...
@@ -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 210
0
"C:\\repo\\src\\EaseaParse.cpp"
#line 210
4
"C:\\repo\\src\\EaseaParse.cpp"
void
YYPARSENAME
::
yytables
()
{
yyattribute_size
=
sizeof
(
YYSTYPE
);
...
...
EaseaParse.h
View file @
bd18b1e4
...
...
@@ -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"
...
...
myonemax.ez
0 → 100755
View file @
bd18b1e4
/*_________________________________________________________
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
onemax.ez
0 → 100755
View file @
bd18b1e4
/*_________________________________________________________
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
tpl/EO.tpl
0 → 100755
View file @
bd18b1e4
\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
tpl/GALib.tpl
0 → 100755
View file @
bd18b1e4
\TEMPLATE_START//********************************************
//
// EASEA.cpp
//
// C++ file generated by AESAE-GALIB Millennium Edition (v0.6)
//
//********************************************
#define \OPERATING_SYSTEM
#include
<stdio.h>
#include
<stdlib.h>
#include
<math.h>
#include
<iostream.h>
#include
<fstream.h>
#include
<time.h>
#include
<ga
/
ga.h
>
#ifdef UNIX_OS
#include
<unistd.h>
#include
<values.h>
#include "gdchart0.94b/gdc.h"
#include "gdchart0.94b/gdchart.h"
#endif
#define MINIMISE -1
#define MAXIMISE 1
int EZ_EVALUATED_GENERATIONS=0;
int EZ_NB_EVALUATIONS=0;
clock_t EZ_START, EZ_AVG_START, EZ_FINISH;
double EZ_LET=0; // Last Elapsed Time
double EZ_LERT=1; // Last Evaluated Remaining Time
float EZ_MUT_PROB, EZ_CROSS_PROB, EZ_REPL_PERC=0;
int EZ_NB_GEN, EZ_POP_SIZE;
double *EZ_daFITNESS;
#ifdef UNIX_OS
extern "C" out_graph( short, short, FILE *, GDC_CHART_T, int, char *[], int, float[]);
#endif
GABoolean tossCoin(float prob=.5){
return GAFlipCoin(prob);
}
GABoolean tossCoin(double prob=.5){
return GAFlipCoin((float)prob);
}
inline int random(int b1=0, int b2=1){
return GARandomInt(b1,b2);
}
inline double random(double b1=0, double b2=1){
return GARandomDouble(b1,b2);
}
inline float random(float b1=0, float b2=1){
return GARandomFloat(b1,b2);
}
\ANALYSE_PARAMETERS\INSERT_USER_DECLARATIONS
\ANALYSE_USER_CLASSES
\INSERT_USER_CLASSES
// User Genome
class EASEAGenome : public GAGenome {
// Default methods for class EASEAGenome
public:
GADefineIdentity("EASEAGenome", 251);
static void Initializer(GAGenome
&);
static int Mutator(GAGenome
&
, float);
static float Comparator(const GAGenome
&
, const GAGenome
&);
static float Evaluator(GAGenome
&);
static int Crossover(const GAGenome
&
, const GAGenome
&
, GAGenome*, GAGenome*);
public:
EASEAGenome::EASEAGenome() :GAGenome(Initializer, Mutator, Comparator){
evaluator(Evaluator); crossover(Crossover);
\GENOME_CTOR }
EASEAGenome(const EASEAGenome
&
orig) {
\GENOME_CTOR copy(orig);
}
~EASEAGenome() {
\GENOME_DTOR }
EASEAGenome
&
operator=(const EASEAGenome
&);
virtual GAGenome *clone(GAGenome::CloneMethod) const ;
virtual void copy(const GAGenome
&
c);
virtual int equal(const GAGenome
&
g) const;
virtual int read(istream
&
is);
virtual int write(ostream
&
os) const ;
\INSERT_GENOME};
EASEAGenome
&
EASEAGenome::operator=(const EASEAGenome
&
arg){
copy(arg);
return *this;
}
void EASEAGenome::copy(const GAGenome
&
g) {
if(
&
g != this){
\GENOME_DTOR
GAGenome::copy(g); // copy the base class part
EASEAGenome
&
genome = (EASEAGenome
&)g;
\COPY_CTOR }
}
GAGenome*EASEAGenome::clone(GAGenome::CloneMethod) const {
return new EASEAGenome(*this);
}
int EASEAGenome::equal(const GAGenome
&
g) const {
EASEAGenome
&
genome = (EASEAGenome
&)g;
\EQUAL return 1;
}
float EASEAGenome::Comparator(const GAGenome
&
g, const GAGenome
&
h) {
EASEAGenome
&
sis = (EASEAGenome
&)g;
EASEAGenome
&
bro = (EASEAGenome
&)h;
int diff = 0;
\COMPARE return (float)diff;
}
int EASEAGenome::read(istream
&
is) {
\READ return is.fail() ? 1 : 0;
}
\INSERT_USER_FUNCTIONS
int EASEAGenome::write(ostream
&
os) const {
\INSERT_DISPLAY
\WRITE return os.fail() ? 1 : 0;
}
\INSERT_INITIALISATION_FUNCTION
\INSERT_INITIALISER
\INSERT_CROSSOVER
\INSERT_MUTATOR
\INSERT_EVALUATOR
\INSERT_GENERATION_FUNCTION
int main(int argc, char *argv[]){
FILE *EZ_PRM;
int i;
if ( (EZ_daFITNESS = (double *) malloc(\NB_GEN * sizeof (double) )) == NULL){
fprintf(stderr,"Not enough memory... bailing out.");
exit (1);
}
#ifdef UNIX_OS
//netscape declarations and initialisations
char EZ_Netscape[]="netscape";
char *EZ_NetscapeParams[]={
"netscape",
"-remote",
"OpenFile /home/collet/progs/easea/EASEA.html",
"-geometry",
"350x500",
NULL
};
// HTML and gdchart declarations and initialisations
FILE *EZ_HtmlFile;
int EZ_GDC_NbXValues;
int EZ_GDC_Sampling=1;
char *EZ_GDC_XLabels[51];
float *EZ_GDC_Values;
unsigned long EZ_GDC_Color=0x8080FF;
FILE *EZ_GDC_OutputFile;
GDC_BGColor = 0xFFFFFFL; // backgound color (white)
GDC_LineColor = 0x000000L; // line color (black)
GDC_SetColor =
&EZ_GDC_Color;
// Creation of the html file
if (!(EZ_HtmlFile=fopen("EASEA.html","w"))){
fprintf(stderr,"Unable to open EASEA.html");
exit (1);
}
fprintf(EZ_HtmlFile,"
<HTML>
<BODY
BGCOLOR=
\"#000000\"
TEXT=
\"#FFFFFF\"
>
");
fprintf(EZ_HtmlFile,"Best individual/generation for
<CODE>
EASEA
</CODE>
experiment.
<BR><BR><HR>
");