Grail (C++)  1.1.1
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 "../ISimulatedActionTranslator.hh"
7 #include "../ISimulatedGameStateTranslator.hh"
8 #include "../ISimulatedGameUnit.hh"
9 #include "../SimulatedGame.hh"
10 #include "../../GrailCore/AIEntity.hh"
11 #include "../../GrailCore/Plan.hh"
12 #include "../../GrailCore/Reasoner.hh"
13 
14 namespace grail
15 {
16  struct SimulatedGameReasonerSnapshot;
17 
18  namespace simulation
19  {
20  class SimulatedGameSnapshotObserver;
21  }
22 
23  class SimulatedGameReasoner final : public Reasoner
24  {
25  public:
34  SimulatedGameReasoner(std::unique_ptr<simulation::ISimulatedGameStateTranslator> worldStateTranslator,
35  std::unique_ptr<simulation::ISimulatedActionTranslator> actionTranslator,
36  std::size_t iterationsPerFrame,
37  std::size_t maxIterations = 1000,
38  std::size_t maxRecalculationIterations = 1000);
39  virtual ~SimulatedGameReasoner() override;
40 
41  virtual void SelectBehavior(AIEntity& entity) override;
42 
44  void SetFallbackBehavior(std::unique_ptr<Behavior> _fallbackBehavior);
45 
47  const simulation::SimulatedGame& getGame() const;
48 
55  void RequestRecalculation(AIEntity& entity);
56 
58  void SetSnapshotProduction(bool shouldProduce);
59 
61  void SetDebugSnapshotFirstIteration(size_t iterationNumber);
62 
64  void SetDebugSnapshotLastIteration(size_t iterationNumber);
65 
67  size_t GetDebugSnapshotFirstIteration() const;
68 
70  size_t GetDebugSnapshotLastIteration() const;
71 
73  bool IsComputing() const;
74 
75  virtual std::unique_ptr<ISnapshotGenerator> CreateSnapshotGenerator(size_t) override;
76 
79  private:
80  void ResetGame();
81 
82  std::unique_ptr<simulation::SimulatedGame> game;
83  std::unique_ptr<Behavior> fallbackBehavior;
84 
85  std::unique_ptr<simulation::ISimulatedGameStateTranslator> worldStateTranslator;
86  std::unique_ptr<simulation::ISimulatedActionTranslator> actionTranslator;
87  planning::Plan plan;
88 
89  std::size_t iterationsPerFrame{};
90  std::size_t maxIterations{};
91  std::size_t maxRecalculationIterations{};
92  std::size_t maxRecalculationIterationsLeft{};
93  std::size_t iterationCounter{};
94 
95  const std::size_t minIterationsPerRun = 2;
96 
97  std::shared_ptr<simulation::ISimulatedGameUnit> pairedAbstractUnit{};
98  std::unique_ptr<simulation::SimulatedGameSnapshotObserver> snapshotObserver{};
99  bool isGameDataReady = false;
100  bool isComputing = false;
101  bool isRecalculating = false;
102  };
103 }
104 #endif // GRAIL_SIMULATED_GAME_REASONER_H
grail::SimulatedGameReasoner::SelectBehavior
virtual void SelectBehavior(AIEntity &entity) override
selectBehavior - Runs reasoner's selection algorithm and assigns chosen behavior to provided entity.
Definition: SimulatedGameReasoner.cpp:33
grail::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::AIEntity
The AIEntity class - Defines a basic object which can execute behaviors.
Definition: AIEntity.hh:39
grail::Reasoner
The Reasoner class - Entity's "brain", assigns them behaviors chosen by user-defined algorithms.
Definition: Reasoner.hh:19
grail::SimulatedGameReasoner::SimulatedGameReasoner
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:13
grail::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::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::simulation::SimulatedGame
The main interface class for the SimulatedGame reasoner based on the MCTS algorithm....
Definition: SimulatedGame.hh:28
grail::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::SimulatedGameReasoner::getGame
const simulation::SimulatedGame & getGame() const
Gets the SimulatedGame object that is currently created by the ISimulatedGameStateTranslator.
Definition: SimulatedGameReasoner.cpp:94
grail::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::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::SimulatedGameReasoner::RequestRecalculation
void RequestRecalculation(AIEntity &entity)
As documented, the ISimulatedActionTranslator translates the actions computed by SimulatedGame to beh...
Definition: SimulatedGameReasoner.cpp:99
grail::SimulatedGameReasoner
Definition: SimulatedGameReasoner.hh:23
grail::SimulatedGameReasonerSnapshot
Definition: SimulatedGamesSnapshots.h:94
grail::planning::Plan
A data structure used by PlannerReasoner to execute a sequence of behaviors.
Definition: Plan.hh:14
grail::SimulatedGameReasoner::ProduceDebugSnapshot
SimulatedGameReasonerSnapshot ProduceDebugSnapshot()
This method returns debug snapshot of data collected so far by SimulatedGameSnapshotObserver....
Definition: SimulatedGameReasoner.cpp:175
grail::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