Grail (C++)  1.4.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
grail::simgames::SimulatedGame Class Reference

The main interface class for the SimulatedGame reasoner based on the MCTS algorithm. The class supports definition of a game, running simulations and obtaining results. More...

#include <SimulatedGame.hh>

Public Member Functions

 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. More...
 
SimulatedGameThinkingUnitAddUnit (std::shared_ptr< SimulatedGameThinkingUnit > unit)
 Adds a new regular unit to the game. More...
 
void AddUnits (std::vector< std::shared_ptr< SimulatedGameThinkingUnit >> units)
 Adds new regular units to the game.
 
SimulatedGameStochasticUnitAddUnit (std::shared_ptr< SimulatedGameStochasticUnit > unit)
 Adds a new stochastic (performing random actions) unit to the game. More...
 
void RemoveUnit (const SimulatedGameThinkingUnit *unit)
 Removes a regular unit from the game Might be useful when the game object is reused for different situations.
 
void RemoveUnit (const SimulatedGameStochasticUnit *unit)
 Removes a stochastic (performing random actions) unit to the game. Might be useful when the game object is reused for different situations.
 
const ISimulatedGameActionGetStartingUnitBestAction () const
 Call this method after certain number of iterations performed (by calling Run() first).
 
const SimulatedGameActionMetadata GetStartingUnitBestActionMetadata () const
 Call this method after certain number of iterations performed (by calling Run() first). More...
 
std::vector< std::pair< const ISimulatedGameUnit *, const ISimulatedGameAction * > > GetExpectedPlayout () const
 Call this method after certain number of iterations performed (by calling Run() first). More...
 
std::vector< SimulatedGameActionMetadataGetStartingUnitActionsWithMetadata () const
 Call this method after certain number of iterations performed (by calling Run() first). More...
 
std::unordered_map< const ISimulatedGameUnit *, std::vector< const ISimulatedGameAction * > > GetExpectedPlans ()
 Call this method after certain number of iterations performed (by calling Run() first). More...
 
std::unordered_map< const ISimulatedGameUnit *, std::vector< SimulatedGameActionMetadata > > GetExpectedPlansWithMetadata ()
 Call this method after certain number of iterations performed (by calling Run() first). More...
 
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 iterations. Both parameters have relatively high default values. The method returns when at least one termination condition occurs. More...
 
void DebugPrintPlan (std::unordered_map< const ISimulatedGameUnit *, std::vector< const ISimulatedGameAction * >> &plan) const
 
void DebugPrintPlanWithMetadata (std::unordered_map< const ISimulatedGameUnit *, std::vector< SimulatedGameActionMetadata >> &plan) const
 
void DebugPrintFullPlayout (std::vector< std::pair< const ISimulatedGameUnit *, const ISimulatedGameAction * >> &playout) const
 
void SetStartingUnit (SimulatedGameThinkingUnit &unit)
 Gets or sets the unit that acts first in the starting state of the game.
 
void ClearStatistics ()
 This method invalidates the tree built for the game. If any offline learners were attached, their gathered data is cleared too. This method effectively reverts all changes done by the Run() method.
 

Detailed Description

The main interface class for the SimulatedGame reasoner based on the MCTS algorithm. The class supports definition of a game, running simulations and obtaining results.

Constructor & Destructor Documentation

◆ SimulatedGame()

grail::simgames::SimulatedGame::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.

Parameters
teamCount- This is the number of intelligent players in the game. Each team shares the score that is given when the game ends.
maxGameScore- The maximum score that can be awarded to a team when the game ends. This value does not need to be given precisely or even correctly. However, the closer to the correct maximum value, the better for the algorithm's efficiency.
explorationBoost- A modifier that affects how much exploration of state-space is boosted compared to the default value of 1. Values lower than 1.0 will reduce the exploration. Values lower than 0.1 will be clamped to 0.1.
freezeVisitsTreshold- When a certain state is visited at least this number of times, then the best action so far will be fixed as the final chosen action (no more exploration here).
seed- Initialization seed to the random number generator.

Member Function Documentation

◆ AddUnit() [1/2]

SimulatedGameStochasticUnit & grail::simgames::SimulatedGame::AddUnit ( std::shared_ptr< SimulatedGameStochasticUnit unit)

Adds a new stochastic (performing random actions) unit to the game.

Returns
Returns a reference to the added unit.

◆ AddUnit() [2/2]

SimulatedGameThinkingUnit & grail::simgames::SimulatedGame::AddUnit ( std::shared_ptr< SimulatedGameThinkingUnit unit)

Adds a new regular unit to the game.

Returns
Returns a reference to the added unit.

◆ GetExpectedPlans()

std::unordered_map< const ISimulatedGameUnit *, std::vector< const ISimulatedGameAction * > > grail::simgames::SimulatedGame::GetExpectedPlans ( )

Call this method after certain number of iterations performed (by calling Run() first).

Returns
List of actions per unit that have are the most promising and probable.

◆ GetExpectedPlansWithMetadata()

std::unordered_map< const ISimulatedGameUnit *, std::vector< SimulatedGameActionMetadata > > grail::simgames::SimulatedGame::GetExpectedPlansWithMetadata ( )

Call this method after certain number of iterations performed (by calling Run() first).

Returns
List of actions per unit with additional statistics that have are the most promising and probable.

◆ GetExpectedPlayout()

std::vector< std::pair< const ISimulatedGameUnit *, const ISimulatedGameAction * > > grail::simgames::SimulatedGame::GetExpectedPlayout ( ) const

Call this method after certain number of iterations performed (by calling Run() first).

Returns
List of ordered pairs (unit, action) that have are the most promising and probable.

◆ GetStartingUnitActionsWithMetadata()

std::vector< SimulatedGameActionMetadata > grail::simgames::SimulatedGame::GetStartingUnitActionsWithMetadata ( ) const

Call this method after certain number of iterations performed (by calling Run() first).

Returns
Currently available actions of the starting unit with additional statistics

◆ GetStartingUnitBestActionMetadata()

const SimulatedGameActionMetadata grail::simgames::SimulatedGame::GetStartingUnitBestActionMetadata ( ) const

Call this method after certain number of iterations performed (by calling Run() first).

Returns
The best action with some additional statistics

◆ Run()

size_t grail::simgames::SimulatedGame::Run ( size_t  milisecondsTotal,
size_t  maxIterationCount,
SimulatedGameSnapshotObserver observer = nullptr 
)
virtual

Performs simulations of the game. Pass the available time for computations and the maximum number of iterations. Both parameters have relatively high default values. The method returns when at least one termination condition occurs.

Parameters
milisecondsTotal- The time allowed for iterations in miliseconds.
maxIterationCount- The number of iterations to perform.
observer- SimulatedGameSnapshotObserver object that provides additional feedback from simulations. Used for GUI tools debugging; warning: comes with computational overhead. Pass null for high-performance execution.
Returns
Returns the number of iterations performed.

The documentation for this class was generated from the following files: