• Main Page
  • Modules
  • Classes
  • Files
  • File List

D:/Projekt/ECF_trunk/ECF/bitstring/BitString.h

00001 #ifndef BitString_h
00002 #define BitString_h
00003 
00004 #include "../Genotype.h"
00005 #include "BitStringCrsOnePoint.h"
00006 #include "BitStringMutSimple.h"
00007 #include "BitStringMutMix.h"
00008 #include "BitStringCrsUniform.h"
00009 #include<sstream>
00010 
00011 namespace BitString
00012 {
00013 
00023 class BitString : public Genotype
00024 {
00025 protected:
00026     uint nBits_;
00027 
00028 public:
00029     std::vector<bool> bits;
00030 
00031     BitString(uint nBits = 0)
00032     {   name_ = "BitString";
00033         nBits_ = nBits;
00034     }
00035 
00036     void registerParameters(StateP state)
00037     {
00038         if(nBits_ == 0)
00039             nBits_ = 10;
00040         registerParameter(state, "size", (voidP) new uint(nBits_), ECF::UINT,
00041             "number of bits (mandatory)");
00042     }
00043 
00044     bool initialize(StateP state)
00045     {
00046         if(!isParameterDefined(state, "size")) {
00047             ECF_LOG_ERROR(state, "Error: BitString genotype size not defined!");
00048             throw("");
00049         }
00050 
00051         voidP sizep = getParameterValue(state, "size");
00052         nBits_ = *((uint*) sizep.get());
00053 
00054         if(nBits_ < 1) {
00055             ECF_LOG_ERROR(state, "Error: invalid size for BitString genotype!");
00056             throw("");
00057         }
00058 
00059         bits.resize(nBits_);
00060         for(uint i = 0; i < bits.size(); i++){
00061             bits[i] = (state->getRandomizer()->getRandomInteger(2) % 2) ? true:false;
00062         }
00063         return true;
00064     }
00065 
00066     BitString* copy()
00067     {
00068         BitString *newObject = new BitString(*this);
00069         return newObject;
00070     }
00071 
00072     std::vector<CrossoverOpP> getCrossoverOp()
00073     {
00074         std::vector<CrossoverOpP> crx;
00075         crx.push_back(static_cast<CrossoverOpP> (new BitStringCrsOnePoint));
00076         crx.push_back(static_cast<CrossoverOpP> (new BitStringCrsUniform));
00077         return crx;
00078     }
00079 
00080     std::vector<MutationOpP> getMutationOp()
00081     {
00082         std::vector<MutationOpP> mut;
00083         mut.push_back(static_cast<MutationOpP> (new BitStringMutSimple));
00084         mut.push_back(static_cast<MutationOpP> (new BitStringMutMix));
00085         return mut;
00086     }
00087 
00088     void write(XMLNode &xBitString)
00089     {
00090         xBitString = XMLNode::createXMLTopNode("BitString");
00091         std::stringstream sValue;
00092         sValue << bits.size();
00093         xBitString.addAttribute("size", sValue.str().c_str());
00094 
00095         sValue.str("");
00096         for(uint iBit = 0; iBit < bits.size(); iBit++)
00097             sValue << ((bits[iBit] == true) ? '1' : '0');
00098         xBitString.addText(sValue.str().c_str());
00099     }
00100 
00101     void read(XMLNode &xBitString)
00102     {
00103         XMLCSTR xBits = xBitString.getText();
00104         for(uint iBit = 0; iBit < bits.size(); iBit++)
00105             bits[iBit] = (xBits[iBit] == '0') ? false : true;
00106     }
00107 };
00108 }
00109 
00110 typedef boost::shared_ptr<BitString::BitString> BitStringP;
00111 
00112 #endif // BitString_h
00113 

Generated on Tue Nov 4 2014 13:04:30 for ECF by  doxygen 1.7.1