Coupure prévue mardi 3 Août au matin pour maintenance du serveur. Nous faisons au mieux pour que celle-ci soit la plus brève possible.

Commit 7b125b06 authored by kruger's avatar kruger
Browse files

Modif mineure avant ajout de parallelisation openmp

parent 8961d1a7
/*_________________________________________________________
Test functions
log normal adaptive mutation
Selection operator: Tournament
__________________________________________________________*/
\User declarations :
#define SIZE 100
#define X_MIN -1.
#define X_MAX 1.
#define ITER 120
#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=0.1;
\end
\User functions:
//fitness function
#include <math.h>
__device__ __host__ inline static float SQR(float d)
{
return (d*d);
}
__device__ __host__ inline float rosenbrock( float const *x)
{
float qualitaet;
int i;
int DIM = SIZE;
qualitaet = 0.0;
for( i = DIM-2; i >= 0; --i)
qualitaet += 100.*SQR(SQR(x[i])-x[i+1]) + SQR(1.-x[i]);
return ( qualitaet);
} /* f_rosenbrock() */
__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)random(0.,1.) * 2.0 - 1.0;
v2 = (float)random(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
\Before everything else function:
//cout<<"Before everything else function called "<<endl;
\end
\After everything else function:
//cout << "After everything else function called" << endl;
\end
\At the beginning of each generation function:
//cout << "At the beginning of each generation function called" << endl;
\end
\At the end of each generation function:
//cout << "At the end of each generation function called" << endl;
\end
\At each generation before reduce function:
//cout << "At each generation before replacement function called" << endl;
\end
\User classes :
GenomeClass {
float x[SIZE];
float sigma[SIZE]; // auto-adaptative mutation parameter
}
\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
\GenomeClass::initialiser : // "initializer" is also accepted
for(int i=0; i<SIZE; i++ ) {
Genome.x[i] = (float)random(X_MIN,X_MAX);
Genome.sigma[i]=(float)random(0.,0.5);
}
\end
\GenomeClass::crossover :
for (int i=0; i<SIZE; i++)
{
float alpha = (float)random(0.,1.); // barycentric crossover
child.x[i] = alpha*parent1.x[i] + (1.-alpha)*parent2.x[i];
}
\end
\GenomeClass::mutator : // Must return the number of mutations
int NbMut=0;
float pond = 1./sqrt((float)SIZE);
for (int i=0; i<SIZE; i++)
if (tossCoin(pMutPerGene)){
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)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
float Score= 0.0;
Score= Weierstrass(Genome.x, SIZE);
// Score= rosenbrock(Genome.x);
return Score;
\end
\GenomeClass::optimiser : // Optimises the Genome
float pas=0.001;
float fitnesstmp = Genome.fitness;
float tmp[SIZE];
int index = 0;
for(int i=0; i<SIZE; i++)
tmp[i] = Genome.x[i];
for(int i=0; i<100; i++){
tmp[index] += pas;
fitnesstmp = Weierstrass(tmp, SIZE);
//fitnesstmp = rosenbrock(tmp);
if(fitnesstmp < Genome.fitness){
Genome.fitness = fitnesstmp;
Genome.x[index] = tmp[index];
}
else{
fitnesstmp = Genome.fitness;
tmp[index] = Genome.x[index];
if(pas < 0)
index = (index + 1)%SIZE;
pas *= -1;
}
}
\end
\User Makefile options:
CPPFLAGS+=
\end
\Default run parameters : // Please let the parameters appear in this order
Number of generations : 100 // NB_GEN
Time limit: 0 // In seconds, 0 to deactivate
Population size : 3840 //POP_SIZE
Offspring size : 3840 // 40%
Mutation probability : 1 // MUT_PROB
Crossover probability : 1 // XOVER_PROB
Evaluator goal : minimise // Maximise
Selection operator: Tournament 2.0
Surviving parents: 100%//percentage or absolute
Surviving offspring: 100%
Reduce parents operator: Tournament 2
Reduce offspring operator: Tournament 2
Final reduce operator: Tournament 2
Elitism: Strong //Weak or Strong
Elite: 1
Number of optimisation iterations : 1 //Number of optimisation iteration
Baldwinism : false //True or False (Lamarckism : keep optimised Genome)
Print stats:1 //Default: 1
Generate csv stats file:0
Generate gnuplot script:0
Generate R script:0
Plot stats:0 //Default: 0
\end
/*_________________________________________________________
Test functions
log normal adaptive mutation
Selection operator: Tournament
__________________________________________________________*/
\User declarations :
#define SIZE 100
#define X_MIN -1.
#define X_MAX 1.
#define ITER 120
#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=0.1;
\end
\User functions:
//fitness function
#include <math.h>
__device__ __host__ inline static float SQR(float d)
{
return (d*d);
}
__device__ __host__ inline float rosenbrock( float const *x)
{
float qualitaet;
int i;
int DIM = SIZE;
qualitaet = 0.0;
for( i = DIM-2; i >= 0; --i)
qualitaet += 100.*SQR(SQR(x[i])-x[i+1]) + SQR(1.-x[i]);
return ( qualitaet);
} /* f_rosenbrock() */
__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)random(0.,1.) * 2.0 - 1.0;
v2 = (float)random(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
\Before everything else function:
//cout<<"Before everything else function called "<<endl;
\end
\After everything else function:
//cout << "After everything else function called" << endl;
\end
\At the beginning of each generation function:
//cout << "At the beginning of each generation function called" << endl;
\end
\At the end of each generation function:
//cout << "At the end of each generation function called" << endl;
\end
\At each generation before reduce function:
//cout << "At each generation before replacement function called" << endl;
\end
\User classes :
GenomeClass {
float x[SIZE];
float sigma[SIZE]; // auto-adaptative mutation parameter
}
\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
\GenomeClass::initialiser : // "initializer" is also accepted
for(int i=0; i<SIZE; i++ ) {
Genome.x[i] = (float)random(X_MIN,X_MAX);
Genome.sigma[i]=(float)random(0.,0.5);
}
\end
\GenomeClass::crossover :
for (int i=0; i<SIZE; i++)
{
float alpha = (float)random(0.,1.); // barycentric crossover
child.x[i] = alpha*parent1.x[i] + (1.-alpha)*parent2.x[i];
}
\end
\GenomeClass::mutator : // Must return the number of mutations
int NbMut=0;
float pond = 1./sqrt((float)SIZE);
for (int i=0; i<SIZE; i++)
if (tossCoin(pMutPerGene)){
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)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
float Score= 0.0;
//Score= Weierstrass(Genome.x, SIZE);
Score= rosenbrock(Genome.x);
return Score;
\end
\GenomeClass::optimiser : // Optimises the Genome
float pas=0.001;
float fitnesstmp = Genome.fitness;
float tmp[SIZE];
int index = 0;
for(int i=0; i<SIZE; i++)
tmp[i] = Genome.x[i];
for(int i=0; i<100; i++){
tmp[index] += pas;
//fitnesstmp = Weierstrass(tmp, SIZE);
fitnesstmp = rosenbrock(tmp);
if(fitnesstmp < Genome.fitness){
Genome.fitness = fitnesstmp;
Genome.x[index] = tmp[index];
}
else{
fitnesstmp = Genome.fitness;
tmp[index] = Genome.x[index];
if(pas < 0)
index = (index + 1)%SIZE;
pas *= -1;
}
}
\end
\User Makefile options:
CPPFLAGS+=
\end
\Default run parameters : // Please let the parameters appear in this order
Number of generations : 100 // NB_GEN
Time limit: 0 // In seconds, 0 to deactivate
Population size : 10 //POP_SIZE
Offspring size : 10 // 40%
Mutation probability : 1 // MUT_PROB
Crossover probability : 1 // XOVER_PROB
Evaluator goal : minimise // Maximise
Selection operator: Tournament 2.0
Surviving parents: 100%//percentage or absolute
Surviving offspring: 100%
Reduce parents operator: Tournament 2
Reduce offspring operator: Tournament 2
Final reduce operator: Tournament 2
Elitism: Strong //Weak or Strong
Elite: 1
Number of optimisation iterations : 1 //Number of optimisation iteration
Baldwinism : false //True or False (Lamarckism : keep optimised Genome
Print stats:1 //Default: 1
Generate csv stats file:0
Generate gnuplot script:0
Generate R script:0
Plot stats:0 //Default: 0
\end
......@@ -112,16 +112,17 @@ void CEvolutionaryAlgorithm::addStoppingCriterion(CStoppingCriterion* sc){
void CEvolutionaryAlgorithm::runEvolutionaryLoop(){
CIndividual** elitistPopulation;
#ifdef WIN32
clock_t begin(clock());
#else
struct timeval begin;
gettimeofday(&begin,NULL);
gettimeofday(&begin,0);
#endif
std::cout << "Parent's population initializing "<< std::endl;
this->initializeParentPopulation();
if(!INSTEAD_EVAL_STEP)
this->population->evaluateParentPopulation();
else
......@@ -148,6 +149,7 @@ void CEvolutionaryAlgorithm::runEvolutionaryLoop(){
EASEABeginningGenerationFunction(this);
population->produceOffspringPopulation();
if(!INSTEAD_EVAL_STEP)
population->evaluateOffspringPopulation();
else
......@@ -213,7 +215,11 @@ void CEvolutionaryAlgorithm::runEvolutionaryLoop(){
}
#ifdef WIN32
void CEvolutionaryAlgorithm::showPopulationStats(clock_t beginTime){
#else
void CEvolutionaryAlgorithm::showPopulationStats(struct timeval beginTime){
#endif
currentAverageFitness=0.0;
currentSTDEV=0.0;
......@@ -238,15 +244,25 @@ void CEvolutionaryAlgorithm::showPopulationStats(struct timeval beginTime){
currentSTDEV/=population->parentPopulationSize;
currentSTDEV=sqrt(currentSTDEV);
#ifdef WIN32
clock_t end(clock());
double duration;
duration = (double)(end-beginTime)/CLOCKS_PER_SEC;
#else
struct timeval end, res;
gettimeofday(&end,0);
timersub(&end,&beginTime,&res);
#endif
//Affichage
if(params->printStats){
if(currentGeneration==0)
printf("GEN\tTIME\t\tEVAL\tBEST\t\tAVG\t\tSTDEV\n\n");
#ifdef WIN32
printf("%lu\t%2.6f\t%lu\t%.15e\t%.15e\t%.15e\n",currentGeneration,duration,population->currentEvaluationNb,population->Best->getFitness(),currentAverageFitness,currentSTDEV);
#else
printf("%d\t%ld.%06ld\t%d\t%.15e\t%.15e\t%.15e\n",currentGeneration,res.tv_sec,res.tv_usec,population->currentEvaluationNb,population->Best->getFitness(),currentAverageFitness,currentSTDEV);
#endif
//printf("%lu\t%ld.%06ld\t%lu\t%f\t%f\t%f\n",currentGeneration,res.tv_sec,res.tv_usec,population->currentEvaluationNb,population->Best->getFitness(),currentAverageFitness,currentSTDEV);
}
......@@ -256,7 +272,11 @@ void CEvolutionaryAlgorithm::showPopulationStats(struct timeval beginTime){
if(f!=NULL){
if(currentGeneration==0)
fprintf(f,"#GEN\tTIME\t\tEVAL\tBEST\t\tAVG\t\tSTDEV\n\n");
#ifdef WIN32
fprintf(f,"%lu\t%2.6f\t%lu\t%.15e\t%.15e\t%.15e\n",currentGeneration,duration,population->currentEvaluationNb,population->Best->getFitness(),currentAverageFitness,currentSTDEV);
#else
fprintf(f,"%d\t%ld.%06ld\t%d\t%.15e\t%.15e\t%.15e\n",currentGeneration,res.tv_sec,res.tv_usec,population->currentEvaluationNb,population->Best->getFitness(),currentAverageFitness,currentSTDEV);
#endif
fclose(f);
}
}
......@@ -268,7 +288,11 @@ void CEvolutionaryAlgorithm::showPopulationStats(struct timeval beginTime){
if(f!=NULL){
if(currentGeneration==0)
fprintf(f,"GEN,TIME,EVAL,BEST,AVG,STDEV\n");
#ifdef WIN32
fprintf(f,"%lu,%2.6f,%lu,%.15e,%.15e,%.15e\n",currentGeneration,duration,population->currentEvaluationNb,population->Best->getFitness(),currentAverageFitness,currentSTDEV);
#else
fprintf(f,"%d,%ld.%06ld,%d,%.15e,%.15e,%.15e\n",currentGeneration,res.tv_sec,res.tv_usec,population->currentEvaluationNb,population->Best->getFitness(),currentAverageFitness,currentSTDEV);
#endif
fclose(f);
}
}
......
......@@ -118,8 +118,8 @@ void cudaPreliminaryProcess(size_t populationSize, dim3* dimBlock, dim3* dimGrid
dimGrid->x = (nbBlock);
dimBlock->x = nbThreadPB;
std::cout << "Number of grid : " << dimGrid->x << std::endl;
std::cout << "Number of block : " << dimBlock->x << std::endl;
//std::cout << "Number of grid : " << dimGrid->x << std::endl;
//std::cout << "Number of block : " << dimBlock->x << std::endl;
}
\INSERT_INITIALISATION_FUNCTION
......@@ -133,10 +133,19 @@ void evale_pop_chunk(CIndividual** population, int popSize){
void EASEAInit(int argc, char** argv){
\INSERT_INIT_FCT_CALL
//Initialiazing GPU
printf("Initializing GPU\n");
void *allocatedDeviceBuffer;
cudaMalloc(&allocatedDeviceBuffer,0);
cudaFree(allocatedDeviceBuffer);
free(allocatedDeviceBuffer);
}
void EASEAFinal(CPopulation* pop){
\INSERT_FINALIZATION_FCT_CALL;
cudaFree(d_offspringPopulationcuda);
cudaFree(d_fitnessescuda);
}
void AESAEBeginningGenerationFunction(CEvolutionaryAlgorithm* evolutionaryAlgorithm){
......@@ -325,6 +334,7 @@ void PopulationImpl::evaluateOffspringPopulation(){
cudaEvaluatePopulation<<< dimGridcuda, dimBlockcuda>>>(d_offspringPopulationcuda,actualPopulationSize,d_fitnessescuda,this->cuda->initOpts);
lastError = cudaGetLastError();
lastError = cudaThreadSynchronize();
//DEBUG_PRT("Kernel execution : %s",cudaGetErrorString(lastError));
lastError = cudaMemcpy(fitnesses,d_fitnessescuda,actualPopulationSize*sizeof(float),cudaMemcpyDeviceToHost);
......
......@@ -91,6 +91,11 @@ void* d_offspringPopulationTmpcuda;
float* d_fitnessescuda;
dim3 dimBlockcuda, dimGridcuda;
void* allocatedDeviceBuffer;
void* allocatedDeviceTmpBuffer;
float* deviceFitness;
dim3 dimBlock, dimGrid;
\INSERT_USER_DECLARATIONS
\ANALYSE_USER_CLASSES
......@@ -311,11 +316,11 @@ __global__ void cudaOptimisePopulation(void* d_population, void* d_populationTmp
void PopulationImpl::evaluateParentPopulation(){
float* fitnesses = new float[this->actualParentPopulationSize];
void* allocatedDeviceBuffer;
/*void* allocatedDeviceBuffer;
void* allocatedDeviceTmpBuffer;
float* deviceFitness;
float* deviceFitness;*/
cudaError_t lastError;
dim3 dimBlock, dimGrid;
// dim3 dimBlock, dimGrid;
size_t actualPopulationSize = this->actualParentPopulationSize;
// ICI il faut allouer la tailler max (entre parentPopualtionSize et offspringpopulationsize)
......@@ -332,9 +337,9 @@ void PopulationImpl::evaluateParentPopulation(){
lastError = cudaMemcpy(fitnesses,deviceFitness,actualPopulationSize*sizeof(float),cudaMemcpyDeviceToHost);
//DEBUG_PRT("Parent's fitnesses gathering : %s",cudaGetErrorString(lastError));
cudaFree(deviceFitness);
/*cudaFree(deviceFitness);
cudaFree(allocatedDeviceBuffer);
cudaFree(allocatedDeviceTmpBuffer);
cudaFree(allocatedDeviceTmpBuffer);*/
#ifdef COMPARE_HOST_DEVICE
......@@ -357,14 +362,14 @@ void PopulationImpl::evaluateParentPopulation(){
void PopulationImpl::optimiseParentPopulation(){
cudaError_t lastError;
void* allocatedDeviceBuffer;
/* void* allocatedDeviceBuffer;
void* allocatedDeviceTmpBuffer;
float* deviceFitness;
float* deviceFitness;*/
size_t actualPopulationSize = this->actualParentPopulationSize;
dim3 dimBlock, dimGrid;
// dim3 dimBlock, dimGrid;