Qualia  0.2
BinaryChromosome.h
Go to the documentation of this file.
1 /*
2  * BinaryChromosome.h
3  *
4  * (c) 2012 Sofian Audry -- info(@)sofianaudry(.)com
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef BINARYCHROMOSOME_H_
21 #define BINARYCHROMOSOME_H_
22 
23 #include <qualia/core/common.h>
24 #include <qualia/util/bitarray.h>
25 #include <qualia/util/random.h>
26 
27 #include "Chromosome.h"
28 
29 #include <string.h>
30 //void defaultMutator(Chromosome& chromosome, float);
31 //void defaultInitializer(Chromosome& chromosome);
32 
34 public:
36  unsigned int _nGenes;
37 
39  uint8_t* _geneSizes;
40 
45 
46  BinaryChromosomeProperties(unsigned int nGenes, const uint8_t* geneSizes,
48 
50  unsigned int nGenes() const { return _nGenes; }
51 
53  uint8_t geneSize(int i) const { return _geneSizes[i]; }
54 
56  unsigned int bitSize() const;
57 
59  unsigned int byteSize() const { return ( (bitSize()+7) / 8); }
60 
62  int getStartBitPosition(int gene) const;
63 
65  virtual bool equals(const BinaryChromosomeProperties& info);
66 };
67 
69 public:
71  uint8_t* code;
72 
74  virtual ~BinaryChromosome();
75 
76  virtual void copyFrom(const Chromosome& c);
77 
78  virtual void init();
79  virtual void mutate(float p);
80  virtual bool equals(const Chromosome& c) const;
81 
83  uint64_t getGeneValue(int gene) const;
84 
86  void setGeneValue(int gene, uint64_t value);
87 
88  // Static methods.
89  static void initializeRandom(Chromosome& chromosome);
90 
91  static void mutateFlip(Chromosome& chromosome, float probability);
92 
93  static void crossoverOnePoint(const Chromosome& parent1, const Chromosome& parent2,
94  Chromosome* offspring1, Chromosome* offspring2);
95  static void crossoverTwoPoint(const Chromosome& parent1, const Chromosome& parent2,
96  Chromosome* offspring1, Chromosome* offspring2);
97 
98  static void _codeCrossoverOnePoint(uint8_t* offspringCode, const uint8_t* parentCode1, const uint8_t* parentCode2,
99  unsigned int point, unsigned int codeSize);
100  static void _codeCrossoverTwoPoint(uint8_t* offspringCode, const uint8_t* parentCode1, const uint8_t* parentCode2,
101  unsigned int point1, unsigned int point2, unsigned int codeSize);
102 // virtual int compare(const Chromosome& g);
103 // virtual float evaluate();
104 
105 };
106 
107 
108 #endif /* BINARYCHROMOSOME_H_ */