SmartEngine  1.6.0
GeneticTrainer.h
1 // Copyright (C) Entropy Software LLC - All Rights Reserved
2 
3 #pragma once
4 
5 #include "Graph.h"
6 #include "Loss.h"
7 #include "LossTrainer.h"
8 
9 namespace SmartEngine
10 {
11 
12 class IGeneticTrainer;
13 
14 #pragma pack(push, 4)
15 struct GeneticTrainerCInfo : public LossTrainerCInfo
19 {
23  ILoss* loss = nullptr;
24 };
25 
30 {
34  int populationCount = 20;
35 
41  bool randomizePopulation = true;
42 };
43 
44 enum class MutationTarget : byte
45 {
49  Auto,
50 
54  Weight,
55 
59  Neuron,
60 };
61 
62 enum class GeneSwapTarget : byte
63 {
67  Weight,
68 
72  Neuron,
73 };
74 
79 {
83  MutationTarget target = MutationTarget::Auto;
84 
94  float nodesToMutate = -1.0f;
95 
104  float targetsToMutate = 0.08f;
105 
110  float standardDeviation = 1.0f;
111 
116  float minStandardDeviation = 1e-3f;
117 
125 
132 
139 };
140 
145 {
150 
158  float countCrossover = 0.4f;
159 
166 };
167 
172 {
181  float countTopPerformers = 0.1f;
182 
190  float countToKeepUnchanged = 0.4f;
191 
199  float countToRandomize = 0.1f;
200 
205 
210 };
211 #pragma pack(pop)
212 
225 class SMARTENGINE_EXPORT IGeneticTrainer : public ILossTrainer
226 {
227 public:
228  SMARTENGINE_DECLARE_CLASS(IGeneticTrainer)
229 
230  using ILossTrainer::GetLoss;
231 
237  virtual void Initialize(const GeneticTrainerInitializationInfo& info) = 0;
238 
242  virtual void Step() = 0;
243 
247  virtual int GetPopulationCount() const = 0;
248 
252  virtual void SortPopulation() = 0;
253 
257  virtual float GetMutationStandardDeviation() const = 0;
258 
264  virtual int GetChromosomeLastIndex(int index) const = 0;
265 
272  virtual void SetChromosome(int index) = 0;
273 
278  virtual void SetBestChromosome() = 0;
279 
287  virtual void CopyOutChromosome(int index, IGraph* graph) = 0;
288 
295  virtual void CopyOutBestChromosome(IGraph* graph) = 0;
296 
307  virtual void CopyOutTopAverage(float count, IGraph* graph, bool weightedAverage) = 0;
308 
314  virtual float GetLoss(int index) = 0;
315 
322  virtual void SetLoss(int index, float loss) = 0;
323 
329  virtual void SetTrainingInfo(const GeneticTrainingInfo& info) = 0;
330 };
331 
336 
338 extern "C"
339 {
340  SMARTENGINE_EXPORT ObjPtr GeneticTrainer_CreateInstance(const GeneticTrainerCInfo& cinfo);
341  SMARTENGINE_EXPORT void GeneticTrainer_Initialize(ObjPtr object, const GeneticTrainerInitializationInfo& info);
342  SMARTENGINE_EXPORT void GeneticTrainer_Step(ObjPtr object);
343  SMARTENGINE_EXPORT void GeneticTrainer_SetTrainingInfo(ObjPtr object, const GeneticTrainingInfo& info);
344  SMARTENGINE_EXPORT int GeneticTrainer_GetPopulationCount(ObjPtr object);
345  SMARTENGINE_EXPORT void GeneticTrainer_SortPopulation(ObjPtr object);
346  SMARTENGINE_EXPORT float GeneticTrainer_GetMutationStandardDeviation(ObjPtr object);
347  SMARTENGINE_EXPORT int GeneticTrainer_GetChromosomeLastIndex(ObjPtr object, int index);
348  SMARTENGINE_EXPORT void GeneticTrainer_SetChromosome(ObjPtr object, int index);
349  SMARTENGINE_EXPORT void GeneticTrainer_SetBestChromosome(ObjPtr object);
350  SMARTENGINE_EXPORT void GeneticTrainer_CopyOutChromosome(ObjPtr object, int index, ObjPtr graph);
351  SMARTENGINE_EXPORT void GeneticTrainer_CopyOutBestChromosome(ObjPtr object, ObjPtr graph);
352  SMARTENGINE_EXPORT void GeneticTrainer_CopyOutTopAverage(ObjPtr object, float count, ObjPtr graph, int weightedAverage);
353  SMARTENGINE_EXPORT float GeneticTrainer_GetLoss(ObjPtr object, int index);
354  SMARTENGINE_EXPORT void GeneticTrainer_SetLoss(ObjPtr object, int index, float loss);
355 }
357 
358 } // namespace SmartEngine
SmartEngine::MutationTarget
MutationTarget
Definition: GeneticTrainer.h:45
SmartEngine::IGeneticTrainer::CopyOutBestChromosome
virtual void CopyOutBestChromosome(IGraph *graph)=0
Sorts chromosomes by loss and then copies out the best chromosome to the specified network list.
SmartEngine::IGeneticTrainer::GetLoss
virtual float GetLoss(int index)=0
Returns the loss of the specified chromosome
SmartEngine::CreateGeneticTrainer
SMARTENGINE_EXPORT ObjectPtr< IGeneticTrainer > CreateGeneticTrainer(const GeneticTrainerCInfo &cinfo)
Creates an instance of IGeneticTrainer
SmartEngine::IGeneticTrainer::GetChromosomeLastIndex
virtual int GetChromosomeLastIndex(int index) const =0
Debug method to help visualize how chromosomes are progressing through generations
SmartEngine::GeneticTrainerInitializationInfo
Initializes the GeneticTrainer with a new population
Definition: GeneticTrainer.h:30
SmartEngine::MutationTarget::Auto
@ Auto
Chooses the default value depending on the node type.
SmartEngine::GeneticTrainingInfo::countToKeepUnchanged
float countToKeepUnchanged
How many of the top chromosomes should be left unchanged during each step.
Definition: GeneticTrainer.h:190
SmartEngine::GeneSwapInfo::countCrossover
float countCrossover
The number of chromosome 2's weight will swap with chromosome 1
Definition: GeneticTrainer.h:158
SmartEngine::MutationInfo::stepsUntilLowerStandardDeviation
int stepsUntilLowerStandardDeviation
The number of steps that the best network is in the top unchanged percent before we lower the mutatio...
Definition: GeneticTrainer.h:131
SmartEngine::GeneSwapTarget::Weight
@ Weight
Swapping occurs at the individual weight level across all neurons.
SmartEngine::MutationInfo::targetsToMutate
float targetsToMutate
How many weights / neurons per node to mutate.
Definition: GeneticTrainer.h:104
SmartEngine::GeneSwapInfo::selectionStandardDeviation
float selectionStandardDeviation
Random parent chromosomes will be chosen to breed with this standard deviation. A lower random value ...
Definition: GeneticTrainer.h:165
SmartEngine::IGeneticTrainer::GetPopulationCount
virtual int GetPopulationCount() const =0
Retrieves the total number of chromosomes (test subjects) in the population.
SmartEngine::MutationInfo
GeneticTrainer mutation info
Definition: GeneticTrainer.h:79
SmartEngine::IGeneticTrainer::GetMutationStandardDeviation
virtual float GetMutationStandardDeviation() const =0
Returns the current mutation standard deviation, taking into account adaptive behavior
SmartEngine::IGeneticTrainer::CopyOutChromosome
virtual void CopyOutChromosome(int index, IGraph *graph)=0
Copies out the specified chromosome to a graph.
SmartEngine::IGeneticTrainer::SetTrainingInfo
virtual void SetTrainingInfo(const GeneticTrainingInfo &info)=0
Sets the genetic parameters of the trainer.
SmartEngine::MutationInfo::stepsUntilRaiseStandardDeviation
int stepsUntilRaiseStandardDeviation
The number of steps that the best network is not in the top unchanged percent before we raise the mut...
Definition: GeneticTrainer.h:138
SmartEngine::ObjectPtr
Smart pointer to an IObject. Automatic ref counting.
Definition: ObjectPtr.h:16
SmartEngine::GeneSwapInfo::target
GeneSwapTarget target
Specifies which weights will be swapped
Definition: GeneticTrainer.h:149
SmartEngine::IGeneticTrainer
Trains a NeuronLayer / NeuralNetwork using a genetic algorithm. With each step, networks are sorted b...
Definition: GeneticTrainer.h:226
SmartEngine
Definition: A2CTrainer.h:10
SmartEngine::IGeneticTrainer::SortPopulation
virtual void SortPopulation()=0
Sorts the population by loss in ascending order
SmartEngine::GeneticTrainerCInfo::loss
ILoss * loss
Optional loss structure to train against a known set of outputs
Definition: GeneticTrainer.h:23
SmartEngine::GeneSwapTarget
GeneSwapTarget
Definition: GeneticTrainer.h:63
SmartEngine::GeneticTrainerInitializationInfo::populationCount
int populationCount
Number of chromosomes in the population
Definition: GeneticTrainer.h:34
SmartEngine::ILossTrainer
Base class for NeuralNetwork loss trainers
Definition: LossTrainer.h:81
SmartEngine::GeneticTrainingInfo::mutation
MutationInfo mutation
Mutation info
Definition: GeneticTrainer.h:204
SmartEngine::GeneticTrainerCInfo
Data used to construct an IA2CTrainer instance
Definition: GeneticTrainer.h:19
SmartEngine::MutationInfo::standardDeviation
float standardDeviation
A random value of this standard deviation will be added to each weight selected for mutation.
Definition: GeneticTrainer.h:110
SmartEngine::IGeneticTrainer::Step
virtual void Step()=0
Steps the trainer, making the population learn through gene swapping and mutation.
SmartEngine::GeneticTrainingInfo
GeneticTrainer parameter info
Definition: GeneticTrainer.h:172
SmartEngine::IGraph
A graph is a collection of buffers and nodes that together form a neural network. The graph is create...
Definition: Graph.h:61
SmartEngine::GeneticTrainingInfo::countTopPerformers
float countTopPerformers
How many of the top chromosomes are marked as top performers. The mutation standard deviation will be...
Definition: GeneticTrainer.h:181
SmartEngine::IGeneticTrainer::Initialize
virtual void Initialize(const GeneticTrainerInitializationInfo &info)=0
Initializes the newtwork with a new population. Not necessary to call if deserializing from disk.
SmartEngine::IGeneticTrainer::CopyOutTopAverage
virtual void CopyOutTopAverage(float count, IGraph *graph, bool weightedAverage)=0
Averages the top chromosome weights and copies the result to specified graph
SmartEngine::MutationInfo::minStandardDeviation
float minStandardDeviation
The minimum standard deviation we are allowed to achieve when AdaptiveStandardDeviation is set to tru...
Definition: GeneticTrainer.h:116
SmartEngine::GeneticTrainerInitializationInfo::randomizePopulation
bool randomizePopulation
True if we should randomize the population upon initialization. False if each staring chromosome is a...
Definition: GeneticTrainer.h:41
SmartEngine::GeneticTrainingInfo::geneSwap
GeneSwapInfo geneSwap
Gene swap info
Definition: GeneticTrainer.h:209
SmartEngine::GeneticTrainingInfo::countToRandomize
float countToRandomize
How many of the bottom chromosomes should be completely randomized.
Definition: GeneticTrainer.h:199
SmartEngine::IGeneticTrainer::SetBestChromosome
virtual void SetBestChromosome()=0
Sorts chromosomes by loss and then sets the networks specified in the constructor to the best chromos...
SmartEngine::ILoss
The loss of a NeuralNetwork is computed using the formula (Expected Ouput - Actual Output)^2 The mean...
Definition: Loss.h:39
SmartEngine::MutationInfo::target
MutationTarget target
Specifies what weights to mutate.
Definition: GeneticTrainer.h:83
SmartEngine::IGeneticTrainer::SetChromosome
virtual void SetChromosome(int index)=0
Replaces the networks specified in the constructor with the specified chromosome index....
SmartEngine::GeneSwapInfo
GeneticTrainer gene swap info
Definition: GeneticTrainer.h:145
SmartEngine::MutationInfo::nodesToMutate
float nodesToMutate
How many trainable nodes in the graph we should mutate
Definition: GeneticTrainer.h:94
SmartEngine::ILossTrainer::GetLoss
virtual float GetLoss()=0
Returns the loss in the graph.
SmartEngine::MutationInfo::adaptiveStandardDeviation
bool adaptiveStandardDeviation
If set to true, the standard deviation will change with the learning rate. If the network isn't learn...
Definition: GeneticTrainer.h:124
SmartEngine::IGeneticTrainer::SetLoss
virtual void SetLoss(int index, float loss)=0
Sets the loss of the specified chromosome. This will replace the loss from the optionally specified L...