Skip to content

Commit

Permalink
Resolve type instability in NormalFormGame(::Matrix)
Browse files Browse the repository at this point in the history
close #2, close #28
  • Loading branch information
oyamad committed Mar 21, 2017
1 parent fd1c11d commit f3e9a62
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
25 changes: 10 additions & 15 deletions src/normal_form_game.jl
Original file line number Diff line number Diff line change
Expand Up @@ -470,27 +470,22 @@ function NormalFormGame{T<:Real,M}(payoffs::Array{T,M})
end

"""
Constructor of a symmetric 2-player NormalFormGame if `size(payoffs, 1) > 1`,
otherwise construct a 1-player NormalFormGame with one action.
NormalFormGame{T<:Real}(payoffs::Matrix{T})
##### Arguments
Construct a symmetric 2-player NormalFormGame with a square matrix.
- `payoffs::Matrix{T<:Real}` : Square matrix representing each player's payoff
matrix.
# Arguments
* `payoffs::Matrix{T<:Real}` : Square matrix representing each player's payoff
matrix.
"""
function NormalFormGame{T<:Real}(payoffs::Matrix{T})
n, m = size(payoffs)
if m >= 2 # Two-player symmetric game
n != m && throw(ArgumentError(
"symmetric two-player game must be represented by a square matrix"
))
player = Player(payoffs)
return NormalFormGame(player, player)
else # Trivial game with 1 player
player = Player(vec(payoffs))
return NormalFormGame(player)
end
n != m && throw(ArgumentError(
"symmetric two-player game must be represented by a square matrix"
))
player = Player(payoffs)
return NormalFormGame(player, player)
end

Base.summary(g::NormalFormGame) =
Expand Down
5 changes: 3 additions & 2 deletions test/test_normal_form_game.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@
end

@testset "NormalFormGame with 1 player" begin
data = transpose([0 1 1])
g = @inferred NormalFormGame(data)
payoffs = [0, 1, 1]
g = @inferred NormalFormGame(Player(payoffs))
@test num_players(g) == 1
@test g.players[1].payoff_array == [0, 1, 1]
@test g[1] == 0
Expand Down Expand Up @@ -162,6 +162,7 @@

@testset "NormalFormGame invalid nonsquare matrix" begin
@test_throws ArgumentError g = NormalFormGame(zeros((2, 3)))
@test_throws ArgumentError g = NormalFormGame(transpose([0 1 1]))
end

@testset "NormalFormGame invalid payoff profiles" begin
Expand Down

0 comments on commit f3e9a62

Please sign in to comment.