Grail (C++)  1.4.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 "SimulatedGameNode.hh"
9 #include "SimulatedGameHelper.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:
42  SimulatedGame(int teamCount = 2,
43  double maxScore = 1.0,
44  double explorationBoost = 1.0,
45  size_t freezeVisitsTreshold = std::numeric_limits<size_t>::max(),
46  RandomGenerator::result_type seed = std::random_device{}());
47 
48  virtual ~SimulatedGame();
49 
54  SimulatedGameThinkingUnit& AddUnit(std::shared_ptr<SimulatedGameThinkingUnit> unit);
55 
59  void AddUnits(std::vector<std::shared_ptr<SimulatedGameThinkingUnit>> units);
60 
65  SimulatedGameStochasticUnit& AddUnit(std::shared_ptr<SimulatedGameStochasticUnit> unit);
66 
71  void RemoveUnit(const SimulatedGameThinkingUnit* unit);
72 
77  void RemoveUnit(const SimulatedGameStochasticUnit* unit);
78 
83 
89 
94  std::vector<std::pair<const ISimulatedGameUnit*, const ISimulatedGameAction*>> GetExpectedPlayout() const;
95 
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 
139 
145  void ClearStatistics();
146 
147  private:
148  void Iteration();
149  void IterationWithDebug(SimulatedGameSnapshotObserver& observer);
150 
151  void Reset();
152  void CreateRoot();
153 
154  std::vector<std::shared_ptr<SimulatedGameThinkingUnit>> thinkingUnits{};
155  std::vector<std::shared_ptr<SimulatedGameStochasticUnit>> stochasticUnits{};
156 
157  RandomGenerator rand_gen{};
158  std::unique_ptr<SimulatedGameNode> rootNode{};
159  SimulatedGameNode* currentNode{};
161  SimulatedGameThinkingUnit* startingUnit{};
162  };
163 }
164 }
165 #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:23
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.hh: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