Normal form games with N players.#

This module implements a class for normal form games (strategic form games) [NN2007]. At present the following algorithms are implemented to compute equilibria of these games:

  • 'enumeration' - An implementation of the support enumeration algorithm built in Sage.

  • 'LCP' - An interface with the ‘gambit’ solver’s implementation of the Lemke-Howson algorithm.

  • 'lp' - A built-in Sage implementation (with a gambit alternative) of a zero-sum game solver using linear programming. See MixedIntegerLinearProgram for more on MILP solvers in Sage.

  • 'lrs' - A solver interfacing with the ‘lrslib’ library.

The architecture for the class is based on the gambit architecture to ensure an easy transition between gambit and Sage. At present the algorithms for the computation of equilibria only solve 2 player games.

A very simple and well known example of normal form game is referred to as the ‘Battle of the Sexes’ in which two players Amy and Bob are modeled. Amy prefers to play video games and Bob prefers to watch a movie. They both however want to spend their evening together. This can be modeled using the following two matrices:

\[ \begin{align}\begin{aligned}\begin{split}A = \begin{pmatrix} 3&1\\ 0&2\\ \end{pmatrix}\end{split}\\\begin{split} B = \begin{pmatrix} 2&1\\ 0&3\\ \end{pmatrix}\end{split}\end{aligned}\end{align} \]

Matrix \(A\) represents the utilities of Amy and matrix \(B\) represents the utility of Bob. The choices of Amy correspond to the rows of the matrices:

  • The first row corresponds to video games.

  • The second row corresponds to movies.

Similarly Bob’s choices are represented by the columns:

  • The first column corresponds to video games.

  • The second column corresponds to movies.

Thus, if both Amy and Bob choose to play video games: Amy receives a utility of 3 and Bob a utility of 2. If Amy is indeed going to stick with video games Bob has no incentive to deviate (and vice versa).

This situation repeats itself if both Amy and Bob choose to watch a movie: neither has an incentive to deviate.

This loosely described situation is referred to as a Nash Equilibrium. We can use Sage to find them, and more importantly, see if there is any other situation where Amy and Bob have no reason to change their choice of action:

Here is how we create the game in Sage:

sage: A = matrix([[3, 1], [0, 2]])
sage: B = matrix([[2, 1], [0, 3]])
sage: battle_of_the_sexes = NormalFormGame([A, B])
sage: battle_of_the_sexes
Normal Form Game with the following utilities: {(0, 0): [3, 2],
 (0, 1): [1, 1], (1, 0): [0, 0], (1, 1): [2, 3]}

To obtain the Nash equilibria we run the obtain_nash() method. In the first few examples, we will use the ‘support enumeration’ algorithm. A discussion about the different algorithms will be given later:

sage: battle_of_the_sexes.obtain_nash(algorithm='enumeration')
[[(0, 1), (0, 1)], [(3/4, 1/4), (1/4, 3/4)], [(1, 0), (1, 0)]]

If we look a bit closer at our output we see that a list of three pairs of tuples have been returned. Each of these correspond to a Nash Equilibrium, represented as a probability distribution over the available strategies:

  • \([(1, 0), (1, 0)]\) corresponds to the first player only playing their first strategy and the second player also only playing their first strategy. In other words Amy and Bob both play video games.

  • \([(0, 1), (0, 1)]\) corresponds to the first player only playing their second strategy and the second player also only playing their second strategy. In other words Amy and Bob both watch movies.

  • \([(3/4, 1/4), (1/4, 3/4)]\) corresponds to players \(mixing\) their strategies. Amy plays video games 75% of the time and Bob watches movies 75% of the time. At this equilibrium point Amy and Bob will only ever do the same activity \(3/8\) of the time.

We can use Sage to compute the expected utility for any mixed strategy pair \((\sigma_1, \sigma_2)\). The payoff to player 1 is given by the vector/matrix multiplication:

\[\sigma_1 A \sigma_2\]

The payoff to player 2 is given by:

\[\sigma_1 B \sigma_2\]

To compute this in Sage we have:

sage: for ne in battle_of_the_sexes.obtain_nash(algorithm='enumeration'):
....:     print("Utility for {}: ".format(ne))
....:     print("{} {}".format(vector(ne[0]) * A * vector(ne[1]), vector(ne[0]) * B * vector(ne[1])))
Utility for [(0, 1), (0, 1)]:
2 3
Utility for [(3/4, 1/4), (1/4, 3/4)]:
3/2 3/2
Utility for [(1, 0), (1, 0)]:
3 2

Allowing players to play mixed strategies ensures that there will always be a Nash Equilibrium for a normal form game. This result is called Nash’s Theorem ([Nas1950]).

Let us consider the game called ‘matching pennies’ where two players each present a coin with either HEADS or TAILS showing. If the coins show the same side then player 1 wins, otherwise player 2 wins:

\[ \begin{align}\begin{aligned}\begin{split}A = \begin{pmatrix} 1&-1\\ -1&1\\ \end{pmatrix}\end{split}\\\begin{split} B = \begin{pmatrix} -1&1\\ 1&-1\\ \end{pmatrix}\end{split}\end{aligned}\end{align} \]

It should be relatively straightforward to observe, that there is no situation, where both players always do the same thing, and have no incentive to deviate.

We can plot the utility of player 1 when player 2 is playing a mixed strategy \(\sigma_2 = (y, 1-y)\) (so that the utility to player 1 for playing strategy number \(i\) is given by the matrix/vector multiplication \((Ay)_i\), ie element in position \(i\) of the matrix/vector multiplication \(Ay\))

sage: y = var('y')
sage: A = matrix([[1, -1], [-1, 1]])
sage: p = plot((A * vector([y, 1 - y]))[0], y, 0, 1, color='blue',
....:          legend_label='$u_1(r_1, (y, 1-y))$', axes_labels=['$y$', ''])
sage: p += plot((A * vector([y, 1 - y]))[1], y, 0, 1, color='red',
....:           legend_label='$u_1(r_2, (y, 1-y))$'); p
Graphics object consisting of 2 graphics primitives

We see that the only point at which player 1 is indifferent amongst the available strategies is when \(y = 1/2\).

If we compute the Nash equilibria we see that this corresponds to a point at which both players are indifferent:

sage: A = matrix([[1, -1], [-1, 1]])
sage: B = matrix([[-1, 1], [1, -1]])
sage: matching_pennies = NormalFormGame([A, B])
sage: matching_pennies.obtain_nash(algorithm='enumeration')
[[(1/2, 1/2), (1/2, 1/2)]]

The utilities to both players at this Nash equilibrium is easily computed:

sage: [vector([1/2, 1/2]) * M * vector([1/2, 1/2])
....:  for M in matching_pennies.payoff_matrices()]
[0, 0]

Note that the above uses the payoff_matrices method which returns the payoff matrices for a 2 player game:

sage: matching_pennies.payoff_matrices()
(
[ 1 -1]  [-1  1]
[-1  1], [ 1 -1]
)

One can also input a single matrix and then a zero sum game is constructed. Here is an instance of Rock-Paper-Scissors-Lizard-Spock:

sage: A = matrix([[0, -1, 1, 1, -1],
....:             [1, 0, -1, -1, 1],
....:             [-1, 1, 0, 1 , -1],
....:             [-1, 1, -1, 0, 1],
....:             [1, -1, 1, -1, 0]])
sage: g = NormalFormGame([A])
sage: g.obtain_nash(algorithm='enumeration')
[[(1/5, 1/5, 1/5, 1/5, 1/5), (1/5, 1/5, 1/5, 1/5, 1/5)]]

We can also study games where players aim to minimize their utility. Here is the Prisoner’s Dilemma (where players are aiming to reduce time spent in prison):

sage: A = matrix([[2, 5], [0, 4]])
sage: B = matrix([[2, 0], [5, 4]])
sage: prisoners_dilemma = NormalFormGame([A, B])
sage: prisoners_dilemma.obtain_nash(algorithm='enumeration', maximization=False)
[[(0, 1), (0, 1)]]

When obtaining Nash equilibrium the following algorithms are currently available:

  • 'lp': A solver for constant sum 2 player games using linear programming. This constructs a MixedIntegerLinearProgram using the solver which was passed in with solver to solve the linear programming representation of the game. See MixedIntegerLinearProgram for more on MILP solvers in Sage.

  • 'lrs': Reverse search vertex enumeration for 2 player games. This algorithm uses the optional ‘lrslib’ package. To install it, type sage -i lrslib in the shell. For more information, see [Av2000].

  • 'LCP': Linear complementarity program algorithm for 2 player games. This algorithm uses the open source game theory package: Gambit [Gambit]. At present this is the only gambit algorithm available in sage but further development will hope to implement more algorithms (in particular for games with more than 2 players). To install it, type sage -i gambit in the shell.

  • 'enumeration': Support enumeration for 2 player games. This algorithm is hard coded in Sage and checks through all potential supports of a strategy. Supports of a given size with a conditionally dominated strategy are ignored. Note: this is not the preferred algorithm. The algorithm implemented is a combination of a basic algorithm described in [NN2007] and a pruning component described in [SLB2008].

Below we show how the these algorithms are called:

sage: matching_pennies.obtain_nash(algorithm='lrs')  # optional - lrslib
[[(1/2, 1/2), (1/2, 1/2)]]
sage: matching_pennies.obtain_nash(algorithm='LCP')  # optional - gambit
[[(0.5, 0.5), (0.5, 0.5)]]
sage: matching_pennies.obtain_nash(algorithm='lp', solver='PPL')
[[(1/2, 1/2), (1/2, 1/2)]]
sage: matching_pennies.obtain_nash(algorithm='lp', solver='gambit') # optional - gambit
[[(0.5, 0.5), (0.5, 0.5)]]
sage: matching_pennies.obtain_nash(algorithm='enumeration')
[[(1/2, 1/2), (1/2, 1/2)]]

Note that if no algorithm argument is passed then the default will be selected according to the following order (if the corresponding package is installed):

  1. 'lp' (if the game is constant-sum; uses the solver chosen by Sage)

  2. 'lrs' (requires ‘lrslib’)

  3. 'enumeration'

Here is a game being constructed using gambit syntax (note that a NormalFormGame object acts like a dictionary with pure strategy tuples as keys and payoffs as their values):

sage: f = NormalFormGame()
sage: f.add_player(2)  # Adding first player with 2 strategies
sage: f.add_player(2)  # Adding second player with 2 strategies
sage: f[0,0][0] = 1
sage: f[0,0][1] = 3
sage: f[0,1][0] = 2
sage: f[0,1][1] = 3
sage: f[1,0][0] = 3
sage: f[1,0][1] = 1
sage: f[1,1][0] = 4
sage: f[1,1][1] = 4
sage: f
Normal Form Game with the following utilities: {(0, 0): [1, 3],
 (0, 1): [2, 3], (1, 0): [3, 1], (1, 1): [4, 4]}

Once this game is constructed we can view the payoff matrices and solve the game:

sage: f.payoff_matrices()
(
[1 2]  [3 3]
[3 4], [1 4]
)
sage: f.obtain_nash(algorithm='enumeration')
[[(0, 1), (0, 1)]]

We can add an extra strategy to the first player:

sage: f.add_strategy(0)
sage: f
Normal Form Game with the following utilities: {(0, 0): [1, 3],
 (0, 1): [2, 3],
 (1, 0): [3, 1],
 (1, 1): [4, 4],
 (2, 0): [False, False],
 (2, 1): [False, False]}

If we do this and try and obtain the Nash equilibrium or view the payoff matrices(without specifying the utilities), an error is returned:

sage: f.obtain_nash()
Traceback (most recent call last):
...
ValueError: utilities have not been populated
sage: f.payoff_matrices()
Traceback (most recent call last):
...
ValueError: utilities have not been populated

Here we populate the missing utilities:

sage: f[2, 1] = [5, 3]
sage: f[2, 0] = [2, 1]
sage: f.payoff_matrices()
(
[1 2]  [3 3]
[3 4]  [1 4]
[2 5], [1 3]
)
sage: f.obtain_nash()
[[(0, 0, 1), (0, 1)]]

We can use the same syntax as above to create games with more than 2 players:

sage: threegame = NormalFormGame()
sage: threegame.add_player(2)  # Adding first player with 2 strategies
sage: threegame.add_player(2)  # Adding second player with 2 strategies
sage: threegame.add_player(2)  # Adding third player with 2 strategies
sage: threegame[0, 0, 0][0] = 3
sage: threegame[0, 0, 0][1] = 1
sage: threegame[0, 0, 0][2] = 4
sage: threegame[0, 0, 1][0] = 1
sage: threegame[0, 0, 1][1] = 5
sage: threegame[0, 0, 1][2] = 9
sage: threegame[0, 1, 0][0] = 2
sage: threegame[0, 1, 0][1] = 6
sage: threegame[0, 1, 0][2] = 5
sage: threegame[0, 1, 1][0] = 3
sage: threegame[0, 1, 1][1] = 5
sage: threegame[0, 1, 1][2] = 8
sage: threegame[1, 0, 0][0] = 9
sage: threegame[1, 0, 0][1] = 7
sage: threegame[1, 0, 0][2] = 9
sage: threegame[1, 0, 1][0] = 3
sage: threegame[1, 0, 1][1] = 2
sage: threegame[1, 0, 1][2] = 3
sage: threegame[1, 1, 0][0] = 8
sage: threegame[1, 1, 0][1] = 4
sage: threegame[1, 1, 0][2] = 6
sage: threegame[1, 1, 1][0] = 2
sage: threegame[1, 1, 1][1] = 6
sage: threegame[1, 1, 1][2] = 4
sage: threegame
Normal Form Game with the following utilities: {(0, 0, 0): [3, 1, 4],
 (0, 0, 1): [1, 5, 9],
 (0, 1, 0): [2, 6, 5],
 (0, 1, 1): [3, 5, 8],
 (1, 0, 0): [9, 7, 9],
 (1, 0, 1): [3, 2, 3],
 (1, 1, 0): [8, 4, 6],
 (1, 1, 1): [2, 6, 4]}

The above requires a lot of input that could be simplified if there is another data structure with our utilities and/or a structure to the utilities. The following example creates a game with a relatively strange utility function:

sage: def utility(strategy_triplet, player):
....:     return sum(strategy_triplet) * player
sage: threegame = NormalFormGame()
sage: threegame.add_player(2)  # Adding first player with 2 strategies
sage: threegame.add_player(2)  # Adding second player with 2 strategies
sage: threegame.add_player(2)  # Adding third player with 2 strategies
sage: for i, j, k in [(i, j, k) for i in [0,1] for j in [0,1] for k in [0,1]]:
....:     for p in range(3):
....:          threegame[i, j, k][p] = utility([i, j, k], p)
sage: threegame
Normal Form Game with the following utilities: {(0, 0, 0): [0, 0, 0],
 (0, 0, 1): [0, 1, 2],
 (0, 1, 0): [0, 1, 2],
 (0, 1, 1): [0, 2, 4],
 (1, 0, 0): [0, 1, 2],
 (1, 0, 1): [0, 2, 4],
 (1, 1, 0): [0, 2, 4],
 (1, 1, 1): [0, 3, 6]}

At present no algorithm has been implemented in Sage for games with more than 2 players:

sage: threegame.obtain_nash()
Traceback (most recent call last):
...
NotImplementedError: Nash equilibrium for games with more than 2 players
 have not been implemented yet. Please see the gambit website
 (http://gambit.sourceforge.net/) that has a variety of available algorithms

There are however a variety of such algorithms available in gambit, further compatibility between Sage and gambit is actively being developed: https://github.com/tturocy/gambit/tree/sage_integration.

It can be shown that linear scaling of the payoff matrices conserves the equilibrium values:

sage: A = matrix([[2, 1], [1, 2.5]])
sage: B = matrix([[-1, 3], [2, 1]])
sage: g = NormalFormGame([A, B])
sage: g.obtain_nash(algorithm='enumeration')
[[(1/5, 4/5), (3/5, 2/5)]]
sage: g.obtain_nash(algorithm='lrs') # optional - lrslib
[[(1/5, 4/5), (3/5, 2/5)]]
sage: A = 2 * A
sage: g = NormalFormGame([A, B])
sage: g.obtain_nash(algorithm='LCP')  # optional - gambit
[[(0.2, 0.8), (0.6, 0.4)]]

It is also possible to generate a Normal form game from a gambit Game:

sage: # optional - gambit
sage: from gambit import Game
sage: gambitgame= Game.new_table([2, 2])
sage: gambitgame[int(0), int(0)][int(0)] = int(8)
sage: gambitgame[int(0), int(0)][int(1)] = int(8)
sage: gambitgame[int(0), int(1)][int(0)] = int(2)
sage: gambitgame[int(0), int(1)][int(1)] = int(10)
sage: gambitgame[int(1), int(0)][int(0)] = int(10)
sage: gambitgame[int(1), int(0)][int(1)] = int(2)
sage: gambitgame[int(1), int(1)][int(0)] = int(5)
sage: gambitgame[int(1), int(1)][int(1)] = int(5)
sage: g = NormalFormGame(gambitgame); g
Normal Form Game with the following utilities: {(0, 0): [8.0, 8.0],
 (0, 1): [2.0, 10.0],
 (1, 0): [10.0, 2.0],
 (1, 1): [5.0, 5.0]}

For more information on using Gambit in Sage see: Using Gambit in Sage. This includes how to access Gambit directly using the version of iPython shipped with Sage and an explanation as to why the int calls are needed to handle the Sage preparser.

Here is a slightly longer game that would take too long to solve with 'enumeration'. Consider the following:

An airline loses two suitcases belonging to two different travelers. Both suitcases happen to be identical and contain identical antiques. An airline manager tasked to settle the claims of both travelers explains that the airline is liable for a maximum of 10 per suitcase, and in order to determine an honest appraised value of the antiques the manager separates both travelers so they can’t confer, and asks them to write down the amount of their value at no less than 2 and no larger than 10. He also tells them that if both write down the same number, he will treat that number as the true dollar value of both suitcases and reimburse both travelers that amount.

However, if one writes down a smaller number than the other, this smaller number will be taken as the true dollar value, and both travelers will receive that amount along with a bonus/malus: 2 extra will be paid to the traveler who wrote down the lower value and a 2 deduction will be taken from the person who wrote down the higher amount. The challenge is: what strategy should both travelers follow to decide the value they should write down?

In the following we create the game (with a max value of 10) and solve it:

sage: K = 10  # Modifying this value lets us play with games of any size
sage: A = matrix([[min(i,j) + 2 * sign(j-i)  for j in range(K, 1, -1)]
....:             for i in range(K, 1, -1)])
sage: B = matrix([[min(i,j) + 2 * sign(i-j)  for j in range(K, 1, -1)]
....:             for i in range(K, 1, -1)])
sage: g = NormalFormGame([A, B])
sage: g.obtain_nash(algorithm='lrs')  # optional - lrslib
[[(0, 0, 0, 0, 0, 0, 0, 0, 1), (0, 0, 0, 0, 0, 0, 0, 0, 1)]]
sage: g.obtain_nash(algorithm='LCP')  # optional - gambit
[[(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0),
  (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0)]]

The output is a pair of vectors (as before) showing the Nash equilibrium. In particular it here shows that out of the 10 possible strategies both players should choose the last. Recall that the above considers a reduced version of the game where individuals can claim integer values from 10 to 2. The equilibrium strategy is thus for both players to state that the value of their suitcase is 2.

Several standard Normal Form Games have also been implemented. For more information on how to access these, see: Game Theory Catalog. Included is information on the situation each Game models. For example:

sage: g = game_theory.normal_form_games.PrisonersDilemma()
sage: g
Prisoners dilemma - Normal Form Game with the following utilities: ...
sage: d = {(0, 1): [-5, 0], (1, 0): [0, -5],
....:      (0, 0): [-2, -2], (1, 1): [-4, -4]}
sage: g == d
True
sage: g.obtain_nash()
[[(0, 1), (0, 1)]]

We can easily obtain the best response for a player to a given strategy. In this example we obtain the best responses for Player 1, when Player 2 uses two different strategies:

sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
sage: g = NormalFormGame([A, B])
sage: g.best_responses((1/2, 1/2), player=0)
[0, 1, 2]
sage: g.best_responses((3/4, 1/4), player=0)
[0]

Here we do the same for player 2:

sage: g.best_responses((4/5, 1/5, 0), player=1)
[0, 1]

We see that for the game Rock-Paper-Scissors-Lizard-Spock any pure strategy has two best responses:

sage: g = game_theory.normal_form_games.RPSLS()
sage: A, B = g.payoff_matrices()
sage: A, B
(
[ 0 -1  1  1 -1]  [ 0  1 -1 -1  1]
[ 1  0 -1 -1  1]  [-1  0  1  1 -1]
[-1  1  0  1 -1]  [ 1 -1  0 -1  1]
[-1  1 -1  0  1]  [ 1 -1  1  0 -1]
[ 1 -1  1 -1  0], [-1  1 -1  1  0]
)
sage: g.best_responses((1, 0, 0, 0, 0), player=0)
[1, 4]
sage: g.best_responses((0, 1, 0, 0, 0), player=0)
[2, 3]
sage: g.best_responses((0, 0, 1, 0, 0), player=0)
[0, 4]
sage: g.best_responses((0, 0, 0, 1, 0), player=0)
[0, 2]
sage: g.best_responses((0, 0, 0, 0, 1), player=0)
[1, 3]
sage: g.best_responses((1, 0, 0, 0, 0), player=1)
[1, 4]
sage: g.best_responses((0, 1, 0, 0, 0), player=1)
[2, 3]
sage: g.best_responses((0, 0, 1, 0, 0), player=1)
[0, 4]
sage: g.best_responses((0, 0, 0, 1, 0), player=1)
[0, 2]
sage: g.best_responses((0, 0, 0, 0, 1), player=1)
[1, 3]

Note that degenerate games can cause problems for most algorithms. The following example in fact has an infinite quantity of equilibria which is evidenced by the various algorithms returning different solutions:

sage: A = matrix([[3,3],[2,5],[0,6]])
sage: B = matrix([[3,3],[2,6],[3,1]])
sage: degenerate_game = NormalFormGame([A,B])
sage: degenerate_game.obtain_nash(algorithm='lrs')  # random, optional - lrslib
[[(0, 1/3, 2/3), (1/3, 2/3)], [(1, 0, 0), (1/2, 3)], [(1, 0, 0), (1, 3)]]
sage: degenerate_game.obtain_nash(algorithm='LCP')  # optional - gambit
[[(0.0, 0.3333333333, 0.6666666667), (0.3333333333, 0.6666666667)],
 [(1.0, -0.0, 0.0), (0.6666666667, 0.3333333333)],
 [(1.0, 0.0, 0.0), (1.0, 0.0)]]
sage: degenerate_game.obtain_nash(algorithm='enumeration')
[[(0, 1/3, 2/3), (1/3, 2/3)], [(1, 0, 0), (1, 0)]]

We can check the cause of this by using is_degenerate():

sage: degenerate_game.is_degenerate()
True

Note the ‘negative’ \(-0.0\) output by gambit. This is due to the numerical nature of the algorithm used.

Here is an example with the trivial game where all payoffs are 0:

sage: g = NormalFormGame()
sage: g.add_player(3)  # Adding first player with 3 strategies
sage: g.add_player(3)  # Adding second player with 3 strategies
sage: for key in g:
....:     g[key] = [0, 0]
sage: g.payoff_matrices()
(
[0 0 0]  [0 0 0]
[0 0 0]  [0 0 0]
[0 0 0], [0 0 0]
)
sage: g.obtain_nash(algorithm='enumeration')
[[(0, 0, 1), (0, 0, 1)], [(0, 0, 1), (0, 1, 0)], [(0, 0, 1), (1, 0, 0)],
 [(0, 1, 0), (0, 0, 1)], [(0, 1, 0), (0, 1, 0)], [(0, 1, 0), (1, 0, 0)],
 [(1, 0, 0), (0, 0, 1)], [(1, 0, 0), (0, 1, 0)], [(1, 0, 0), (1, 0, 0)]]

A good description of degenerate games can be found in [NN2007].

REFERENCES:

AUTHORS:

  • James Campbell and Vince Knight (06-2014): Original version

  • Tobenna P. Igwe: Constant-sum game solvers

class sage.game_theory.normal_form_game.NormalFormGame(generator=None)#

Bases: SageObject, MutableMapping

An object representing a Normal Form Game. Primarily used to compute the Nash Equilibria.

INPUT:

  • generator – can be a list of 2 matrices, a single matrix or left blank

add_player(num_strategies)#

Add a player to a NormalFormGame.

INPUT:

  • num_strategies – the number of strategies the player should have

EXAMPLES:

sage: g = NormalFormGame()
sage: g.add_player(2)  # Adding first player with 2 strategies
sage: g.add_player(1)  # Adding second player with 1 strategy
sage: g.add_player(1)  # Adding third player with 1 strategy
sage: g
Normal Form Game with the following utilities:
 {(0, 0, 0): [False, False, False],
  (1, 0, 0): [False, False, False]}
add_strategy(player)#

Add a strategy to a player, will not affect already completed strategy profiles.

INPUT:

  • player – the index of the player

EXAMPLES:

A simple example:

sage: s = matrix([[1, 0], [-2, 3]])
sage: t = matrix([[3, 2], [-1, 0]])
sage: example = NormalFormGame([s, t])
sage: example
Normal Form Game with the following utilities: {(0, 0): [1, 3],
 (0, 1): [0, 2], (1, 0): [-2, -1], (1, 1): [3, 0]}
sage: example.add_strategy(0)
sage: example
Normal Form Game with the following utilities: {(0, 0): [1, 3],
 (0, 1): [0, 2],
 (1, 0): [-2, -1],
 (1, 1): [3, 0],
 (2, 0): [False, False],
 (2, 1): [False, False]}
best_responses(strategy, player)#

For a given strategy for a player and the index of the opponent, computes the payoff for the opponent and returns a list of the indices of the best responses. Only implemented for two player games

INPUT:

  • strategy – a probability distribution vector

  • player – the index of the opponent, 0 for the row player, 1 for the column player.

EXAMPLES:

sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
sage: g = NormalFormGame([A, B])

Now we can obtain the best responses for Player 1, when Player 2 uses different strategies:

sage: g.best_responses((1/2, 1/2), player=0)
[0, 1, 2]
sage: g.best_responses((3/4, 1/4), player=0)
[0]

To get the best responses for Player 2 we pass the argument player=1:

sage: g.best_responses((4/5, 1/5, 0), player=1)
[0, 1]

sage: A = matrix([[1, 0], [0, 1], [0, 0]])
sage: B = matrix([[1, 0], [0, 1], [0.7, 0.8]])
sage: g = NormalFormGame([A, B])
sage: g.best_responses((0, 1, 0), player=1)
[1]

sage: A = matrix([[3,3],[2,5],[0,6]])
sage: B = matrix([[3,3],[2,6],[3,1]])
sage: degenerate_game = NormalFormGame([A,B])
sage: degenerate_game.best_responses((1, 0, 0), player=1)
[0, 1]

sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
sage: g = NormalFormGame([A, B])
sage: g.best_responses((1/3, 1/3, 1/3), player=1)
[1]

Note that this has only been implemented for 2 player games:

sage: g = NormalFormGame()
sage: g.add_player(2)  # adding first player with 2 strategies
sage: g.add_player(2)  # adding second player with 2 strategies
sage: g.add_player(2)  # adding third player with 2 strategies
sage: g.best_responses((1/2, 1/2), player=2)
Traceback (most recent call last):
...
ValueError: Only available for 2 player games

If the strategy is not of the correct dimension for the given player then an error is returned:

sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
sage: g = NormalFormGame([A, B])
sage: g.best_responses((1/2, 1/2), player=1)
Traceback (most recent call last):
...
ValueError: Strategy is not of correct dimension

sage: g.best_responses((1/3, 1/3, 1/3), player=0)
Traceback (most recent call last):
...
ValueError: Strategy is not of correct dimension

If the strategy is not a true probability vector then an error is passed:

sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
sage: g = NormalFormGame([A, B])
sage: g.best_responses((1/3, 1/2, 0), player=1)
Traceback (most recent call last):
...
ValueError: Strategy is not a probability distribution vector

sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
sage: g = NormalFormGame([A, B])
sage: g.best_responses((3/2, -1/2), player=0)
Traceback (most recent call last):
...
ValueError: Strategy is not a probability distribution vector

If the player specified is not \(0\) or \(1\), an error is raised:

sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
sage: g = NormalFormGame([A, B])
sage: g.best_responses((1/2, 1/2), player='Player1')
Traceback (most recent call last):
...
ValueError: Player1 is not an index of the opponent, must be 0 or 1
is_constant_sum()#

Checks if the game is constant sum.

EXAMPLES:

sage: A = matrix([[2, 1], [1, 2.5]])
sage: g = NormalFormGame([A])
sage: g.is_constant_sum()
True
sage: g = NormalFormGame([A, A])
sage: g.is_constant_sum()
False
sage: A = matrix([[1, 1], [1, 1]])
sage: g = NormalFormGame([A, A])
sage: g.is_constant_sum()
True
sage: A = matrix([[1, 1, 2], [1, 1, -1], [1, -1, 1]])
sage: B = matrix([[2, 2, 1], [2, 2, 4], [2, 4, 2]])
sage: g = NormalFormGame([A, B])
sage: g.is_constant_sum()
True
sage: A = matrix([[1, 1, 2], [1, 1, -1], [1, -1, 1]])
sage: B = matrix([[2, 2, 1], [2, 2.1, 4], [2, 4, 2]])
sage: g = NormalFormGame([A, B])
sage: g.is_constant_sum()
False
is_degenerate(certificate=False)#

A function to check whether the game is degenerate or not. Will return a boolean.

A two-player game is called nondegenerate if no mixed strategy of support size \(k\) has more than \(k\) pure best responses [NN2007]. In a degenerate game, this definition is violated, for example if there is a pure strategy that has two pure best responses.

The implementation here transforms the search over mixed strategies to a search over supports which is a discrete search. A full explanation of this is given in [CK2015]. This problem is known to be NP-Hard [Du2009]. Another possible implementation is via best response polytopes, see github issue #18958.

The game Rock-Paper-Scissors is an example of a non-degenerate game,:

sage: g = game_theory.normal_form_games.RPS()
sage: g.is_degenerate()
False

whereas Rock-Paper-Scissors-Lizard-Spock is degenerate because for every pure strategy there are two best responses.:

sage: g = game_theory.normal_form_games.RPSLS()
sage: g.is_degenerate()
True

EXAMPLES:

Here is an example of a degenerate game given in [DGRB2010]:

sage: A = matrix([[3, 3], [2, 5], [0, 6]])
sage: B = matrix([[3, 3], [2, 6], [3, 1]])
sage: degenerate_game = NormalFormGame([A,B])
sage: degenerate_game.is_degenerate()
True

Here is an example of a degenerate game given in [NN2007]:

sage: A = matrix([[0, 6], [2, 5], [3, 3]])
sage: B = matrix([[1, 0], [0, 2], [4, 4]])
sage: d_game = NormalFormGame([A, B])
sage: d_game.is_degenerate()
True

Here are some other examples of degenerate games:

sage: M = matrix([[2, 1], [1, 1]])
sage: N = matrix([[1, 1], [1, 2]])
sage: game  = NormalFormGame([M, N])
sage: game.is_degenerate()
True

If more information is required, it may be useful to use certificate=True. This will return a boolean of whether the game is degenerate or not, and if True; a tuple containing the strategy where degeneracy was found and the player it belongs to. 0 is the row player and 1 is the column player.:

sage: M = matrix([[2, 1], [1, 1]])
sage: N = matrix([[1, 1], [1, 2]])
sage: g  = NormalFormGame([M, N])
sage: test, certificate = g.is_degenerate(certificate=True)
sage: test, certificate
(True, ((1, 0), 0))

Using the output, we see that the opponent has more best responses than the size of the support of the strategy in question (1, 0). (We specify the player as (player + 1) % 2 to ensure that we have the opponent’s index.):

sage: g.best_responses(certificate[0], (certificate[1] + 1) % 2)
[0, 1]

Another example with a mixed strategy causing degeneracy.:

sage: A = matrix([[3, 0], [0, 3], [1.5, 1.5]])
sage: B = matrix([[4, 3], [2, 6], [3, 1]])
sage: g = NormalFormGame([A, B])
sage: test, certificate = g.is_degenerate(certificate=True)
sage: test, certificate
(True, ((1/2, 1/2), 1))

Again, we see that the opponent has more best responses than the size of the support of the strategy in question (1/2, 1/2).:

sage: g.best_responses(certificate[0], (certificate[1] + 1) % 2)
[0, 1, 2]

Sometimes, the different algorithms for obtaining nash_equilibria don’t agree with each other. This can happen when games are degenerate:

sage: a = matrix([[-75, 18, 45, 33],
....:            [42, -8, -77, -18],
....:            [83, 18, 11, 40],
....:            [-10, -38, 76, -9]])
sage: b = matrix([[62, 64, 87, 51],
....:            [-41, -27, -69, 52],
....:            [-17, 25, -97, -82],
....:            [30, 31, -1, 50]])
sage: d_game = NormalFormGame([a, b])
sage: d_game.obtain_nash(algorithm='lrs')  # optional - lrslib
[[(0, 0, 1, 0), (0, 1, 0, 0)],
 [(17/29, 0, 0, 12/29), (0, 0, 42/73, 31/73)],
 [(122/145, 0, 23/145, 0), (0, 1, 0, 0)]]
sage: d_game.obtain_nash(algorithm='LCP')  # optional - gambit
[[(0.5862068966, 0.0, 0.0, 0.4137931034),
  (0.0, 0.0, 0.5753424658, 0.4246575342)]]
sage: d_game.obtain_nash(algorithm='enumeration')
[[(0, 0, 1, 0), (0, 1, 0, 0)], [(17/29, 0, 0, 12/29), (0, 0, 42/73, 31/73)]]
sage: d_game.is_degenerate()
True
obtain_nash(algorithm=False, maximization=True, solver=None)#

A function to return the Nash equilibrium for the game. Optional arguments can be used to specify the algorithm used. If no algorithm is passed then an attempt is made to use the most appropriate algorithm.

INPUT:

  • algorithm - the following algorithms should be available through this function:

    • 'lrs' - This algorithm is only suited for 2 player games. See the lrs web site (http://cgm.cs.mcgill.ca/~avis/C/lrs.html).

    • 'LCP' - This algorithm is only suited for 2 player games. See the gambit web site (http://gambit.sourceforge.net/).

    • 'lp' - This algorithm is only suited for 2 player constant sum games. Uses MILP solver determined by the solver argument.

    • 'enumeration' - This is a very inefficient algorithm (in essence a brute force approach).

      1. For each k in 1…min(size of strategy sets)

      2. For each I,J supports of size k

      3. Prune: check if supports are dominated

      4. Solve indifference conditions and check that have Nash Equilibrium.

      Solving the indifference conditions is done by building the corresponding linear system. If \(\rho_1, \rho_2\) are the supports player 1 and 2 respectively. Then, indifference implies:

      \[u_1(s_1,\rho_2) = u_1(s_2, \rho_2)\]

      for all \(s_1, s_2\) in the support of \(\rho_1\). This corresponds to:

      \[\sum_{j\in S(\rho_2)}A_{s_1,j}{\rho_2}_j = \sum_{j\in S(\rho_2)}A_{s_2,j}{\rho_2}_j\]

      for all \(s_1, s_2\) in the support of \(\rho_1\) where \(A\) is the payoff matrix of player 1. Equivalently we can consider consecutive rows of \(A\) (instead of all pairs of strategies). Thus the corresponding linear system can be written as:

      \[\left(\sum_{j \in S(\rho_2)}A_{i,j} - A_{i+1,j}\right){\rho_2}_j\]

      for all \(1\leq i \leq |S(\rho_1)|\) (where \(A\) has been modified to only contain the rows corresponding to \(S(\rho_1)\)). We also require all elements of \(\rho_2\) to sum to 1:

      \[\sum_{j\in S(\rho_1)}{\rho_2}_j = 1\]
  • maximization – (default: True) whether a player is trying to maximize their utility or minimize it:

    • When set to True it is assumed that players aim to maximise their utility.

    • When set to False it is assumed that players aim to minimise their utility.

  • solver – (optional) see MixedIntegerLinearProgram for more information on the MILP solvers in Sage, may also be 'gambit' to use the MILP solver included with the gambit library. Note that None means to use the default Sage LP solver, normally GLPK.

EXAMPLES:

A game with 1 equilibrium when maximization is True and 3 when maximization is False:

sage: A = matrix([[10, 500, 44],
....:       [15, 10, 105],
....:       [19, 204, 55],
....:       [20, 200, 590]])
sage: B = matrix([[2, 1, 2],
....:             [0, 5, 6],
....:             [3, 4, 1],
....:             [4, 1, 20]])
sage: g=NormalFormGame([A, B])
sage: g.obtain_nash(algorithm='lrs')  # optional - lrslib
[[(0, 0, 0, 1), (0, 0, 1)]]
sage: g.obtain_nash(algorithm='lrs', maximization=False)  # optional - lrslib
[[(2/3, 1/12, 1/4, 0), (6333/8045, 247/8045, 293/1609)],
 [(3/4, 0, 1/4, 0), (0, 11/307, 296/307)],
 [(5/6, 1/6, 0, 0), (98/99, 1/99, 0)]]

This particular game has 3 Nash equilibria:

sage: A = matrix([[3,3],
....:             [2,5],
....:             [0,6]])
sage: B = matrix([[3,2],
....:             [2,6],
....:             [3,1]])
sage: g = NormalFormGame([A, B])
sage: g.obtain_nash(algorithm='enumeration')
[[(0, 1/3, 2/3), (1/3, 2/3)],
 [(4/5, 1/5, 0), (2/3, 1/3)],
 [(1, 0, 0), (1, 0)]]

Here is a slightly larger game:

sage: A = matrix([[160, 205, 44],
....:             [175, 180, 45],
....:             [201, 204, 50],
....:             [120, 207, 49]])
sage: B = matrix([[2, 2, 2],
....:             [1, 0, 0],
....:             [3, 4, 1],
....:             [4, 1, 2]])
sage: g=NormalFormGame([A, B])
sage: g.obtain_nash(algorithm='enumeration')
[[(0, 0, 3/4, 1/4), (1/28, 27/28, 0)]]
sage: g.obtain_nash(algorithm='lrs')  # optional - lrslib
[[(0, 0, 3/4, 1/4), (1/28, 27/28, 0)]]
sage: g.obtain_nash(algorithm='LCP')  # optional - gambit
[[(0.0, 0.0, 0.75, 0.25), (0.0357142857, 0.9642857143, 0.0)]]

2 random matrices:

sage: player1 = matrix([[2, 8, -1, 1, 0],
....:                   [1, 1, 2, 1, 80],
....:                   [0, 2, 15, 0, -12],
....:                   [-2, -2, 1, -20, -1],
....:                   [1, -2, -1, -2, 1]])
sage: player2 = matrix([[0, 8, 4, 2, -1],
....:                   [6, 14, -5, 1, 0],
....:                   [0, -2, -1, 8, -1],
....:                   [1, -1, 3, -3, 2],
....:                   [8, -4, 1, 1, -17]])
sage: fivegame = NormalFormGame([player1, player2])
sage: fivegame.obtain_nash(algorithm='enumeration')
[[(1, 0, 0, 0, 0), (0, 1, 0, 0, 0)]]
sage: fivegame.obtain_nash(algorithm='lrs')  # optional - lrslib
[[(1, 0, 0, 0, 0), (0, 1, 0, 0, 0)]]
sage: fivegame.obtain_nash(algorithm='LCP')  # optional - gambit
[[(1.0, 0.0, 0.0, 0.0, 0.0), (0.0, 1.0, 0.0, 0.0, 0.0)]]

Here are some examples of finding Nash equilibria for constant-sum games:

sage: A = matrix.identity(2)
sage: cg = NormalFormGame([A])
sage: cg.obtain_nash(algorithm='lp')
[[(0.5, 0.5), (0.5, 0.5)]]
sage: cg.obtain_nash(algorithm='lp', solver='Coin')                   # optional - sage_numerical_backends_coin
[[(0.5, 0.5), (0.5, 0.5)]]
sage: cg.obtain_nash(algorithm='lp', solver='PPL')
[[(1/2, 1/2), (1/2, 1/2)]]
sage: cg.obtain_nash(algorithm='lp', solver='gambit')                 # optional - gambit
[[(0.5, 0.5), (0.5, 0.5)]]
sage: A = matrix([[2, 1], [1, 3]])
sage: cg = NormalFormGame([A])
sage: ne = cg.obtain_nash(algorithm='lp', solver='glpk')
sage: [[[round(el, 6) for el in v] for v in eq] for eq in ne]
[[[0.666667, 0.333333], [0.666667, 0.333333]]]
sage: ne = cg.obtain_nash(algorithm='lp', solver='Coin')              # optional - sage_numerical_backends_coin
sage: [[[round(el, 6) for el in v] for v in eq] for eq in ne]         # optional - sage_numerical_backends_coin
[[[0.666667, 0.333333], [0.666667, 0.333333]]]
sage: cg.obtain_nash(algorithm='lp', solver='PPL')
[[(2/3, 1/3), (2/3, 1/3)]]
sage: ne = cg.obtain_nash(algorithm='lp', solver='gambit')            # optional - gambit
sage: [[[round(el, 6) for el in v] for v in eq] for eq in ne]         # optional - gambit
[[[0.666667, 0.333333], [0.666667, 0.333333]]]
sage: A = matrix([[1, 2, 1], [1, 1, 2], [2, 1, 1]])
sage: B = matrix([[2, 1, 2], [2, 2, 1], [1, 2, 2]])
sage: cg = NormalFormGame([A, B])
sage: ne = cg.obtain_nash(algorithm='lp', solver='glpk')
sage: [[[round(el, 6) for el in v] for v in eq] for eq in ne]
[[[0.333333, 0.333333, 0.333333], [0.333333, 0.333333, 0.333333]]]
sage: ne = cg.obtain_nash(algorithm='lp', solver='Coin')              # optional - sage_numerical_backends_coin
sage: [[[round(el, 6) for el in v] for v in eq] for eq in ne]         # optional - sage_numerical_backends_coin
[[[0.333333, 0.333333, 0.333333], [0.333333, 0.333333, 0.333333]]]
sage: cg.obtain_nash(algorithm='lp', solver='PPL')
[[(1/3, 1/3, 1/3), (1/3, 1/3, 1/3)]]
sage: ne = cg.obtain_nash(algorithm='lp', solver='gambit')            # optional - gambit
sage: [[[round(el, 6) for el in v] for v in eq] for eq in ne]         # optional - gambit
[[[0.333333, 0.333333, 0.333333], [0.333333, 0.333333, 0.333333]]]
sage: A = matrix([[160, 205, 44],
....:             [175, 180, 45],
....:             [201, 204, 50],
....:             [120, 207, 49]])
sage: cg = NormalFormGame([A])
sage: cg.obtain_nash(algorithm='lp', solver='PPL')
[[(0, 0, 1, 0), (0, 0, 1)]]

Running the constant-sum solver on a game which is not a constant sum game generates a ValueError:

sage: cg = NormalFormGame([A, A])
sage: cg.obtain_nash(algorithm='lp', solver='glpk')
Traceback (most recent call last):
...
ValueError: Input game needs to be a two player constant sum game

Here is an example of a 3 by 2 game with 3 Nash equilibrium:

sage: A = matrix([[3,3],
....:             [2,5],
....:             [0,6]])
sage: B = matrix([[3,2],
....:             [2,6],
....:             [3,1]])
sage: g = NormalFormGame([A, B])
sage: g.obtain_nash(algorithm='enumeration')
[[(0, 1/3, 2/3), (1/3, 2/3)], [(4/5, 1/5, 0), (2/3, 1/3)], [(1, 0, 0), (1, 0)]]

Of the algorithms implemented, only 'lrs' and 'enumeration' are guaranteed to find all Nash equilibria in a game. The solver for constant sum games only ever finds one Nash equilibrium. Although it is possible for the 'LCP' solver to find all Nash equilibria in some instances, there are instances where it will not be able to find all Nash equilibria.:

sage: A = matrix(2, 2)
sage: gg = NormalFormGame([A])
sage: gg.obtain_nash(algorithm='enumeration')
[[(0, 1), (0, 1)], [(0, 1), (1, 0)], [(1, 0), (0, 1)], [(1, 0), (1, 0)]]
sage: gg.obtain_nash(algorithm='lrs')  # optional - lrs
[[(0, 1), (0, 1)], [(0, 1), (1, 0)], [(1, 0), (0, 1)], [(1, 0), (1, 0)]]
sage: gg.obtain_nash(algorithm='lp', solver='glpk')
[[(1.0, 0.0), (1.0, 0.0)]]
sage: gg.obtain_nash(algorithm='LCP')  # optional - gambit
[[(1.0, 0.0), (1.0, 0.0)]]
sage: gg.obtain_nash(algorithm='enumeration', maximization=False)
[[(0, 1), (0, 1)], [(0, 1), (1, 0)], [(1, 0), (0, 1)], [(1, 0), (1, 0)]]
sage: gg.obtain_nash(algorithm='lrs', maximization=False)  # optional - lrs
[[(0, 1), (0, 1)], [(0, 1), (1, 0)], [(1, 0), (0, 1)], [(1, 0), (1, 0)]]
sage: gg.obtain_nash(algorithm='lp', solver='glpk', maximization=False)
[[(1.0, 0.0), (1.0, 0.0)]]
sage: gg.obtain_nash(algorithm='LCP', maximization=False)  # optional - gambit
[[(1.0, 0.0), (1.0, 0.0)]]

Note that outputs for all algorithms are as lists of lists of tuples and the equilibria have been sorted so that all algorithms give a comparable output (although 'LCP' returns floats):

sage: enumeration_eqs = g.obtain_nash(algorithm='enumeration')
sage: [[type(s) for s in eq] for eq in enumeration_eqs]
[[<... 'tuple'>, <... 'tuple'>], [<... 'tuple'>, <... 'tuple'>], [<... 'tuple'>, <... 'tuple'>]]
sage: lrs_eqs = g.obtain_nash(algorithm='lrs')  # optional - lrslib
sage: [[type(s) for s in eq] for eq in lrs_eqs]  # optional - lrslib
[[<... 'tuple'>, <... 'tuple'>], [<... 'tuple'>, <... 'tuple'>], [<... 'tuple'>, <... 'tuple'>]]
sage: LCP_eqs = g.obtain_nash(algorithm='LCP')  # optional - gambit
sage: [[type(s) for s in eq] for eq in LCP_eqs]  # optional - gambit
[[<... 'tuple'>, <... 'tuple'>], [<... 'tuple'>, <... 'tuple'>], [<... 'tuple'>, <... 'tuple'>]]
sage: enumeration_eqs == sorted(enumeration_eqs)
True
sage: lrs_eqs == sorted(lrs_eqs)  # optional - lrslib
True
sage: LCP_eqs == sorted(LCP_eqs)  # optional - gambit
True
sage: lrs_eqs == enumeration_eqs  # optional - lrslib
True
sage: enumeration_eqs == LCP_eqs  # optional - gambit
False
sage: [[[round(float(p), 6) for p in str] for str in eq] for eq in enumeration_eqs] == [[[round(float(p), 6) for p in str] for str in eq] for eq in LCP_eqs]  # optional - gambit
True

Also, not specifying a valid solver would lead to an error:

sage: A = matrix.identity(2)
sage: g = NormalFormGame([A])
sage: g.obtain_nash(algorithm="invalid")
Traceback (most recent call last):
...
ValueError: 'algorithm' should be set to 'enumeration', 'LCP', 'lp' or 'lrs'
sage: g.obtain_nash(algorithm="lp", solver="invalid")
Traceback (most recent call last):
...
ValueError: 'solver' should be set to 'GLPK', ..., None
 (in which case the default one is used), or a callable.
payoff_matrices()#

Return 2 matrices representing the payoffs for each player.

EXAMPLES:

sage: p1 = matrix([[1, 2], [3, 4]])
sage: p2 = matrix([[3, 3], [1, 4]])
sage: g = NormalFormGame([p1, p2])
sage: g.payoff_matrices()
(
[1 2]  [3 3]
[3 4], [1 4]
)

If we create a game with 3 players we will not be able to obtain payoff matrices:

sage: g = NormalFormGame()
sage: g.add_player(2)  # adding first player with 2 strategies
sage: g.add_player(2)  # adding second player with 2 strategies
sage: g.add_player(2)  # adding third player with 2 strategies
sage: g.payoff_matrices()
Traceback (most recent call last):
...
ValueError: Only available for 2 player games

If we do create a two player game but it is not complete then an error is also raised:

sage: g = NormalFormGame()
sage: g.add_player(1)  # Adding first player with 1 strategy
sage: g.add_player(1)  # Adding second player with 1 strategy
sage: g.payoff_matrices()
Traceback (most recent call last):
...
ValueError: utilities have not been populated

The above creates a 2 player game where each player has a single strategy. Here we populate the strategies and can then view the payoff matrices:

sage: g[0, 0] = [1,2]
sage: g.payoff_matrices()
([1], [2])