ECF 1.5
FunctionMinEvalOp.cpp
1#include <ECF/ECF.h>
2#include "FunctionMinEvalOp.h"
3#include <float.h>
4#include <fstream>
5
6
7// CEC externals
8extern void cec14_test_func(double *, double *, int, int, int);
9double *OShift,*M,*y,*z,*x_bound;
10int ini_flag=0,n_flag,func_flag,*SS;
11
12
14{
15 state->getRegistry()->registerEntry("cec.function", (voidP) (new uint(1)), ECF::UINT,
16 "Sets the CEC test function number, 1-30 (default: 1)");
17}
18
19
21{
22 voidP sptr = state->getRegistry()->getEntry("cec.function"); // get parameter value
23 iFunction_ = *((uint*) sptr.get()); // convert from voidP to user defined type
24
25 // check function index (CEC does not abort on invalid id)
26 if(iFunction_ < 1 || iFunction_ > 30) {
27 ECF_LOG_ERROR(state, "CEC EvalOp: There are only 30 test functions in this test suite!");
28 throw("");
29 }
30
31 // check for CEC input files
32 std::string fileName("input_data/M_" + uint2str(iFunction_) + "_D2.txt");
33 ifstream m_file(fileName.c_str());
34 if (!m_file) {
35 std::string msg("\nError: the CEC input data folder must be in path to run the experiments.\n");
36 msg += "DOWNLOAD the required data and place them in \"input_data\" folder from http://web.mysites.ntu.edu.sg/epnsugan/PublicSite/Shared%20Documents/CEC-2014/cec14-c-code.zip.\n";
37 msg += "Since the data size is 35MB, it is NOT included in the ECF package.";
38 ECF_LOG_ERROR(state, msg);
39 throw("");
40 }
41
42 return true;
43}
44
45
46FitnessP FunctionMinEvalOp::evaluate(IndividualP individual)
47{
48 // evaluation creates a new fitness object using a smart pointer
49 // in our case, we try to minimize the function value, so we use FitnessMin fitness (for minimization problems)
50 FitnessP fitness (new FitnessMin);
51
52 // we define FloatingPoint as the only genotype (in the configuration file)
53 FloatingPoint::FloatingPoint* gen = (FloatingPoint::FloatingPoint*) individual->getGenotype().get();
54 // (you can also use boost smart pointers:)
55 //FloatingPointP gen = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (individual->getGenotype());
56
57 // alternative encoding: Binary Genotype
58 //Binary::Binary* gen = (Binary::Binary*) individual->getGenotype().get();
59 //BinaryP gen = boost::dynamic_pointer_cast<Binary::Binary> (individual->getGenotype());
60
61 // we implement the fitness function 'as is', without any translation
62 // the number of variables is read from the genotype itself (size of 'realValue' vactor)
63 double realTemp = 0, value = 0;
64
65 int n = (int) gen->realValue.size();
66
67 // call external CEC 2014 evaluator
68 cec14_test_func(&gen->realValue[0], &value, n, 1, iFunction_);
69
70 fitness->setValue(value);
71 return fitness;
72}
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