ECF 1.5
BitString.h
1#ifndef BitString_h
2#define BitString_h
3
4#include "../Genotype.h"
5#include "BitStringCrsOnePoint.h"
6#include "BitStringMutSimple.h"
7#include "BitStringMutMix.h"
8#include "BitStringCrsUniform.h"
9#include<sstream>
10
11namespace BitString
12{
13
23class BitString : public Genotype
24{
25protected:
26 uint nBits_;
27
28public:
29 std::vector<bool> bits;
30
31 BitString(uint nBits = 0)
32 { name_ = "BitString";
33 nBits_ = nBits;
34 }
35
36 void registerParameters(StateP state)
37 {
38 if(nBits_ == 0)
39 nBits_ = 10;
40 registerParameter(state, "size", (voidP) new uint(nBits_), ECF::UINT,
41 "number of bits (mandatory)");
42 }
43
44 bool initialize(StateP state)
45 {
46 if(!isParameterDefined(state, "size")) {
47 ECF_LOG_ERROR(state, "Error: BitString genotype size not defined!");
48 throw("");
49 }
50
51 voidP sizep = getParameterValue(state, "size");
52 nBits_ = *((uint*) sizep.get());
53
54 if(nBits_ < 1) {
55 ECF_LOG_ERROR(state, "Error: invalid size for BitString genotype!");
56 throw("");
57 }
58
59 bits.resize(nBits_);
60 for(uint i = 0; i < bits.size(); i++){
61 bits[i] = (state->getRandomizer()->getRandomInteger(2) % 2) ? true:false;
62 }
63 return true;
64 }
65
67 {
68 BitString *newObject = new BitString(*this);
69 return newObject;
70 }
71
72 std::vector<CrossoverOpP> getCrossoverOp()
73 {
74 std::vector<CrossoverOpP> crx;
75 crx.push_back(static_cast<CrossoverOpP> (new BitStringCrsOnePoint));
76 crx.push_back(static_cast<CrossoverOpP> (new BitStringCrsUniform));
77 return crx;
78 }
79
80 std::vector<MutationOpP> getMutationOp()
81 {
82 std::vector<MutationOpP> mut;
83 mut.push_back(static_cast<MutationOpP> (new BitStringMutSimple));
84 mut.push_back(static_cast<MutationOpP> (new BitStringMutMix));
85 return mut;
86 }
87
88 void write(XMLNode &xBitString)
89 {
90 xBitString = XMLNode::createXMLTopNode("BitString");
91 std::stringstream sValue;
92 sValue << bits.size();
93 xBitString.addAttribute("size", sValue.str().c_str());
94
95 sValue.str("");
96 for(uint iBit = 0; iBit < bits.size(); iBit++)
97 sValue << ((bits[iBit] == true) ? '1' : '0');
98 xBitString.addText(sValue.str().c_str());
99 }
100
101 void read(XMLNode &xBitString)
102 {
103 XMLCSTR xBits = xBitString.getText();
104 for(uint iBit = 0; iBit < bits.size(); iBit++)
105 bits[iBit] = (xBits[iBit] == '0') ? false : true;
106 }
107};
108}
109
110typedef boost::shared_ptr<BitString::BitString> BitStringP;
111
112#endif // BitString_h
113
BitString genotype: one point crossover operator.
BitString genotype uniform crossover operator.
BitString class - implements genotype as a series of bits.
Definition: BitString.h:24
void write(XMLNode &xBitString)
Write genotype data to XMLNode.
Definition: BitString.h:88
void registerParameters(StateP state)
Register genotype's parameters (called before Genotype::initialize)
Definition: BitString.h:36
void read(XMLNode &xBitString)
Read genotype data from XMLNode.
Definition: BitString.h:101
std::vector< MutationOpP > getMutationOp()
Create and return a vector of mutation operators.
Definition: BitString.h:80
std::vector< CrossoverOpP > getCrossoverOp()
Create and return a vector of crossover operators.
Definition: BitString.h:72
BitString * copy()
Create an identical copy of the genotype object.
Definition: BitString.h:66
bool initialize(StateP state)
Initialize a genotype object (read parameters, perform sanity check, build data)
Definition: BitString.h:44
BitString genotype mixing mutation operator.
BitString genotype simple (one bit) mutation operator.
Genotype base class.
Definition: Genotype.h:36