Modernized GAlib  3.0.0 current
GAIncGA.h
1 // $Header$
2 /* ----------------------------------------------------------------------------
3  gainc.h
4  mbwall 28jul94
5  Copyright (c) 1995 Massachusetts Institute of Technology
6  all rights reserved
7 
8  Header file for the incremental genetic algorithm class.
9 
10 replacement
11  The replacement strategy defines how the new children will be stuck into the
12 population. If you want the new child to replace one of its parents, use
13 the Parent strategy. If you want the child to replace a random population
14 member, use the Random strategy. If you want the child to replace the worst
15 population member, use the Worst strategy. These are meaningful only for
16 overlapping populations. To do DeJong-style speciation (crowding), use the
17 Crowding strategy. You must also specify a crowding function as the
18 replacement function if you choose this strategy. If you use Custom as the
19 replacement strategy you must also specify a replacement function.
20  Note that not every replacement scheme can be used with every type of
21 genetic algorithm. If a GA supports replacement schemes, it will specify
22 which schemes are valid and which are not.
23  The replacement function is required for crowding and custom replacement
24 strategies. This function is used to pick which genome will be
25 replaced. The first argument passed to the replacement function is the
26 individual that is supposed to go into the population. The second argument
27 is the population into which the individual is supposed to go.
28 The replacement function should return a reference to the genome that the
29 individual should replace. If no replacement should take place, the
30 replacement function should return a reference to the individual.
31 ---------------------------------------------------------------------------- */
32 #ifndef _ga_gainc_h_
33 #define _ga_gainc_h_
34 
35 #include <GABaseGA.h>
36 
38 public:
39  GADefineIdentity("GAIncrementalGA", GAID::IncrementalGA);
40 
41  using ReplacementFunction = GAGenome &(*)(GAGenome &, GAPopulation &);
42 
43  enum ReplacementScheme {
44  RANDOM = GAPopulation::RANDOM,
45  BEST = GAPopulation::BEST,
46  WORST = GAPopulation::WORST,
47  CUSTOM = -30,
48  CROWDING = -30,
49  PARENT = -10
50  };
51 
52  static GAParameterList& registerDefaultParameters(GAParameterList&);
53 
54 public:
55  explicit GAIncrementalGA(const GAGenome&);
56  explicit GAIncrementalGA(const GAPopulation&);
58  GAIncrementalGA& operator=(const GAIncrementalGA&);
59  ~GAIncrementalGA() override;
60  void copy(const GAGeneticAlgorithm &) override;
61 
62  void initialize(unsigned int seed=0) override;
63  void step() override;
64  GAIncrementalGA & operator++() { step(); return *this; }
65 
66  int setptr(const std::string &name, const void* value) override;
67  int get(const char* name, void* value) const override;
68 
69  void objectiveFunction(GAGenome::Evaluator f) override;
70  void objectiveData(const GAEvalData& v) override;
71 
72  int nOffspring() const {return noffspr;}
73  int nOffspring(unsigned int);
74 
75  ReplacementScheme replacement() const {return rs;}
76  ReplacementScheme replacement(ReplacementScheme, ReplacementFunction f=nullptr);
77 
78 protected:
79  GAGenome *child1, *child2; // children that will be generated each gen
80  ReplacementScheme rs; // replacement strategy
81  ReplacementFunction rf; // (optional) replacement function
82  unsigned int noffspr; // number of children to generate in crossover
83 };
84 
85 
86 
87 inline std::ostream & operator<< (std::ostream & os, GAIncrementalGA & arg)
88 { arg.write(os); return(os); }
89 inline std::istream & operator>> (std::istream & is, GAIncrementalGA & arg)
90 { arg.read(is); return(is); }
91 
92 #endif
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
Definition: GAIncGA.h:37
void step() override
Evolve by one generation.
void initialize(unsigned int seed=0) override
Undefined for the base class.
Parameter List.
Definition: GAParameter.h:83
Definition: GAPopulation.h:66