(C++)  1.1.0
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 "SimulatedGameActionMetadata.hh"
5 #include "SimulatedGameSnapshotObserver.h"
6 #include "ISimulatedGameUnit.hh"
7 #include "SimulatedGameHelper.h"
8 #include "SimulatedGameNode.h"
9 
10 
11 #include <unordered_map>
12 #include <memory>
13 #include <vector>
14 #include <limits>
15 
16 namespace grail
17 {
18  namespace simulation
19  {
20  class SimulatedGameThinkingUnit;
21  class SimulatedGameStochasticUnit;
22  class ISimulatedGameAction;
23  class SimulatedGameNode;
24 
30  {
31  public:
42  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{}());
43 
44  virtual ~SimulatedGame();
45 
50  SimulatedGameThinkingUnit& AddUnit(std::shared_ptr<SimulatedGameThinkingUnit> unit);
51 
55  void AddUnits(std::vector<std::shared_ptr<SimulatedGameThinkingUnit>> units);
56 
61  SimulatedGameStochasticUnit& AddUnit(std::shared_ptr<SimulatedGameStochasticUnit> unit);
62 
67  void RemoveUnit(const SimulatedGameThinkingUnit* unit);
68 
73  void RemoveUnit(const SimulatedGameStochasticUnit* unit);
74 
79 
85 
90  std::vector<std::pair<const ISimulatedGameUnit*, const ISimulatedGameAction*>> GetExpectedPlayout() const;
91 
96  std::vector<SimulatedGameActionMetadata> GetStartingUnitActionsWithMetadata() const;
97 
102  std::unordered_map<const ISimulatedGameUnit*, std::vector<const ISimulatedGameAction*>> GetExpectedPlans();
103 
108  std::unordered_map<const ISimulatedGameUnit*, std::vector<SimulatedGameActionMetadata>> GetExpectedPlansWithMetadata();
109 
118  virtual size_t Run(size_t milisecondsTotal, size_t maxIterationCount, SimulatedGameSnapshotObserver* observer = nullptr);
119 
120  void DebugPrintPlan(std::unordered_map<const ISimulatedGameUnit*, std::vector<const ISimulatedGameAction*>>& plan) const;
121  void DebugPrintPlanWithMetadata(std::unordered_map<const ISimulatedGameUnit*, std::vector<SimulatedGameActionMetadata>>& plan) const;
122  void DebugPrintFullPlayout(std::vector<std::pair<const ISimulatedGameUnit*, const ISimulatedGameAction*>>& playout) const;
123 
126 
132  void ClearStatistics();
133 
134  private:
135  void Iteration();
136  void IterationWithDebug(SimulatedGameSnapshotObserver& observer);
137 
138  void Reset();
139  void CreateRoot();
140 
141  std::vector<std::shared_ptr<SimulatedGameThinkingUnit>> thinkingUnits{};
142  std::vector<std::shared_ptr<SimulatedGameStochasticUnit>> stochasticUnits{};
143 
144  RandomGenerator rand_gen{};
145  std::unique_ptr<SimulatedGameNode> rootNode{};
146  SimulatedGameNode* currentNode{};
148  SimulatedGameThinkingUnit* startingUnit{};
149  };
150  }
151 }
152 #endif //GRAIL_SIMULATED_GAME_H
Base class for all actions in SimulatedGame. Derive from it for your actions.
Definition: ISimulatedGameAction.hh:41
Base class of a unit in the SimulatedGame framework. A unit represents part of the game-state and pef...
Definition: ISimulatedGameUnit.hh:27
The main interface class for the SimulatedGame reasoner based on the MCTS algorithm....
Definition: SimulatedGame.hh:30
void AddUnits(std::vector< std::shared_ptr< SimulatedGameThinkingUnit >> units)
Adds new regular units to the game.
Definition: SimulatedGame.cpp:236
void SetStartingUnit(SimulatedGameThinkingUnit &unit)
Gets or sets the unit that acts first in the starting state of the game.
Definition: SimulatedGame.cpp:83
const ISimulatedGameAction * GetStartingUnitBestAction() const
Call this method after certain number of iterations performed (by calling Run() first).
Definition: SimulatedGame.cpp:243
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:263
std::vector< SimulatedGameActionMetadata > GetStartingUnitActionsWithMetadata() const
Call this method after certain number of iterations performed (by calling Run() first).
Definition: SimulatedGame.cpp:278
SimulatedGameThinkingUnit & AddUnit(std::shared_ptr< SimulatedGameThinkingUnit > unit)
Adds a new regular unit to the game.
Definition: SimulatedGame.cpp:191
const SimulatedGameActionMetadata GetStartingUnitBestActionMetadata() const
Call this method after certain number of iterations performed (by calling Run() first).
Definition: SimulatedGame.cpp:250
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:205
void ClearStatistics()
This method invalidates the tree built for the game. If any offline learners were attached,...
Definition: SimulatedGame.cpp:88
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:127
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:169
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:271
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:255
This class should not be visible to developers at all.
Definition: SimulatedGameNode.h:24
Definition: SimulatedGameSnapshotObserver.h:22
A base class of a unit in SimulatedGame that peforms actions according to some probability distributi...
Definition: SimulatedGameStochasticUnit.hh:22
A base class of a unit related to a rational/intelligent player. MCTS chooses actions for this kind o...
Definition: SimulatedGameThinkingUnit.hh:21
This class represents action defined in the SimulatedGames module with additional statistics (score,...
Definition: SimulatedGameActionMetadata.hh:14
Definition: SimulatedGameHelper.h:17