• 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 
00035     // uncomment the following line when using FloatingPoint genotype:
00036     sptr = state->getRegistry()->getEntry("FloatingPoint.dimension");
00037 
00038     // uncomment the following line when using Binary genotype:
00039     //sptr = state->getRegistry()->getEntry("Binary.dimension");
00040 
00041     uint dim = *((uint*) sptr.get());
00042 
00043 
00044     //
00045     // COCO initialization (with excerpts from exampleexperimet.c)
00046     //
00047 
00048     // if repeated runs: finalize first
00049     if(isCocoInitialized_) {
00050         fgeneric_finalize();
00051     }
00052 
00053     ParamStruct params = fgeneric_getDefaultPARAMS();
00054     strcpy(params.dataPath, cocoFolder_.c_str());  /* different folder for each experiment! */
00055 
00056     /* set DIM, funcId, instanceId to initialize BBOB fgeneric */
00057     params.DIM = dim;
00058     params.funcId = iFunction_;
00059     // instanceId is unchanged by default
00060     params.instanceId = 1;
00061 
00062     /* call the BBOB initialization */
00063     fgeneric_initialize(params);
00064     // get function optimal value
00065     coco_optimum_ = fgeneric_ftarget() - params.precision;
00066     isCocoInitialized_ = true;
00067 
00068     return true;
00069 }
00070 
00071 
00072 FitnessP FunctionMinEvalOp::evaluate(IndividualP individual)
00073 {
00074     // evaluation creates a new fitness object using a smart pointer
00075     // in our case, we try to minimize the function value, so we use FitnessMin fitness (for minimization problems)
00076     FitnessP fitness (new FitnessMin);
00077 
00078     // we define FloatingPoint as the only genotype (in the configuration file)
00079 
00080     // uncomment the following line when using FloatingPoint genotype:
00081     FloatingPoint::FloatingPoint* gen = (FloatingPoint::FloatingPoint*) individual->getGenotype().get();
00082     // (you can also use boost smart pointers:)
00083     //FloatingPointP gen = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (individual->getGenotype());
00084 
00085     // alternative encoding: Binary Genotype
00086 
00087     // uncomment the following line when using Binary genotype:
00088     //Binary::Binary* gen = (Binary::Binary*) individual->getGenotype().get();
00089     //BinaryP gen = boost::dynamic_pointer_cast<Binary::Binary> (individual->getGenotype());
00090 
00091     double value = 0;
00092 
00093 
00094     //
00095     // COCO
00096     //
00097     // evaluate
00098     value = fgeneric_evaluate(&(gen->realValue[0]));
00099     // scale to zero minimum
00100     value -= coco_optimum_;
00101 
00102     fitness->setValue(value);
00103     return fitness;
00104 }

Generated on Tue Dec 10 2013 11:03:06 for ECF by  doxygen 1.7.1