Grail (C++)  1.4.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
Mutation.hh
1 // Copyright QED Software 2023.
2 
3 #ifndef GRAIL_MUTATION_H
4 #define GRAIL_MUTATION_H
5 
6 #include <memory>
7 #include <random>
8 #include <vector>
9 
10 namespace grail
11 {
12 namespace evolution
13 {
14  class Individual;
15  class BaseEvoParam;
16 
20  struct Mutation
21  {
22  friend class EAOptimizer;
23 
24  public:
29  {
39  };
40 
50  Mutation(double mutationPhaseRate = 1.0,
51  double mutateIndividualRate = 0.09,
52  double mutateGeneRate = 0.5,
53  bool mutateBothParentsAndChildren = true,
54  MutationSelectionType randomResolutionTypeForIndividual = MutationSelectionType::SELECT_P_OF_N,
55  MutationSelectionType randomResolutionTypeForGene = MutationSelectionType::SELECT_P_OF_N,
56  std::mt19937_64::result_type seed = std::random_device{}());
57 
58  virtual ~Mutation();
59 
65  virtual void Perform(const std::vector<std::unique_ptr<Individual>>& parentPopulation,
66  const std::vector<std::unique_ptr<Individual>>& childrenPopulation);
67 
69  double MutationPhaseRate = 1.0;
70 
73  double MutateIndividualRate = 0.09;
74 
77  double MutateGeneRate = 0.5;
78 
81 
84 
87 
88  protected:
91  virtual void MutateIndividual(Individual& individual);
92 
96  virtual void MutateGene(Individual& individual, BaseEvoParam& gene);
97 
98  protected:
99  std::mt19937_64 randGen;
100 
101  private:
103  std::vector<std::unique_ptr<Individual>> populationAfter{};
104  };
105 }
106 }
107 
108 #endif
grail::evolution::BaseEvoParam
BaseEvoParam - A class that represents a parameter that is modifiable (optimizable) by evolutionary a...
Definition: EvoParam.hh:20
grail::evolution::Mutation::MutateGene
virtual void MutateGene(Individual &individual, BaseEvoParam &gene)
The function that performs the mutation operation of a single parameter within the mutated individual...
Definition: Mutation.cpp:124
grail::evolution::Mutation::MutationSelectionType::ITERATE_AND_TEST_P
@ ITERATE_AND_TEST_P
ITERATE_AND_TEST_P - a random test whether to mutate an individual will be performed for each individ...
grail::evolution::Mutation::MutationSelectionType
MutationSelectionType
Predefined ways in which individuals are selected from the population for mutation....
Definition: Mutation.hh:28
grail::evolution::Mutation::Perform
virtual void Perform(const std::vector< std::unique_ptr< Individual >> &parentPopulation, const std::vector< std::unique_ptr< Individual >> &childrenPopulation)
Perform - Attempts to mutate the entire population. The umber of individuals mutates is based on muta...
Definition: Mutation.cpp:31
grail::evolution::Mutation::Mutation
Mutation(double mutationPhaseRate=1.0, double mutateIndividualRate=0.09, double mutateGeneRate=0.5, bool mutateBothParentsAndChildren=true, MutationSelectionType randomResolutionTypeForIndividual=MutationSelectionType::SELECT_P_OF_N, MutationSelectionType randomResolutionTypeForGene=MutationSelectionType::SELECT_P_OF_N, std::mt19937_64::result_type seed=std::random_device{}())
Mutation - Constructor.
Definition: Mutation.cpp:10
grail::evolution::Mutation::MutateBothParentsAndChildren
bool MutateBothParentsAndChildren
If true: both the previous population and children can be mutated; if false: only the individuals aft...
Definition: Mutation.hh:80
grail::evolution::Mutation::MutationSelectionType::SELECT_P_OF_N
@ SELECT_P_OF_N
SELECT_P_OF_N - upfront, the PopulationSize*MutationIndividualRate of individuals will be randomly ch...
grail::evolution::Mutation::MutateGeneRate
double MutateGeneRate
Probability that a gene of an individual will be considered for mutation [if (randomResolutionTypeFor...
Definition: Mutation.hh:77
grail::evolution::Mutation::RandomResolutionTypeForIndividual
MutationSelectionType RandomResolutionTypeForIndividual
Specifies how individuals for mutations are chosen. See MutationSelectionType enum.
Definition: Mutation.hh:83
grail::evolution::Individual
Represents an individual for evolutionary algorithms (EA). It stores the encoding consisting of opti...
Definition: Individual.hh:23
grail::evolution::Mutation
The Mutation structure that encapsulates the mutation operation and its configuration in evolutionary...
Definition: Mutation.hh:20
grail::evolution::Mutation::MutateIndividualRate
double MutateIndividualRate
Probability that an individual will be considered for mutation [if (randomResolutionTypeForIndividual...
Definition: Mutation.hh:73
grail::evolution::Mutation::MutationPhaseRate
double MutationPhaseRate
Probability of a mutation phase to happen (globally). If a mutation is included in the algorithm,...
Definition: Mutation.hh:69
grail::evolution::Mutation::MutateIndividual
virtual void MutateIndividual(Individual &individual)
The function that performs the mutation operation of one individual.
Definition: Mutation.cpp:94
grail::evolution::Mutation::RandomResolutionTypeForGene
MutationSelectionType RandomResolutionTypeForGene
Specifies how genes in an individual (already chosen for mutation) are chosen for mutation....
Definition: Mutation.hh:86
grail::evolution::EAOptimizer
A class that serves as the main interface for evolutionary algorithms in Grail.
Definition: EAOptimizer.hh:49