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
, andsave_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
andcalculate_payoffs
which should define how the game assigns payoffs to each strategy for each possible game context. In particular,calculate_payoffs
should fill the arrayself.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
Calculates the Fitness of a strategy for a given population state.
This method calculates the payoffs for each strategy in each possible group configuration.
Number of different strategies playing the game.
Returns the payoff of a strategy given a group composition.
Returns the payoff matrix of the game.
This method fills the
game_payoffs
container with the payoff of each strategy given thegroup_composition
.Stores the payoff matrix in a txt file.
returns the type of game.
- __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)¶
- 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
- 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
- payoff(strategy, group_composition)[source]¶
Returns the payoff of a strategy given a group composition.
If the group composition does not include the strategy, the payoff should be zero.
- abstract play(group_composition, game_payoffs)[source]¶
This method fills the
game_payoffs
container with the payoff of each strategy given thegroup_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