(C++)  1.0.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 "SimulatedGameObserverForGUI.h"
6 #include "ISimulatedGameUnit.hh"
7 #include "SimulatedGameHelper.h"
8 
9 
10 #include <unordered_map>
11 #include <memory>
12 #include <vector>
13 #include <limits>
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, double maxScore = 1.0, double explorationBoost = 1.0, size_t freezeVisitsTreshold = std::numeric_limits<size_t>::max(), RandomGenerator::result_type seed = std::random_device{}());
42 
43  virtual ~SimulatedGame();
44 
49  SimulatedGameThinkingUnit& AddUnit(std::shared_ptr<SimulatedGameThinkingUnit> unit);
50 
54  void AddUnits(std::vector<std::shared_ptr<SimulatedGameThinkingUnit>> units);
55 
60  SimulatedGameStochasticUnit& AddUnit(std::shared_ptr<SimulatedGameStochasticUnit> unit);
61 
66  void RemoveUnit(const SimulatedGameThinkingUnit* unit);
67 
72  void RemoveUnit(const SimulatedGameStochasticUnit* unit);
73 
78 
84 
89  std::vector<std::pair<const ISimulatedGameUnit*, const ISimulatedGameAction*>> GetExpectedPlayout() const;
90 
95  std::vector<SimulatedGameActionMetadata> GetStartingUnitActionsWithMetadata() const;
96 
101  std::unordered_map<const ISimulatedGameUnit*, std::vector<const ISimulatedGameAction*>> GetExpectedPlans();
102 
107  std::unordered_map<const ISimulatedGameUnit*, std::vector<SimulatedGameActionMetadata>> GetExpectedPlansWithMetadata();
108 
117  virtual size_t Run(size_t milisecondsTotal, size_t maxIterationCount, SimulatedGameObserverForGUI* observer = nullptr);
118 
119  void DebugPrintPlan(std::unordered_map<const ISimulatedGameUnit*, std::vector<const ISimulatedGameAction*>>& plan) const;
120  void DebugPrintPlanWithMetadata(std::unordered_map<const ISimulatedGameUnit*, std::vector<SimulatedGameActionMetadata>>& plan) const;
121  void DebugPrintFullPlayout(std::vector<std::pair<const ISimulatedGameUnit*, const ISimulatedGameAction*>>& playout) const;
122 
125 
131  void ClearStatistics();
132 
133  private:
134  void Iteration();
135  void IterationWithDebug(SimulatedGameObserverForGUI& observer);
136 
137  void Reset();
138  void CreateRoot();
139 
140  std::vector<std::shared_ptr<SimulatedGameThinkingUnit>> thinkingUnits;
141  std::vector<std::shared_ptr<SimulatedGameStochasticUnit>> stochasticUnits;
142 
143  RandomGenerator rand_gen;
144  std::unique_ptr<SimulatedGameNode> rootNode;
145  SimulatedGameNode* currentNode;
147  SimulatedGameThinkingUnit* startingUnit;
148  };
149  }
150 }
151 #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:29
void AddUnits(std::vector< std::shared_ptr< SimulatedGameThinkingUnit >> units)
Adds new regular units to the game.
Definition: SimulatedGame.cpp:237
void SetStartingUnit(SimulatedGameThinkingUnit &unit)
Gets or sets the unit that acts first in the starting state of the game.
Definition: SimulatedGame.cpp:84
const ISimulatedGameAction * GetStartingUnitBestAction() const
Call this method after certain number of iterations performed (by calling Run() first).
Definition: SimulatedGame.cpp:244
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:264
std::vector< SimulatedGameActionMetadata > GetStartingUnitActionsWithMetadata() const
Call this method after certain number of iterations performed (by calling Run() first).
Definition: SimulatedGame.cpp:279
SimulatedGameThinkingUnit & AddUnit(std::shared_ptr< SimulatedGameThinkingUnit > unit)
Adds a new regular unit to the game.
Definition: SimulatedGame.cpp:192
const SimulatedGameActionMetadata GetStartingUnitBestActionMetadata() const
Call this method after certain number of iterations performed (by calling Run() first).
Definition: SimulatedGame.cpp:251
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:206
void ClearStatistics()
This method invalidates the tree built for the game. If any offline learners were attached,...
Definition: SimulatedGame.cpp:89
virtual size_t Run(size_t milisecondsTotal, size_t maxIterationCount, SimulatedGameObserverForGUI *observer=nullptr)
Performs simulations of the game. Pass the available time for computations and the maximum number of ...
Definition: SimulatedGame.cpp:128
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:170
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:272
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:256
This class should not be visible to developers at all.
Definition: SimulatedGameNode.h:24
Definition: SimulatedGameObserverForGUI.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