Grail (C++)  1.3.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
SimulatedGameReasoner.hh
1 // Copyright QED Software 2023.
2 
3 #ifndef GRAIL_SIMULATED_GAME_REASONER_H
4 #define GRAIL_SIMULATED_GAME_REASONER_H
5 
6 #include <memory>
7 
8 #include "../ISimulatedActionTranslator.hh"
9 #include "../ISimulatedGameStateTranslator.hh"
10 #include "../ISimulatedGameUnit.hh"
11 #include "../SimulatedGame.hh"
12 #include "../../GrailCore/AIEntity.hh"
13 #include "../../GrailCore/Plan.hh"
14 #include "../../GrailCore/Reasoner.hh"
15 
16 namespace grail
17 {
18  namespace data
19  {
20  struct SimulatedGameReasonerSnapshot;
21  }
22 
23 namespace simgames
24 {
25  class SimulatedGameSnapshotObserver;
26 
27  class SimulatedGameReasoner final : public Reasoner
28  {
29  public:
38  SimulatedGameReasoner(std::unique_ptr<simgames::ISimulatedGameStateTranslator> worldStateTranslator,
39  std::unique_ptr<simgames::ISimulatedActionTranslator> actionTranslator,
40  std::size_t iterationsPerFrame,
41  std::size_t maxIterations = 1000,
42  std::size_t maxRecalculationIterations = 1000);
43  virtual ~SimulatedGameReasoner() override;
44 
45  virtual void StageBehavior(AIEntity& entity) override;
46 
48  void SetFallbackBehavior(std::unique_ptr<Behavior> _fallbackBehavior);
49 
51  const simgames::SimulatedGame& getGame() const;
52 
59  void RequestRecalculation(AIEntity& entity);
60 
62  void SetSnapshotProduction(bool shouldProduce);
63 
65  void SetDebugSnapshotFirstIteration(size_t iterationNumber);
66 
68  void SetDebugSnapshotLastIteration(size_t iterationNumber);
69 
71  size_t GetDebugSnapshotFirstIteration() const;
72 
74  size_t GetDebugSnapshotLastIteration() const;
75 
77  bool IsComputing() const;
78 
79  virtual std::unique_ptr<ISnapshotGenerator> CreateSnapshotGenerator(size_t) override;
80 
83  private:
84  void ResetGame();
85 
86  std::unique_ptr<simgames::SimulatedGame> game;
87  std::unique_ptr<Behavior> fallbackBehavior;
88 
89  std::unique_ptr<simgames::ISimulatedGameStateTranslator> worldStateTranslator;
90  std::unique_ptr<simgames::ISimulatedActionTranslator> actionTranslator;
91  Plan plan;
92 
93  std::size_t iterationsPerFrame{};
94  std::size_t maxIterations{};
95  std::size_t maxRecalculationIterations{};
96  std::size_t maxRecalculationIterationsLeft{};
97  std::size_t iterationCounter{};
98 
99  const std::size_t minIterationsPerRun = 2;
100 
101  std::shared_ptr<simgames::ISimulatedGameUnit> pairedAbstractUnit{};
102  std::unique_ptr<simgames::SimulatedGameSnapshotObserver> snapshotObserver{};
103  bool isGameDataReady = false;
104  bool isComputing = false;
105  bool isRecalculating = false;
106  };
107 }
108 }
109 
110 #endif // GRAIL_SIMULATED_GAME_REASONER_H
grail::AIEntity
The AIEntity class - Defines a basic object which can execute behaviors.
Definition: AIEntity.hh:50
grail::simgames::SimulatedGameReasoner::SetDebugSnapshotFirstIteration
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:139
grail::simgames::SimulatedGameReasoner::SimulatedGameReasoner
SimulatedGameReasoner(std::unique_ptr< simgames::ISimulatedGameStateTranslator > worldStateTranslator, std::unique_ptr< simgames::ISimulatedActionTranslator > actionTranslator, std::size_t iterationsPerFrame, std::size_t maxIterations=1000, std::size_t maxRecalculationIterations=1000)
Constructs a new SimulatedGameReasoner
Definition: SimulatedGameReasoner.cpp:13
grail::simgames::SimulatedGameReasoner::StageBehavior
virtual void StageBehavior(AIEntity &entity) override
selectBehavior - Runs reasoner's selection algorithm and assigns chosen behavior to provided entity.
Definition: SimulatedGameReasoner.cpp:33
grail::Reasoner
The Reasoner class - Entity's "brain", assigns them behaviors chosen by user-defined algorithms.
Definition: Reasoner.hh:21
grail::simgames::SimulatedGameReasoner
Definition: SimulatedGameReasoner.hh:27
grail::simgames::SimulatedGameReasoner::GetDebugSnapshotFirstIteration
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:155
grail::simgames::SimulatedGameReasoner::IsComputing
bool IsComputing() const
Returns true is the reasoner has not finished computations (i.e. there are still simulations to perfo...
Definition: SimulatedGameReasoner.cpp:165
grail::simgames::SimulatedGame
The main interface class for the SimulatedGame reasoner based on the MCTS algorithm....
Definition: SimulatedGame.hh:30
grail::simgames::SimulatedGameReasoner::SetFallbackBehavior
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:89
grail::simgames::SimulatedGameReasoner::getGame
const simgames::SimulatedGame & getGame() const
Gets the SimulatedGame object that is currently created by the ISimulatedGameStateTranslator.
Definition: SimulatedGameReasoner.cpp:94
grail::data::SimulatedGameReasonerSnapshot
Definition: SimulatedGamesSnapshots.h:129
grail::Plan
A data structure used by PlannerReasoner to execute a sequence of behaviors.
Definition: Plan.hh:14
grail::simgames::SimulatedGameReasoner::RequestRecalculation
void RequestRecalculation(AIEntity &entity)
As documented, the ISimulatedActionTranslator translates the actions computed by SimulatedGame to beh...
Definition: SimulatedGameReasoner.cpp:99
grail::simgames::SimulatedGameReasoner::SetDebugSnapshotLastIteration
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:147
grail::simgames::SimulatedGameReasoner::SetSnapshotProduction
void SetSnapshotProduction(bool shouldProduce)
Calling this function makes the reasoner collect debug data. The GrailDebugger will call it automatic...
Definition: SimulatedGameReasoner.cpp:127
grail::simgames::SimulatedGameReasoner::GetDebugSnapshotLastIteration
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:160
grail::simgames::SimulatedGameReasoner::ProduceDebugSnapshot
data::SimulatedGameReasonerSnapshot ProduceDebugSnapshot()
This method returns debug snapshot of data collected so far by SimulatedGameSnapshotObserver....
Definition: SimulatedGameReasoner.cpp:175