00001 #include <cmath>
00002 #include "../ECF_base.h"
00003 #include "Permutation.h"
00004 #include<sstream>
00005
00006
00007 namespace Permutation
00008 {
00009
00010 void Permutation::registerParameters(StateP state)
00011 {
00012 registerParameter(state, "size", (voidP) new uint(1), ECF::UINT);
00013 }
00014
00015
00016 bool Permutation::initialize (StateP state)
00017 {
00018 voidP genp = getParameterValue(state, "size");
00019 size_ = *((uint*) genp.get());
00020
00021 if(size_ < 1) {
00022 ECF_LOG_ERROR(state, "Error: 'size' must be > 0 for Permutation genotype!");
00023 throw("");
00024 }
00025 variables.resize(static_cast<int>(size_));
00026
00027 for(uint i = 0; i < size_; i++) {
00028 variables[i] = i;
00029 }
00030 int ind1, ind2, temp;
00031
00032
00033 for(uint i = 0; i < size_; i++) {
00034 ind1 = state->getRandomizer()->getRandomInteger(size_);
00035 ind2 = state->getRandomizer()->getRandomInteger(size_);
00036 temp = variables[ind1];
00037 variables[ind1] = variables[ind2];
00038 variables[ind2] = temp;
00039 }
00040 return true;
00041 }
00042
00043
00044 void Permutation::write(XMLNode &xPermutation)
00045 {
00046 xPermutation = XMLNode::createXMLTopNode("Permutation");
00047 std::stringstream sValue;
00048 sValue << size_;
00049 xPermutation.addAttribute("size", sValue.str().c_str());
00050
00051 sValue.str("");
00052 for(uint i = 0; i < size_; i++)
00053 sValue << "\t" << variables[i];
00054 xPermutation.addText(sValue.str().c_str());
00055 }
00056
00057
00058 void Permutation::read(XMLNode &xPermutation)
00059 {
00060 XMLCSTR xIndices = xPermutation.getText();
00061 std::stringstream sValues;
00062 sValues << xIndices;
00063
00064 for(uint index = 0; index < size_; index++) {
00065 sValues >> variables[index];
00066 }
00067 }
00068
00069 }