SmartEngine  1.6.0
GeneticAgentTrainer.h
1 // Copyright (C) Entropy Software LLC - All Rights Reserved
2 
3 #pragma once
4 
5 #include "Agent.h"
6 #include "Context.h"
7 #include "CuriosityModule.h"
8 #include "GeneticTrainer.h"
9 #include "Graph.h"
10 #include "Resource.h"
11 
12 namespace SmartEngine
13 {
14 
15 #pragma pack(push, 4)
16 enum class GeneticAgentScoringMethod : byte
20 {
24  Sum,
25 
29  Average,
30 };
31 
36 {
40  IContext* context = nullptr;
41 
45  IGraph* graph = nullptr;
46 
51 
55  const char* agentName = "";
56 
62  const char** policyNodeNames = nullptr;
63 
68 
73 
79 
83  float gamma = 0.99f;
84 
88  GeneticAgentScoringMethod scoringMethod = GeneticAgentScoringMethod::Sum;
89 };
90 #pragma pack(pop)
91 
97 class SMARTENGINE_EXPORT IGeneticAgentTrainer : public IGeneticTrainer, public IAgentFactory
98 {
99 public:
100  SMARTENGINE_DECLARE_CLASS(IGeneticAgentTrainer)
101 
102 
103  virtual void MapAgentToChromosome(IAgent* agent, int chromosomeIndex) = 0;
113 
120  virtual float GetSumRawRewards(int chromosomeIndex, bool discounted = true) const = 0;
121 
127  virtual float GetCuriosityRewards(int chromosomeIndex, bool discounted = true) const = 0;
128 
137  virtual void ProcessAgentData(bool onlyCompletedEpisodes = true) = 0;
138 };
139 
144 
146 extern "C"
147 {
148  SMARTENGINE_EXPORT ObjPtr GeneticAgentTrainer_CreateInstance(const GeneticAgentTrainerCInfo& cinfo);
149  SMARTENGINE_EXPORT void GeneticAgentTrainer_MapAgentToChromosome(ObjPtr object, ObjPtr agent, int chromosomeIndex);
150  SMARTENGINE_EXPORT float GeneticAgentTrainer_GetSumRawRewards(ObjPtr object, int chromosomeIndex, int discounted);
151  SMARTENGINE_EXPORT float GeneticAgentTrainer_GetCuriosityRewards(ObjPtr object, int chromosomeIndex, int discounted);
152  SMARTENGINE_EXPORT void GeneticAgentTrainer_ProcessAgentData(ObjPtr object, bool onlyCompletedEpisodes);
153  SMARTENGINE_EXPORT ObjPtr GeneticAgentTrainer_CreateAgent(ObjPtr object);
154 }
156 
157 } // namespace SmartEngine
SmartEngine::GeneticAgentTrainerCInfo::gamma
float gamma
Gamma to apply to rewards over time.
Definition: GeneticAgentTrainer.h:83
SmartEngine::ResourceCInfo
Data used to construct an IResource instance
Definition: Resource.h:16
SmartEngine::IAgentFactory
RL trainers implement this to create agents.
Definition: Agent.h:129
SmartEngine::IGeneticAgentTrainer::GetSumRawRewards
virtual float GetSumRawRewards(int chromosomeIndex, bool discounted=true) const =0
Returns the sum of the raw agent rewards for a given chromosome.
SmartEngine::GeneticAgentTrainerCInfo::curiosityTrainEpochs
int curiosityTrainEpochs
The number of epochs to train the curiosity module on the incoming agent data.
Definition: GeneticAgentTrainer.h:78
SmartEngine::GeneticAgentTrainerCInfo::dataStore
IAgentDataStore * dataStore
The data store that agents will feed into.
Definition: GeneticAgentTrainer.h:50
SmartEngine::GeneticAgentTrainerCInfo::context
IContext * context
The context this trainer belongs to.
Definition: GeneticAgentTrainer.h:40
SmartEngine::GeneticAgentScoringMethod::Sum
@ Sum
Sum the scores of all agents associated with the chromosome
SmartEngine::GeneticAgentTrainerCInfo::scoringMethod
GeneticAgentScoringMethod scoringMethod
Defines how the trainer deals with chromosome scores from multiple agents.
Definition: GeneticAgentTrainer.h:88
SmartEngine::GeneticAgentTrainerCInfo::policyNodeNames
const char ** policyNodeNames
The names of the output nodes of the actor (the network used to manipulate the environment)....
Definition: GeneticAgentTrainer.h:62
SmartEngine::GeneticAgentTrainerCInfo
Data used to construct an IGeneticAgentTrainer instance
Definition: GeneticAgentTrainer.h:36
SmartEngine::GeneticAgentTrainerCInfo::agentName
const char * agentName
The base name for created agents.
Definition: GeneticAgentTrainer.h:55
SmartEngine::CreateGeneticAgentTrainer
SMARTENGINE_EXPORT ObjectPtr< IGeneticAgentTrainer > CreateGeneticAgentTrainer(const GeneticAgentTrainerCInfo &cinfo)
Creates an instance of IGeneticAgentTrainer
SmartEngine::ObjectPtr
Smart pointer to an IObject. Automatic ref counting.
Definition: ObjectPtr.h:16
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::IGeneticAgentTrainer::ProcessAgentData
virtual void ProcessAgentData(bool onlyCompletedEpisodes=true)=0
Crunches and aggregates whatever data the agents have collected so far. This can be called at any tim...
SmartEngine::IGeneticAgentTrainer::GetCuriosityRewards
virtual float GetCuriosityRewards(int chromosomeIndex, bool discounted=true) const =0
Returns the sum of the curiosity rewards for a given chromosome.
SmartEngine::ICuriosityModule
A curiosity module is a way of rewarding an agent for behavior not yet seen. Rewards are given based ...
Definition: CuriosityModule.h:108
SmartEngine::GeneticAgentTrainerCInfo::policyNodeNameCount
int policyNodeNameCount
The number of elements in the policy node name array
Definition: GeneticAgentTrainer.h:67
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::GeneticAgentScoringMethod
GeneticAgentScoringMethod
Defines how the trainer deals with chromosome scores from multiple agents.
Definition: GeneticAgentTrainer.h:20
SmartEngine::GeneticAgentTrainerCInfo::graph
IGraph * graph
The graph we are going to train.
Definition: GeneticAgentTrainer.h:45
SmartEngine::IAgentDataStore
The agent data store keeps experience data for the purpose of training. Some RL trainers don't store ...
Definition: Agent.h:57
SmartEngine::IContext
Every node in the AI graph must belong to the same context.
Definition: Context.h:40
SmartEngine::IGeneticAgentTrainer
A genetic trainer that uses agents to automatically set the loss of each chromosome....
Definition: GeneticAgentTrainer.h:98
SmartEngine::IAgent
Agents are used to track the performance of one instance of a network.
Definition: Agent.h:84
SmartEngine::GeneticAgentTrainerCInfo::curiosityModule
ICuriosityModule * curiosityModule
Optional curiosity module for additional curiosity rewards.
Definition: GeneticAgentTrainer.h:72