ECF 1.5
Permutation.cpp
1#include <cmath>
2#include "../ECF_base.h"
3#include "Permutation.h"
4#include<sstream>
5
6
7namespace Permutation
8{
9
10void Permutation::registerParameters(StateP state)
11{
12 registerParameter(state, "size", (voidP) new uint(1), ECF::UINT,
13 "genotype size: number of indices (mandatory)");
14}
15
16
17bool Permutation::initialize (StateP state)
18{
19 voidP genp = getParameterValue(state, "size");
20 size_ = *((uint*) genp.get());
21
22 if(size_ < 1) {
23 ECF_LOG_ERROR(state, "Error: 'size' must be > 0 for Permutation genotype!");
24 throw("");
25 }
26 variables.resize(static_cast<int>(size_));
27
28 for(uint i = 0; i < size_; i++) {
29 variables[i] = (int) i;
30 }
31 int ind1, ind2, temp;
32
33 // generate random permutation
34 for(uint i = 0; i < size_; i++) {
35 ind1 = state->getRandomizer()->getRandomInteger(size_);
36 ind2 = state->getRandomizer()->getRandomInteger(size_);
37 temp = variables[ind1];
38 variables[ind1] = variables[ind2];
39 variables[ind2] = temp;
40 }
41 return true;
42}
43
44
45void Permutation::write(XMLNode &xPermutation)
46{
47 xPermutation = XMLNode::createXMLTopNode("Permutation");
48 std::stringstream sValue;
49 sValue << size_;
50 xPermutation.addAttribute("size", sValue.str().c_str());
51
52 sValue.str("");
53 for(uint i = 0; i < size_; i++)
54 sValue << "\t" << variables[i];
55 xPermutation.addText(sValue.str().c_str());
56}
57
58
59void Permutation::read(XMLNode &xPermutation)
60{
61 XMLCSTR xIndices = xPermutation.getText();
62 std::stringstream sValues;
63 sValues << xIndices;
64
65 for(uint index = 0; index < size_; index++) {
66 sValues >> variables[index];
67 }
68}
69
70}