• 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     }
00042 
00043     bool initialize(StateP state)
00044     {
00045         if(!isParameterDefined(state, "size")) {
00046             ECF_LOG_ERROR(state, "Error: BitString genotype size not defined!");
00047             throw("");
00048         }
00049 
00050         voidP sizep = getParameterValue(state, "size");
00051         nBits_ = *((uint*) sizep.get());
00052 
00053         if(nBits_ < 1) {
00054             ECF_LOG_ERROR(state, "Error: invalid size for BitString genotype!");
00055             throw("");
00056         }
00057 
00058         bits.resize(nBits_);
00059         for(uint i = 0; i < bits.size(); i++){
00060             bits[i] = (state->getRandomizer()->getRandomInteger(2) % 2) ? true:false;
00061         }
00062         return true;
00063     }
00064 
00065     BitString* copy()
00066     {
00067         BitString *newObject = new BitString(*this);
00068         return newObject;
00069     }
00070 
00071     std::vector<CrossoverOpP> getCrossoverOp()
00072     {
00073         std::vector<CrossoverOpP> crx;
00074         crx.push_back(static_cast<CrossoverOpP> (new BitStringCrsOnePoint));
00075         crx.push_back(static_cast<CrossoverOpP> (new BitStringCrsUniform));
00076         return crx;
00077     }
00078 
00079     std::vector<MutationOpP> getMutationOp()
00080     {
00081         std::vector<MutationOpP> mut;
00082         mut.push_back(static_cast<MutationOpP> (new BitStringMutSimple));
00083         mut.push_back(static_cast<MutationOpP> (new BitStringMutMix));
00084         return mut;
00085     }
00086 
00087     void write(XMLNode &xBitString)
00088     {
00089         xBitString = XMLNode::createXMLTopNode("BitString");
00090         std::stringstream sValue;
00091         sValue << bits.size();
00092         xBitString.addAttribute("size", sValue.str().c_str());
00093 
00094         sValue.str("");
00095         for(uint iBit = 0; iBit < bits.size(); iBit++)
00096             sValue << ((bits[iBit] == true) ? '1' : '0');
00097         xBitString.addText(sValue.str().c_str());
00098     }
00099 
00100     void read(XMLNode &xBitString)
00101     {
00102         XMLCSTR xBits = xBitString.getText();
00103         for(uint iBit = 0; iBit < bits.size(); iBit++)
00104             bits[iBit] = (xBits[iBit] == '0') ? false : true;
00105     }
00106 };
00107 }
00108 
00109 typedef boost::shared_ptr<BitString::BitString> BitStringP;
00110 
00111 #endif // BitString_h
00112 

Generated on Fri Jul 5 2013 09:34:23 for ECF by  doxygen 1.7.1