Generic_T_ad.ez 6.82 KB
Newer Older
maitre's avatar
maitre committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*_________________________________________________________

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

 
maitre's avatar
maitre committed
20
float pMutPerGene=0.1;
maitre's avatar
maitre committed
21 22
#define N_LIM 10
int n = N_LIM;
maitre's avatar
maitre committed
23 24 25 26 27 28 29

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);
maitre's avatar
maitre committed
30 31 32
float Rosenbrock(float *, int);
float Schwefel(float *, int);
float Weierstrass(float *, int);
maitre's avatar
maitre committed
33

maitre's avatar
maitre committed
34 35 36
struct gpuOptions{
};

maitre's avatar
maitre committed
37 38 39 40 41 42
\end

\User functions:
//fitness function
#include <math.h>

maitre's avatar
maitre committed
43
inline float Sphere(float x[SIZE], int n) // Ex-DeJong function 1
maitre's avatar
maitre committed
44
{
maitre's avatar
maitre committed
45
   float ret = 0;
maitre's avatar
maitre committed
46 47 48 49
    	for (int i = 0;i<n; i++) ret += x[ i ] * x[ i ];
      	return ret;
}

maitre's avatar
maitre committed
50
inline float AckleyPath(float x[SIZE], int n)
maitre's avatar
maitre committed
51 52
{
    //Parameters
maitre's avatar
maitre committed
53 54 55 56
    float a = 20.; 
    float b = 0.2; 
    float c = 2*PI;
    float Scale = 32.768; // pour matcher avec les limites [-1,1]
maitre's avatar
maitre committed
57 58
 
    //Function computation
maitre's avatar
maitre committed
59 60
    float sum_x2 = 0.0;
    float sum_cos_cx = 0.0;
maitre's avatar
maitre committed
61 62 63 64
    for (int i=0; i<n; i++) {
		sum_x2 += Scale*Scale*x[i]*x[i];
    		sum_cos_cx += cos(c*Scale*x[i]);
		}
maitre's avatar
maitre committed
65
    float res = -a * exp( -b * sqrt( 1/(float)n * sum_x2) ) - exp( 1/(float)n * sum_cos_cx ) + a + exp(1);
maitre's avatar
maitre committed
66 67
    return res;
}
maitre's avatar
maitre committed
68
inline float Easom(float x[SIZE], int n) 
maitre's avatar
maitre committed
69
{
maitre's avatar
maitre committed
70
    float res = 1.;
maitre's avatar
maitre committed
71 72 73 74 75
    //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.
}
maitre's avatar
maitre committed
76
inline float Griewangk(float x[SIZE], int n) 
maitre's avatar
maitre committed
77
{
maitre's avatar
maitre committed
78 79
   float res, sum = 0., prod =1.;
   float Scale = 600.; // pour matcher avec les limites [-1,1]
maitre's avatar
maitre committed
80 81
 
	for (int i=1; i<=n; i++) {
maitre's avatar
maitre committed
82
		prod *= cos( Scale*x[i]/sqrt( (float)i ) );
maitre's avatar
maitre committed
83 84 85 86 87 88
    		sum += (Scale*Scale*x[i]*x[i]/4000.);
		}
  	res = sum - prod + 1.;
    	return res;
}

maitre's avatar
maitre committed
89
inline float Rastrigin(float x[SIZE], int n)
maitre's avatar
maitre committed
90
{
maitre's avatar
maitre committed
91 92
   float res = 0.;
   float Scale = 5.12; // pour matcher avec les limites [-1,1]
maitre's avatar
maitre committed
93 94 95 96 97 98

   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);
}

maitre's avatar
maitre committed
99
inline float Rosenbrock(float x[SIZE], int n)
maitre's avatar
maitre committed
100
{
maitre's avatar
maitre committed
101 102
   float res = 0.;
   float Scale = 2.048; // pour matcher avec les limites [-1,1]
maitre's avatar
maitre committed
103 104 105 106 107 108 109

   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);
}

maitre's avatar
maitre committed
110
inline float Schwefel(float x[SIZE], int n)
maitre's avatar
maitre committed
111
{
maitre's avatar
maitre committed
112 113
   float res = 0.;
   float Scale = 500.; // pour matcher avec les limites [-1,1]
maitre's avatar
maitre committed
114 115 116

   for (int i = 0;i<n; i++) 
    	res += (-Scale*x[i]*sin( sqrt(Abs(Scale*x[i]))));
maitre's avatar
maitre committed
117
   return ((float)n*418.9829 + res);
maitre's avatar
maitre committed
118 119
}

maitre's avatar
maitre committed
120
__device__ __host__ inline float Weierstrass(float x[SIZE], int n)  // Weierstrass multimidmensionnel h = 0.25
maitre's avatar
maitre committed
121
{
maitre's avatar
maitre committed
122 123 124 125
   float res = 0.;
   float val[SIZE];
   float b=2.;
   float h = 0.25;
maitre's avatar
maitre committed
126 127 128 129

   for (int i = 0;i<n; i++) {
	val[i] = 0.;
    	for (int k=0;k<ITER;k++)
maitre's avatar
maitre committed
130
		val[i] += pow(b,-(float)k*h) * sin(pow(b,(float)k)*x[i]);
maitre's avatar
maitre committed
131 132 133 134 135
	res += Abs(val[i]);
	}
   return (res);
} 

maitre's avatar
maitre committed
136
float gauss()
maitre's avatar
maitre committed
137 138 139 140
/* Generates a normally distributed random value with variance 1 and 0 mean.
    Algorithm based on "gasdev" from Numerical recipes' pg. 203. */
{
  int iset = 0;
maitre's avatar
maitre committed
141 142 143
  float gset = 0.0;
  float v1 = 0.0, v2 = 0.0, r = 0.0;
  float factor = 0.0;
maitre's avatar
maitre committed
144 145 146 147 148 149 150

  if (iset) {
        iset = 0;
        return gset;
      	}
  else {    
        do {
maitre's avatar
maitre committed
151 152
            v1 = (float)globalRandomGenerator->randFloat(0.,1.) * 2.0 - 1.0;
            v2 = (float)globalRandomGenerator->randFloat(0.,1.) * 2.0 - 1.0;
maitre's avatar
maitre committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
            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:
171
  std::cout<<"*************  n: "<<n<<std::endl;
maitre's avatar
maitre committed
172 173
\end

174 175 176 177
\Finalization function:
cout << "finalization function called" << endl;
\end

maitre's avatar
maitre committed
178 179
\User classes :
GenomeClass { 
maitre's avatar
maitre committed
180 181 182
  float x[SIZE];
  float sigma[SIZE]; // auto-adaptative mutation parameter
}
maitre's avatar
maitre committed
183 184 185 186
\end

\GenomeClass::initialiser : // "initializer" is also accepted
  for(int i=0; i<n; i++ ) {
maitre's avatar
maitre committed
187 188
     	Genome.x[i] = (float)random(X_MIN,X_MAX);
	Genome.sigma[i]=(float)random(0.,0.5);
maitre's avatar
maitre committed
189 190 191 192
	}
\end

\GenomeClass::crossover :
maitre's avatar
maitre committed
193
  for (int i=0; i<n; i++)
maitre's avatar
maitre committed
194
  {
maitre's avatar
maitre committed
195
     float alpha = (float)globalRandomGenerator->getRandomIntMax(1.); // barycentric crossover
maitre's avatar
maitre committed
196
     child1.x[i] = alpha*parent1.x[i] + (1.-alpha)*parent2.x[i];
maitre's avatar
maitre committed
197 198 199 200 201 202
     //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;
maitre's avatar
maitre committed
203
  float pond = 1./sqrt((float)n);
maitre's avatar
maitre committed
204 205 206 207

    for (int i=0; i<n; i++)
    if (tossCoin(pMutPerGene)){
    	NbMut++;
maitre's avatar
maitre committed
208
       	Genome.sigma[i] = Genome.sigma[i] * exp(SIGMA*pond*(float)gauss());
maitre's avatar
maitre committed
209 210
       	Genome.sigma[i] = MIN(0.5,Genome.sigma[0]);              
       	Genome.sigma[i] = MAX(0.,Genome.sigma[0]);
maitre's avatar
maitre committed
211
       	Genome.x[i] += Genome.sigma[i]*(float)gauss();
maitre's avatar
maitre committed
212 213 214 215 216 217 218
       	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
maitre's avatar
maitre committed
219 220 221 222
  float Score= 0.0;
  float Point[SIZE];
  for (int i=0; i<N_LIM; i++) Point[i] = Genome.x[i];
  Score= Weierstrass(Point, N_LIM);         
maitre's avatar
maitre committed
223 224 225 226 227 228 229
  return Score;
\end

\User Makefile options: 
CPPFLAGS+=
\end

maitre's avatar
maitre committed
230 231


maitre's avatar
maitre committed
232 233 234 235
\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
maitre's avatar
maitre committed
236 237
  Population size : 20     // POP_SIZE
  Offspring size : 20 // 40%  
maitre's avatar
maitre committed
238
  Genitors selector: Tournament 2
maitre's avatar
maitre committed
239 240 241 242 243 244 245
  Final reduce: Tournament 3

    Reduce parents: Tournament 2
    Surviving parents : 5

    Reduce offspring: Tournament 2
    Surviving offspring: 17
maitre's avatar
maitre committed
246 247

		    	   //  Selection operator : Tournament // RouletteWheel, Deterministic, Ranking, Random
maitre's avatar
maitre committed
248

maitre's avatar
maitre committed
249 250 251
		   //Replacement strategy : Plus    // Comma, SteadyState, Generational
		   //  Discarding operator : Worst    // Best, Tournament, Parent, Random
  Evaluator goal : Minimise      // Maximise
maitre's avatar
maitre committed
252 253
  Elitism: Strong
  Elite: 1
maitre's avatar
maitre committed
254
\end
255