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