(C++)  1.0.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
SimulatedGameReasoner.hh
1 #ifndef GRAIL_SIMULATED_GAME_REASONER_H
2 #define GRAIL_SIMULATED_GAME_REASONER_H
3 
4 #include <memory>
5 
6 #include "../GrailSystem/AIEntity.hh"
7 #include "../GrailSystem/Reasoner.hh"
8 #include "SimulatedGame.hh"
9 #include "ISimulatedGameUnit.hh"
10 #include "ISimulatedActionTranslator.hh"
11 #include "ISimulatedGameStateTranslator.hh"
12 #include "../GrailSystem/Plan.hh"
13 
14 namespace grail
15 {
16  struct SimulatedGameReasonerSnapshot;
17 
18  namespace simulation
19  {
20  class SimulatedGameObserverForGUI;
21  }
22 
23  class SimulatedGameReasoner final : public Reasoner
24  {
25  private:
26  std::unique_ptr<simulation::SimulatedGame> game;
27  std::unique_ptr<Behavior> fallbackBehavior;
28 
29  std::unique_ptr<simulation::ISimulatedGameStateTranslator> worldStateTranslator;
30  std::unique_ptr<simulation::ISimulatedActionTranslator> actionTranslator;
31  planning::Plan plan;
32 
33  std::size_t iterationsPerFrame;
34  std::size_t maxIterations;
35  std::size_t maxRecalculationIterations;
36  std::size_t maxRecalculationIterationsLeft;
37  std::size_t iterationCounter = 0;
38 
39  const std::size_t minIterationsPerRun = 2;
40 
41  std::shared_ptr<simulation::ISimulatedGameUnit> pairedAbstractUnit;
42  std::unique_ptr<simulation::SimulatedGameObserverForGUI> guiObserver;
43  bool isGameDataReady = false;
44  bool isComputing = false;
45  bool isRecalculating = false;
46  public:
55  SimulatedGameReasoner(std::unique_ptr<simulation::ISimulatedGameStateTranslator> worldStateTranslator,
56  std::unique_ptr<simulation::ISimulatedActionTranslator> actionTranslator,
57  std::size_t iterationsPerFrame, std::size_t maxIterations = 1000, std::size_t maxRecalculationIterations = 1000);
58  ~SimulatedGameReasoner() override;
59 
60  virtual void SelectBehavior(AIEntity& entity) override;
61 
63  void SetFallbackBehavior(std::unique_ptr<Behavior> _fallbackBehavior);
64 
66  const simulation::SimulatedGame& getGame() const;
67 
74  void RequestRecalculation(AIEntity& entity);
75 
77  void SetSnapshotProduction(bool shouldProduce);
78 
80  void SetDebugSnapshotFirstIteration(size_t iterationNumber);
81 
83  void SetDebugSnapshotLastIteration(size_t iterationNumber);
84 
86  size_t GetDebugSnapshotFirstIteration() const;
87 
89  size_t GetDebugSnapshotLastIteration() const;
90 
92  bool IsComputing() const;
93 
94  std::unique_ptr<ISnapshotGenerator> CreateSnapshotGenerator(size_t) override;
95 
98  private:
99  void resetGame();
100  };
101 }
102 #endif // GRAIL_SIMULATED_GAME_REASONER_H
The AIEntity class - Defines a basic object which can execute behaviors.
Definition: AIEntity.hh:39
The Reasoner class - Entity's "brain", assigns them behaviors chosen by user-defined algorithms.
Definition: Reasoner.hh:20
Definition: SimulatedGameReasoner.hh:24
size_t GetDebugSnapshotFirstIteration() const
If the debug data is collected, this method will return the first iteration number from which the dat...
Definition: SimulatedGameReasoner.cpp:136
size_t GetDebugSnapshotLastIteration() const
If the debug data is collected, this method will return the last iteration number up to which the dat...
Definition: SimulatedGameReasoner.cpp:141
const simulation::SimulatedGame & getGame() const
Gets the SimulatedGame object that is currently created by the ISimulatedGameStateTranslator.
Definition: SimulatedGameReasoner.cpp:89
void SetDebugSnapshotLastIteration(size_t iterationNumber)
You may limit the number of debug data collected to only data produced by simulations with numbers be...
Definition: SimulatedGameReasoner.cpp:130
virtual void SelectBehavior(AIEntity &entity) override
selectBehavior - Runs reasoner's selection algorithm and assigns chosen behavior to provided entity.
Definition: SimulatedGameReasoner.cpp:33
SimulatedGameReasoner(std::unique_ptr< simulation::ISimulatedGameStateTranslator > worldStateTranslator, std::unique_ptr< simulation::ISimulatedActionTranslator > actionTranslator, std::size_t iterationsPerFrame, std::size_t maxIterations=1000, std::size_t maxRecalculationIterations=1000)
Constructs a new SimulatedGameReasoner
Definition: SimulatedGameReasoner.cpp:15
SimulatedGameReasonerSnapshot ProduceDebugSnapshot()
This method returns debug snapshot of data collected so far by SimulatedGameObserverForGUI....
Definition: SimulatedGameReasoner.cpp:156
void SetFallbackBehavior(std::unique_ptr< Behavior > _fallbackBehavior)
Set a fallback behavior if no valid behaviors are produced from actions returned by SimulatedGame....
Definition: SimulatedGameReasoner.cpp:84
bool IsComputing() const
Returns true is the reasoner has not finished computations (i.e. there are still simulations to perfo...
Definition: SimulatedGameReasoner.cpp:146
void SetSnapshotProduction(bool shouldProduce)
Calling this function makes the reasoner collect debug data. The GrailDebugger will call it automatic...
Definition: SimulatedGameReasoner.cpp:116
void SetDebugSnapshotFirstIteration(size_t iterationNumber)
You may limit the number of debug data collected to only data produced by simulations with numbers be...
Definition: SimulatedGameReasoner.cpp:124
void RequestRecalculation(AIEntity &entity)
As documented, the ISimulatedActionTranslator translates the actions computed by SimulatedGame to beh...
Definition: SimulatedGameReasoner.cpp:94
A data structure used by PlannerReasoner to execute a sequence of behaviors.
Definition: Plan.hh:16
The main interface class for the SimulatedGame reasoner based on the MCTS algorithm....
Definition: SimulatedGame.hh:29
Definition: SimulatedGamesSnapshots.h:95