ECF 1.7
IntGenotype.cpp
1#include <cmath>
2#include <sstream>
3#include "../ECF_base.h"
4#include "IntGenotype.h"
5
6namespace IntGenotype
7{
8
10 {
11 registerParameter(state, "lbound", (voidP) new int(-10), ECF::INT);
12 registerParameter(state, "ubound", (voidP) new int(10), ECF::INT);
13 registerParameter(state, "size", (voidP)(new uint(1)), ECF::UINT);
14 }
15
16 bool IntGenotype::initialize(StateP state)
17 {
18 if (!isParameterDefined(state, "lbound") ||
19 !isParameterDefined(state, "ubound") ||
20 !isParameterDefined(state, "size")) {
21 ECF_LOG_ERROR(state, "Error: required parameters for IntGenotype genotype not defined (lbound, ubound, size)!");
22 throw("");
23 }
24
25 voidP genp = getParameterValue(state, "lbound");
26 minValue_ = *((int*)genp.get());
27
28 genp = getParameterValue(state, "ubound");
29 maxValue_ = *((int*)genp.get());
30
31 if (minValue_ >= maxValue_) {
32 ECF_LOG_ERROR(state, "Error: 'lbound' must be smaller than 'ubound' for IntGenotype genotype!");
33 throw("");
34 }
35
36 genp = getParameterValue(state, "size");
37 nSize_ = *((uint*)genp.get());
38
39 if (nSize_ < 1) {
40 ECF_LOG_ERROR(state, "Error: 'size' must be > 0 for IntGenotype genotype!");
41 throw("");
42 }
43
44 intValues.resize(nSize_);
45 // randomly create each dimension
46 for (uint i = 0; i < nSize_; i++){
47 intValues[i] = state->getRandomizer()->getRandomInteger(minValue_, maxValue_);
48 }
49
50 return true;
51 }
52
53 void IntGenotype::write(XMLNode &xIntGenotype)
54 {
55 xIntGenotype = XMLNode::createXMLTopNode("IntGenotype");
56 std::stringstream sValue;
57 sValue << intValues.size();
58 xIntGenotype.addAttribute("size", sValue.str().c_str());
59
60 sValue.str("");
61 for (uint iVar = 0; iVar < intValues.size(); iVar++)
62 sValue << " " << intValues[iVar];
63 xIntGenotype.addText(sValue.str().c_str());
64 }
65
66 // mandatory if running parallel ECF or reading population from a milestone file
67 void IntGenotype::read(XMLNode& xIntGenotype)
68 {
69 XMLCSTR values = xIntGenotype.getText();
70 std::stringstream sValues;
71 sValues << values;
72
73 for (uint iVar = 0; iVar < intValues.size(); iVar++)
74 sValues >> intValues[iVar];
75 }
76
77}
voidP getParameterValue(StateP state, std::string name)
Read single parameter value from Registry.
Definition Genotype.cpp:10
bool registerParameter(StateP state, std::string name, voidP value, enum ECF::type T, std::string description="")
Register a single parameter.
Definition Genotype.cpp:4
bool isParameterDefined(StateP state, std::string name)
Check if parameter is defined in the configuration.
Definition Genotype.cpp:22
void write(XMLNode &xIntGenotype)
Write genotype data to XMLNode.
bool initialize(StateP state)
Initialize a genotype object (read parameters, perform sanity check, build data).
void read(XMLNode &xIntGenotype)
Read genotype data from XMLNode.
void registerParameters(StateP state)
Register genotype's parameters (called before Genotype::initialize).