(C++)  1.0.0
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 "ISimulatedGameAction.hh"
5 #include "SimulatedGameActionMetadata.hh"
6 #include <vector>
7 #include <memory>
8 #include <random>
9 #include <unordered_map>
10 
11 typedef std::mt19937_64 RandomGenerator;
12 
13 namespace grail
14 {
15  namespace simulation
16  {
17  class SimulatedGameThinkingUnit;
18  class ISimulatedGameUnit;
19  class SimulatedGameObserverForGUI;
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, RandomGenerator& rand_gen, SimulatedGameObserverForGUI& observer);
38  SimulatedGameNode* PerformUCT(SimulatedGameHelper& helper, RandomGenerator& rand_gen);
39 
40  std::vector<SimulatedGameActionMetadata> GetActionsWithMetadata() const;
41  void FillPlan(std::unordered_map<const ISimulatedGameUnit*, std::vector<const ISimulatedGameAction*>>& plan, int teamIndex) const;
42  void FillPlanWithMetadata(std::unordered_map<const ISimulatedGameUnit*, std::vector<SimulatedGameActionMetadata>>& plan, int teamIndex) const;
43  void FillPath(std::vector<std::pair<const ISimulatedGameUnit*, const ISimulatedGameAction*>>& path) const;
44 
45  const ISimulatedGameAction* GetBestAction() const;
46  const SimulatedGameActionMetadata GetBestActionMetadata() const;
47  const ISimulatedGameAction& GetActionByIndex(size_t actionIndex) const;
48  double GetVisits() const;
49 
50  std::vector<std::unique_ptr<SimulatedGameNode>> children{};
51 
52  bool IsTerminal() const;
53  private:
54  size_t GetBestActionIndex() const;
55 
56  ISimulatedGameUnit* activeUnit = nullptr;
57  double totalScore = 0.0;
58  double q = 0.0;
59  std::vector<std::unique_ptr<const ISimulatedGameAction>> actions{};
60  std::vector<float> terminalScores{};
61 
62  double visits = 0.0;
63 
64  friend class SimulatedGame;
65  };
66  }
67 }
68 #endif //GRAIL_SIMULATED_GAME_NODE_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
This class should not be visible to developers at all.
Definition: SimulatedGameNode.h:24
Definition: SimulatedGameObserverForGUI.h:22
An interface that is used to terminate simulation in MCTS and set scores to players....
Definition: SimulatedGameRuntime.hh:16
This class represents action defined in the SimulatedGames module with additional statistics (score,...
Definition: SimulatedGameActionMetadata.hh:14
Definition: SimulatedGameHelper.h:17