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

D:/Projekt/ECF_trunk/examples/COCO/FunctionMinEvalOp.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         "sets the COCO objective function (default: 1)");
00014     state->getRegistry()->registerEntry("coco.instance", (voidP) (new uint(1)), ECF::UINT,
00015         "set instance no. for COCO function (default: 1)");
00016     state->getRegistry()->registerEntry("coco.enableoutput", (voidP) (new uint(0)), ECF::UINT,
00017         "enable COCO folder output (default: 0/no)");
00018     state->getRegistry()->registerEntry("coco.folder", (voidP) (new std::string("")), ECF::STRING,
00019         "sets the COCO output folder (default: none)");
00020     state->getRegistry()->registerEntry("coco.algorithm", (voidP) (new std::string("")), ECF::STRING,
00021         "set algorithm name for post processing (default: none)");
00022     state->getRegistry()->registerEntry("coco.comments", (voidP) (new std::string("")), ECF::STRING,
00023         "set algorithm description for post processing (default: none)");
00024 }
00025 
00026 
00027 bool FunctionMinEvalOp::initialize(StateP state)
00028 {
00029     if(experimentMode_)
00030         return true;
00031 
00032     // if repeated runs: finalize first
00033     if(isCocoInitialized_) {
00034         fgeneric_finalize();
00035     }
00036 
00037     voidP sptr = state->getRegistry()->getEntry("coco.function"); // get parameter value
00038     iFunction_ = *((uint*) sptr.get()); // convert from voidP to user defined type
00039 
00040     sptr = state->getRegistry()->getEntry("coco.instance"); // get parameter value
00041     cocoInstance_ = *((uint*) sptr.get()); // convert from voidP to user defined type
00042 
00043     // read COCO output folder name
00044     if(state->getRegistry()->isModified("coco.folder")) {
00045         sptr = state->getRegistry()->getEntry("coco.folder");
00046         cocoFolder_ = *((std::string*) sptr.get());
00047     }
00048 
00049     // should COCO output be enabled (if not previously enabled manually)
00050     if(!enableOutput) {
00051         sptr = state->getRegistry()->getEntry("coco.enableoutput");
00052         enableOutput = *((uint*) sptr.get());
00053     }
00054 
00055     //
00056     // read genotype dimension from the registry; two options:
00057     //
00058     // uncomment the following line when using FloatingPoint genotype:
00059     sptr = state->getRegistry()->getEntry("FloatingPoint.dimension");
00060     // uncomment the following line when using Binary genotype:
00061     //sptr = state->getRegistry()->getEntry("Binary.dimension");
00062 
00063     uint dim = *((uint*) sptr.get());
00064 
00065 
00066     //
00067     // COCO initialization (with excerpts from exampleexperiment.c)
00068     //
00069 
00070     ParamStruct params = fgeneric_getDefaultPARAMS();
00071     strcpy(params.dataPath, cocoFolder_.c_str());  /* different folder for each experiment! */
00072 
00073     /* set DIM, funcId, instanceId to initialize BBOB fgeneric */
00074     params.DIM = dim;
00075     params.funcId = iFunction_;
00076     // instanceId is 1 by default
00077     params.instanceId = cocoInstance_;
00078 
00079     // algorithm name
00080     sptr = state->getRegistry()->getEntry("coco.algorithm");
00081     cocoAlgName_ = *((std::string*) sptr.get());
00082     strcpy(params.algName, cocoAlgName_.c_str());
00083 
00084     // algorithm description
00085     sptr = state->getRegistry()->getEntry("coco.comments");
00086     cocoAlgComments_ = *((std::string*) sptr.get());
00087     strcpy(params.comments, cocoAlgComments_.c_str());
00088 
00089     /* call the BBOB initialization */
00090     fgeneric_initialize(params);
00091     // get function optimal value
00092     coco_optimum_ = fgeneric_ftarget() - params.precision;
00093     isCocoInitialized_ = true;
00094 
00095     return true;
00096 }
00097 
00098 
00099 FitnessP FunctionMinEvalOp::evaluate(IndividualP individual)
00100 {
00101     // evaluation creates a new fitness object using a smart pointer
00102     // in our case, we try to minimize the function value, so we use FitnessMin fitness (for minimization problems)
00103     FitnessP fitness (new FitnessMin);
00104 
00105     // we define FloatingPoint as the only genotype (in the configuration file)
00106 
00107     // uncomment the following line when using FloatingPoint genotype:
00108     FloatingPoint::FloatingPoint* gen = (FloatingPoint::FloatingPoint*) individual->getGenotype().get();
00109     // (you can also use boost smart pointers:)
00110     //FloatingPointP gen = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (individual->getGenotype());
00111 
00112     // alternative encoding: Binary Genotype
00113 
00114     // uncomment the following line when using Binary genotype:
00115     //Binary::Binary* gen = (Binary::Binary*) individual->getGenotype().get();
00116     //BinaryP gen = boost::dynamic_pointer_cast<Binary::Binary> (individual->getGenotype());
00117 
00118     double value = 0;
00119 
00120 
00121     //
00122     // COCO
00123     //
00124     // evaluate
00125     value = fgeneric_evaluate(&(gen->realValue[0]));
00126     // scale to zero minimum
00127     value -= coco_optimum_;
00128 
00129     fitness->setValue(value);
00130     return fitness;
00131 }

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