Commit d4c4f50b authored by maitre's avatar maitre

Makefile example

parent 4110cd42
/*_________________________________________________________
Test functions
log normal adaptive mutation
Selection operator: Tournament
__________________________________________________________*/
\User declarations :
#define SIZE 10
#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
float pMutPerGene=1.;
int n = 10;
double (*Fitness)(double *, int); // pointeur sur la fonction de fitness designee dans argv[1]
double Sphere(double *, int);
double AckleyPath(double *, int);
double Easom(double *, int);
double Griewangk(double *, int);
double Rastrigin(double *, int);
double Rosenbrock(double *, int);
double Schwefel(double *, int);
double Weierstrass(double *, int);
\end
\User functions:
//fitness function
#include <math.h>
inline double Sphere(double x[SIZE], int n) // Ex-DeJong function 1
{
double ret = 0;
for (int i = 0;i<n; i++) ret += x[ i ] * x[ i ];
return ret;
}
inline double AckleyPath(double x[SIZE], int n)
{
//Parameters
double a = 20.;
double b = 0.2;
double c = 2*PI;
double Scale = 32.768; // pour matcher avec les limites [-1,1]
//Function computation
double sum_x2 = 0.0;
double 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]);
}
double res = -a * exp( -b * sqrt( 1/(double)n * sum_x2) ) - exp( 1/(double)n * sum_cos_cx ) + a + exp(1);
return res;
}
inline double Easom(double x[SIZE], int n)
{
double 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.
}
inline double Griewangk(double x[SIZE], int n)
{
double res, sum = 0., prod =1.;
double Scale = 600.; // pour matcher avec les limites [-1,1]
for (int i=1; i<=n; i++) {
prod *= cos( Scale*x[i]/sqrt( (double)i ) );
sum += (Scale*Scale*x[i]*x[i]/4000.);
}
res = sum - prod + 1.;
return res;
}
inline double Rastrigin(double x[SIZE], int n)
{
double res = 0.;
double 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 double Rosenbrock(double x[SIZE], int n)
{
double res = 0.;
double 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);
}
inline double Schwefel(double x[SIZE], int n)
{
double res = 0.;
double 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 ((double)n*418.9829 + res);
}
inline double Weierstrass(double x[SIZE], int n) // Weierstrass multimidmensionnel h = 0.25
{
double res = 0.;
double val[SIZE];
double b=2.;
double h = 0.25;
for (int i = 0;i<n; i++) {
val[i] = 0.;
for (int k=0;k<ITER;k++)
val[i] += pow(b,-(double)k*h) * sin(pow(b,(double)k)*x[i]);
res += Abs(val[i]);
}
return (res);
}
double 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;
double gset = 0.0;
double v1 = 0.0, v2 = 0.0, r = 0.0;
double factor = 0.0;
if (iset) {
iset = 0;
return gset;
}
else {
do {
v1 = (double)globalRandomGenerator->randFloat(0.,1.) * 2.0 - 1.0;
v2 = (double)globalRandomGenerator->randFloat(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
\GenomeClass::display:
for( size_t i=0 ; i<SIZE ; i++){
// cout << Genome.x[i] << ":" << Genome.sigma[i] << "|";
printf("%.02f:%.02f|",Genome.x[i],Genome.sigma[i]);
}
\end
\Initialisation function:
if(argc>1 ){
if((!(strcmp(argv[1],"Sphere")))) Fitness = Sphere;
if((!(strcmp(argv[1],"AckleyPath")))) Fitness = AckleyPath;
if((!(strcmp(argv[1],"Easom")))) Fitness = Easom;
if((!(strcmp(argv[1],"Griewangk")))) Fitness = Griewangk;
if((!(strcmp(argv[1],"Rastrigin")))) Fitness = Rastrigin;
if((!(strcmp(argv[1],"Rosenbrock")))) Fitness = Rosenbrock;
if((!(strcmp(argv[1],"Schwefel")))) Fitness = Schwefel;
if((!(strcmp(argv[1],"Weierstrass")))) Fitness = Weierstrass;
if(argc>2) n = atoi(argv[2]);
}
else
Fitness = Weierstrass;
n=MIN(n, SIZE);
std::cout<<"n: "<<n<<std::endl;
// pour l'impression dans le fichier de resultats
double MinTheo = 0.;
// printf("%s_T_ad n= %s MinTheo= %f ",argv[1],argv[2],MinTheo);
\end
\User classes :
GenomeClass {
double x[SIZE];
double sigma[SIZE]; // auto-adaptative mutation parameter
}
\end
\GenomeClass::initialiser : // "initializer" is also accepted
for(int i=0; i<n; i++ ) {
Genome.x[i] = (double)random(X_MIN,X_MAX);
Genome.sigma[i]=(double)random(0.,0.5);
}
\end
\GenomeClass::crossover :
for (int i=0; i<n; i++)
{
double alpha = (double)globalRandomGenerator->getRandomIntMax(1.); // barycentric crossover
if (&child1) child1.x[i] = alpha*parent1.x[i] + (1.-alpha)*parent2.x[i];
//if (&child2) child2.x[i] = alpha*parent2.x[i] + (1.-alpha)*parent1.x[i];
}
\end
\GenomeClass::mutator : // Must return the number of mutations
int NbMut=0;
double pond = 1./sqrt((double)n);
for (int i=0; i<n; i++)
if (tossCoin(pMutPerGene)){
NbMut++;
Genome.sigma[i] = Genome.sigma[i] * exp(SIGMA*pond*(double)gauss());
Genome.sigma[i] = MIN(0.5,Genome.sigma[0]);
Genome.sigma[i] = MAX(0.,Genome.sigma[0]);
Genome.x[i] += Genome.sigma[i]*(double)gauss();
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
double Score= 0.0;
double Point[SIZE];
for (int i=0; i<n; i++) Point[i] = Genome.x[i];
Score= Weierstrass(Point, n);
return Score;
\end
\User Makefile options:
CPPFLAGS+=
\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 : 100 // 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
\ No newline at end of file
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