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 registerParameter(state, "ubound", (voidP) new double(10), ECF::DOUBLE);
00013 registerParameter(state, "dimension", (voidP) new uint(1), ECF::UINT);
00014 }
00015
00016
00017 bool FloatingPoint::initialize (StateP state)
00018 {
00019 if(!isParameterDefined(state, "lbound") ||
00020 !isParameterDefined(state, "ubound") ||
00021 !isParameterDefined(state, "dimension")) {
00022 ECF_LOG_ERROR(state, "Error: required parameters for FloatingPoint genotype not defined (lbound, ubound, dimension)!");
00023 throw("");
00024 }
00025
00026 voidP genp = getParameterValue(state, "lbound");
00027 minValue_ = *((double*) genp.get());
00028
00029 genp = getParameterValue(state, "ubound");
00030 maxValue_ = *((double*) genp.get());
00031
00032 if(minValue_ >= maxValue_) {
00033 ECF_LOG_ERROR(state, "Error: 'lbound' must be smaller than 'ubound' for FloatingPoint genotype!");
00034 throw("");
00035 }
00036
00037 genp = getParameterValue(state, "dimension");
00038 nDimension_ = *((uint*) genp.get());
00039
00040 if(nDimension_ < 1) {
00041 ECF_LOG_ERROR(state, "Error: 'dimension' must be > 0 for FloatingPoint genotype!");
00042 throw("");
00043 }
00044
00045 realValue.resize(nDimension_);
00046
00047 for (uint i = 0; i < nDimension_; i++){
00048 realValue[i] = ( minValue_ + (maxValue_ - minValue_) * state->getRandomizer()->getRandomDouble() );
00049 }
00050
00051 return true;
00052 }
00053
00054
00055 void FloatingPoint::write(XMLNode &xFloatingPoint)
00056 {
00057 xFloatingPoint = XMLNode::createXMLTopNode("FloatingPoint");
00058 std::stringstream sValue;
00059 sValue << nDimension_;
00060 xFloatingPoint.addAttribute("size", sValue.str().c_str());
00061
00062 sValue.str("");
00063 for(uint iVar = 0; iVar < nDimension_; iVar++)
00064 sValue << "\t" << realValue[iVar];
00065 xFloatingPoint.addText(sValue.str().c_str());
00066 }
00067
00068
00069 void FloatingPoint::read(XMLNode& xFloatingPoint)
00070 {
00071 XMLCSTR values = xFloatingPoint.getText();
00072 std::stringstream sValues;
00073 sValues << values;
00074
00075 for(uint iVar = 0; iVar < nDimension_; iVar++) {
00076 sValues >> realValue[iVar];
00077 }
00078 }
00079
00080 }