Utilities

Exported

GameTheory.NormalFormGame — Method
NormalFormGame([T], p)

Construct a NormalFormGame (of eltype T if specified) from a GAMPayoffVector p.

Examples

julia> nums_actions = (3, 2);

julia> payoffs = collect(1:12);

julia> @show payoffs;
payoffs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

julia> p = GameTheory.GAMPayoffVector(nums_actions, payoffs);

julia> NormalFormGame(p)
3×2 NormalFormGame{2, Int64}:
 (1, 7)  (4, 10)
 (2, 8)  (5, 11)
 (3, 9)  (6, 12)
source
GameTheory.gam_string — Method
gam_string(g)

Return the GameTracer .gam representation of the game g as a string. See write_gam for the requirement on the element type of g.

Arguments

  • g::Union{NormalFormGame,GAMPayoffVector} : Game to write.

Returns

  • ::String : The .gam representation of g.

Examples

julia> g = NormalFormGame(Player([3 1; 0 4; 2 5]), Player([2 6 1; 3 0 4]));

julia> gam_string(g)
"2\n3 2\n\n3 0 2 1 4 5 2 6 1 3 0 4\n"

julia> print(gam_string(g))
2
3 2

3 0 2 1 4 5 2 6 1 3 0 4
source
GameTheory.parse_gam — Method
parse_gam([T], text)

Parse the string text in the GameTracer .gam format and return the game as a NormalFormGame. See read_gam for the meaning of T and for reading from a stream or a file.

Arguments

  • T::Type : Element type of the payoffs, where T<:Real. If omitted, Int when every payoff in text is written as an integer (BigInt if one does not fit in Int) and Float64 otherwise.
  • text::AbstractString : String in the .gam format.

Returns

  • ::NormalFormGame{N,T} : The game described by text.

Examples

julia> s = """
       2
       3 2

       3 2 0 3 5 6 3 2 3 2 6 1
       """;

julia> parse_gam(s)
3×2 NormalFormGame{2, Int64}:
 (3, 3)  (3, 2)
 (2, 2)  (5, 6)
 (0, 3)  (6, 1)

julia> parse_gam(Float64, s)
3×2 NormalFormGame{2, Float64}:
 (3.0, 3.0)  (3.0, 2.0)
 (2.0, 2.0)  (5.0, 6.0)
 (0.0, 3.0)  (6.0, 1.0)
source
GameTheory.read_gam — Method
read_gam([T], io)
read_gam([T], path)

Read a normal form game in the GameTracer .gam format from the stream io or the file at path, and return it as a NormalFormGame. See GAMPayoffVector for the ordering of the payoffs in the format, and parse_gam for reading from a string.

Arguments

  • T::Type : Element type of the payoffs, where T<:Real. If omitted, Int when every payoff in the input is written as an integer (BigInt if one does not fit in Int) and Float64 otherwise.
  • io::IO : Input stream.
  • path::AbstractString : Path to the file to read.

Returns

  • ::NormalFormGame{N,T} : The game described by the input.

Examples

julia> g = NormalFormGame(Player([3 1; 0 4; 2 5]), Player([2 6 1; 3 0 4]));

julia> path = tempname();

julia> write_gam(path, g)

julia> read_gam(path)
3×2 NormalFormGame{2, Int64}:
 (3, 2)  (1, 3)
 (0, 6)  (4, 0)
 (2, 1)  (5, 4)

julia> read_gam(Float64, path)
3×2 NormalFormGame{2, Float64}:
 (3.0, 2.0)  (1.0, 3.0)
 (0.0, 6.0)  (4.0, 0.0)
 (2.0, 1.0)  (5.0, 4.0)

A file at a URL can be read with read_gam(Downloads.download(url)).

source
GameTheory.write_gam — Method
write_gam(io, g)
write_gam(path, g)

Write the game g to the stream io or the file at path in the GameTracer .gam format. Each payoff is written with print, so the element type of g must be an Integer or an AbstractFloat type; convert first otherwise, e.g. with NormalFormGame(Float64, g). See gam_string for writing to a string.

Arguments

  • io::IO : Output stream.
  • path::AbstractString : Path to the file to write; an existing file is overwritten.
  • g::Union{NormalFormGame,GAMPayoffVector} : Game to write.

Examples

julia> g = NormalFormGame(Player([3 1; 0 4; 2 5]), Player([2 6 1; 3 0 4]));

julia> write_gam(stdout, g)
2
3 2

3 0 2 1 4 5 2 6 1 3 0 4

julia> write_gam("game.gam", g)
source

Internal

GameTheory.GAMPayoffVector — Type
GAMPayoffVector{N,T}

Intermediate representation that stores payoffs in a single flat vector.

Payoff values are ordered as in the GameTracer .gam format:

  1. Player-major blocks: player 1, ..., player N.
  2. Within each block, action profiles are ordered with player 1 varying fastest, then player 2, ..., player N (i.e., Fortran/column-major order).

Fields

  • nums_actions::NTuple{N,Int} : Tuple of the numbers of actions, one for each player.
  • payoffs::Vector{T} : Vector storing payoffs in .gam order.
source
GameTheory.GAMPayoffVector — Method
GAMPayoffVector([T], g)

Construct a GAMPayoffVector (of eltype T if specified) from a NormalFormGame g.

Examples

julia> player1 = Player([1 4; 2 5; 3 6]);

julia> player2 = Player([7 8 9; 10 11 12]);

julia> g = NormalFormGame(player1, player2)
3×2 NormalFormGame{2, Int64}:
 (1, 7)  (4, 10)
 (2, 8)  (5, 11)
 (3, 9)  (6, 12)

julia> p = GameTheory.GAMPayoffVector(g);

julia> @show p.payoffs;
p.payoffs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
source