SmartEngine  1.6.0
GraphModel.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 "Graph.h"
8 #include "Hash.h"
9 #include "Matrix.h"
10 #include "Object.h"
11 
12 namespace SmartEngine
13 {
14 
19 const float cUpdateEveryFrameRate = 0.0f;
20 
26 const float cUpdateManuallyRate = -1.0f;
27 
34 const float cDefaultUpdateRate = -2.0f;
35 
36 #pragma pack(push, 4)
41 {
42 };
43 
48 {
53  IContext* context = nullptr;
54 };
55 
60 {
64  IGraph* graph = nullptr;
65 };
66 
67 typedef void (SMARTENGINE_CALLBACK *GraphModel_ClearData_Ptr)(void* userData);
68 typedef void (SMARTENGINE_CALLBACK *GraphModel_AddData_Ptr)(void* userData, const char* nodeName, ObjPtr data);
69 typedef void (SMARTENGINE_CALLBACK *GraphModel_RequestOutput_Ptr)(void* userData, int controllerId, const char* nodeName,
70  int64 expectedRowCount);
71 typedef void (SMARTENGINE_CALLBACK *GraphModel_Execute_Ptr)(void* userData);
72 typedef void (SMARTENGINE_CALLBACK *GraphModel_RetrieveOutput_Ptr)(void* userData, int controllerId, ObjPtr modelOut);
73 typedef void (SMARTENGINE_CALLBACK *GraphModel_Reset_Ptr)(void* userData);
74 
79 {
80  void* userData = nullptr;
87 };
88 #pragma pack(pop)
89 
94 class SMARTENGINE_EXPORT IGraphInputOutput : public virtual IObject
95 {
96 public:
97  SMARTENGINE_DECLARE_CLASS(IGraphInputOutput)
98 
99 
100  virtual ObjectPtr<IGraphInputOutput> GetGraphComponent(const char* componentNodeName) = 0;
105 
118  virtual ObjectPtr<IMatrix> GetInput(const char* nodeName) = 0;
119 
130  virtual void RequestOutput(const char* nodeName, MatrixIndex expectedRowCount = -1) = 0;
131 
138  virtual ObjectPtr<IMatrix> GetOutput(const char* nodeName) = 0;
139 };
140 
146 class SMARTENGINE_EXPORT IGraphController : public IGraphInputOutput
147 {
148 public:
149  SMARTENGINE_DECLARE_CLASS(IGraphController)
150 
151 
152  virtual bool HaveNewOutputData() = 0;
157 };
158 
162 class SMARTENGINE_EXPORT IGraphModelOutput : public virtual IObject
163 {
164 public:
165  SMARTENGINE_DECLARE_CLASS(IGraphModelOutput)
166 
167 
168  virtual ObjectPtr<IMatrix> GetOutputRW(const char* nodeName) = 0;
171 
176  virtual ObjectPtr<IMatrix> GetOutputRW(HashKey nameHash) = 0;
177 };
178 
185 class SMARTENGINE_EXPORT IGraphModel : public virtual IObject
186 {
187 public:
188  SMARTENGINE_DECLARE_CLASS(IGraphModel)
189 
190 
191  virtual void ClearData() = 0;
194 
198  virtual void AddData(const char* nodeName, IMatrix* data) = 0;
199 
207  virtual void RequestOutput(int controllerId, const char* nodeName, MatrixIndex expectedRowCount) = 0;
208 
212  virtual void Execute() = 0;
213 
220  virtual void RetrieveOutput(int controllerId, IGraphModelOutput* modelOut) = 0;
221 
225  virtual void Reset() = 0;
226 };
227 
232 class SMARTENGINE_EXPORT IStandardGraphModel : public IGraphModel
233 {
234 public:
235  SMARTENGINE_DECLARE_CLASS(IStandardGraphModel)
236 
237 
238  virtual ObjectPtr<IGraph> GetGraph() const = 0;
241 
246  virtual MatrixIndex GetExpectedRowCount(int controllerId, const char* outputName) const = 0;
247 };
248 
259 class SMARTENGINE_EXPORT IGraphManager : public virtual IObject
260 {
261 public:
262  SMARTENGINE_DECLARE_CLASS(IGraphManager)
263 
264 
265  virtual void RegisterController(IGraphController* controller, const char* modelName,
274  float updateRate = cDefaultUpdateRate) = 0;
275 
279  virtual void UnregisterController(IGraphController* controller) = 0;
280 
284  virtual bool IsControllerRegistered(IGraphController* controller) const = 0;
285 
294  virtual void RegisterAgent(IAgent* agent, IGraphController* controller) = 0;
295 
299  virtual void UnregisterAgent(IAgent* agent) = 0;
300 
309  virtual void RegisterModel(IGraphModel* model, const char* modelName, float updateRate = cUpdateEveryFrameRate) = 0;
310 
319  virtual void RegisterModelFromGraph(IGraph* graph, const char* modelName,
320  float updateRate = cUpdateEveryFrameRate) = 0;
321 
331  virtual void RegisterModelFromGraphResource(const char* resourceName, const char* modelName,
332  float updateRate = cUpdateEveryFrameRate) = 0;
333 
337  virtual void UnregisterModel(const char* modelName) = 0;
338 
342  virtual void Update(float deltaTime) = 0;
343 
347  virtual void UpdateController(IGraphController* controller) = 0;
348 
360  virtual void BeginStreaming(IGraphController* controller) = 0;
361 
366  virtual void PushStreamData(IGraphController* controller) = 0;
367 
373 
377  virtual void EndStreaming(IGraphController* controller) = 0;
378 
382  virtual void Reset() = 0;
383 };
384 
389 
394 
398 
403 
405 extern "C"
406 {
407  SMARTENGINE_EXPORT ObjPtr GraphInputOutput_GetGraphComponent(ObjPtr object, const char* componentNodeName);
408  SMARTENGINE_EXPORT ObjPtr GraphInputOutput_GetInput(ObjPtr object, const char* nodeName);
409  SMARTENGINE_EXPORT void GraphInputOutput_RequestOutput(ObjPtr object, const char* nodeName, int64 expectedRowCount);
410  SMARTENGINE_EXPORT ObjPtr GraphInputOutput_GetOutput(ObjPtr object, const char* nodeName);
411 
412  SMARTENGINE_EXPORT ObjPtr GraphController_CreateInstance(const GraphControllerCInfo& cinfo);
413  SMARTENGINE_EXPORT int GraphController_HaveNewOutputData(ObjPtr object);
414 
415  SMARTENGINE_EXPORT ObjPtr GraphModelOutput_GetOutputRWString(ObjPtr object, const char* nodeName);
416  SMARTENGINE_EXPORT ObjPtr GraphModelOutput_GetOutputRWHash(ObjPtr object, HashKey nameHash);
417 
418  SMARTENGINE_EXPORT void GraphModel_ClearData(ObjPtr object);
419  SMARTENGINE_EXPORT void GraphModel_AddData(ObjPtr object, const char* nodeName, ObjPtr data);
420  SMARTENGINE_EXPORT void GraphModel_RequestOutput(ObjPtr object, int controllerId, const char* nodeName,
421  int64 expectedRowCount);
422  SMARTENGINE_EXPORT void GraphModel_Execute(ObjPtr object);
423  SMARTENGINE_EXPORT void GraphModel_RetrieveOutput(ObjPtr object, int controllerId, ObjPtr modelOut);
424  SMARTENGINE_EXPORT void GraphModel_Reset(ObjPtr object);
425 
426  SMARTENGINE_EXPORT ObjPtr StandardGraphModel_CreateInstance(const StandardGraphModelCInfo& cinfo);
427  SMARTENGINE_EXPORT ObjPtr StandardGraphModel_GetGraph(ObjPtr object);
428  SMARTENGINE_EXPORT int64 StandardGraphModel_GetExpectedRowCount(ObjPtr object, int controllerId, const char* outputName);
429 
430  SMARTENGINE_EXPORT ObjPtr UserGraphModel_CreateInstance(const UserGraphModelCInfo& cinfo);
431 
432  SMARTENGINE_EXPORT ObjPtr GraphManager_CreateInstance(const GraphManagerCInfo& cinfo);
433  SMARTENGINE_EXPORT void GraphManager_RegisterController(ObjPtr object, ObjPtr controller, const char* modelName,
434  float updateRate);
435  SMARTENGINE_EXPORT void GraphManager_UnregisterController(ObjPtr object, ObjPtr controller);
436  SMARTENGINE_EXPORT int GraphManager_IsControllerRegistered(ObjPtr object, ObjPtr controller);
437  SMARTENGINE_EXPORT void GraphManager_RegisterAgent(ObjPtr object, ObjPtr agent, ObjPtr controller);
438  SMARTENGINE_EXPORT void GraphManager_UnregisterAgent(ObjPtr object, ObjPtr agent);
439  SMARTENGINE_EXPORT void GraphManager_RegisterModel(ObjPtr object, ObjPtr model, const char* modelName, float updateRate);
440  SMARTENGINE_EXPORT void GraphManager_RegisterModelFromGraph(ObjPtr object, ObjPtr graph, const char* modelName,
441  float updateRate);
442  SMARTENGINE_EXPORT void GraphManager_RegisterModelFromGraphResource(ObjPtr object, const char* resourceName,
443  const char* modelName, float updateRate);
444  SMARTENGINE_EXPORT void GraphManager_UnregisterModel(ObjPtr object, const char* modelName);
445  SMARTENGINE_EXPORT void GraphManager_Update(ObjPtr object, float deltaTime);
446  SMARTENGINE_EXPORT void GraphManager_UpdateController(ObjPtr object, ObjPtr controller);
447  SMARTENGINE_EXPORT void GraphManager_BeginStreaming(ObjPtr object, ObjPtr controller);
448  SMARTENGINE_EXPORT void GraphManager_PushStreamData(ObjPtr object, ObjPtr controller);
449  SMARTENGINE_EXPORT int64 GraphManager_GetStreamingInputRowCount(ObjPtr object, ObjPtr controller);
450  SMARTENGINE_EXPORT void GraphManager_EndStreaming(ObjPtr object, ObjPtr controller);
451  SMARTENGINE_EXPORT void GraphManager_Reset(ObjPtr object);
452 }
454 
455 } // namespace SmartEngine
SmartEngine::IGraphManager::UnregisterController
virtual void UnregisterController(IGraphController *controller)=0
Unregisters a controller. It will no longer receive output data.
SmartEngine::UserGraphModelCInfo::userData
void * userData
Definition: GraphModel.h:80
SmartEngine::GraphModel_AddData_Ptr
void(SMARTENGINE_CALLBACK * GraphModel_AddData_Ptr)(void *userData, const char *nodeName, ObjPtr data)
Definition: GraphModel.h:68
SmartEngine::IGraphModel
Takes input from a controller and produces an output. If using a graph as the model,...
Definition: GraphModel.h:186
SmartEngine::GraphModel_ClearData_Ptr
void(SMARTENGINE_CALLBACK * GraphModel_ClearData_Ptr)(void *userData)
Definition: GraphModel.h:67
SmartEngine::IGraphManager::RegisterModelFromGraph
virtual void RegisterModelFromGraph(IGraph *graph, const char *modelName, float updateRate=cUpdateEveryFrameRate)=0
Registers a graph as a model with the given name and update rate. If a model with the given name is a...
SmartEngine::UserGraphModelCInfo
Data used to construct an overridable instance of IGraphModel
Definition: GraphModel.h:79
SmartEngine::StrongNumber
Definition: StrongNumber.h:9
SmartEngine::IGraphModel::RetrieveOutput
virtual void RetrieveOutput(int controllerId, IGraphModelOutput *modelOut)=0
Dump all output for the specified controller from the last execution into modelOut.
SmartEngine::IGraphManager::Reset
virtual void Reset()=0
Resets all models.
SmartEngine::cDefaultUpdateRate
const float cDefaultUpdateRate
Use the model's default update rate. Not valid when registering a model. This is the only value that ...
Definition: GraphModel.h:34
SmartEngine::IGraphManager::RegisterModel
virtual void RegisterModel(IGraphModel *model, const char *modelName, float updateRate=cUpdateEveryFrameRate)=0
Registers a model with the given name and update rate. If a model with the given name is already regi...
SmartEngine::GraphModel_RequestOutput_Ptr
void(SMARTENGINE_CALLBACK * GraphModel_RequestOutput_Ptr)(void *userData, int controllerId, const char *nodeName, int64 expectedRowCount)
Definition: GraphModel.h:69
SmartEngine::GraphModel_Execute_Ptr
void(SMARTENGINE_CALLBACK * GraphModel_Execute_Ptr)(void *userData)
Definition: GraphModel.h:71
SmartEngine::IGraphManager::BeginStreaming
virtual void BeginStreaming(IGraphController *controller)=0
Streaming lets you feed in chunks of data over time into the model. After beginning the stream,...
SmartEngine::StandardGraphModelCInfo
Data used to construct an IStandardGraphModel instance
Definition: GraphModel.h:60
SmartEngine::IGraphInputOutput::RequestOutput
virtual void RequestOutput(const char *nodeName, MatrixIndex expectedRowCount=-1)=0
Requests that the following node name be produced as output during execution. This must be called bef...
SmartEngine::GraphControllerCInfo
Data used to construct an IGraphController instance
Definition: GraphModel.h:41
SmartEngine::IGraphManager::PushStreamData
virtual void PushStreamData(IGraphController *controller)=0
Pushes whatever inputs are set on the controller into the model. The model will not be cleared during...
SmartEngine::IGraphModel::Execute
virtual void Execute()=0
Execute the model and save the results locally.
SmartEngine::IStandardGraphModel
Interface to the standard model that interacts with a graph. This can be used to extend the standard ...
Definition: GraphModel.h:233
SmartEngine::GraphManagerCInfo::context
IContext * context
Context to use when creating standard graph models from a graph resource.
Definition: GraphModel.h:53
SmartEngine::IGraphManager::UnregisterModel
virtual void UnregisterModel(const char *modelName)=0
Unregisters a model. Any connected controllers will no longer receive data.
SmartEngine::GraphManagerCInfo
Data used to construct an IGraphManager instance
Definition: GraphModel.h:48
SmartEngine::cUpdateManuallyRate
const float cUpdateManuallyRate
An update rate set to this value means we shouldn't update the controller automatically,...
Definition: GraphModel.h:26
SmartEngine::IGraphModel::AddData
virtual void AddData(const char *nodeName, IMatrix *data)=0
Add one or more rows of data to the specified node.
SmartEngine::IGraphModel::Reset
virtual void Reset()=0
Called upon request from the user. Reset any internal state.
SmartEngine::ObjectPtr
Smart pointer to an IObject. Automatic ref counting.
Definition: ObjectPtr.h:16
SmartEngine::GraphModel_Reset_Ptr
void(SMARTENGINE_CALLBACK * GraphModel_Reset_Ptr)(void *userData)
Definition: GraphModel.h:73
SmartEngine::IGraphManager::Update
virtual void Update(float deltaTime)=0
Advance the models ahead in time, updating controllers as necessary.
SmartEngine::IGraphManager::UnregisterAgent
virtual void UnregisterAgent(IAgent *agent)=0
Unregisters an agent
SmartEngine::IGraphManager::EndStreaming
virtual void EndStreaming(IGraphController *controller)=0
Ends the streaming cycle. The graph will execute any requested outputs.
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::UserGraphModelCInfo::execute
GraphModel_Execute_Ptr execute
Definition: GraphModel.h:84
SmartEngine::IGraphManager
Connects graph controllers with graph models. Graph controllers define a single use of a graph....
Definition: GraphModel.h:260
SmartEngine::IGraphManager::RegisterModelFromGraphResource
virtual void RegisterModelFromGraphResource(const char *resourceName, const char *modelName, float updateRate=cUpdateEveryFrameRate)=0
Registers a model with the given name and update rate. This will generate a standard model from a gra...
SmartEngine::UserGraphModelCInfo::retrieveOutput
GraphModel_RetrieveOutput_Ptr retrieveOutput
Definition: GraphModel.h:85
SmartEngine::IGraphInputOutput::GetOutput
virtual ObjectPtr< IMatrix > GetOutput(const char *nodeName)=0
Returns the output associated with the specified node name. The returned matrix is read-only.
SmartEngine::IGraphInputOutput
Views on top of models deal with this class to inject and extract data from the underlying model.
Definition: GraphModel.h:95
SmartEngine::IMatrix
Wrapper around either a const or a mutable matrix.
Definition: Matrix.h:17
SmartEngine::GraphModel_RetrieveOutput_Ptr
void(SMARTENGINE_CALLBACK * GraphModel_RetrieveOutput_Ptr)(void *userData, int controllerId, ObjPtr modelOut)
Definition: GraphModel.h:72
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::IGraphController
Represents a use of a graph. Owners of the controller feed data to the controller at regular interval...
Definition: GraphModel.h:147
SmartEngine::UserGraphModelCInfo::clearData
GraphModel_ClearData_Ptr clearData
Definition: GraphModel.h:81
SmartEngine::IGraphModel::RequestOutput
virtual void RequestOutput(int controllerId, const char *nodeName, MatrixIndex expectedRowCount)=0
Execute() should produce output for this node.
SmartEngine::UserGraphModelCInfo::requestOutput
GraphModel_RequestOutput_Ptr requestOutput
Definition: GraphModel.h:83
SmartEngine::UserGraphModelCInfo::addData
GraphModel_AddData_Ptr addData
Definition: GraphModel.h:82
SmartEngine::HashKey
unsigned int HashKey
Definition: Hash.h:8
SmartEngine::CreateGraphManager
SMARTENGINE_EXPORT ObjectPtr< IGraphManager > CreateGraphManager(const GraphManagerCInfo &cinfo)
Creates an instance of IGraphManager.
SmartEngine::IGraphInputOutput::GetInput
virtual ObjectPtr< IMatrix > GetInput(const char *nodeName)=0
Returns a matrix associated with the specified node name. The node should be a BufferInput node in th...
SmartEngine::IGraphModelOutput
Used to return output back to controllers.
Definition: GraphModel.h:163
SmartEngine::IGraphManager::UpdateController
virtual void UpdateController(IGraphController *controller)=0
Updates a controller now. This is the only way to update manually stepped controllers.
SmartEngine::IStandardGraphModel::GetExpectedRowCount
virtual MatrixIndex GetExpectedRowCount(int controllerId, const char *outputName) const =0
Returns the number of rows we expect to see for the specified controller and output....
SmartEngine::CreateGraphController
SMARTENGINE_EXPORT ObjectPtr< IGraphController > CreateGraphController(const GraphControllerCInfo &cinfo)
Creates an instance of IGraphController.
SmartEngine::UserGraphModelCInfo::reset
GraphModel_Reset_Ptr reset
Definition: GraphModel.h:86
SmartEngine::IGraphModelOutput::GetOutputRW
virtual ObjectPtr< IMatrix > GetOutputRW(HashKey nameHash)=0
Returns a read-write matrix for the given name hash. Faster version than the one that takes a string.
SmartEngine::cUpdateEveryFrameRate
const float cUpdateEveryFrameRate
An update rate set to this value means we should update the controller with model output every frame.
Definition: GraphModel.h:19
SmartEngine::IContext
Every node in the AI graph must belong to the same context.
Definition: Context.h:40
SmartEngine::IGraphManager::IsControllerRegistered
virtual bool IsControllerRegistered(IGraphController *controller) const =0
Returns true if the controller is registered with us.
SmartEngine::IGraphManager::GetStreamingInputRowCount
virtual MatrixIndex GetStreamingInputRowCount(IGraphController *controller) const =0
Returns the current number of streamed rows currently pushed to the model. If the input buffers have ...
SmartEngine::CreateUserGraphModel
SMARTENGINE_EXPORT ObjectPtr< IGraphModel > CreateUserGraphModel(const UserGraphModelCInfo &cinfo)
Creates an overridable instance if IGraphModel
SmartEngine::CreateStandardGraphModel
SMARTENGINE_EXPORT ObjectPtr< IStandardGraphModel > CreateStandardGraphModel(const StandardGraphModelCInfo &cinfo)
Creates an instance of IStandardGraphModel.
SmartEngine::IGraphManager::RegisterAgent
virtual void RegisterAgent(IAgent *agent, IGraphController *controller)=0
Registers an agent for the specified controller. The agent will automatically start new experiences a...
SmartEngine::IAgent
Agents are used to track the performance of one instance of a network.
Definition: Agent.h:84
SmartEngine::StandardGraphModelCInfo::graph
IGraph * graph
The graph this model interacts with.
Definition: GraphModel.h:64