Grail (C++)  1.2.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
SimulatedGame.hh
1 // Copyright QED Software 2023.
2 
3 #ifndef GRAIL_SIMULATED_GAME_H
4 #define GRAIL_SIMULATED_GAME_H
5 
6 #include "ISimulatedGameUnit.hh"
7 #include "SimulatedGameActionMetadata.hh"
8 #include "SimulatedGameHelper.h"
9 #include "SimulatedGameNode.h"
10 #include "SimulatedGameSnapshotObserver.h"
11 
12 #include <limits>
13 #include <memory>
14 #include <unordered_map>
15 #include <vector>
16 
17 namespace grail
18 {
19 namespace simgames
20 {
21  class SimulatedGameThinkingUnit;
22  class SimulatedGameStochasticUnit;
23  class ISimulatedGameAction;
24  class SimulatedGameNode;
25 
31  {
32  public:
43  SimulatedGame(int teamCount = 2,
44  double maxScore = 1.0,
45  double explorationBoost = 1.0,
46  size_t freezeVisitsTreshold = std::numeric_limits<size_t>::max(),
47  RandomGenerator::result_type seed = std::random_device{}());
48 
49  virtual ~SimulatedGame();
50 
55  SimulatedGameThinkingUnit& AddUnit(std::shared_ptr<SimulatedGameThinkingUnit> unit);
56 
60  void AddUnits(std::vector<std::shared_ptr<SimulatedGameThinkingUnit>> units);
61 
66  SimulatedGameStochasticUnit& AddUnit(std::shared_ptr<SimulatedGameStochasticUnit> unit);
67 
72  void RemoveUnit(const SimulatedGameThinkingUnit* unit);
73 
78  void RemoveUnit(const SimulatedGameStochasticUnit* unit);
79 
84 
90 
95  std::vector<std::pair<const ISimulatedGameUnit*, const ISimulatedGameAction*>> GetExpectedPlayout() const;
96 
101  std::vector<SimulatedGameActionMetadata> GetStartingUnitActionsWithMetadata() const;
102 
107  std::unordered_map<const ISimulatedGameUnit*, std::vector<const ISimulatedGameAction*>> GetExpectedPlans();
108 
113  std::unordered_map<const ISimulatedGameUnit*, std::vector<SimulatedGameActionMetadata>>
115 
124  virtual size_t Run(size_t milisecondsTotal,
125  size_t maxIterationCount,
126  SimulatedGameSnapshotObserver* observer = nullptr);
127 
128  void DebugPrintPlan(
129  std::unordered_map<const ISimulatedGameUnit*, std::vector<const ISimulatedGameAction*>>& plan) const;
130  void DebugPrintPlanWithMetadata(
131  std::unordered_map<const ISimulatedGameUnit*, std::vector<SimulatedGameActionMetadata>>& plan) const;
132  void DebugPrintFullPlayout(
133  std::vector<std::pair<const ISimulatedGameUnit*, const ISimulatedGameAction*>>& playout) const;
134 
137 
143  void ClearStatistics();
144 
145  private:
146  void Iteration();
147  void IterationWithDebug(SimulatedGameSnapshotObserver& observer);
148 
149  void Reset();
150  void CreateRoot();
151 
152  std::vector<std::shared_ptr<SimulatedGameThinkingUnit>> thinkingUnits{};
153  std::vector<std::shared_ptr<SimulatedGameStochasticUnit>> stochasticUnits{};
154 
155  RandomGenerator rand_gen{};
156  std::unique_ptr<SimulatedGameNode> rootNode{};
157  SimulatedGameNode* currentNode{};
159  SimulatedGameThinkingUnit* startingUnit{};
160  };
161 }
162 }
163 #endif //GRAIL_SIMULATED_GAME_H
grail::simgames::SimulatedGame::AddUnit
SimulatedGameThinkingUnit & AddUnit(std::shared_ptr< SimulatedGameThinkingUnit > unit)
Adds a new regular unit to the game.
Definition: SimulatedGame.cpp:225
grail::simgames::SimulatedGame::GetStartingUnitBestActionMetadata
const SimulatedGameActionMetadata GetStartingUnitBestActionMetadata() const
Call this method after certain number of iterations performed (by calling Run() first).
Definition: SimulatedGame.cpp:293
grail::simgames::SimulatedGame::GetExpectedPlans
std::unordered_map< const ISimulatedGameUnit *, std::vector< const ISimulatedGameAction * > > GetExpectedPlans()
Call this method after certain number of iterations performed (by calling Run() first).
Definition: SimulatedGame.cpp:310
grail::simgames::SimulatedGame::SetStartingUnit
void SetStartingUnit(SimulatedGameThinkingUnit &unit)
Gets or sets the unit that acts first in the starting state of the game.
Definition: SimulatedGame.cpp:91
grail::simgames::SimulatedGameStochasticUnit
A base class of a unit in SimulatedGame that peforms actions according to some probability distributi...
Definition: SimulatedGameStochasticUnit.hh:23
grail::simgames::SimulatedGameHelper
Definition: SimulatedGameHelper.h:18
grail::simgames::SimulatedGame
The main interface class for the SimulatedGame reasoner based on the MCTS algorithm....
Definition: SimulatedGame.hh:30
grail::simgames::ISimulatedGameAction
Base class for all actions in SimulatedGame. Derive from it for your actions.
Definition: ISimulatedGameAction.hh:41
grail::simgames::SimulatedGameThinkingUnit
A base class of a unit related to a rational/intelligent player. MCTS chooses actions for this kind o...
Definition: SimulatedGameThinkingUnit.hh:26
grail::simgames::SimulatedGame::SimulatedGame
SimulatedGame(int teamCount=2, double maxScore=1.0, double explorationBoost=1.0, size_t freezeVisitsTreshold=std::numeric_limits< size_t >::max(), RandomGenerator::result_type seed=std::random_device{}())
Constructs a new SimulatedGame object. This object can be reused.
Definition: SimulatedGame.cpp:195
grail::simgames::SimulatedGameSnapshotObserver
Definition: SimulatedGameSnapshotObserver.h:23
grail::simgames::SimulatedGame::GetExpectedPlayout
std::vector< std::pair< const ISimulatedGameUnit *, const ISimulatedGameAction * > > GetExpectedPlayout() const
Call this method after certain number of iterations performed (by calling Run() first).
Definition: SimulatedGame.cpp:299
grail::simgames::SimulatedGame::ClearStatistics
void ClearStatistics()
This method invalidates the tree built for the game. If any offline learners were attached,...
Definition: SimulatedGame.cpp:96
grail::simgames::SimulatedGame::AddUnits
void AddUnits(std::vector< std::shared_ptr< SimulatedGameThinkingUnit >> units)
Adds new regular units to the game.
Definition: SimulatedGame.cpp:275
grail::simgames::SimulatedGame::GetExpectedPlansWithMetadata
std::unordered_map< const ISimulatedGameUnit *, std::vector< SimulatedGameActionMetadata > > GetExpectedPlansWithMetadata()
Call this method after certain number of iterations performed (by calling Run() first).
Definition: SimulatedGame.cpp:321
grail::simgames::SimulatedGame::Run
virtual size_t Run(size_t milisecondsTotal, size_t maxIterationCount, SimulatedGameSnapshotObserver *observer=nullptr)
Performs simulations of the game. Pass the available time for computations and the maximum number of ...
Definition: SimulatedGame.cpp:144
grail::simgames::ISimulatedGameUnit
Base class of a unit in the SimulatedGame framework. A unit represents part of the game-state and pef...
Definition: ISimulatedGameUnit.hh:28
grail::simgames::SimulatedGame::RemoveUnit
void RemoveUnit(const SimulatedGameThinkingUnit *unit)
Removes a regular unit from the game Might be useful when the game object is reused for different sit...
Definition: SimulatedGame.cpp:242
grail::simgames::SimulatedGame::GetStartingUnitActionsWithMetadata
std::vector< SimulatedGameActionMetadata > GetStartingUnitActionsWithMetadata() const
Call this method after certain number of iterations performed (by calling Run() first).
Definition: SimulatedGame.cpp:331
grail::simgames::SimulatedGameActionMetadata
This class represents action defined in the SimulatedGames module with additional statistics (score,...
Definition: SimulatedGameActionMetadata.hh:15
grail::simgames::SimulatedGameNode
This class should not be visible to developers at all.
Definition: SimulatedGameNode.h:25
grail::simgames::SimulatedGame::GetStartingUnitBestAction
const ISimulatedGameAction * GetStartingUnitBestAction() const
Call this method after certain number of iterations performed (by calling Run() first).
Definition: SimulatedGame.cpp:284