(C++)  1.0.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
grail::simulation::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. More...
 
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. More...
 
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. More...
 
const ISimulatedGameActionGetStartingUnitBestAction () const
 Call this method after certain number of iterations performed (by calling Run() first). More...
 
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, SimulatedGameObserverForGUI *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. More...
 

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::simulation::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
teamCountThis is the number of intelligent players in the game. Each team shares the score that is given when the game ends.
maxGameScoreThe 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.
explorationBoostA 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.
freezeVisitsTresholdWhen 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).///
seedInitialization seed to the random number generator.

Member Function Documentation

◆ AddUnit() [1/2]

SimulatedGameStochasticUnit & grail::simulation::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::simulation::SimulatedGame::AddUnit ( std::shared_ptr< SimulatedGameThinkingUnit unit)

Adds a new regular unit to the game.

Returns
Returns a reference to the added unit.

◆ AddUnits()

void grail::simulation::SimulatedGame::AddUnits ( std::vector< std::shared_ptr< SimulatedGameThinkingUnit >>  units)

Adds new regular units to the game.

◆ ClearStatistics()

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


◆ GetExpectedPlans()

std::unordered_map< const ISimulatedGameUnit *, std::vector< const ISimulatedGameAction * > > grail::simulation::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::simulation::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::simulation::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::simulation::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

◆ GetStartingUnitBestAction()

const ISimulatedGameAction * grail::simulation::SimulatedGame::GetStartingUnitBestAction ( ) const

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

◆ GetStartingUnitBestActionMetadata()

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

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

returns>The best action with some additional statistics

◆ RemoveUnit() [1/2]

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

◆ RemoveUnit() [2/2]

void grail::simulation::SimulatedGame::RemoveUnit ( const SimulatedGameThinkingUnit unit)

Removes a regular unit from the game Might be useful when the game object is reused for different situations.

◆ Run()

size_t grail::simulation::SimulatedGame::Run ( size_t  milisecondsTotal,
size_t  maxIterationCount,
SimulatedGameObserverForGUI 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
milisecondsTotalThe time allowed for iterations in miliseconds.
maxIterationCountThe number of iterations to perform.
observerSimulatedGameObserverForGUI 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: