Modernized GAlib  3.0.0 current
GA2DBinStrGenome.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 
17 {
18  public:
19  GADefineIdentity("GA2DBinaryStringGenome", GAID::BinaryStringGenome2D);
20 
21  static void UniformInitializer(GAGenome &);
22  static void UnsetInitializer(GAGenome &);
23  static void SetInitializer(GAGenome &);
24  static int FlipMutator(GAGenome &, float);
25  static float BitComparator(const GAGenome &, const GAGenome &);
26  static int UniformCrossover(const GAGenome &, const GAGenome &, GAGenome *,
27  GAGenome *);
28  static int EvenOddCrossover(const GAGenome &, const GAGenome &, GAGenome *,
29  GAGenome *);
30  static int OnePointCrossover(const GAGenome &, const GAGenome &, GAGenome *,
31  GAGenome *);
32 
33  public:
34  GA2DBinaryStringGenome(unsigned int x, unsigned int y,
35  GAGenome::Evaluator f = (GAGenome::Evaluator)nullptr,
36  void *u = nullptr);
38  GA2DBinaryStringGenome &operator=(const GAGenome &arg)
39  {
40  copy(arg);
41  return *this;
42  }
43  GA2DBinaryStringGenome &operator=(const short array[])
44  {
45  for (unsigned int i = 0; i < nx; i++)
46  {
47  for (unsigned int j = 0; j < ny; j++)
48  {
49  gene(i, j, *(array + j * nx + i));
50  }
51  }
52  return *this;
53  }
54  GA2DBinaryStringGenome &operator=(const int array[])
55  {
56  for (unsigned int i = 0; i < nx; i++)
57  {
58  for (unsigned int j = 0; j < ny; j++)
59  {
60  gene(i, j, *(array + j * nx + i));
61  }
62  }
63  return *this;
64  }
65  ~GA2DBinaryStringGenome() override;
66  GAGenome *clone(GAGenome::CloneMethod flag = CloneMethod::CONTENTS) const override;
67  void copy(const GAGenome &chrom) override;
68 
69  int read(std::istream &) override;
70  int write(std::ostream &) const override;
71 
72  bool equal(const GAGenome &c) const override;
73 
74  // specific to this class
75  short gene(unsigned int x, unsigned int y) const { return bit(x + nx * y); }
76  short gene(unsigned int x, unsigned int y, short value)
77  {
78  _evaluated = false;
79  return ((bit(x + nx * y) == value) ? value : bit(x + nx * y, value));
80  }
81  int width() const { return nx; }
82  int width(int w)
83  {
84  resize(w, ny);
85  return nx;
86  }
87  int height() const { return ny; }
88  int height(int h)
89  {
90  resize(nx, h);
91  return ny;
92  }
93  int resize(int x, int y);
94  int resizeBehaviour(Dimension which) const;
95  int resizeBehaviour(Dimension which, unsigned int lowerX,
96  unsigned int upperX);
97  int resizeBehaviour(unsigned int lowerX, unsigned int upperX,
98  unsigned int lowerY, unsigned int upperY)
99  {
100  return (resizeBehaviour(Dimension::WIDTH, lowerX, upperX) *
101  resizeBehaviour(Dimension::HEIGHT, lowerY, upperY));
102  }
103  void copy(const GA2DBinaryStringGenome &, unsigned int, unsigned int,
104  unsigned int, unsigned int, unsigned int, unsigned int);
105  bool equal(const GA2DBinaryStringGenome &, unsigned int, unsigned int,
106  unsigned int, unsigned int, unsigned int, unsigned int) const;
107  void set(unsigned int, unsigned int, unsigned int, unsigned int);
108  void unset(unsigned int, unsigned int, unsigned int, unsigned int);
109  void randomize(unsigned int, unsigned int, unsigned int, unsigned int);
110  void randomize() { GABinaryString::randomize(); }
111  void move(unsigned int, unsigned int, unsigned int, unsigned int,
112  unsigned int, unsigned int);
113 
114  protected:
115  unsigned int nx, ny, minX, minY, maxX, maxY;
116 };
This header defines the interface for the 2D binary string genome, including crossover objects and al...
Definition: GA2DBinStrGenome.h:17
This header defines the interface for the binary string.
Definition: GABinStr.hpp:24
The base genome class just defines the genome interface - how to mutate, crossover,...
Definition: GAGenome.h:200