SmartEngine  1.6.0
Public Member Functions | List of all members
SmartEngine::IGeneticTrainer Class Referenceabstract

Trains a NeuronLayer / NeuralNetwork using a genetic algorithm. With each step, networks are sorted by loss. The top networks are left unchanged, while the bottom networks are replaced by new ones. The new networks are formed by mixing the weights of the top networks and randomly mutating weights / neurons. Lastly, the bottom percent of the networks can be replaced with completely random weights, adding some variability to the pool. More...

#include <GeneticTrainer.h>

Inheritance diagram for SmartEngine::IGeneticTrainer:
SmartEngine::ILossTrainer SmartEngine::IObject SmartEngine::IResource SmartEngine::IObject SmartEngine::ISerializable SmartEngine::IObject SmartEngine::IGeneticAgentTrainer

Public Member Functions

virtual void Initialize (const GeneticTrainerInitializationInfo &info)=0
 Initializes the newtwork with a new population. Not necessary to call if deserializing from disk. More...
 
virtual void Step ()=0
 Steps the trainer, making the population learn through gene swapping and mutation. More...
 
virtual int GetPopulationCount () const =0
 Retrieves the total number of chromosomes (test subjects) in the population. More...
 
virtual void SortPopulation ()=0
 Sorts the population by loss in ascending order More...
 
virtual float GetMutationStandardDeviation () const =0
 Returns the current mutation standard deviation, taking into account adaptive behavior More...
 
virtual int GetChromosomeLastIndex (int index) const =0
 Debug method to help visualize how chromosomes are progressing through generations More...
 
virtual void SetChromosome (int index)=0
 Replaces the networks specified in the constructor with the specified chromosome index. Call this before evaluating a chromosome for its performance. More...
 
virtual void SetBestChromosome ()=0
 Sorts chromosomes by loss and then sets the networks specified in the constructor to the best chromosome. More...
 
virtual void CopyOutChromosome (int index, IGraph *graph)=0
 Copies out the specified chromosome to a graph. More...
 
virtual void CopyOutBestChromosome (IGraph *graph)=0
 Sorts chromosomes by loss and then copies out the best chromosome to the specified network list. More...
 
virtual void CopyOutTopAverage (float count, IGraph *graph, bool weightedAverage)=0
 Averages the top chromosome weights and copies the result to specified graph More...
 
virtual float GetLoss (int index)=0
 Returns the loss of the specified chromosome More...
 
virtual void SetLoss (int index, float loss)=0
 Sets the loss of the specified chromosome. This will replace the loss from the optionally specified Loss structure passed into the constructor. More...
 
virtual void SetTrainingInfo (const GeneticTrainingInfo &info)=0
 Sets the genetic parameters of the trainer. More...
 
virtual float GetLoss ()=0
 Returns the loss in the graph. More...
 
- Public Member Functions inherited from SmartEngine::ILossTrainer
virtual void SetTrainingMethod (const LossTrainingMethodInfo &info)=0
 Sets the training method and associated parameters More...
 
virtual uint32 GetGenerationCount () const =0
 Returns the number of generations we have been training for. More...
 
- Public Member Functions inherited from SmartEngine::IObject
virtual ObjectId GetId () const =0
 Returns the ID of this object. More...
 
virtual void AddRef () const =0
 Increments the internal reference count on this object. It is not common to use this method directly. More...
 
virtual void Release () const =0
 Decrements the internal reference count on this object. It is not common to use this method directly. More...
 
virtual int GetRefCount () const =0
 Returns the number of references to this object. More...
 
virtual void * QueryInterface (ObjectClassId id)=0
 Queries the object for an interface and returns a pointer to that interface if found. More...
 
void operator= (IObject const &x)=delete
 
- Public Member Functions inherited from SmartEngine::IResource
virtual const char * GetResourceName () const =0
 Returns the name of this resource passed to the constructor. More...
 
virtual SerializationResult GetLastLoadResult () const =0
 Returns the result of the last call to Load(). Useful for checking loaded data state after creation. More...
 
virtual SerializationResult Load (const char *appendName=nullptr)=0
 Load this object from disk. More...
 
virtual SerializationResult Save (const char *appendName=nullptr)=0
 Save this object to disk. More...
 
- Public Member Functions inherited from SmartEngine::ISerializable
virtual SerializationResult Serialize (IMemoryBuffer *buffer)=0
 Write the contents of this object to a buffer. More...
 
virtual SerializationResult Deserialize (IMemoryBuffer *buffer)=0
 Fill this object with contents from a buffer. More...
 

Additional Inherited Members

- Public Attributes inherited from SmartEngine::IObject
 private
 
 __pad0__: IObject() {} IObject(IObject const&) = delete
 

Detailed Description

Trains a NeuronLayer / NeuralNetwork using a genetic algorithm. With each step, networks are sorted by loss. The top networks are left unchanged, while the bottom networks are replaced by new ones. The new networks are formed by mixing the weights of the top networks and randomly mutating weights / neurons. Lastly, the bottom percent of the networks can be replaced with completely random weights, adding some variability to the pool.

Individual chromosome loss can either be set manually or pulled automatically from a Loss instance. Manually setting the loss is necessary when you don't know what the output of the network should be; you only know how the newtork performs relative to each other.

Member Function Documentation

◆ CopyOutBestChromosome()

virtual void SmartEngine::IGeneticTrainer::CopyOutBestChromosome ( IGraph graph)
pure virtual

Sorts chromosomes by loss and then copies out the best chromosome to the specified network list.

Parameters
graphThe graph must be identical in structure and order to the one specified in the constructor

◆ CopyOutChromosome()

virtual void SmartEngine::IGeneticTrainer::CopyOutChromosome ( int  index,
IGraph graph 
)
pure virtual

Copies out the specified chromosome to a graph.

Parameters
index
graphThe graph must be identical in structure and order to the one specified in the constructor

◆ CopyOutTopAverage()

virtual void SmartEngine::IGeneticTrainer::CopyOutTopAverage ( float  count,
IGraph graph,
bool  weightedAverage 
)
pure virtual

Averages the top chromosome weights and copies the result to specified graph

Parameters
count(-Population Count, 1.0] If positive, the value acts as a percentage of the population. If negative, it acts as an exact integer value.
weightedAverageIf true, the first nodes will have more weight than the last nodes averaged.

◆ GetChromosomeLastIndex()

virtual int SmartEngine::IGeneticTrainer::GetChromosomeLastIndex ( int  index) const
pure virtual

Debug method to help visualize how chromosomes are progressing through generations

Parameters
index
Returns

◆ GetLoss() [1/2]

virtual float SmartEngine::ILossTrainer::GetLoss
virtual

Returns the loss in the graph.

Implements SmartEngine::ILossTrainer.

◆ GetLoss() [2/2]

virtual float SmartEngine::IGeneticTrainer::GetLoss ( int  index)
pure virtual

Returns the loss of the specified chromosome

Parameters
index
Returns

◆ GetMutationStandardDeviation()

virtual float SmartEngine::IGeneticTrainer::GetMutationStandardDeviation ( ) const
pure virtual

Returns the current mutation standard deviation, taking into account adaptive behavior

◆ GetPopulationCount()

virtual int SmartEngine::IGeneticTrainer::GetPopulationCount ( ) const
pure virtual

Retrieves the total number of chromosomes (test subjects) in the population.

◆ Initialize()

virtual void SmartEngine::IGeneticTrainer::Initialize ( const GeneticTrainerInitializationInfo info)
pure virtual

Initializes the newtwork with a new population. Not necessary to call if deserializing from disk.

Parameters
info

◆ SetBestChromosome()

virtual void SmartEngine::IGeneticTrainer::SetBestChromosome ( )
pure virtual

Sorts chromosomes by loss and then sets the networks specified in the constructor to the best chromosome.

◆ SetChromosome()

virtual void SmartEngine::IGeneticTrainer::SetChromosome ( int  index)
pure virtual

Replaces the networks specified in the constructor with the specified chromosome index. Call this before evaluating a chromosome for its performance.

Chromosomes are not sorted prior to being set.

Parameters
index

◆ SetLoss()

virtual void SmartEngine::IGeneticTrainer::SetLoss ( int  index,
float  loss 
)
pure virtual

Sets the loss of the specified chromosome. This will replace the loss from the optionally specified Loss structure passed into the constructor.

Parameters
index
loss

◆ SetTrainingInfo()

virtual void SmartEngine::IGeneticTrainer::SetTrainingInfo ( const GeneticTrainingInfo info)
pure virtual

Sets the genetic parameters of the trainer.

Not necessary to call when deserializing from disk

Parameters
info

◆ SortPopulation()

virtual void SmartEngine::IGeneticTrainer::SortPopulation ( )
pure virtual

Sorts the population by loss in ascending order

◆ Step()

virtual void SmartEngine::IGeneticTrainer::Step ( )
pure virtual

Steps the trainer, making the population learn through gene swapping and mutation.