Commit b99dcf89 authored by wcancino's avatar wcancino Committed by Waldo Cancino
Browse files

Add BBOB 2013 benchmarks, first version, it should be polished to work

in a EASEA way.
parent 3991236c
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <limits.h>
void MY_OPTIMIZER(double(*fitnessfunction)(double*), unsigned int dim, double ftarget, double maxfunevals)
{
double * x = (double *)malloc(sizeof(double) * dim);
double f;
double iter;
unsigned int j;
if (maxfunevals > 1000000000. * dim)
maxfunevals = 1000000000. * dim;
for (iter = 0.; iter < maxfunevals; iter++)
{
/* Generate individual */
for (j = 0; j < dim; j++)
x[j] = 10. * ((double)rand() / RAND_MAX) - 5.;
/* evaluate x on the objective function */
f = fitnessfunction(x);
if (f < ftarget)
break;
}
free(x);
}
bbob2013.mak
\ No newline at end of file
#This is an example Makefile that will create the executables exampletiming and
#exampleexperiment. This Makefile can either be completed with rules to create
#the executables for your own experiment or the rules here can be added to your
#own Makefile.
#Define your compiler/linker if needed.
#CC=
#Strict options
LDFLAGS=
CFLAGS=-Wextra -Wall -ansi -pedantic
#Lenient: add/remove any compilation or linking flags that you feel are necessary or not.
# LDFLAGS=
# CFLAGS=
DIROK=dirOK.o
#Linux/Unix users can try to comment the above and uncomment the following line instead.
# DIROK=dirOK-linux.o
#Lists all executable, used by the "all" and "clean" rules.
EXECS=exampletiming exampleexperiment
all: $(EXECS)
#Add here any rules needed for your own experiments
#Example experiments:
exampletiming: MY_OPTIMIZER.o benchmarksdeclare.o $(DIROK) benchmarkshelper.o benchmarks.o benchmarksnoisy.o fgeneric.o exampletiming.o
$(CC) $(LDFLAGS) -o $@ $^ -lm
exampleexperiment: MY_OPTIMIZER.o benchmarksdeclare.o $(DIROK) benchmarkshelper.o benchmarks.o benchmarksnoisy.o fgeneric.o exampleexperiment.o
$(CC) $(LDFLAGS) -o $@ $^ -lm
#Your own experiment should have bbobStructures.h as a dependency and so should be added here.
exampletiming.o exampleexperiment.o benchmarkshelper.o fgeneric.o benchmarks.o benchmarksnoisy.o benchmarksdeclare.o : bbobStructures.h
#Benchmarks object files are optimized so they are exceptions to the implicit *.o generation rule.
benchmarks.o : benchmarks.c
$(CC) -c $(CFLAGS) -O2 $<
benchmarksnoisy.o : benchmarksnoisy.c
$(CC) -c $(CFLAGS) -O2 $<
#Clean
.PHONY: clean
#Comment the command starting with rm and uncomment the comment starting with del to make the makefile work under Windows.
clean:
rm -f *.o $(EXECS)
# del *.o exampleexperiment.exe exampletiming.exe
/**
This is program entry for STD template for bbob2013
*/
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include "COptionParser.h"
#include "CRandomGenerator.h"
#include "CEvolutionaryAlgorithm.h"
#include "global.h"
#include "bbob2013Individual.hpp"
using namespace std;
/** Global variables for the whole algorithm */
CIndividual** pPopulation = NULL;
CIndividual* bBest = NULL;
float* pEZ_MUT_PROB = NULL;
float* pEZ_XOVER_PROB = NULL;
unsigned *EZ_NB_GEN;
unsigned *EZ_current_generation;
CEvolutionaryAlgorithm* EA;
int main(int argc, char** argv){
parseArguments("bbob2013.prm",argc,argv);
ParametersImpl p;
p.setDefaultParameters(argc,argv);
CEvolutionaryAlgorithm* ea = p.newEvolutionaryAlgorithm();
EA = ea;
bbob2013Init(argc,argv);
CPopulation* pop = ea->getPopulation();
ea->runEvolutionaryLoop();
bbob2013Final(pop);
delete pop;
return 0;
}
/*_________________________________________________________
Test functions
log normal adaptive mutation
Selection operator: Tournament
__________________________________________________________*/
\User declarations :
#define SIZE 100
#define X_MIN -5.
#define X_MAX 5.
#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;
int DIM = 2;
int trialid = 1;
int funcId = 2; //passed by parameter
int instanceId = 1;
double * peaks;
double * Xopt; /*Initialized in benchmarkhelper.c*/
double Fopt;
unsigned int isInitDone=0;
\end
\User functions:
#include <math.h>
extern "C" {
#include "bbobStructures.h"
}
__device__ __host__ inline double bbob_eval( double *x)
{
return fgeneric_evaluate(x);
}
float gauss()
/* Generates a normally distributed random value with variance 1 and 0 mean.
Algorithm based on "gasdev" from Numerical recipes' pg. 203. */
{
static 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
\User CUDA:
\end
\Before everything else function:
fgeneric_initialize();
\end
\After everything else function:
//cout << "After everything else function called" << endl;
fgeneric_finalize();
\end
\At the beginning of each generation function:
\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 {
double *x;
double *sigma; // 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
Genome.x = (double *) malloc( DIM * sizeof(double) );
Genome.sigma = (double *) malloc( DIM * sizeof(double) );
for(int i=0; i<DIM; 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<DIM; 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)DIM);
for (int i=0; i<DIM; i++)
if (tossCoin(pMutPerGene)){
NbMut++;
Genome.sigma[i] = Genome.sigma[i] * exp(SIGMA*pond*(float)gauss());
Genome.sigma[i] = MIN(0.5,Genome.sigma[i]);
Genome.sigma[i] = MAX(0.,Genome.sigma[i]);
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
double Score= 0.0;
Score= bbob_eval(Genome.x);
//Score= rosenbrock(Genome.x);
return Score;
\end
\User Makefile options:
CXXFLAGS+=-I/usr/local/cuda/common/inc/ -I/usr/local/cuda/include/ -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include
LDFLAGS+=-lgfal_pthr -llcg_util
\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 : 2048 //POP_SIZE
Offspring size : 2048 // 40%
Mutation probability : 1 // MUT_PROB
Crossover probability : 1 // XOVER_PROB
Evaluator goal : minimise // Maximise
Selection operator: Tournament 2.0
Surviving parents: 100%//perceor 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
Print stats: true //Default: 1
Generate csv stats file:false
Generate gnuplot script:false
Generate R script:false
Plot stats:true //Default: 0
Remote island model: false
IP file: ip.txt //File containing all the remote island's IP
Experiment ID: weierstrass
Grid Working Path: lfn:/grid/vo.complex-systems.eu/easea/experiments/
Server port : 2929
Migration probability: 0.33
Save population: false
Start from file:false
\end
#include <fstream>
#include <time.h>
#include <string>
#include <sstream>
#include "CRandomGenerator.h"
#include "CPopulation.h"
#include "COptionParser.h"
#include "CStoppingCriterion.h"
#include "CEvolutionaryAlgorithm.h"
#include "global.h"
#include "CIndividual.h"
using namespace std;
#include "bbob2013Individual.hpp"
bool INSTEAD_EVAL_STEP = false;
CRandomGenerator* globalRandomGenerator;
extern CEvolutionaryAlgorithm* EA;
#define STD_TPL
// User declarations
#line 8 "bbob2013.ez"
#define SIZE 100
#define X_MIN -5.
#define X_MAX 5.
#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;
int DIM = 2;
int trialid = 3;
int funcId = 1; //passed by parameter
int instanceId = 3;
double * peaks;
double * Xopt; /*Initialized in benchmarkhelper.c*/
double Fopt;
unsigned int isInitDone=0;
// User classes
// User functions
#line 33 "bbob2013.ez"
#include <math.h>
extern "C" {
#include "bbobStructures.h"
}
inline double bbob_eval( double *x)
{
return fgeneric_evaluate(x);
}
float gauss()
/* Generates a normally distributed globalRandomGenerator->random value with variance 1 and 0 mean.
Algorithm based on "gasdev" from Numerical recipes' pg. 203. */
{
static 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)globalRandomGenerator->random(0.,1.) * 2.0 - 1.0;
v2 = (float)globalRandomGenerator->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);
}
}
// Initialisation function
void EASEAInitFunction(int argc, char *argv[]){
#line 76 "bbob2013.ez"
fgeneric_initialize();
}
// Finalization function
void EASEAFinalization(CPopulation* population){
#line 80 "bbob2013.ez"
//cout << "After everything else function called" << endl;
//fgeneric_finalize();
}
void evale_pop_chunk(CIndividual** population, int popSize){
// No Instead evaluation step function.
}
void bbob2013Init(int argc, char** argv){
EASEAInitFunction(argc, argv);
}
void bbob2013Final(CPopulation* pop){
EASEAFinalization(pop);
;
}
void EASEABeginningGenerationFunction(CEvolutionaryAlgorithm* evolutionaryAlgorithm){
#line 192 "bbob2013.ez"
{
#line 85 "bbob2013.ez"
}
}
void EASEAEndGenerationFunction(CEvolutionaryAlgorithm* evolutionaryAlgorithm){
{
//cout << "At the end of each generation function called" << endl;
}
}
void EASEAGenerationFunctionBeforeReplacement(CEvolutionaryAlgorithm* evolutionaryAlgorithm){
{
//cout << "At each generation before replacement function called" << endl;
}
}
IndividualImpl::IndividualImpl() : CIndividual() {
sigma=NULL;
x=NULL;
// Genome Initialiser
#line 111 "bbob2013.ez"
// "initializer" is also accepted
(*this).x = new double[DIM];
(*this).sigma = new double[DIM];
for(int i=0; i<DIM; i++ ) {
(*this).x[i] = (double)globalRandomGenerator->random(X_MIN,X_MAX);
(*this).sigma[i]=(double)globalRandomGenerator->random(0.,0.5);
}
valid = false;
isImmigrant = false;
}
CIndividual* IndividualImpl::clone(){
return new IndividualImpl(*this);
}
IndividualImpl::~IndividualImpl(){
// Destructing pointers
if (sigma) delete []sigma;
sigma=NULL;
if (x) delete []x;
x=NULL;
}
float IndividualImpl::evaluate(){
if(valid)
return fitness;
else{
valid = true;
#line 145 "bbob2013.ez"
// Returns the score
double Score= 0.0;
Score= bbob_eval((*this).x);
//Score= rosenbrock(Genome.x);
return fitness = Score;
}
}
void IndividualImpl::boundChecking(){
// No Bound checking function.
}
string IndividualImpl::serialize(){
ostringstream EASEA_Line(ios_base::app);
// Memberwise serialization
EASEA_Line << this->fitness;
return EASEA_Line.str();
}
void IndividualImpl::deserialize(string Line){
istringstream EASEA_Line(Line);
string line;
// Memberwise deserialization
EASEA_Line >> this->fitness;
this->valid=true;
this->isImmigrant = false;
}
IndividualImpl::IndividualImpl(const IndividualImpl& genome){
// ********************
// Problem specific part
// Memberwise copy
// sigma=(genome.sigma ? new double(*(genome.sigma)) : NULL);
// x=(genome.x ? new double(*(genome.x)) : NULL);
sigma = new double[DIM];
x = new double[DIM];
for(int i=0; i<DIM; i++)
{
x[i] = genome.x[i];
sigma[i] = genome.sigma[i];
}
// ********************
// Generic part
this->valid = genome.valid;
this->fitness = genome.fitness;
this->isImmigrant = false;
}
CIndividual* IndividualImpl::crossover(CIndividual** ps){
// ********************
// Generic part
IndividualImpl** tmp = (IndividualImpl**)ps;
IndividualImpl parent1(*this);
IndividualImpl parent2(*tmp[0]);
IndividualImpl child(*this);
//DEBUG_PRT("Xover");
/* cout << "p1 : " << parent1 << endl; */
/* cout << "p2 : " << parent2 << endl; */
// ********************
// Problem specific part
#line 120 "bbob2013.ez"
for (int i=0; i<DIM; i++)
{
float alpha = (float)globalRandomGenerator->random(0.,1.); // barycentric crossover
child.x[i] = alpha*parent1.x[i] + (1.-alpha)*parent2.x[i];
}
child.valid = false;
/* cout << "child : " << child << endl; */
return new IndividualImpl(child);
}
void IndividualImpl::printOn(std::ostream& os) const{
/* for( size_t i=0 ; i<SIZE ; i++){ */
/* // cout << Genome.x[i] << ":" << Genome.sigma[i] << "|"; */
/* printf("0.00:0.00|",Genome.x[i],Genome.sigma[i]); */
/* } */
}
std::ostream& operator << (std::ostream& O, const IndividualImpl& B)
{
// ********************
// Problem specific part