ECF 1.5
FunctionMinEvalOp.cpp
1#include <ECF/ECF.h>
2#include "FunctionMinEvalOp.h"
3
4
6{
7 state->getRegistry()->registerEntry("function", (voidP) (new uint(1)), ECF::UINT);
8}
9
10
11bool FunctionMinEvalOp::initialize(StateP state)
12{
13 voidP sptr = state->getRegistry()->getEntry("function"); // get parameter value
14 iFunction_ = *((uint*) sptr.get()); // convert from voidP to user defined type
15
16 return true;
17}
18
19
20FitnessP FunctionMinEvalOp::evaluate(IndividualP individual)
21{
22 // evaluation creates a new fitness object using a smart pointer
23 // in our case, we try to minimize the function value, so we use FitnessMin fitness (for minimization problems)
24 FitnessP fitness (new FitnessMin);
25
26 // we define FloatingPoint as the only genotype (in the configuration file)
27 FloatingPoint::FloatingPoint* gen = (FloatingPoint::FloatingPoint*) individual->getGenotype().get();
28 // (you can also use boost smart pointers:)
29 //FloatingPointP gen = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (individual->getGenotype());
30
31 // alternative encoding: Binary Genotype
32 //Binary::Binary* gen = (Binary::Binary*) individual->getGenotype().get();
33 //BinaryP gen = boost::dynamic_pointer_cast<Binary::Binary> (individual->getGenotype());
34
35 // we implement the fitness function 'as is', without any translation
36 // the number of variables is read from the genotype itself (size of 'realValue' vactor)
37 double realTemp = 0, value = 0;
38
39 switch(iFunction_) {
40 case 1:
41 for (uint i = 0; i < gen->realValue.size(); i++){
42 realTemp = pow((gen->realValue[i] - (i + 1)), 2.);
43 value += realTemp;
44 }
45 break;
46
47 // Schaffer's F6 function
48 case 2: {
49 double z = 0;
50 for(uint i = 0; i < gen->realValue.size(); ++i) {
51 z += (gen->realValue[i] * gen->realValue[i]);
52 }
53 value = 0.5 - (pow(sin(sqrt(z)), 2) - 0.5) / pow(1 + 0.001 * z, 2);
54 value = -1 * value + 1;
55 } break;
56
57 // Griewangk
58 case 3: {
59 double valueSum = 0, valueProduct = 1, realSum = 0, realProduct = 0;
60 for (uint i = 0; i < gen->realValue.size(); i++){
61 realSum = pow(gen->realValue[i], 2.)/4000;
62 valueSum += realSum;
63 realProduct = cos(gen->realValue[i] / sqrt((double)(i+1)));
64 valueProduct *= realProduct;
65 }
66 value = valueSum - valueProduct + 1;
67 } break;
68
69 // Ackley
70 case 4: {
71 double realSum = 0, valueSum = 0, realCos = 0, valueCos = 0, pi = 3.141592;
72 for (uint i = 0; i < gen->realValue.size(); i++){
73 realSum = pow(gen->realValue[i], 2.);
74 valueSum += realSum;
75 realCos = cos (2 * pi * gen->realValue[i]);
76 valueCos += realCos;
77 }
78 value = -20 * exp(-0.2*sqrt(valueSum / gen->realValue.size())) - exp(valueCos / gen->realValue.size()) + 20 + exp(1.);
79 } break;
80
81 // Rastrigin
82 case 5: {
83 double realTemp = 0, pi = 3.141592;
84 for (uint i = 0; i < gen->realValue.size(); i++) {
85 realTemp = pow(gen->realValue[i], 2.) - 10 * cos(2 * pi * gen->realValue[i]);
86 value += realTemp;
87 }
88 value = value + 10 * gen->realValue.size();
89 } break;
90
91 // Rosenbrock
92 case 6: {
93 double realTemp = 0;
94 for (uint i = 0; i < gen->realValue.size() - 1; i++){
95 realTemp = 100 * pow(gen->realValue[i + 1] - pow(gen->realValue[i], 2.), 2.) + pow(1 - gen->realValue[i], 2.);
96 value += realTemp;
97 }
98 } break;
99
100 // Schaffer's F7 function
101 case 7: {
102 double z = 0;
103 for(uint i = 0; i < gen->realValue.size(); ++i) {
104 z += (gen->realValue[i] * gen->realValue[i]);
105 }
106 value = pow(z, 0.25) * (1.0 + pow( sin( 50 * pow(z, 0.1)), 2));
107 } break;
108
109 case 10: {
110 double x1 = gen->realValue[0];
111 double x2 = gen->realValue[1];
112 value = pow(x1, 4)/4 - x1*x1 + 2*x1 + (x2 - 1)*(x2 - 1);
113 } break;
114
116 case 20: {
117 double x10 = gen->realValue[0];
118 double x11 = gen->realValue[1];
119 double x20 = gen->realValue[2];
120 double x21 = gen->realValue[3];
121 double x30 = gen->realValue[4];
122 double x31 = gen->realValue[5];
123 double x32 = gen->realValue[6];
124 double error = 0;
125 for(uint i = 1; i <= 10; i++) {
126 double net1, net2;
127 //net1 = x10 + i * x11;
128 net1 = 1. / (1 + exp(-(x10 + i * x11)));
129
130 //net2 = x20 + i * x21;
131 net2 = 1. / (1 + exp(-(x20 + i * x21)));
132
133 net1 = x30 + net1 * x31 + net2 * x32;
134
135 //error += pow(fabs(net1 - (200 + i/2.)), 2);
136 error += pow(fabs(net1 - (i*i)), 2);
137 }
138 value = sqrt(error);
139 } break;
140
141
142 default:
143 throw("FunctionMinEvalOp: invalid function index in configuration!");
144 }
145
146 fitness->setValue(value);
147 return fitness;
148}
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.
uint iFunction_
function index
void registerParameters(StateP)
Register evaluator parameters. Called before EvaluateOp::initialize method.
bool initialize(StateP)
Initialize the evaluator. Called before first evaluation occurs.
std::vector< double > realValue
vector of floating point values