ECF 1.5
FloatingPoint.cpp
1#include <cmath>
2#include "../ECF_base.h"
3#include "FloatingPoint.h"
4#include<sstream>
5
6namespace FloatingPoint
7{
8
9void FloatingPoint::registerParameters(StateP state)
10{
11 registerParameter(state, "lbound", (voidP) new double(-10), ECF::DOUBLE,
12 "lower bound for each variable (mandatory)");
13 registerParameter(state, "ubound", (voidP) new double(10), ECF::DOUBLE,
14 "upper bound for each variable (mandatory)");
15 registerParameter(state, "dimension", (voidP) new uint(1), ECF::UINT,
16 "number of real valued variables (mandatory)");
17}
18
19
20bool FloatingPoint::initialize (StateP state)
21{
22 if(!isParameterDefined(state, "lbound") ||
23 !isParameterDefined(state, "ubound") ||
24 !isParameterDefined(state, "dimension")) {
25 ECF_LOG_ERROR(state, "Error: required parameters for FloatingPoint genotype not defined (lbound, ubound, dimension)!");
26 throw("");
27 }
28
29 voidP genp = getParameterValue(state, "lbound");
30 minValue_ = *((double*) genp.get());
31
32 genp = getParameterValue(state, "ubound");
33 maxValue_ = *((double*) genp.get());
34
35 if(minValue_ >= maxValue_) {
36 ECF_LOG_ERROR(state, "Error: 'lbound' must be smaller than 'ubound' for FloatingPoint genotype!");
37 throw("");
38 }
39
40 genp = getParameterValue(state, "dimension");
41 nDimension_ = *((uint*) genp.get());
42
43 if(nDimension_ < 1) {
44 ECF_LOG_ERROR(state, "Error: 'dimension' must be > 0 for FloatingPoint genotype!");
45 throw("");
46 }
47
48 realValue.resize(nDimension_);
49 // randomly create each dimension
50 for (uint i = 0; i < nDimension_; i++){
51 realValue[i] = ( minValue_ + (maxValue_ - minValue_) * state->getRandomizer()->getRandomDouble() );
52 }
53
54 return true;
55}
56
57
58void FloatingPoint::write(XMLNode &xFloatingPoint)
59{
60 xFloatingPoint = XMLNode::createXMLTopNode("FloatingPoint");
61 std::stringstream sValue;
62 sValue << nDimension_;
63 xFloatingPoint.addAttribute("size", sValue.str().c_str());
64
65 sValue.str("");
66 for(uint iVar = 0; iVar < nDimension_; iVar++)
67 sValue << "\t" << realValue[iVar];
68 xFloatingPoint.addText(sValue.str().c_str());
69}
70
71
72void FloatingPoint::read(XMLNode& xFloatingPoint)
73{
74 XMLCSTR values = xFloatingPoint.getText();
75 std::stringstream sValues;
76 sValues << values;
77
78 for(uint iVar = 0; iVar < nDimension_; iVar++) {
79 sValues >> realValue[iVar];
80 }
81}
82
83}