Modernized GAlib  3.0.0 current
GADemeGA.h
1 /* ----------------------------------------------------------------------------
2  gademe.h
3  mbwall jan96
4  Copyright (c) 1995-1996 Massachusetts Institute of Technology
5  all rights reserved
6 
7  Header for the deme (parallel population) genetic algorithm class.
8  This genetic algorithm lets you specify a number of individuals to migrate
9 from one population to another at the end of each generation. You can specify
10 how many populations to maintain. Each population evolves using a steady-state
11 genetic algorithm. At the end of each generation, the specified number of
12 individuals migrate from one population to the next (we use the loop migration
13 topology in this implementation).
14  You can modify the migration method by deriving a new class from this one and
15 redefine the migration method. If you want to use a different kind of genetic
16 algorithm for each population then you'll have to modify the mechanics of the
17 step method.
18 ---------------------------------------------------------------------------- */
19 #ifndef _ga_gademe_h_
20 #define _ga_gademe_h_
21 
22 #include <GABaseGA.h>
23 
25 {
26  public:
27  GADefineIdentity("GADemeGA", GAID::DemeGA);
28 
29  enum
30  {
31  ALL = (-1)
32  };
33  static GAParameterList &registerDefaultParameters(GAParameterList &);
34 
35  public:
36  explicit GADemeGA(const GAGenome &);
37  explicit GADemeGA(const GAPopulation &);
38  GADemeGA(const GADemeGA &);
39  GADemeGA &operator=(const GADemeGA &);
40  ~GADemeGA() override;
41  void copy(const GAGeneticAlgorithm &) override;
42 
43  void initialize(unsigned int seed = 0) override;
44  void step() override;
45  virtual void migrate(); // new for this derived class
46  GADemeGA &operator++()
47  {
48  step();
49  return *this;
50  }
51 
52  int setptr(const std::string &name, const void *value) override;
53  int get(const char *name, void *value) const override;
54 
55  int minimaxi() const override { return minmax; }
56  int minimaxi(int m) override;
57 
58  const GAPopulation &population() const override { return *pop; }
59  const GAPopulation &population(const GAPopulation &p) override
60  {
61  GAGeneticAlgorithm::population(p);
62  return population(ALL, p);
63  }
64  int populationSize() const override { return pop->size(); }
65  int populationSize(unsigned int n) override
66  {
67  GAGeneticAlgorithm::populationSize(n);
68  return populationSize(ALL, n);
69  }
70  GAScalingScheme &scaling() const override { return pop->scaling(); }
71  GAScalingScheme &scaling(const GAScalingScheme &s) override
72  {
73  GAGeneticAlgorithm::scaling(s);
74  return scaling(ALL, s);
75  }
76  GASelectionScheme &selector() const override { return pop->selector(); }
77  GASelectionScheme &selector(const GASelectionScheme &s) override
78  {
79  GAGeneticAlgorithm::selector(s);
80  return selector(ALL, s);
81  }
82  void objectiveFunction(GAGenome::Evaluator f) override
83  {
84  GAGeneticAlgorithm::objectiveFunction(f);
85  objectiveFunction(ALL, f);
86  }
87  void objectiveData(const GAEvalData &v) override
88  {
89  GAGeneticAlgorithm::objectiveData(v);
90  objectiveData(ALL, v);
91  }
92 
93  const GAPopulation &population(unsigned int i) const { return *deme[i]; }
94  const GAPopulation &population(int i, const GAPopulation &);
95  int populationSize(unsigned int i) const { return deme[i]->size(); }
96  int populationSize(int i, unsigned int n);
97  int nReplacement(unsigned int i) const { return nrepl[i]; }
98  int nReplacement(int i, unsigned int n);
99  int nMigration() const { return nmig; }
100  int nMigration(unsigned int i);
101  int nPopulations() const { return npop; }
102  int nPopulations(unsigned int i);
103 
104  GAScalingScheme &scaling(unsigned int i) const
105  {
106  return deme[i]->scaling();
107  }
108  GAScalingScheme &scaling(int i, const GAScalingScheme &s);
109  GASelectionScheme &selector(unsigned int i) const
110  {
111  return deme[i]->selector();
112  }
113  GASelectionScheme &selector(int i, const GASelectionScheme &s);
114  void objectiveFunction(int i, GAGenome::Evaluator f);
115  void objectiveData(int i, const GAEvalData &);
116 
117  const GAStatistics &statistics() const { return stats; }
118  const GAStatistics &statistics(unsigned int i) const { return pstats[i]; }
119 
120  protected:
121  unsigned int npop; // how many populations do we have?
122  int *nrepl; // how many to replace each generation
123  GAPopulation **deme; // array of populations that we'll use
124  GAPopulation *tmppop; // temp pop for doing the evolutions
125  GAStatistics *pstats; // statistics for each population
126  unsigned int nmig; // number to migrate from each population
127 };
128 
129 inline std::ostream &operator<<(std::ostream &os, GADemeGA &arg)
130 {
131  arg.write(os);
132  return (os);
133 }
134 inline std::istream &operator>>(std::istream &is, GADemeGA &arg)
135 {
136  arg.read(is);
137  return (is);
138 }
139 
140 #endif
Definition: GADemeGA.h:25
void step() override
Evolve by one generation.
void initialize(unsigned int seed=0) override
Undefined for the base class.
This is the basic interface for the object that contains evaluation data.
Definition: GAEvalData.h:15
The base GA class is virtual - it defines the core data elements and parts of the interface that are ...
Definition: GABaseGA.h:89
The base genome class just defines the genome interface - how to mutate, crossover,...
Definition: GAGenome.h:200
Parameter List.
Definition: GAParameter.h:83
Definition: GAPopulation.h:66
Definition: GAScaling.h:46
Definition: GASelector.h:55
Statistics class.
Definition: GAStatistics.h:30