game_converters

Functions for converting between ways of storing games.

Examples

Create a QuantEcon NormalFormGame from a gam file storing a 3-player Minimum Effort Game

>>> import os
>>> import quantecon.game_theory as gt
>>> filepath = os.path.dirname(gt.__file__)
>>> filepath += "/tests/game_files/minimum_effort_game.gam"
>>> nfg = gt.from_gam(filepath)
>>> print(nfg)
3-player NormalFormGame with payoff profile array:
[[[[  1.,   1.,   1.],   [  1.,   1.,  -9.],   [  1.,   1., -19.]],
  [[  1.,  -9.,   1.],   [  1.,  -9.,  -9.],   [  1.,  -9., -19.]],
  [[  1., -19.,   1.],   [  1., -19.,  -9.],   [  1., -19., -19.]]],

 [[[ -9.,   1.,   1.],   [ -9.,   1.,  -9.],   [ -9.,   1., -19.]],
  [[ -9.,  -9.,   1.],   [  2.,   2.,   2.],   [  2.,   2.,  -8.]],
  [[ -9., -19.,   1.],   [  2.,  -8.,   2.],   [  2.,  -8.,  -8.]]],

 [[[-19.,   1.,   1.],   [-19.,   1.,  -9.],   [-19.,   1., -19.]],
  [[-19.,  -9.,   1.],   [ -8.,   2.,   2.],   [ -8.,   2.,  -8.]],
  [[-19., -19.,   1.],   [ -8.,  -8.,   2.],   [  3.,   3.,   3.]]]]
class quantecon.game_theory.game_converters.GAMReader[source]

Bases: object

Reader object that converts a game in GameTracer gam format into a NormalFormGame.

Methods

from_file(file_path)

Read from a gam format file.

from_string(string)

Read from a gam format string.

from_url(url)

Read from a URL.

classmethod from_file(file_path)[source]

Read from a gam format file.

Parameters:
file_pathstr

Path to gam file.

Returns:
NormalFormGame

Examples

Save a gam format string in a temporary file:

>>> import tempfile
>>> fname = tempfile.mkstemp()[1]
>>> with open(fname, mode='w') as f:
...       f.write("""\
... 2
... 3 2
...
... 3 2 0 3 5 6 3 2 3 2 6 1""")

Read the file:

>>> g = GAMReader.from_file(fname)
>>> print(g)
2-player NormalFormGame with payoff profile array:
[[[3, 3],  [3, 2]],
 [[2, 2],  [5, 6]],
 [[0, 3],  [6, 1]]]
classmethod from_string(string)[source]

Read from a gam format string.

Parameters:
stringstr

String in gam format.

Returns:
NormalFormGame

Examples

>>> string = """\
... 2
... 3 2
...
... 3 2 0 3 5 6 3 2 3 2 6 1"""
>>> g = GAMReader.from_string(string)
>>> print(g)
2-player NormalFormGame with payoff profile array:
[[[3, 3],  [3, 2]],
 [[2, 2],  [5, 6]],
 [[0, 3],  [6, 1]]]
classmethod from_url(url)[source]

Read from a URL.

class quantecon.game_theory.game_converters.GAMWriter[source]

Bases: object

Writer object that converts a NormalFormgame into a game in GameTracer gam format.

Methods

to_file(g, file_path)

Save the GameTracer gam format string representation of the NormalFormGame g to a file.

to_string(g)

Return a GameTracer gam format string representing the NormalFormGame g.

classmethod to_file(g, file_path)[source]

Save the GameTracer gam format string representation of the NormalFormGame g to a file.

Parameters:
gNormalFormGame
file_pathstr

Path to the file to write to.

classmethod to_string(g)[source]

Return a GameTracer gam format string representing the NormalFormGame g.

Parameters:
gNormalFormGame
Returns:
str

String representation in gam format.

quantecon.game_theory.game_converters.from_gam(filename: str) NormalFormGame[source]

Makes a QuantEcon Normal Form Game from a gam file.

Gam files are described by GameTracer [1].

Parameters:
filenamestr

path to gam file.

Returns:
NormalFormGame

The QuantEcon Normal Form Game described by the gam file.

References

[1]

Bem Blum, Daphne Kohler, Christian Shelton http://dags.stanford.edu/Games/gametracer.html

quantecon.game_theory.game_converters.str2num(s)[source]
quantecon.game_theory.game_converters.to_gam(g, file_path=None)[source]

Write a NormalFormGame to a file in gam format.

Parameters:
gNormalFormGame
file_pathstr, optional(default=None)

Path to the file to write to. If None, the result is returned as a string.