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

D:/Projekt/ECF_trunk/examples/COCO/FunctionMinEvalOp_coco.cpp

00001 #include <ecf/ECF.h>
00002 #include "FunctionMinEvalOp.h"
00003 
00004 // COCO
00005 #include "./bbob/bbobStructures.h" /* Include all declarations for BBOB calls */
00006 #include <string.h>
00007 extern int enableOutput;
00008 
00009 
00010 void FunctionMinEvalOp::registerParameters(StateP state)
00011 {
00012     state->getRegistry()->registerEntry("coco.function", (voidP) (new uint(1)), ECF::UINT);
00013     state->getRegistry()->registerEntry("coco.enableoutput", (voidP) (new uint(0)), ECF::UINT);
00014     state->getRegistry()->registerEntry("coco.folder", (voidP) (new std::string("")), ECF::STRING);
00015 }
00016 
00017 
00018 bool FunctionMinEvalOp::initialize(StateP state)
00019 {
00020     voidP sptr = state->getRegistry()->getEntry("coco.function"); // get parameter value
00021     iFunction_ = *((uint*) sptr.get()); // convert from voidP to user defined type
00022 
00023     // read COCO output folder name
00024     if(state->getRegistry()->isModified("coco.folder")) {
00025         sptr = state->getRegistry()->getEntry("coco.folder");
00026         cocoFolder_ = *((std::string*) sptr.get());
00027     }
00028 
00029     // should COCO output be enabled
00030     sptr = state->getRegistry()->getEntry("coco.enableoutput");
00031     enableOutput = *((uint*) sptr.get());
00032 
00033     // read genotype dimension from the registry
00034     sptr = state->getRegistry()->getEntry("FloatingPoint.dimension");
00035     uint dim = *((uint*) sptr.get());
00036 
00037 
00038     //
00039     // COCO initialization (with excerpts from exampleexperimet.c)
00040     //
00041 
00042     // if repeated runs: finalize first
00043     if(isCocoInitialized_) {
00044         fgeneric_finalize();
00045     }
00046 
00047     ParamStruct params = fgeneric_getDefaultPARAMS();
00048     strcpy(params.dataPath, cocoFolder_.c_str());  /* different folder for each experiment! */
00049 
00050     /* set DIM, funcId, instanceId to initialize BBOB fgeneric */
00051     params.DIM = dim;
00052     params.funcId = iFunction_;
00053     // instanceId is unchanged by default
00054     params.instanceId = 1;
00055 
00056     /* call the BBOB initialization */
00057     fgeneric_initialize(params);
00058     // get function optimal value
00059     coco_optimum_ = fgeneric_ftarget() - params.precision;
00060     isCocoInitialized_ = true;
00061 
00062     return true;
00063 }
00064 
00065 
00066 FitnessP FunctionMinEvalOp::evaluate(IndividualP individual)
00067 {
00068     // evaluation creates a new fitness object using a smart pointer
00069     // in our case, we try to minimize the function value, so we use FitnessMin fitness (for minimization problems)
00070     FitnessP fitness (new FitnessMin);
00071 
00072     // we define FloatingPoint as the only genotype (in the configuration file)
00073     FloatingPoint::FloatingPoint* gen = (FloatingPoint::FloatingPoint*) individual->getGenotype().get();
00074     // (you can also use boost smart pointers:)
00075     //FloatingPointP gen = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (individual->getGenotype());
00076 
00077     // alternative encoding: Binary Genotype
00078     //Binary::Binary* gen = (Binary::Binary*) individual->getGenotype().get();
00079     //BinaryP gen = boost::dynamic_pointer_cast<Binary::Binary> (individual->getGenotype());
00080 
00081     double value = 0;
00082 
00083 
00084     //
00085     // COCO
00086     //
00087     // evaluate
00088     value = fgeneric_evaluate(&(gen->realValue[0]));
00089     // scale to zero minimum
00090     value -= coco_optimum_;
00091 
00092     fitness->setValue(value);
00093     return fitness;
00094 }

Generated on Fri Jul 5 2013 09:34:24 for ECF by  doxygen 1.7.1