Modernized GAlib  3.0.0 current
GASimpleGA.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 "GABaseGA.h"
10 
16 {
17  public:
18  GADefineIdentity("GASimpleGA", GAID::SimpleGA);
19 
20  static GAParameterList &registerDefaultParameters(GAParameterList &);
21 
22  public:
23  explicit GASimpleGA(const GAGenome &);
24  explicit GASimpleGA(const GAPopulation &);
25  GASimpleGA(const GASimpleGA &);
26  GASimpleGA &operator=(const GASimpleGA &);
27  ~GASimpleGA() override;
28  void copy(const GAGeneticAlgorithm &) override;
29 
30  void initialize(unsigned int seed = 0) override;
31  void step() override;
32  GASimpleGA &operator++()
33  {
34  step();
35  return *this;
36  }
37 
38  int setptr(const std::string &name, const void *value) override;
39  int get(const char *name, void *value) const override;
40 
41  bool elitist() const { return el; }
42  bool elitist(bool flag)
43  {
44  params.set(gaNelitism, static_cast<int>(flag));
45  return el = flag;
46  }
47 
48  int minimaxi() const override { return minmax; }
49  int minimaxi(int m) override;
50 
51  const GAPopulation &population() const override { return *pop; }
52  const GAPopulation &population(const GAPopulation &) override;
53  int populationSize() const override { return pop->size(); }
54  int populationSize(unsigned int n) override;
55  GAScalingScheme &scaling() const override { return pop->scaling(); }
56  GAScalingScheme &scaling(const GAScalingScheme &s) override
57  {
58  oldPop->scaling(s);
59  return GAGeneticAlgorithm::scaling(s);
60  }
61  GASelectionScheme &selector() const override { return pop->selector(); }
62  GASelectionScheme &selector(const GASelectionScheme &s) override
63  {
64  oldPop->selector(s);
65  return GAGeneticAlgorithm::selector(s);
66  }
67  void objectiveFunction(GAGenome::Evaluator f) override;
68  void objectiveData(const GAEvalData &v) override;
69 
70  protected:
71  GAPopulation *oldPop; // current and old populations
72  bool el; // are we elitist?
73 };
74 
75 inline std::ostream &operator<<(std::ostream &os, GASimpleGA &arg)
76 {
77  arg.write(os);
78  return (os);
79 }
80 
81 inline std::istream &operator>>(std::istream &is, GASimpleGA &arg)
82 {
83  arg.read(is);
84  return (is);
85 }
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
Simple genetic algorithm class.
Definition: GASimpleGA.h:16
void step() override
Evolve by one generation.
void initialize(unsigned int seed=0) override
Undefined for the base class.