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