egttools.games.AbstractTwoPLayerGame

class AbstractTwoPLayerGame(nb_strategies)[source]

Bases: AbstractGame

This abstract Game class can be used in most scenarios where the fitness of a strategy is calculated as its expected payoff given the population state.

It assumes that the game is 2 player and the fitness is calculated with this assumption!

Notes

It might be a good idea to overwrite the methods __str__, type, and save_payoffs to adapt to your given game implementation

It assumes that you have at least the following attributes:

1. And an attribute self.nb_strategies_ which contains the number of strategies that you are going to analyse for the given game. 2. self.payoffs_ which must be a numpy.ndarray and contain the payoff matrix of the game. This array must be of shape (self.nb_strategies_, self.nb_strategies_).

For normal form games: 1. There is already a class called NormalFormGame available which you can use for these types of games. If for any reason this does not cover your needs then: 2. If your game is normal form, but iterated, you should create another variable to contain the payoff matrix for one round of the game, since self.payoffs_ will contain the expected payoffs over the several rounds of the game. 3. If the game is one-shot and normal form, self.payoffs_ is the payoff matrix of the game, and you do not need to do anything in calculate_payoffs besides calling this matrix.

You must still implement the methods play and calculate_payoffs which should define how the game assigns payoffs to each strategy for each possible game context. In particular, calculate_payoffs should fill the array self.payoffs_ with the correct values as explained above. We recommend that you run this method in the __init__ (initialization of the object) since, these values must be set before passing the game object to the numerical simulator (e.g., egttools.numerical.PairwiseComparisonNumerical).

This class must be initialized with the total number of strategies that will be used and the size of the group in which the game takes place. This is required to calculate the number of group configurations and the correct shape of the payoff matrix.

Parameters:

nb_strategies (int) – total number of possible strategies.

Methods

calculate_fitness

Calculates the Fitness of a strategy for a given population state.

calculate_payoffs

This method calculates the payoffs for each strategy in each possible group configuration.

nb_strategies

Returns the number of strategies available in the game.

payoff

Returns the expected payoff of a specific strategy in a group.

payoffs

Returns the current payoff matrix of the game.

play

This method fills the game_payoffs container with the payoff of each strategy given the group_composition.

save_payoffs

Saves the current payoff matrix to a file.

type

Returns the type of the game as a string.

__init__(nb_strategies)[source]

This class must be initialized with the total number of strategies that will be used and the size of the group in which the game takes place. This is required to calculate the number of group configurations and the correct shape of the payoff matrix.

Parameters:

nb_strategies (int) – total number of possible strategies.

__new__(**kwargs)
__str__()[source]

Returns a string representation of the game object.

Returns:

A string describing the game instance.

Return type:

str

calculate_fitness(player_strategy, pop_size, population_state)[source]

Calculates the Fitness of a strategy for a given population state.

The calculation is done by computing the expected payoff over all possible strategy matches.

Parameters:
  • player_strategy (int) – index of the strategy.

  • pop_size (int) – size of the population - Only necessary for compatibility with the C++ implementation (might be eliminated in the future).

  • population_state (numpy.ndarray[numpy.uint64[m, 1]]) – vector with the population state (the number of players adopting each strategy).

Returns:

The fitness of the population.

Return type:

float

abstract calculate_payoffs()[source]

This method calculates the payoffs for each strategy in each possible group configuration. Thus, it must fill the self.payoffs_ numpy.ndarray with these payoffs values. This array must be of shape (self.nb_strategies_, nb_group_configurations), where nb_group_configurations is the number of possible combinations of strategies in the group. Thus, each row should give the (expected) payoff of the row strategy when playing in a group with the column configuration.

Returns:

The payoff matrix of the game.

Return type:

numpy.ndarray

nb_strategies()[source]

Returns the number of strategies available in the game.

Returns:

The total number of strategies.

Return type:

int

payoff(strategy, group_composition)[source]

Returns the expected payoff of a specific strategy in a group.

Parameters:
  • strategy (int) – Index of the focal strategy.

  • group_composition (NDArray[np.int64]) – Vector specifying the number of individuals using each strategy.

Returns:

Expected payoff of the strategy in the given group context.

Return type:

float

payoffs()[source]

Returns the current payoff matrix of the game.

Returns:

The stored payoff matrix used in the game.

Return type:

NDArray[np.float64]

abstract play(group_composition, game_payoffs)[source]

This method fills the game_payoffs container with the payoff of each strategy given the group_composition.

Strategies not present in the group will receive 0 payoff by default.

Parameters:
  • group_composition (Union[List[int], numpy.ndarray]) – A List or a numpy.ndarray containing the counts of each strategy in the group (e.g., for a game with 3 possible strategies and group size 4, the following List is possible [3, 0, 1]).

  • game_payoffs (numpy.ndarray) – A container for the payoffs that will be calculated. This avoids needing to create a new array at each call and should speed up computation.

Return type:

None

save_payoffs(file_name)[source]

Saves the current payoff matrix to a file.

Parameters:

file_name (str) – Name of the file to which the matrix should be saved.

Return type:

None

type()[source]

Returns the type of the game as a string.

Returns:

A label identifying the game type (e.g., “NormalFormGame”).

Return type:

str

__annotations__ = {}