Skip to content
Prev Previous commit
Next Next commit
src/sage/game_theory: Update imports for pygambit
  • Loading branch information
Matthias Koeppe committed Apr 16, 2024
commit 8e8a66257bd6ab7bf0be1bde64ab4abef1a2d299
20 changes: 10 additions & 10 deletions src/sage/game_theory/gambit_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

Here is an example that constructs the Prisoner's Dilemma::

In [1]: import gambit
In [2]: g = gambit.Game.new_table([2,2])
In [1]: import pygambit
In [2]: g = pygambit.Game.new_table([2,2])
In [3]: g.title = "A prisoner's dilemma game"
In [4]: g.players[0].label = "Alphonse"
In [5]: g.players[1].label = "Gaston"
Expand Down Expand Up @@ -79,16 +79,16 @@

Here is how to use the ``ExternalEnumPureSolver``::

In [21]: solver = gambit.nash.ExternalEnumPureSolver()
In [21]: solver = pygambit.nash.ExternalEnumPureSolver()
In [22]: solver.solve(g)
Out[22]: [<NashProfile for 'A prisoner's dilemma game': [Fraction(0, 1), Fraction(1, 1), Fraction(0, 1), Fraction(1, 1)]>]

Note that the above finds the equilibria by investigating all potential pure
pure strategy pairs. This will fail to find all Nash equilibria in certain
games. For example here is an implementation of Matching Pennies::

In [1]: import gambit
In [2]: g = gambit.Game.new_table([2,2])
In [1]: import pygambit
In [2]: g = pygambit.Game.new_table([2,2])
In [3]: g[0, 0][0] = 1
In [4]: g[0, 0][1] = -1
In [5]: g[0, 1][0] = -1
Expand All @@ -97,13 +97,13 @@
In [8]: g[1, 0][1] = 1
In [9]: g[1, 1][0] = 1
In [10]: g[1, 1][1] = -1
In [11]: solver = gambit.nash.ExternalEnumPureSolver()
In [11]: solver = pygambit.nash.ExternalEnumPureSolver()
In [12]: solver.solve(g)
Out[12]: []

If we solve this with the ``LCP`` solver we get the expected Nash equilibrium::

In [13]: solver = gambit.nash.ExternalLCPSolver()
In [13]: solver = pygambit.nash.ExternalLCPSolver()
In [14]: solver.solve(g)
Out[14]: [<NashProfile for '': [0.5, 0.5, 0.5, 0.5]>]

Expand All @@ -117,8 +117,8 @@
showing the Battle of the Sexes::

sage: # optional - pygambit
sage: import gambit
sage: g = gambit.Game.new_table([2,2])
sage: import pygambit
sage: g = pygambit.Game.new_table([2,2])
sage: g[int(0), int(0)][int(0)] = int(2)
sage: g[int(0), int(0)][int(1)] = int(1)
sage: g[int(0), int(1)][int(0)] = int(0)
Expand All @@ -127,7 +127,7 @@
sage: g[int(1), int(0)][int(1)] = int(0)
sage: g[int(1), int(1)][int(0)] = int(1)
sage: g[int(1), int(1)][int(1)] = int(2)
sage: solver = gambit.nash.ExternalLCPSolver()
sage: solver = pygambit.nash.ExternalLCPSolver()
sage: solver.solve(g)
[<NashProfile for '': [[1.0, 0.0], [1.0, 0.0]]>,
<NashProfile for '': [[0.6666666667, 0.3333333333], [0.3333333333, 0.6666666667]]>,
Expand Down
14 changes: 7 additions & 7 deletions src/sage/game_theory/normal_form_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@
It is also possible to generate a Normal form game from a gambit Game::

sage: # optional - pygambit
sage: from gambit import Game
sage: from pygambit 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)
Expand Down Expand Up @@ -584,7 +584,7 @@
sage: degenerate_game.is_degenerate()
True

Note the 'negative' `-0.0` output by gambit. This is due to the numerical
Note the 'negative' `-0.0` output by pygambit. This is due to the numerical
nature of the algorithm used.

Here is an example with the trivial game where all payoffs are 0::
Expand Down Expand Up @@ -652,8 +652,8 @@
from sage.cpython.string import bytes_to_str

try:
from gambit import Game
from gambit.nash import ExternalLPSolver, ExternalLCPSolver
from pygambit import Game
from pygambit.nash import ExternalLPSolver, ExternalLCPSolver
except ImportError:
Game = None
ExternalLPSolver = None
Expand Down Expand Up @@ -719,7 +719,7 @@ def __init__(self, generator=None):
Can initialise a game from a gambit game object::

sage: # optional - pygambit
sage: from gambit import Game
sage: from pygambit import Game
sage: gambitgame= Game.new_table([2, 2])
sage: gambitgame[int(0), int(0)][int(0)] = int(5)
sage: gambitgame[int(0), int(0)][int(1)] = int(8)
Expand Down Expand Up @@ -980,7 +980,7 @@ def _gambit_game(self, game):
TESTS::

sage: # optional - pygambit
sage: from gambit import Game
sage: from pygambit import Game
sage: testgame = Game.new_table([2, 2])
sage: testgame[int(0), int(0)][int(0)] = int(8)
sage: testgame[int(0), int(0)][int(1)] = int(8)
Expand Down Expand Up @@ -1021,7 +1021,7 @@ def _gambit_(self, as_integer=False, maximization=True):
TESTS::

sage: # optional - pygambit
sage: from gambit import Game
sage: from pygambit import Game
sage: A = matrix([[2, 1], [1, 2.5]])
sage: g = NormalFormGame([A])
sage: gg = g._gambit_(); gg
Expand Down
14 changes: 7 additions & 7 deletions src/sage/game_theory/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ def format_gambit(self, gambit_game):
Here we construct a two by two game in gambit::

sage: # optional - pygambit
sage: import gambit
sage: import pygambit
sage: from sage.game_theory.parser import Parser
sage: g = gambit.Game.new_table([2,2])
sage: g = pygambit.Game.new_table([2,2])
sage: g[int(0), int(0)][int(0)] = int(2)
sage: g[int(0), int(0)][int(1)] = int(1)
sage: g[int(0), int(1)][int(0)] = int(0)
Expand All @@ -207,7 +207,7 @@ def format_gambit(self, gambit_game):
sage: g[int(1), int(0)][int(1)] = int(0)
sage: g[int(1), int(1)][int(0)] = int(1)
sage: g[int(1), int(1)][int(1)] = int(2)
sage: solver = gambit.nash.ExternalLCPSolver()
sage: solver = pygambit.nash.ExternalLCPSolver()

Here is the output of the LCP algorithm::

Expand All @@ -228,7 +228,7 @@ def format_gambit(self, gambit_game):
Here is another game::

sage: # optional - pygambit
sage: g = gambit.Game.new_table([2,2])
sage: g = pygambit.Game.new_table([2,2])
sage: g[int(0), int(0)][int(0)] = int(4)
sage: g[int(0), int(0)][int(1)] = int(8)
sage: g[int(0), int(1)][int(0)] = int(0)
Expand All @@ -237,7 +237,7 @@ def format_gambit(self, gambit_game):
sage: g[int(1), int(0)][int(1)] = int(3)
sage: g[int(1), int(1)][int(0)] = int(1)
sage: g[int(1), int(1)][int(1)] = int(0)
sage: solver = gambit.nash.ExternalLCPSolver()
sage: solver = pygambit.nash.ExternalLCPSolver()

Here is the LCP output::

Expand All @@ -254,7 +254,7 @@ def format_gambit(self, gambit_game):
Here is a larger degenerate game::

sage: # optional - pygambit
sage: g = gambit.Game.new_table([3,3])
sage: g = pygambit.Game.new_table([3,3])
sage: g[int(0), int(0)][int(0)] = int(-7)
sage: g[int(0), int(0)][int(1)] = int(-9)
sage: g[int(0), int(1)][int(0)] = int(-5)
Expand All @@ -273,7 +273,7 @@ def format_gambit(self, gambit_game):
sage: g[int(2), int(1)][int(1)] = int(6)
sage: g[int(2), int(2)][int(0)] = int(1)
sage: g[int(2), int(2)][int(1)] = int(-10)
sage: solver = gambit.nash.ExternalLCPSolver()
sage: solver = pygambit.nash.ExternalLCPSolver()

Here is the LCP output::

Expand Down