Grail (C++)  1.1.1
A multi-platform, modular, universal engine for embedding advanced AI in games.
SimulatedGameNode.h
1 #ifndef GRAIL_SIMULATED_GAME_NODE_H
2 #define GRAIL_SIMULATED_GAME_NODE_H
3 
4 #include <memory>
5 #include <random>
6 #include <unordered_map>
7 #include <vector>
8 #include "ISimulatedGameAction.hh"
9 #include "SimulatedGameActionMetadata.hh"
10 
11 using RandomGenerator = std::mt19937_64;
12 
13 namespace grail
14 {
15  namespace simulation
16  {
17  class SimulatedGameThinkingUnit;
18  class ISimulatedGameUnit;
19  class SimulatedGameSnapshotObserver;
20  struct SimulatedGameHelper;
21 
24  {
25  public:
28 
29  void Backpropagate(float score);
30 
31  const ISimulatedGameAction* GetRandomAction(RandomGenerator& rand_gen);
32  void PerformActionByIndex(size_t actionIndex, SimulatedGameRuntime& runtime);
33 
34  ISimulatedGameUnit* GetActiveUnit() const;
35 
36  SimulatedGameNode* SelectNext(SimulatedGameHelper& helper, RandomGenerator& rand_gen);
37  SimulatedGameNode* SelectNext(SimulatedGameHelper& helper,
38  RandomGenerator& rand_gen,
40  SimulatedGameNode* PerformUCT(SimulatedGameHelper& helper, RandomGenerator& rand_gen);
41 
42  std::vector<SimulatedGameActionMetadata> GetActionsWithMetadata() const;
43  void FillPlan(std::unordered_map<const ISimulatedGameUnit*, std::vector<const ISimulatedGameAction*>>& plan,
44  int teamIndex) const;
45  void FillPlanWithMetadata(
46  std::unordered_map<const ISimulatedGameUnit*, std::vector<SimulatedGameActionMetadata>>& plan,
47  int teamIndex) const;
48  void FillPath(std::vector<std::pair<const ISimulatedGameUnit*, const ISimulatedGameAction*>>& path) const;
49 
50  const ISimulatedGameAction* GetBestAction() const;
51  const SimulatedGameActionMetadata GetBestActionMetadata() const;
52  const ISimulatedGameAction& GetActionByIndex(size_t actionIndex) const;
53  double GetVisits() const;
54 
55  std::vector<std::unique_ptr<SimulatedGameNode>> children{};
56 
57  bool IsTerminal() const;
58  private:
59  size_t GetBestActionIndex() const;
60 
61  ISimulatedGameUnit* activeUnit = nullptr;
62  double totalScore = 0.0;
63  double q = 0.0;
64  std::vector<std::unique_ptr<const ISimulatedGameAction>> actions{};
65  std::vector<float> terminalScores{};
66 
67  double visits = 0.0;
68 
69  friend class SimulatedGame;
70  };
71  }
72 }
73 #endif //GRAIL_SIMULATED_GAME_NODE_H
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::SimulatedGameNode
This class should not be visible to developers at all.
Definition: SimulatedGameNode.h:23
grail::simulation::SimulatedGame
The main interface class for the SimulatedGame reasoner based on the MCTS algorithm....
Definition: SimulatedGame.hh:28
grail::simulation::SimulatedGameHelper
Definition: SimulatedGameHelper.h:16
grail::simulation::SimulatedGameSnapshotObserver
Definition: SimulatedGameSnapshotObserver.h:21
grail::simulation::SimulatedGameRuntime
An interface that is used to terminate simulation in MCTS and set scores to players....
Definition: SimulatedGameRuntime.hh:15
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