• Main Page
  • Modules
  • Classes
  • Files
  • File List

D:/Projekt/ECF_trunk/examples/CEC/FunctionMinEvalOp.cpp

00001 #include <ecf/ECF.h>
00002 #include "FunctionMinEvalOp.h"
00003 #include <float.h>
00004 #include <fstream>
00005 
00006 
00007 // CEC externals
00008 extern void cec14_test_func(double *, double *, int, int, int);
00009 double *OShift,*M,*y,*z,*x_bound;
00010 int ini_flag=0,n_flag,func_flag,*SS;
00011 
00012 
00013 void FunctionMinEvalOp::registerParameters(StateP state)
00014 {
00015     state->getRegistry()->registerEntry("cec.function", (voidP) (new uint(1)), ECF::UINT, 
00016         "Sets the CEC test function number, 1-30 (default: 1)");
00017 }
00018 
00019 
00020 bool FunctionMinEvalOp::initialize(StateP state)
00021 {
00022     voidP sptr = state->getRegistry()->getEntry("cec.function"); // get parameter value
00023     iFunction_ = *((uint*) sptr.get()); // convert from voidP to user defined type
00024 
00025     // check function index (CEC does not abort on invalid id)
00026     if(iFunction_ < 1 || iFunction_ > 30) {
00027         ECF_LOG_ERROR(state, "CEC EvalOp: There are only 30 test functions in this test suite!");
00028         throw("");
00029     }
00030 
00031     // check for CEC input files
00032     std::string fileName("input_data/M_" + uint2str(iFunction_) + "_D2.txt");
00033     ifstream m_file(fileName.c_str());
00034     if (!m_file) {
00035         std::string msg("\nError: the CEC input data folder must be in path to run the experiments.\n");
00036         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";
00037         msg += "Since the data size is 35MB, it is NOT included in the ECF package.";
00038         ECF_LOG_ERROR(state, msg);
00039         throw("");
00040     }
00041 
00042     return true;
00043 }
00044 
00045 
00046 FitnessP FunctionMinEvalOp::evaluate(IndividualP individual)
00047 {
00048     // evaluation creates a new fitness object using a smart pointer
00049     // in our case, we try to minimize the function value, so we use FitnessMin fitness (for minimization problems)
00050     FitnessP fitness (new FitnessMin);
00051 
00052     // we define FloatingPoint as the only genotype (in the configuration file)
00053     FloatingPoint::FloatingPoint* gen = (FloatingPoint::FloatingPoint*) individual->getGenotype().get();
00054     // (you can also use boost smart pointers:)
00055     //FloatingPointP gen = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (individual->getGenotype());
00056 
00057     // alternative encoding: Binary Genotype
00058     //Binary::Binary* gen = (Binary::Binary*) individual->getGenotype().get();
00059     //BinaryP gen = boost::dynamic_pointer_cast<Binary::Binary> (individual->getGenotype());
00060 
00061     // we implement the fitness function 'as is', without any translation
00062     // the number of variables is read from the genotype itself (size of 'realValue' vactor)
00063     double realTemp = 0, value = 0;
00064 
00065     int n = (int) gen->realValue.size();
00066 
00067     // call external CEC 2014 evaluator
00068     cec14_test_func(&gen->realValue[0], &value, n, 1, iFunction_);
00069 
00070     fitness->setValue(value);
00071     return fitness;
00072 }

Generated on Tue Nov 4 2014 13:04:31 for ECF by  doxygen 1.7.1