|
Grail (C++)
1.4.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
|
3 #ifndef GRAIL_EA_OPTIMIZER_H
4 #define GRAIL_EA_OPTIMIZER_H
6 #include "Crossover.hh"
7 #include "Individual.hh"
9 #include "Selection.hh"
32 enum class ElitismType
61 EAOptimizer(std::vector<std::unique_ptr<Individual>>& initialPopulation,
63 std::unique_ptr<Crossover> crossover = std::make_unique<Crossover>(),
64 std::unique_ptr<Mutation> mutation = std::make_unique<Mutation>(),
65 std::unique_ptr<Selection> selection = std::make_unique<Selection>(),
66 std::mt19937_64::result_type seed = std::random_device{}());
73 std::function<
void(
void)> onFinished =
nullptr,
74 std::function<
void(
void)> onNewEpoch =
nullptr);
82 std::vector<std::unique_ptr<Individual>>
GetPopulation()
const;
88 void SetElitism(
double rate, ElitismType type);
155 void DeserializeSimpleValue(std::ifstream& fileStream, T& value)
158 std::getline(fileStream, line);
159 std::istringstream stringStreamLine(line);
160 stringStreamLine >> std::skipws >> line >> value;
163 void AddPopulation(std::vector<std::unique_ptr<Individual>>& destination,
164 std::vector<std::unique_ptr<Individual>>& source)
const;
165 void SelectPopulation();
166 void CrossoverPopulation();
167 void MutatePopulation();
169 float CalculateAverageDiversity(
const std::vector<std::unique_ptr<Individual>>& population)
const;
170 size_t CalculateIdenticalFront(
const std::vector<std::unique_ptr<Individual>>& population)
const;
173 Individual* FindFirstNotUpdated(
const std::vector<std::unique_ptr<Individual>>& population)
const;
175 std::vector<std::unique_ptr<Individual>> corePopulation{};
176 std::vector<std::unique_ptr<Individual>> emptyPopulation{};
178 size_t epochNumber = 0;
180 size_t populationCount = 0;
181 bool evaluationInProgress =
false;
183 double elitismRate = 0.2;
184 ElitismType elitismType = ElitismType::ROULETTE;
186 std::unique_ptr<Crossover> crossover;
187 std::unique_ptr<Mutation> mutation;
188 std::unique_ptr<Selection> selection;
190 std::mt19937_64 randGen;
194 #endif // GRAIL_EA_OPTIMIZER_H
Mutation & GetMutation() const
GetMutation - gets the mutation instance that is used by this evolutionary algorithm.
Definition: EAOptimizer.cpp:185
The Crossover structure that encapsulates the crossover operation and its configuration in evolutiona...
Definition: Crossover.hh:40
std::vector< std::unique_ptr< Individual > > GetPopulation() const
Definition: EAOptimizer.cpp:111
double GetElitismRate() const
Definition: EAOptimizer.cpp:131
Selection & GetSelection() const
GetSelection - gets the selection instance that is used by this evolutionary algorithm.
Definition: EAOptimizer.cpp:190
void SerializeState(const std::string &filename) const
Serializes the inner state of the evolutionary algorithm, including the current population....
Definition: EAOptimizer.cpp:195
ElitismType GetElitismType() const
Definition: EAOptimizer.cpp:136
Crossover & GetCrossover() const
GetCrossover - gets the crossover instance that is used by this evolutionary algorithm.
Definition: EAOptimizer.cpp:180
const Individual & GetBestIndividual() const
GetBestIndividual - gets the individual with the highest fitness value from the population.
Definition: EAOptimizer.cpp:156
float CalculatePopulationIdenticalFrontRatio() const
Returns the number of the most frequent identical individuals divided by the population size.
Definition: EAOptimizer.cpp:151
Represents an individual for evolutionary algorithms (EA). It stores the encoding consisting of opti...
Definition: Individual.hh:23
size_t CalculatePopulationIdenticalFrontCount() const
Finds the most frequent individuals (which are identical to each other) and returns the number of the...
Definition: EAOptimizer.cpp:146
void RunInteractive(std::function< void(Individual &individualToEvaluate)> onEvaluateNeeded, std::function< void(void)> onFinished=nullptr, std::function< void(void)> onNewEpoch=nullptr)
Advances the computations of the evolutionary algorithm (EA). This is the main method to run it....
Definition: EAOptimizer.cpp:49
float CalculatePopulationDiversity() const
Returns the diversity ratio as a number [0,1]. The lower the value, the population is more homogeneou...
Definition: EAOptimizer.cpp:141
The Mutation structure that encapsulates the mutation operation and its configuration in evolutionary...
Definition: Mutation.hh:20
bool DeserializeState(const std::string &filename)
Loads a previously serialized state of the evolutionary algorithm. You can, for instance,...
Definition: EAOptimizer.cpp:223
size_t GetPopulationSize() const
PopulationSize - returns the number of individuals in the current population.
Definition: EAOptimizer.cpp:106
void SetElitism(double rate, ElitismType type)
SetElitism - sets how many individuals advance to the next generation in an unmodified form and how t...
Definition: EAOptimizer.cpp:125
The Selection class encapsulates the selection operation, i.e., how individuals are selected from the...
Definition: Selection.hh:19
size_t GetEpochNumber() const
Definition: EAOptimizer.cpp:170
EAOptimizer(std::vector< std::unique_ptr< Individual >> &initialPopulation, size_t maxEpochCount, std::unique_ptr< Crossover > crossover=std::make_unique< Crossover >(), std::unique_ptr< Mutation > mutation=std::make_unique< Mutation >(), std::unique_ptr< Selection > selection=std::make_unique< Selection >(), std::mt19937_64::result_type seed=std::random_device{}())
The EA algorithm will maintain the size of the initial population after the selection phase.
Definition: EAOptimizer.cpp:19
size_t GetMaxEpochCount() const
Definition: EAOptimizer.cpp:175
A class that serves as the main interface for evolutionary algorithms in Grail.
Definition: EAOptimizer.hh:49
friend std::ostream & operator<<(std::ostream &out, const EAOptimizer &eaOptimizer)
Appends to stream the serialized state of the evolutionary algorithm including the whole population a...
Definition: EAOptimizer.cpp:271