SmartEngine  1.6.0
Agent.h
1 // Copyright (C) Entropy Software LLC - All Rights Reserved
2 
3 #pragma once
4 
5 #include "Context.h"
6 #include "Graph.h"
7 #include "MemoryBuffer.h"
8 #include "Object.h"
9 #include "Resource.h"
10 
11 namespace SmartEngine
12 {
13 
14 class IAgentDataStore;
15 
16 #pragma pack(push, 4)
17 struct AgentDataStoreCInfo : public ResourceCInfo
21 {
27  int maxExperiences = 100000;
28 };
29 
30 typedef void (SMARTENGINE_CALLBACK *Agent_StartNewExperience_Ptr)(void* userData);
31 typedef void (SMARTENGINE_CALLBACK *Agent_RecordObservation_Ptr)(void* userData, const char* inputName, const float* buffer, int64 bufferCount);
32 typedef void (SMARTENGINE_CALLBACK *Agent_RecordAction_Ptr)(void* userData, const char* actionName, const float* buffer, int64 bufferCount);
33 typedef void (SMARTENGINE_CALLBACK *Agent_GiveReward_Ptr)(void* userData, float reward);
34 typedef void (SMARTENGINE_CALLBACK *Agent_EndEpisode_Ptr)(void* userData);
35 
40 {
41  void* userData = nullptr;
47 };
48 #pragma pack(pop)
49 
56 class SMARTENGINE_EXPORT IAgentDataStore : public IResource
57 {
58 public:
59  SMARTENGINE_DECLARE_CLASS(IAgentDataStore)
60 
61 
62  virtual void ClearAllExperiences() = 0;
65 
70  virtual void ClearExperiences(const char* agentName) = 0;
71 };
72 
83 class SMARTENGINE_EXPORT IAgent : public virtual IObject, public ISerializable
84 {
85 public:
86  SMARTENGINE_DECLARE_CLASS(IAgent)
87 
88 
89  virtual void StartNewExperience() = 0;
93 
99  virtual void RecordObservation(const char* inputName, const float* buffer, int64 bufferCount) = 0;
100 
109  virtual void RecordAction(const char* actionName, const float* buffer, int64 bufferCount) = 0;
110 
116  virtual void GiveReward(float reward) = 0;
117 
122  virtual void EndEpisode() = 0;
123 };
124 
128 class SMARTENGINE_EXPORT IAgentFactory : public virtual IObject
129 {
130 public:
131  SMARTENGINE_DECLARE_CLASS(IAgentFactory)
132 
133 
134  virtual ObjectPtr<IAgent> CreateAgent() = 0;
138 };
139 
144 
148 SMARTENGINE_EXPORT ObjectPtr<IAgent> CreateUserAgent(const UserAgentCInfo& cinfo);
149 
151 extern "C"
152 {
153  SMARTENGINE_EXPORT ObjPtr AgentDataStore_CreateInstance(const AgentDataStoreCInfo& cinfo);
154  SMARTENGINE_EXPORT void AgentDataStore_ClearAllExperiences(ObjPtr object);
155  SMARTENGINE_EXPORT void AgentDataStore_ClearExperiences(ObjPtr object, const char* agentName);
156 
157  SMARTENGINE_EXPORT void Agent_StartNewExperience(ObjPtr object);
158  SMARTENGINE_EXPORT void Agent_RecordObservation(ObjPtr object, const char* inputName, const float* buffer,
159  int64 bufferCount);
160  SMARTENGINE_EXPORT void Agent_RecordAction(ObjPtr object, const char* actionName, const float* buffer, int64 bufferCount);
161  SMARTENGINE_EXPORT void Agent_GiveReward(ObjPtr object, float reward);
162  SMARTENGINE_EXPORT void Agent_EndEpisode(ObjPtr object);
163 
164  SMARTENGINE_EXPORT ObjPtr UserAgent_CreateInstance(const UserAgentCInfo& cinfo);
165 }
167 
168 } // namespace SmartEngine
SmartEngine::UserAgentCInfo::recordObservation
Agent_RecordObservation_Ptr recordObservation
Definition: Agent.h:43
SmartEngine::Agent_EndEpisode_Ptr
void(SMARTENGINE_CALLBACK * Agent_EndEpisode_Ptr)(void *userData)
Definition: Agent.h:34
SmartEngine::IAgent::RecordAction
virtual void RecordAction(const char *actionName, const float *buffer, int64 bufferCount)=0
Records the output of the network we applied to the environment (the game or character)
SmartEngine::IAgentFactory
RL trainers implement this to create agents.
Definition: Agent.h:129
SmartEngine::IAgent::RecordObservation
virtual void RecordObservation(const char *inputName, const float *buffer, int64 bufferCount)=0
Records the input data to the network we are watching.
SmartEngine::Agent_RecordObservation_Ptr
void(SMARTENGINE_CALLBACK * Agent_RecordObservation_Ptr)(void *userData, const char *inputName, const float *buffer, int64 bufferCount)
Definition: Agent.h:31
SmartEngine::ISerializable
Base class for objects that can be loaded from and saved to an in memory buffer.
Definition: Resource.h:54
SmartEngine::UserAgentCInfo::endEpisode
Agent_EndEpisode_Ptr endEpisode
Definition: Agent.h:46
SmartEngine::AgentDataStoreCInfo
Data used to construct an IAgentDataStore instance
Definition: Agent.h:21
SmartEngine::Agent_RecordAction_Ptr
void(SMARTENGINE_CALLBACK * Agent_RecordAction_Ptr)(void *userData, const char *actionName, const float *buffer, int64 bufferCount)
Definition: Agent.h:32
SmartEngine::UserAgentCInfo::startNewExperience
Agent_StartNewExperience_Ptr startNewExperience
Definition: Agent.h:42
SmartEngine::ObjectPtr
Smart pointer to an IObject. Automatic ref counting.
Definition: ObjectPtr.h:16
SmartEngine::UserAgentCInfo::userData
void * userData
Definition: Agent.h:41
SmartEngine
Definition: A2CTrainer.h:10
SmartEngine::IObject
Base class for SmartEngine AI objects. It is not common to deal with this class directly.
Definition: Object.h:19
SmartEngine::IAgentDataStore::ClearExperiences
virtual void ClearExperiences(const char *agentName)=0
Removes all experiences from the store for a given agent name.
SmartEngine::AgentDataStoreCInfo::maxExperiences
int maxExperiences
The maximum number of experiences to keep per RL trainer before we start removing old ones
Definition: Agent.h:27
SmartEngine::UserAgentCInfo::recordAction
Agent_RecordAction_Ptr recordAction
Definition: Agent.h:44
SmartEngine::UserAgentCInfo::giveReward
Agent_GiveReward_Ptr giveReward
Definition: Agent.h:45
SmartEngine::CreateUserAgent
SMARTENGINE_EXPORT ObjectPtr< IAgent > CreateUserAgent(const UserAgentCInfo &cinfo)
Creates an overridable instance of IAgent from a previously serialized agent.
SmartEngine::Agent_StartNewExperience_Ptr
void(SMARTENGINE_CALLBACK * Agent_StartNewExperience_Ptr)(void *userData)
Definition: Agent.h:30
SmartEngine::IResource
Base class for objects that can be loaded from and saved to disk.
Definition: Resource.h:77
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::IAgent::EndEpisode
virtual void EndEpisode()=0
Called at the logical conclusion of a training session. Usually some event like the player died or th...
SmartEngine::CreateAgentDataStore
SMARTENGINE_EXPORT ObjectPtr< IAgentDataStore > CreateAgentDataStore(const AgentDataStoreCInfo &cinfo)
Creates an instance of IAgentDataStore
SmartEngine::Agent_GiveReward_Ptr
void(SMARTENGINE_CALLBACK * Agent_GiveReward_Ptr)(void *userData, float reward)
Definition: Agent.h:33
SmartEngine::IAgent::GiveReward
virtual void GiveReward(float reward)=0
The trainers will try to maximize rewards. The higher the value, the better the rewards....
SmartEngine::IAgent
Agents are used to track the performance of one instance of a network.
Definition: Agent.h:84
SmartEngine::UserAgentCInfo
Data used to construct an overridable instance of IAgent
Definition: Agent.h:40