Grail (C++)  1.1.1
A multi-platform, modular, universal engine for embedding advanced AI in games.
SimulatedGame.hh
1 #ifndef GRAIL_SIMULATED_GAME_H
2 #define GRAIL_SIMULATED_GAME_H
3 
4 #include "ISimulatedGameUnit.hh"
5 #include "SimulatedGameActionMetadata.hh"
6 #include "SimulatedGameHelper.h"
7 #include "SimulatedGameNode.h"
8 #include "SimulatedGameSnapshotObserver.h"
9 
10 #include <limits>
11 #include <memory>
12 #include <unordered_map>
13 #include <vector>
14 
15 namespace grail
16 {
17  namespace simulation
18  {
19  class SimulatedGameThinkingUnit;
20  class SimulatedGameStochasticUnit;
21  class ISimulatedGameAction;
22  class SimulatedGameNode;
23 
29  {
30  public:
41  SimulatedGame(int teamCount = 2,
42  double maxScore = 1.0,
43  double explorationBoost = 1.0,
44  size_t freezeVisitsTreshold = std::numeric_limits<size_t>::max(),
45  RandomGenerator::result_type seed = std::random_device{}());
46 
47  virtual ~SimulatedGame();
48 
53  SimulatedGameThinkingUnit& AddUnit(std::shared_ptr<SimulatedGameThinkingUnit> unit);
54 
58  void AddUnits(std::vector<std::shared_ptr<SimulatedGameThinkingUnit>> units);
59 
64  SimulatedGameStochasticUnit& AddUnit(std::shared_ptr<SimulatedGameStochasticUnit> unit);
65 
70  void RemoveUnit(const SimulatedGameThinkingUnit* unit);
71 
76  void RemoveUnit(const SimulatedGameStochasticUnit* unit);
77 
82 
88 
93  std::vector<std::pair<const ISimulatedGameUnit*, const ISimulatedGameAction*>> GetExpectedPlayout() const;
94 
99  std::vector<SimulatedGameActionMetadata> GetStartingUnitActionsWithMetadata() const;
100 
105  std::unordered_map<const ISimulatedGameUnit*, std::vector<const ISimulatedGameAction*>> GetExpectedPlans();
106 
111  std::unordered_map<const ISimulatedGameUnit*, std::vector<SimulatedGameActionMetadata>>
113 
122  virtual size_t Run(size_t milisecondsTotal,
123  size_t maxIterationCount,
124  SimulatedGameSnapshotObserver* observer = nullptr);
125 
126  void DebugPrintPlan(
127  std::unordered_map<const ISimulatedGameUnit*, std::vector<const ISimulatedGameAction*>>& plan) const;
128  void DebugPrintPlanWithMetadata(
129  std::unordered_map<const ISimulatedGameUnit*, std::vector<SimulatedGameActionMetadata>>& plan) const;
130  void DebugPrintFullPlayout(
131  std::vector<std::pair<const ISimulatedGameUnit*, const ISimulatedGameAction*>>& playout) const;
132 
135 
141  void ClearStatistics();
142 
143  private:
144  void Iteration();
145  void IterationWithDebug(SimulatedGameSnapshotObserver& observer);
146 
147  void Reset();
148  void CreateRoot();
149 
150  std::vector<std::shared_ptr<SimulatedGameThinkingUnit>> thinkingUnits{};
151  std::vector<std::shared_ptr<SimulatedGameStochasticUnit>> stochasticUnits{};
152 
153  RandomGenerator rand_gen{};
154  std::unique_ptr<SimulatedGameNode> rootNode{};
155  SimulatedGameNode* currentNode{};
157  SimulatedGameThinkingUnit* startingUnit{};
158  };
159  }
160 }
161 #endif //GRAIL_SIMULATED_GAME_H
grail::simulation::SimulatedGame::GetStartingUnitActionsWithMetadata
std::vector< SimulatedGameActionMetadata > GetStartingUnitActionsWithMetadata() const
Call this method after certain number of iterations performed (by calling Run() first).
Definition: SimulatedGame.cpp:329
grail::simulation::SimulatedGameActionMetadata
This class represents action defined in the SimulatedGames module with additional statistics (score,...
Definition: SimulatedGameActionMetadata.hh:13
grail::simulation::ISimulatedGameAction
Base class for all actions in SimulatedGame. Derive from it for your actions.
Definition: ISimulatedGameAction.hh:39
grail::simulation::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:240
grail::simulation::SimulatedGameNode
This class should not be visible to developers at all.
Definition: SimulatedGameNode.h:23
grail::simulation::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:142
grail::simulation::SimulatedGame::ClearStatistics
void ClearStatistics()
This method invalidates the tree built for the game. If any offline learners were attached,...
Definition: SimulatedGame.cpp:94
grail::simulation::SimulatedGame::GetStartingUnitBestAction
const ISimulatedGameAction * GetStartingUnitBestAction() const
Call this method after certain number of iterations performed (by calling Run() first).
Definition: SimulatedGame.cpp:282
grail::simulation::SimulatedGame
The main interface class for the SimulatedGame reasoner based on the MCTS algorithm....
Definition: SimulatedGame.hh:28
grail::simulation::SimulatedGame::SetStartingUnit
void SetStartingUnit(SimulatedGameThinkingUnit &unit)
Gets or sets the unit that acts first in the starting state of the game.
Definition: SimulatedGame.cpp:89
grail::simulation::SimulatedGameHelper
Definition: SimulatedGameHelper.h:16
grail::simulation::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:308
grail::simulation::SimulatedGame::AddUnits
void AddUnits(std::vector< std::shared_ptr< SimulatedGameThinkingUnit >> units)
Adds new regular units to the game.
Definition: SimulatedGame.cpp:273
grail::simulation::SimulatedGameSnapshotObserver
Definition: SimulatedGameSnapshotObserver.h:21
grail::simulation::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:297
grail::simulation::SimulatedGame::GetStartingUnitBestActionMetadata
const SimulatedGameActionMetadata GetStartingUnitBestActionMetadata() const
Call this method after certain number of iterations performed (by calling Run() first).
Definition: SimulatedGame.cpp:291
grail::simulation::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:319
grail::simulation::SimulatedGameThinkingUnit
A base class of a unit related to a rational/intelligent player. MCTS chooses actions for this kind o...
Definition: SimulatedGameThinkingUnit.hh:21
grail::simulation::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:193
grail::simulation::SimulatedGameStochasticUnit
A base class of a unit in SimulatedGame that peforms actions according to some probability distributi...
Definition: SimulatedGameStochasticUnit.hh:21
grail::simulation::SimulatedGame::AddUnit
SimulatedGameThinkingUnit & AddUnit(std::shared_ptr< SimulatedGameThinkingUnit > unit)
Adds a new regular unit to the game.
Definition: SimulatedGame.cpp:223
grail::simulation::ISimulatedGameUnit
Base class of a unit in the SimulatedGame framework. A unit represents part of the game-state and pef...
Definition: ISimulatedGameUnit.hh:26