Commit eb546cbc authored by moh_lo's avatar moh_lo

Almost ready version

with working weierstrass algorithm
parent eb43e1e1
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -35,6 +35,7 @@ public:
CListItem<T> *pNext; // pointer to the next object in the list
};
/////////////////////////////////////////////////////////////////////////////
// Linked List
......@@ -55,7 +56,7 @@ public :
CListItem<T> *walkToNextItem();
CListItem<T> *remove(T *p);
};
void reInitAlreadyPtedBit(CLList<CSymbol *> *pSymbolList);
/////////////////////////////////////////////////////////////////////////////
// Symbol
......@@ -83,7 +84,11 @@ public:
// Operations
public:
void print(FILE *f);
void printHdr(FILE *fp);
void printCode(FILE* fp);
void printClasses(FILE *f);
void printClassesHdr(FILE* f);
void printClassesCode(FILE* f);
void printAllSymbols(FILE *f, char *, EObjectType, CListItem<CSymbol *> *pSym);
};
......
/*_________________________________________________________
Test functions
log normal adaptive mutation
Selection operator: Tournament
__________________________________________________________*/
\User declarations :
#define SIZE 501
#define X_MIN -1.
#define X_MAX 1.
#define ITER 50
#define Abs(x) ((x) < 0 ? -(x) : (x))
#define MAX(x,y) ((x)>(y)?(x):(y))
#define MIN(x,y) ((x)<(y)?(x):(y))
#define SIGMA 1. /* mutation parameter */
#define PI 3.141592654
#include <math.h>
float pMutPerGene=1.;
int mutationOccured = 0;
#define N 10
float (*Fitness)(float *, int); // pointeur sur la fonction de fitness designee dans argv[1]
float Sphere(float *, int);
float AckleyPath(float *, int);
float Easom(float *, int);
float Griewangk(float *, int);
float Rastrigin(float *, int);
float Rosenbrock(float *, int);
float Schwefel(float *, int);
float Weierstrass(float *, int);
\end
\User functions proto:
float gauss();
\end
\User functions:
//fitness function
inline float Sphere(float x[SIZE], int n) {// Ex-DeJong function 1
float ret = 0;
for (int i = 0;i<n; i++) ret += x[ i ] * x[ i ];
return ret;
}
/* __host__ __device__ inline float AckleyPath(float x[SIZE], int n) */
/* { */
/* //Parameters */
/* float a = 20.; */
/* float b = 0.2; */
/* float c = 2*PI; */
/* float Scale = 32.768; // pour matcher avec les limites [-1,1] */
/* //Function computation */
/* float sum_x2 = 0.0; */
/* float sum_cos_cx = 0.0; */
/* for (int i=0; i<n; i++) { */
/* sum_x2 += Scale*Scale*x[i]*x[i]; */
/* sum_cos_cx += cos(c*Scale*x[i]); */
/* } */
/* float res = -a * exp( -b * sqrt( 1/(float)n * sum_x2) ) - exp( 1/(float)n * sum_cos_cx ) + a + exp(1); */
/* return res; */
/* } */
/* __host__ __device__ inline float Easom(float x[SIZE], int n) */
/* { */
/* float res = 1.; */
/* //function computation */
/* for (int i=0; i<n; i++) */
/* res *= cos(100.*x[i])*exp(-(100.*x[i]-PI)*(100.*x[i]-PI)); */
/* return (1.-res); // pour avoir un minimum a 0. */
/* } */
/* __host__ __device__ inline float Griewangk(float x[SIZE], int n) */
/* { */
/* float res, sum = 0., prod =1.; */
/* float Scale = 600.; // pour matcher avec les limites [-1,1] */
/* for (int i=1; i<=n; i++) { */
/* prod *= cos( Scale*x[i]/sqrt( (float)i ) ); */
/* sum += (Scale*Scale*x[i]*x[i]/4000.); */
/* } */
/* res = sum - prod + 1.; */
/* return res; */
/* } */
__host__ __device__ inline float Rastrigin(float x[SIZE], int n)
{
float res = 0.;
float Scale = 5.12; // pour matcher avec les limites [-1,1]
for (int i = 0;i<n; i++)
res += ((Scale*Scale*x[i]*x[i])-10*cos( 2*PI*Scale*x[i]));
return (10.*N + res);
}
/* inline float Rosenbrock(float x[SIZE], int n) */
/* { */
/* float res = 0.; */
/* float Scale = 2.048; // pour matcher avec les limites [-1,1] */
/* for (int i = 0;i<n; i++) */
/* res += 100.*((Scale*x[i+1] - Scale*Scale*x[i]*x[i])*(Scale*x[i+1] */
/* - Scale*Scale*x[i]*x[i]))+(1-Scale*x[i])*(1-Scale*x[i]); */
/* return (res); */
/* } */
__host__ __device__ inline float Schwefel(float x[SIZE], int n)
{
float res = 0.;
float Scale = 500.; // pour matcher avec les limites [-1,1]
for (int i = 0;i<N; i++)
res += (-Scale*x[i]*sin(sqrt(Abs(Scale*x[i]))));
return ((float)n*418.9829 + res);
}
__device__ __host__ inline float Weierstrass(float x[SIZE], int n) // Weierstrass multimidmensionnel h = 0.25
{
float res = 0.;
float val[SIZE];
float b=2.;
float h = 0.25;
for (int i = 0;i<N; i++) {
val[i] = 0.;
for (int k=0;k<ITER;k++)
val[i] += pow(b,-(float)k*h) * sin(pow(b,(float)k)*x[i]);
res += Abs(val[i]);
}
return (res);
}
float gauss()
/* Generates a normally distributed random value with variance 1 and 0 mean.
Algorithm based on "gasdev" from Numerical recipes' pg. 203. */
{
int iset = 0;
float gset = 0.0;
float v1 = 0.0, v2 = 0.0, r = 0.0;
float factor = 0.0;
if (iset) {
iset = 0;
return gset;
}
else {
do {
v1 = (float)fRandomLoc(0.,1.) * 2.0 - 1.0;
v2 = (float)fRandomLoc(0.,1.) * 2.0 - 1.0;
r = v1 * v1 + v2 * v2;
}
while (r > 1.0);
factor = sqrt (-2.0 * log (r) / r);
gset = v1 * factor;
iset = 1;
return (v2 * factor);
} }
\end
\Initialisation function:
cout<<"N: "<<N<<endl;
\end
\User classes :
GenomeClass {
float x[SIZE];
float sigma[SIZE]; // auto-adaptative mutation parameter
}
\end
\GenomeClass::initialiser : // "initializer" is also accepted
for(int i=0; i<N; i++ ) {
Genome.x[i] = (float)fRandomLoc(X_MIN,X_MAX);
Genome.sigma[i]=(float)fRandomLoc(0.,0.5);
}
\end
\GenomeClass::crossover :
for (int i=0; i<N; i++)
{
float alpha = (float)randomLoc(0.,1.); // barycentric crossover
if (&child1) child1->Genome.x[i] = alpha*parent1->Genome.x[i] + (1.-alpha)*parent2->Genome.x[i];
//if (&child2) child2->Genome.x[i] = alpha*parent2->Genome.x[i] + (1.-alpha)*parent1->Genome.x[i];
}
\end
\GenomeClass::mutator : // Must return the number of mutations
int NbMut=0;
float pond = 1./sqrt((float)N);
for (int i=0; i<N; i++)
if (tossCoin(pMutPerGene)){
mutationOccured++;
NbMut++;
Genome.sigma[i] = Genome.sigma[i] * exp(SIGMA*pond*(float)gauss());
Genome.sigma[i] = MIN(0.5,Genome.sigma[0]);
Genome.sigma[i] = MAX(0.,Genome.sigma[0]);
Genome.x[i] += Genome.sigma[i]*(float)fRandomLoc(0,1);
Genome.x[i] = MIN(X_MAX,Genome.x[i]); // pour eviter les depassements
Genome.x[i] = MAX(X_MIN,Genome.x[i]);
}
return NbMut;
\end
\GenomeClass::evaluator : // Returns the score
float Score= 0.0;
Score= Weierstrass(Genome.x,N);
return Score;
\end
\Default run parameters : // Please let the parameters appear in this order
Number of generations : 100 // NB_GEN
Mutation probability : 1 // MUT_PROB
Crossover probability : 1 // XOVER_PROB
Population size : 10 // POP_SIZE
Genitors selector: Tournament 2
Final reduce: Tournament 2
// Selection operator : Tournament // RouletteWheel, Deterministic, Ranking, Random
Offspring size : 80% // 40%
//Replacement strategy : Plus // Comma, SteadyState, Generational
// Discarding operator : Worst // Best, Tournament, Parent, Random
Evaluator goal : Minimise // Maximise
Elitism : On // Off
\end
#define FITNESS_TYPE float
#define BOOLEAN_EA char
#define true 1
#define false 0
/* #ifdef DEBUG_KRNL */
/* #define CDC( lastError, errorMsg, fun ) \ */
/* fun ; \ */
/* cout <<__FILE__<< " : "<<__LINE__<<" "<<errorMsg<<" : "<<endl; \ */
/* cout << "\t" <<(cudaGetErrorString(lastError=cudaGetLastError())) \ */
/* << endl; \ */
/* if( lastError != cudaSuccess )exit(-1) */
/* #else */
/* #define CDC( lastError, errorMsg, fun ) \ */
/* fun */
/* #endif */
#ifdef DEBUG_KRNL
#define CDC( lastError, errorMsg, fun ) \
fun ; \
if((lastError=cudaGetLastError())!=cudaSuccess ){ \
cout <<__FILE__<< " : "<<__LINE__<<" "<<errorMsg<<" : "<<endl; \
cout << "\t" <<(cudaGetErrorString(lastError)) \
<< endl; \
exit(-1); \
}
#else
#define CDC( lastError, errorMsg, fun ) \
fun
#endif
#ifndef OUTPUT_EA_H
#define OUTPUT_EA_H
#define TMP_BUF_LENGTH 512
#define TIMING
#include "timing.h"
#include <libxml/tree.h>
#include "basetype.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
DECLARE_TIME(gpu);
DECLARE_TIME(cpu);
DECLARE_TIME(init);
DECLARE_TIME(krnl);
DECLARE_TIME(memCpy1);
DECLARE_TIME(memCpy2);
DECLARE_TIME(alloc);
BOOLEAN_EA cmp;
BOOLEAN_EA repartition;
size_t popSize;
size_t nbBlock,nbThreadPB,nbThreadLB,memSize,sharedMemSize;
size_t fakeIteration;
}OutputEa;
xmlNodePtr outputEaToXmlNode(OutputEa* tt);
xmlChar* timevalToXmlChar(struct timeval* ts);
void outputTss(const char* filename);
void addTs(OutputEa* ts);
void initTss(const char* args);
void attachSignal();
#ifdef __cplusplus
}
#endif
#endif
#include <time.h> //gettimeofday
#include <sys/time.h>
#include <stdio.h>
#ifndef TIMING_H
#define TIMING_H
#ifdef TIMING
#define DECLARE_TIME(t) \
struct timeval t##_beg, t##_end, t##_res
#define TIME_ST(t) \
gettimeofday(&t##_beg,NULL)
#define TIME_END(t) \
gettimeofday(&t##_end,NULL)
#define SHOW_TIME(t) \
timersub(&t##_end,&t##_beg,&t##_res); \
printf("%s : %d.%06d\n",#t,t##_res.tv_sec,t##_res.tv_usec)
#define SHOW_SIMPLE_TIME(t) \
printf("%s : %d.%06d\n",#t,t.tv_sec,t.tv_usec)
#define COMPUTE_TIME(t) \
timersub(&t##_end,&t##_beg,&t##_res)
#else
#define DECLARE_TIME(t)
#define TIME_ST(t)
#define TIME_END(t)
#define SHOW_TIME(t)
#define SHOW_SIMPLE_TIME(t)
#endif
#endif
#include <stdlib.h>
#include <stdio.h>
#include "tool.h"
#include <math.h>
int tossCoin(double p){
if( rand() < p*RAND_MAX)
return 1;
else
return 0;
}
int getRandomIntMax(int max){
double r = rand();
r = r / RAND_MAX;
r = r * max;
return r;
}
int randomLoc(int min, int max){
return min+getRandomIntMax(max-min);
}
float getRandomFloatMax(float max){
float r = rand();
r = r / RAND_MAX;
r = r * max;
return r;
}
float fRandomLoc(float min,float max){
return min+getRandomFloatMax(max-min);
}
size_t
partieEntiereSup(float E){
int fl = floor(E);
if( fl == E )
return E;
else
return floor(E)+1;
}
#ifndef TOOL_H
#define TOOL_H
#ifdef __cpluplus
extern "C" {
#endif
int tossCoin(double p);
int getRandomIntMax(int max);
int randomLoc(int min, int max);
size_t partieEntiereSup(float E);
float fRandomLoc(float min,float max);
#ifdef __cpluplus
}
#endif
#endif
......@@ -79,8 +79,8 @@ for( size_t i=0; i<SIZE ; i++ )
// Variation operators:
Operators are called: Sequentially
Mutation probability: 0.4
Crossover probability: 0.8
Mutation probability: 0.05
Crossover probability: 1
// Evolution Engine:
Evaluator goal: Maximise
......@@ -96,7 +96,7 @@ for( size_t i=0; i<SIZE ; i++ )
Surviving parents: 0
Reduce offspring: Tournament 2
Surviving offspring: 10
Final reduce: Tournament 2
Final reduce: Tournament 5
Elitism: Strong
// Island model:
......
/*_________________________________________________________
This C:\WINDOWS\a_listmarc\listsort.ez file was automatically created by GUIDE v0.1
_________________________________________________________*/
\User declarations:
int SIZE=10;
double pMutPerGene=0.4;
inline void swap(int& a,int& b) {int c=a; a=b; b=c;}
\end
\User functions:
void cross(GenomeClass *pChild, GenomeClass *pParent, int locus){
Element *p, *pChildList, *pParentList;
pChildList=pChild->pList; pParentList=pParent->pList;
for (int i=0;i<SIZE;i++){
if (i>=locus){
for(p=pChild->pList;pParentList->Value!=p->Value;p=p->pNext);
swap(p->Value, pChildList->Value);
}
pChildList=pChildList->pNext;
pParentList=pParentList->pNext;
}
}
\end
\Initialisation function:
if ((argc>1)&&(!strcmp(argv[1],"size"))) SIZE=atoi(argv[2]);
\end
\User classes:
Element { int Value;
Element *pNext; }
GenomeClass { Element *pList;
int Size; }
\end
\GenomeClass::initialiser:
Element *pElt;
Genome.Size=0;
Genome.pList=NULL;
for (int i=0;i<SIZE;i++){ // creation of a linked list of SIZE elements
pElt=new Element; // with the decreasing values:
pElt->Value=i+1; // (SIZE, SIZE-1, ... , 3, 2, 1)
pElt->pNext=Genome.pList;
Genome.pList=pElt;
Genome.Size++;
}
\end
\GenomeClass::crossover:
int locus=random (0,SIZE-1);
cross(&child1, &parent2, locus);
cross(&child2, &parent1, locus);
\end
\GenomeClass::mutator:
int NbMut=0;
Element *p=Genome.pList;
while (p->pNext){
if (tossCoin(pMutPerGene)){ //We swap the current value with the next
swap(p->Value,p->pNext->Value);
NbMut++;
}
p=p->pNext;
}
return NbMut;
\end
\GenomeClass::evaluator:
int i=0,eval=0;
Element *p=Genome.pList;
while(p->pNext){
if (p->Value==++i) eval+=10;
if (p->Value<p->pNext->Value) eval+=4;
else eval-=2;
p=p->pNext;
}
if (p->Value==SIZE) eval+=10;
return (eval<0 ? 0 : eval);
\end
\GenomeClass::display:
os << "Size:" << Genome.Size << "\n";
os << "pList:" << *(Genome.pList) << "\n";
os << "This was MY display function !\n";
\end
\At each new generation:
if (NB_GEN-currentGeneration==10) pMutPerGene=0.1;
\end
\Default run parameters:
// Variation operators:
Operators are called: Sequentially
Mutation probability: 0.3
Crossover probability: 0.8
// Evolution Engine:
Evaluator goal: Maximise
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
EXEC = main.out
CPPFLAGS = -DUNIX_OS -Ialexyacc/include/ -DDEBUG
CPPFLAGS = -DUNIX_OS -Ialexyacc/include/ -DDEBUG -g
CPPC = g++
LDFLAGS =
......@@ -21,7 +21,7 @@ alexyacc/libalex.so:alexyacc/*.cpp
clean:
rm -f *.o $(EXEC)
rm -f EaseaParse.cpp EaseaParse.h EaseaLex.cpp EaseaLex.h
#rm -f EaseaParse.cpp EaseaParse.h EaseaLex.cpp EaseaLex.h
cd alexyacc && make clean
......
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