Modernized GAlib  3.0.0 current
GABaseGA.h
1 /* ----------------------------------------------------------------------------
2  mbwall 28jul94
3  Copyright (c) 1995 Massachusetts Institute of Technology
4  all rights reserved
5 ---------------------------------------------------------------------------- */
6 
7 #pragma once
8 
9 #include <GAGenome.h>
10 #include <GAParameter.h>
11 #include <GAPopulation.h>
12 #include <GAStatistics.h>
13 #include <gaconfig.h>
14 #include <gaid.h>
15 
16 // When specifying parameters for a GAlib object, you can use the fullname (the
17 // name used in parameters data files) or the short name (the name typically
18 // used on the command line). When specifying parameters in your code you can
19 // use a string, or use the predefined macros below (kind of like using the
20 // resource/class names in Motif for you Xt jocks).
21 constexpr auto gaNnGenerations = "number_of_generations";
22 constexpr auto gaSNnGenerations = "ngen";
23 constexpr auto gaNpConvergence = "convergence_percentage";
24 constexpr auto gaSNpConvergence = "pconv";
25 constexpr auto gaNnConvergence = "generations_to_convergence";
26 constexpr auto gaSNnConvergence = "nconv";
27 constexpr auto gaNpCrossover = "crossover_probability";
28 constexpr auto gaSNpCrossover = "pcross";
29 constexpr auto gaNpMutation = "mutation_probability";
30 constexpr auto gaSNpMutation = "pmut";
31 constexpr auto gaNpopulationSize = "population_size";
32 constexpr auto gaSNpopulationSize = "popsize";
33 constexpr auto gaNnPopulations = "number_of_populations";
34 constexpr auto gaSNnPopulations = "npop";
35 constexpr auto gaNpReplacement = "replacement_percentage";
36 constexpr auto gaSNpReplacement = "prepl";
37 constexpr auto gaNnReplacement = "replacement_number";
38 constexpr auto gaSNnReplacement = "nrepl";
39 constexpr auto gaNnBestGenomes = "number_of_best";
40 constexpr auto gaSNnBestGenomes = "nbest";
41 constexpr auto gaNscoreFrequency = "score_frequency";
42 constexpr auto gaSNscoreFrequency = "sfreq";
43 constexpr auto gaNflushFrequency = "flush_frequency";
44 constexpr auto gaSNflushFrequency = "ffreq";
45 constexpr auto gaNscoreFilename = "score_filename";
46 constexpr auto gaSNscoreFilename = "sfile";
47 constexpr auto gaNselectScores = "select_scores";
48 constexpr auto gaSNselectScores = "sscores";
49 constexpr auto gaNelitism = "elitism";
50 constexpr auto gaSNelitism = "el";
51 constexpr auto gaNnOffspring = "number_of_offspring";
52 constexpr auto gaSNnOffspring = "noffspr";
53 constexpr auto gaNrecordDiversity = "record_diversity";
54 constexpr auto gaSNrecordDiversity = "recdiv";
55 constexpr auto gaNpMigration = "migration_percentage";
56 constexpr auto gaSNpMigration = "pmig";
57 constexpr auto gaNnMigration = "migration_number";
58 constexpr auto gaSNnMigration = "nmig";
59 constexpr auto gaNminimaxi = "minimaxi";
60 constexpr auto gaSNminimaxi = "mm";
61 constexpr auto gaNseed = "seed";
62 constexpr auto gaSNseed = "seed";
63 
64 extern int gaDefNumGen;
65 extern float gaDefPConv;
66 extern int gaDefNConv;
67 extern float gaDefPMut;
68 extern float gaDefPCross;
69 extern int gaDefPopSize;
70 extern int gaDefNPop;
71 extern float gaDefPRepl;
72 extern int gaDefNRepl;
73 extern int gaDefNumOff;
74 extern float gaDefPMig;
75 extern int gaDefNMig;
76 extern int gaDefSelectScores;
77 extern int gaDefMiniMaxi;
78 extern bool gaDefDivFlag;
79 extern bool gaDefElitism;
80 extern int gaDefSeed;
81 
82 
88 class GAGeneticAlgorithm : public GAID
89 {
90  public:
91  GADefineIdentity("GAIncrementalGA", GAID::BaseGA);
92 
93  using Terminator = bool (*)(GAGeneticAlgorithm &);
94 
95  enum
96  {
97  MINIMIZE = -1,
98  MAXIMIZE = 1
99  };
100 
101  static GAParameterList &registerDefaultParameters(GAParameterList &);
102 
103  static bool TerminateUponGeneration(GAGeneticAlgorithm &);
104  static bool TerminateUponConvergence(GAGeneticAlgorithm &);
105  static bool TerminateUponPopConvergence(GAGeneticAlgorithm &);
106 
107  public:
108  explicit GAGeneticAlgorithm(const GAGenome &);
109  explicit GAGeneticAlgorithm(const GAPopulation &);
111  ~GAGeneticAlgorithm() override;
112  virtual void copy(const GAGeneticAlgorithm &);
113 
117  bool done() { return (*cf)(*this); }
118 
124  virtual void initialize(unsigned int seed = 0) = 0;
125 
133  virtual void step() = 0;
134 
140  virtual void evolve(unsigned int seed = 0)
141  {
142  initialize(seed);
143  while (!done())
144  {
145  step();
146  }
147  if (stats.flushFrequency() > 0)
148  {
149  stats.flushScores();
150  }
151  }
152  virtual int write(const char *) const { return 0; }
153  virtual int write(std::ostream &) const { return 0; }
154  virtual int read(const char *) { return 0; }
155  virtual int read(std::istream &) { return 0; }
156 
157  void *userData() const { return ud; }
158  void *userData(void *d) { return ud = d; }
159  Terminator terminator() const { return cf; }
160  Terminator terminator(Terminator f) { return cf = f; }
161 
162  const GAParameterList &parameters() const { return params; }
163  const GAParameterList &parameters(const GAParameterList &);
164  const GAParameterList &parameters(int &, char **, bool flag = false);
165  const GAParameterList &parameters(const std::string &filename, bool f = false);
166  const GAParameterList &parameters(std::istream &, bool flag = false);
167  virtual int get(const char *, void *) const;
168  virtual int setptr(const std::string &name, const void *);
169  int set(const std::string &s, int v) { return setptr(s, (void *)&v); }
170  int set(const std::string &s, unsigned int v) { return setptr(s, (void *)&v); }
171  int set(const std::string &s, char v) { return setptr(s, (void *)&v); }
172  int set(const std::string &s, const char *v) { return setptr(s, (void *)v); }
173  int set(const std::string &s, const void *v) { return setptr(s, v); }
174  int set(const std::string &name, double v);
175 
176  virtual int minimaxi() const { return minmax; }
177  virtual int minimaxi(int m);
178  int minimize() { return minimaxi(MINIMIZE); }
179  int maximize() { return minimaxi(MAXIMIZE); }
180  int nGenerations() const { return ngen; }
181  int nGenerations(unsigned int n)
182  {
183  params.set(gaNnGenerations, n);
184  return ngen = n;
185  }
186  int nConvergence() const { return nconv; }
187  int nConvergence(unsigned int n)
188  {
189  params.set(gaNnConvergence, n);
190  return nconv = stats.nConvergence(n);
191  }
192  float pConvergence() const { return pconv; }
193  float pConvergence(float p)
194  {
195  params.set(gaNpConvergence, p);
196  return pconv = p;
197  }
198  float pCrossover() const { return pcross; }
199  float pCrossover(float p)
200  {
201  params.set(gaNpCrossover, p);
202  return pcross = p;
203  }
204  float pMutation() const { return pmut; }
205  float pMutation(float p)
206  {
207  params.set(gaNpMutation, p);
208  return pmut = p;
209  }
210 
211  GAGenome::SexualCrossover crossover(GAGenome::SexualCrossover f)
212  {
213  return scross = f;
214  }
215  GAGenome::SexualCrossover sexual() const { return scross; }
216  GAGenome::AsexualCrossover crossover(GAGenome::AsexualCrossover f)
217  {
218  return across = f;
219  }
220  GAGenome::AsexualCrossover asexual() const { return across; }
221 
222  const GAStatistics &statistics() const { return stats; }
223  float convergence() const { return stats.convergence(); }
224  int generation() const { return stats.generation(); }
225  void flushScores()
226  {
227  if (stats.flushFrequency() > 0)
228  {
229  stats.flushScores();
230  }
231  }
232 
233  int scoreFrequency() const { return stats.scoreFrequency(); }
234  int scoreFrequency(unsigned int x)
235  {
236  params.set(gaNscoreFrequency, x);
237  return stats.scoreFrequency(x);
238  }
239  int flushFrequency() const { return stats.flushFrequency(); }
240  int flushFrequency(unsigned int x)
241  {
242  params.set(gaNflushFrequency, x);
243  return stats.flushFrequency(x);
244  }
245  std::string scoreFilename() const { return stats.scoreFilename(); }
246  std::string scoreFilename(const std::string &fn)
247  {
248  params.set(gaNscoreFilename, fn.c_str());
249  return stats.scoreFilename(fn);
250  }
251  int selectScores() { return stats.selectScores(); }
252  int selectScores(int w)
253  {
254  params.set(gaNselectScores, w);
255  return stats.selectScores(w);
256  }
257  bool recordDiversity() const { return stats.recordDiversity(); }
258  bool recordDiversity(bool f)
259  {
260  params.set(gaNrecordDiversity, static_cast<int>(f));
261  return stats.recordDiversity(f);
262  }
263 
264  virtual const GAPopulation &population() const { return *pop; }
265  virtual const GAPopulation &population(const GAPopulation &);
266  virtual int populationSize() const { return pop->size(); }
267  virtual int populationSize(unsigned int value);
268  virtual int nBestGenomes() const { return stats.nBestGenomes(); }
269  virtual int nBestGenomes(unsigned int n)
270  {
271  params.set(gaNnBestGenomes, n);
272  return stats.nBestGenomes(pop->individual(0), n);
273  }
274 
275  virtual GAScalingScheme &scaling() const { return pop->scaling(); }
276  virtual GAScalingScheme &scaling(const GAScalingScheme &s)
277  {
278  return pop->scaling(s);
279  }
280  virtual GASelectionScheme &selector() const { return pop->selector(); }
281  virtual GASelectionScheme &selector(const GASelectionScheme &s)
282  {
283  return pop->selector(s);
284  }
285  virtual void objectiveFunction(GAGenome::Evaluator f);
286  virtual void objectiveData(const GAEvalData &v);
287 
288  protected:
289  GAStatistics stats;
290  GAParameterList params;
291  GAPopulation *pop;
292 
294  Terminator cf;
295 
297  void *ud;
298 
299  int d_seed;
300  unsigned int ngen;
301  unsigned int nconv;
302  float pconv;
303  float pcross;
304  float pmut;
305  int minmax;
306 
308  GAGenome::SexualCrossover scross;
309 
311  GAGenome::AsexualCrossover across;
312 };
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
virtual void initialize(unsigned int seed=0)=0
Undefined for the base class.
GAGenome::AsexualCrossover across
asexual crossover to use
Definition: GABaseGA.h:311
GAGenome::SexualCrossover scross
sexual crossover to use
Definition: GABaseGA.h:308
virtual void step()=0
Evolve by one generation.
void * ud
pointer to user data structure
Definition: GABaseGA.h:297
Terminator cf
function for determining done-ness
Definition: GABaseGA.h:294
bool done()
Calls the completion measure routine to tell whether or not the GA is done.
Definition: GABaseGA.h:117
virtual void evolve(unsigned int seed=0)
This method is provided as a convenience so that you don't have to increment the GA generation-by-gen...
Definition: GABaseGA.h:140
The base genome class just defines the genome interface - how to mutate, crossover,...
Definition: GAGenome.h:200
This defines the identifiers for polymorphic classes.
Definition: gaid.h:20
Parameter List.
Definition: GAParameter.h:83
Definition: GAPopulation.h:66
Definition: GAScaling.h:46
Definition: GASelector.h:55
Statistics class.
Definition: GAStatistics.h:30