ECF 1.5
FunctionMinEvalOp.cpp
1#include <ECF/ECF.h>
2#include "FunctionMinEvalOp.h"
3
4// COCO
5#include "./bbob/bbobStructures.h" /* Include all declarations for BBOB calls */
6#include <string.h>
7extern int enableOutput;
8
9
11{
12 state->getRegistry()->registerEntry("coco.function", (voidP) (new uint(1)), ECF::UINT,
13 "sets the COCO objective function (default: 1)");
14 state->getRegistry()->registerEntry("coco.instance", (voidP) (new uint(1)), ECF::UINT,
15 "set instance no. for COCO function (default: 1)");
16 state->getRegistry()->registerEntry("coco.enableoutput", (voidP) (new uint(0)), ECF::UINT,
17 "enable COCO folder output (default: 0/no)");
18 state->getRegistry()->registerEntry("coco.folder", (voidP) (new std::string("")), ECF::STRING,
19 "sets the COCO output folder (default: none)");
20 state->getRegistry()->registerEntry("coco.algorithm", (voidP) (new std::string("")), ECF::STRING,
21 "set algorithm name for post processing (default: none)");
22 state->getRegistry()->registerEntry("coco.comments", (voidP) (new std::string("")), ECF::STRING,
23 "set algorithm description for post processing (default: none)");
24}
25
26
27bool FunctionMinEvalOp::initialize(StateP state)
28{
30 return true;
31
32 // if repeated runs: finalize first
34 fgeneric_finalize();
35 }
36
37 voidP sptr = state->getRegistry()->getEntry("coco.function"); // get parameter value
38 iFunction_ = *((uint*) sptr.get()); // convert from voidP to user defined type
39
40 sptr = state->getRegistry()->getEntry("coco.instance"); // get parameter value
41 cocoInstance_ = *((uint*) sptr.get()); // convert from voidP to user defined type
42
43 // read COCO output folder name
44 if(state->getRegistry()->isModified("coco.folder")) {
45 sptr = state->getRegistry()->getEntry("coco.folder");
46 cocoFolder_ = *((std::string*) sptr.get());
47 }
48
49 // should COCO output be enabled (if not previously enabled manually)
50 if(!enableOutput) {
51 sptr = state->getRegistry()->getEntry("coco.enableoutput");
52 enableOutput = *((uint*) sptr.get());
53 }
54
55 //
56 // read genotype dimension from the registry; two options:
57 //
58 // uncomment the following line when using FloatingPoint genotype:
59 sptr = state->getRegistry()->getEntry("FloatingPoint.dimension");
60 // uncomment the following line when using Binary genotype:
61 //sptr = state->getRegistry()->getEntry("Binary.dimension");
62
63 uint dim = *((uint*) sptr.get());
64
65
66 //
67 // COCO initialization (with excerpts from exampleexperiment.c)
68 //
69
70 ParamStruct params = fgeneric_getDefaultPARAMS();
71 strcpy(params.dataPath, cocoFolder_.c_str()); /* different folder for each experiment! */
72
73 /* set DIM, funcId, instanceId to initialize BBOB fgeneric */
74 params.DIM = dim;
75 params.funcId = iFunction_;
76 // instanceId is 1 by default
77 params.instanceId = cocoInstance_;
78
79 // algorithm name
80 sptr = state->getRegistry()->getEntry("coco.algorithm");
81 cocoAlgName_ = *((std::string*) sptr.get());
82 strcpy(params.algName, cocoAlgName_.c_str());
83
84 // algorithm description
85 sptr = state->getRegistry()->getEntry("coco.comments");
86 cocoAlgComments_ = *((std::string*) sptr.get());
87 strcpy(params.comments, cocoAlgComments_.c_str());
88
89 /* call the BBOB initialization */
90 fgeneric_initialize(params);
91 // get function optimal value
92 coco_optimum_ = fgeneric_ftarget() - params.precision;
93 isCocoInitialized_ = true;
94
95 return true;
96}
97
98
99FitnessP FunctionMinEvalOp::evaluate(IndividualP individual)
100{
101 // evaluation creates a new fitness object using a smart pointer
102 // in our case, we try to minimize the function value, so we use FitnessMin fitness (for minimization problems)
103 FitnessP fitness (new FitnessMin);
104
105 // we define FloatingPoint as the only genotype (in the configuration file)
106
107 // uncomment the following line when using FloatingPoint genotype:
108 FloatingPoint::FloatingPoint* gen = (FloatingPoint::FloatingPoint*) individual->getGenotype().get();
109 // (you can also use boost smart pointers:)
110 //FloatingPointP gen = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (individual->getGenotype());
111
112 // alternative encoding: Binary Genotype
113
114 // uncomment the following line when using Binary genotype:
115 //Binary::Binary* gen = (Binary::Binary*) individual->getGenotype().get();
116 //BinaryP gen = boost::dynamic_pointer_cast<Binary::Binary> (individual->getGenotype());
117
118 double value = 0;
119
120
121 //
122 // COCO
123 //
124 // evaluate
125 value = fgeneric_evaluate(&(gen->realValue[0]));
126 // scale to zero minimum
127 value -= coco_optimum_;
128
129 fitness->setValue(value);
130 return fitness;
131}
Fitness for minimization problems.
Definition: FitnessMin.h:12
FloatingPoint class - implements genotype as a vector of floating point values.
Definition: FloatingPoint.h:39
FitnessP evaluate(IndividualP individual)
Evaluate a single individual. Method must create and return a Fitness object.
std::string cocoAlgComments_
COCO algorithm description.
double coco_optimum_
COCO function optimal value.
uint cocoInstance_
COCO function instance.
std::string cocoAlgName_
COCO algorithm name.
bool experimentMode_
enable COCO experiment
uint iFunction_
function index
void registerParameters(StateP)
Register evaluator parameters. Called before EvaluateOp::initialize method.
std::string cocoFolder_
COCO output folder.
bool initialize(StateP)
Initialize the evaluator. Called before first evaluation occurs.
bool isCocoInitialized_
is COCO initialized
std::vector< double > realValue
vector of floating point values