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

D:/Projekt/ECF_trunk/ECF/floatingpoint/FloatingPoint.cpp

00001 #include <cmath>
00002 #include "../ECF_base.h"
00003 #include "FloatingPoint.h"
00004 #include<sstream>
00005 
00006 namespace FloatingPoint
00007 {
00008 
00009 void FloatingPoint::registerParameters(StateP state)
00010 {
00011     registerParameter(state, "lbound", (voidP) new double(-10), ECF::DOUBLE,
00012         "lower bound for each variable (mandatory)");
00013     registerParameter(state, "ubound", (voidP) new double(10), ECF::DOUBLE,
00014         "upper bound for each variable (mandatory)");
00015     registerParameter(state, "dimension", (voidP) new uint(1), ECF::UINT,
00016         "number of real valued variables (mandatory)");
00017 }
00018  
00019 
00020 bool FloatingPoint::initialize (StateP state)
00021 {
00022     if(!isParameterDefined(state, "lbound") ||
00023         !isParameterDefined(state, "ubound") ||
00024         !isParameterDefined(state, "dimension")) {
00025             ECF_LOG_ERROR(state, "Error: required parameters for FloatingPoint genotype not defined (lbound, ubound, dimension)!");
00026             throw("");
00027     }
00028 
00029     voidP genp = getParameterValue(state, "lbound");
00030     minValue_ = *((double*) genp.get());
00031 
00032     genp = getParameterValue(state, "ubound");
00033     maxValue_ = *((double*) genp.get());
00034 
00035     if(minValue_ >= maxValue_) {
00036         ECF_LOG_ERROR(state, "Error: 'lbound' must be smaller than 'ubound' for FloatingPoint genotype!");
00037         throw("");
00038     }
00039 
00040     genp = getParameterValue(state, "dimension");
00041     nDimension_ = *((uint*) genp.get());
00042 
00043     if(nDimension_ < 1) {
00044         ECF_LOG_ERROR(state, "Error: 'dimension' must be > 0 for FloatingPoint genotype!");
00045         throw("");
00046     }
00047 
00048     realValue.resize(nDimension_);
00049     // randomly create each dimension
00050     for (uint i = 0; i < nDimension_; i++){
00051         realValue[i] = ( minValue_ + (maxValue_ - minValue_) * state->getRandomizer()->getRandomDouble() );
00052     }
00053 
00054     return true;
00055 }
00056 
00057 
00058 void FloatingPoint::write(XMLNode &xFloatingPoint)
00059 {
00060     xFloatingPoint = XMLNode::createXMLTopNode("FloatingPoint");
00061     std::stringstream sValue;
00062     sValue << nDimension_;
00063     xFloatingPoint.addAttribute("size", sValue.str().c_str());
00064 
00065     sValue.str("");
00066     for(uint iVar = 0; iVar < nDimension_; iVar++)
00067         sValue << "\t" << realValue[iVar];
00068     xFloatingPoint.addText(sValue.str().c_str());
00069 }
00070 
00071 
00072 void FloatingPoint::read(XMLNode& xFloatingPoint)
00073 {
00074     XMLCSTR values = xFloatingPoint.getText();
00075     std::stringstream sValues;
00076     sValues << values;
00077 
00078     for(uint iVar = 0; iVar < nDimension_; iVar++) {
00079         sValues >> realValue[iVar];
00080     }
00081 }
00082 
00083 }

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