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 "genotype size: number of indices (mandatory)");
00014 }
00015
00016
00017 bool Permutation::initialize (StateP state)
00018 {
00019 voidP genp = getParameterValue(state, "size");
00020 size_ = *((uint*) genp.get());
00021
00022 if(size_ < 1) {
00023 ECF_LOG_ERROR(state, "Error: 'size' must be > 0 for Permutation genotype!");
00024 throw("");
00025 }
00026 variables.resize(static_cast<int>(size_));
00027
00028 for(uint i = 0; i < size_; i++) {
00029 variables[i] = i;
00030 }
00031 int ind1, ind2, temp;
00032
00033
00034 for(uint i = 0; i < size_; i++) {
00035 ind1 = state->getRandomizer()->getRandomInteger(size_);
00036 ind2 = state->getRandomizer()->getRandomInteger(size_);
00037 temp = variables[ind1];
00038 variables[ind1] = variables[ind2];
00039 variables[ind2] = temp;
00040 }
00041 return true;
00042 }
00043
00044
00045 void Permutation::write(XMLNode &xPermutation)
00046 {
00047 xPermutation = XMLNode::createXMLTopNode("Permutation");
00048 std::stringstream sValue;
00049 sValue << size_;
00050 xPermutation.addAttribute("size", sValue.str().c_str());
00051
00052 sValue.str("");
00053 for(uint i = 0; i < size_; i++)
00054 sValue << "\t" << variables[i];
00055 xPermutation.addText(sValue.str().c_str());
00056 }
00057
00058
00059 void Permutation::read(XMLNode &xPermutation)
00060 {
00061 XMLCSTR xIndices = xPermutation.getText();
00062 std::stringstream sValues;
00063 sValues << xIndices;
00064
00065 for(uint index = 0; index < size_; index++) {
00066 sValues >> variables[index];
00067 }
00068 }
00069
00070 }