Modernized GAlib  3.0.0 current
GA1DBinStrGenome.h
1 /* ----------------------------------------------------------------------------
2  mbwall 19apr95
3  Copyright (c) 1995 Massachusetts Institute of Technology
4  all rights reserved
5 ---------------------------------------------------------------------------- */
6 
7 #pragma once
8 
9 #include "GABinStr.hpp"
10 #include "GAGenome.h"
11 
12 /* ----------------------------------------------------------------------------
13 1DBinaryStringGenome
14 -------------------------------------------------------------------------------
15 resize
16  These genomes are resizable. In addition, you can set a resize behaviour
17  to specify the bounds in which the resize can occur.
18 
19 copy
20  Copy bits from the specified genome using the location and length
21  that are passed to us. If the current genome is too short for the
22  entire length, copy whatever we can. If the original genome is too
23  short for the specified length, copy whatever we can. If either location
24  is out of bounds, return without doing anything.
25  We do NOT check for negative values in the locations!
26  This routine clips if the copy sizes do not match - it does NOT resize the
27  genome to fit the copy. You'll have to do resizes before you call this
28  routine if you want the copy to fit the original.
29 
30 ==, !=
31  Are two genomes equal? Our test for equality is based upon the
32  contents of the genome, NOT the behaviour. So as long as the bitstreams
33  match, the genomes are 'equal'. This means that a resizeable genome
34  may be equal to a fixed-length genome. But a chromsome with 500
35  bits allocated is not equal to a genome with 10 bits allocated unless
36  both are the same size.
37 ---------------------------------------------------------------------------- */
38 
43 {
44  public:
45  GADefineIdentity("GA1DBinaryStringGenome", GAID::BinaryStringGenome);
46 
47  static void UniformInitializer(GAGenome &);
48  static void UnsetInitializer(GAGenome &);
49  static void SetInitializer(GAGenome &);
50  static int FlipMutator(GAGenome &, float);
51  static int UniformCrossover(const GAGenome &, const GAGenome &, GAGenome *, GAGenome *);
52  static int EvenOddCrossover(const GAGenome &, const GAGenome &, GAGenome *, GAGenome *);
53  static int OnePointCrossover(const GAGenome &, const GAGenome &, GAGenome *, GAGenome *);
54  static int TwoPointCrossover(const GAGenome &, const GAGenome &, GAGenome *, GAGenome *);
55  static float BitComparator(const GAGenome &, const GAGenome &);
56 
57  public:
58  explicit GA1DBinaryStringGenome(unsigned int len, GAGenome::Evaluator f = nullptr, void *u = nullptr);
60  GA1DBinaryStringGenome &operator=(const GAGenome &arg)
61  {
62  copy(arg);
63  return *this;
64  }
65  GA1DBinaryStringGenome &operator=(const short array[]) // no err checks!
66  {
67  for (unsigned int i = 0; i < data.size(); i++)
68  {
69  gene(i, *(array + i));
70  }
71  return *this;
72  }
73  GA1DBinaryStringGenome &operator=(const int array[]) // no err checks!
74  {
75  for (unsigned int i = 0; i < data.size(); i++)
76  {
77  gene(i, *(array + i));
78  }
79  return *this;
80  }
81  ~GA1DBinaryStringGenome() override;
82  GAGenome *clone(GAGenome::CloneMethod flag = CloneMethod::CONTENTS) const override;
83  void copy(const GAGenome &) override;
84 
85  int read(std::istream &is) override;
86  int write(std::ostream &os) const override;
87 
88  bool equal(const GAGenome &c) const override;
89 
90  short gene(unsigned int x = 0) const { return bit(x); }
91  short gene(unsigned int x, short value)
92  {
93  _evaluated = false;
94  return ((bit(x) == value) ? value : bit(x, value));
95  }
96  short operator[](unsigned int x) { return gene(x); }
97  int length() const { return nx; }
98  int length(int x)
99  {
100  resize(x);
101  return nx;
102  }
103  int resize(int l);
104  int resizeBehaviour() const;
105  int resizeBehaviour(unsigned int lower, unsigned int upper);
106  void copy(const GA1DBinaryStringGenome &orig, unsigned int r,
107  unsigned int x, unsigned int length);
108  bool equal(const GA1DBinaryStringGenome &orig, unsigned int r,
109  unsigned int x, unsigned int length) const;
110  void set(unsigned int x, unsigned int length);
111  void unset(unsigned int x, unsigned int length);
112  void randomize(unsigned int x, unsigned int length);
113  void randomize() { GABinaryString::randomize(); }
114  void move(unsigned int destx, unsigned int srcx, unsigned int length);
115 
116  protected:
117  unsigned int nx; // how long is the data string?
118  unsigned int minX; // what is the lower limit?
119  unsigned int maxX; // what is the upper limit?
120 };
121 
122 inline void GA1DBinaryStringGenome::copy(const GA1DBinaryStringGenome &orig,
123  unsigned int r, unsigned int x,
124  unsigned int l)
125 {
126  if (l > 0 && x < orig.nx && r < nx)
127  {
128  if (x + l > orig.nx)
129  {
130  l = orig.nx - x;
131  }
132  if (r + l > nx)
133  {
134  l = nx - r;
135  }
136  GABinaryString::copy(orig, r, x, l);
137  }
138  _evaluated = false;
139 }
140 inline void GA1DBinaryStringGenome::set(unsigned int x, unsigned int l)
141 {
142  if (x + l > nx)
143  {
144  l = nx - x;
145  }
146  GABinaryString::set(x, l);
147  _evaluated = false;
148 }
149 inline void GA1DBinaryStringGenome::unset(unsigned int x, unsigned int l)
150 {
151  if (x + l > nx)
152  {
153  l = nx - x;
154  }
155  GABinaryString::unset(x, l);
156  _evaluated = false;
157 }
158 inline void GA1DBinaryStringGenome::randomize(unsigned int x, unsigned int l)
159 {
160  if (x + l > nx)
161  {
162  l = nx - x;
163  }
164  GABinaryString::randomize(x, l);
165  _evaluated = false;
166 }
167 inline void GA1DBinaryStringGenome::move(unsigned int x, unsigned int srcx,
168  unsigned int l)
169 {
170  if (srcx + l > nx)
171  {
172  l = nx - srcx;
173  }
174  if (x + l > nx)
175  {
176  l = nx - x;
177  }
178  GABinaryString::move(x, srcx, l);
179  _evaluated = false;
180 }
Interface for the 1D binary string genome, including crossover objects and all the default and built-...
Definition: GA1DBinStrGenome.h:43
This header defines the interface for the binary string.
Definition: GABinStr.hpp:24
std::vector< GABit > data
the data themselves
Definition: GABinStr.hpp:137
void copy(const GABinaryString &orig)
Copy the contents of the bitstream.
Definition: GABinStr.hpp:35
void move(unsigned int destIdx, unsigned int sourceIdx, unsigned int l)
Copy (sub) bitstream.
Definition: GABinStr.hpp:98
The base genome class just defines the genome interface - how to mutate, crossover,...
Definition: GAGenome.h:200