Skip to content

Commit

Permalink
Merge 58d6408 into d096d50
Browse files Browse the repository at this point in the history
  • Loading branch information
oyamad committed Feb 18, 2018
2 parents d096d50 + 58d6408 commit 6f19ffb
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 12 deletions.
1 change: 1 addition & 0 deletions benchmark/REQUIRE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BenchmarkTools
25 changes: 25 additions & 0 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Games
using BenchmarkTools

const SUITE = BenchmarkGroup()

SUITE["support_enumeration"] = BenchmarkGroup(["support_enumeration"])
SUITE["support_enumeration"]["Float"] = BenchmarkGroup()
seed = 0
rng = MersenneTwister(seed)
ns = [8, 9, 10, 11]
for n in ns
sz = (n, n)
g = random_game(rng, sz)
SUITE["support_enumeration"]["Float"][sz] =
@benchmarkable support_enumeration($g)
end
SUITE["support_enumeration"]["Rational"] = BenchmarkGroup()
T = Rational{Int}
ns = [8]
for n in ns
sz = (n, n)
g = NormalFormGame(eye(T, n))
SUITE["support_enumeration"]["Rational"][sz] =
@benchmarkable support_enumeration($g)
end
48 changes: 36 additions & 12 deletions src/support_enumeration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,26 @@ function _support_enumeration_producer(c::Channel,

end

function _solve!(A::Matrix{T}, b::Vector{T}) where T <: Union{Float64,Float32}
r = 0
try
LAPACK.gesv!(A, b)
catch LinAlg.LAPACKException
r = 1
end
return r
end

@inline function _solve!(A::Matrix, b::Vector)
r = 0
try
A_ldiv_B!(lufact!(A), b)
catch LinAlg.SingularException
r = 1
end
return r
end

"""
_indiff_mixed_action!(A, b, out, payoff_matrix, own_supp, opp_supp)
Expand All @@ -141,13 +161,17 @@ steps.
# Arguments
- `A::Matrix{T}`: Matrix used in intermediate steps, where `T<:Real`.
- `b::Vector{T}`: Vector used in intermediate steps, where `T<:Real`.
- `out::Vector{T}`: Vector to store the nonzero values of the
- `A::Matrix{T}`: Matrix of shape (k+1, k+1) used in intermediate steps, where
`T<:Real`.
- `b::Vector{T}`: Vector of length k+1 used in intermediate steps, where
`T<:Real`.
- `out::Vector{T}`: Vector of length k to store the nonzero values of the
desired mixed action, where `T<:Real`.
- `payoff_matrix::Matrix`: The player's payoff matrix.
- `own_supp::Vector{Int}`: Vector containing the player's action indices.
- `opp_supp::Vector{Int}`: Vector containing the opponent's action indices.
- `payoff_matrix::Matrix`: The player's payoff matrix, of shape (m, n).
- `own_supp::Vector{Int}`: Vector containing the player's action indices, of
length k.
- `opp_supp::Vector{Int}`: Vector containing the opponent's action indices, of
length k.
# Returns
Expand All @@ -162,17 +186,17 @@ function _indiff_mixed_action!(A::Matrix{T}, b::Vector{T},
m = size(payoff_matrix, 1)
k = length(own_supp)

A[1:end-1, 1:end-1] = payoff_matrix[own_supp, opp_supp]
for j in 1:k, i in 1:k
A[i, j] = payoff_matrix[own_supp[i], opp_supp[j]]
end
A[1:end-1, end] = -one(T)
A[end, 1:end-1] = one(T)
A[end, end] = zero(T)
b[1:end-1] = zero(T)
b[end] = one(T)
try
b = A_ldiv_B!(lufact!(A), b)
catch LinAlg.SingularException
return false
end

r = _solve!(A, b)
r == 0 || return false # A: singular

for i in 1:k
b[i] <= zero(T) && return false
Expand Down
31 changes: 31 additions & 0 deletions test/test_support_enumeration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,36 @@
end
end

@testset "Pure coordination game" begin
function generate_NEs(T::Type, n::Int)
S = typeof(zero(T)/one(T))
out = [tuple([zeros(S, n) for p in 1:2]...) for i in 1:(2^n-1)]
i = 1
for k in 1:n
a = collect(1:k)
while a[end] < n + 1
for p in 1:2
out[i][p][a] = one(S) / k
end
Games._next_k_array!(a)
i += 1
end
end
return out
end

n = 3
Ts = [Int, Float64, Rational{Int}]
for T in Ts
g = NormalFormGame(eye(T, n))
NEs = generate_NEs(T, n)

for (actions_computed, actions) in zip(support_enumeration(g), NEs)
for (action_computed, action) in zip(actions_computed, actions)
@test action_computed action
end
end
end
end

end

0 comments on commit 6f19ffb

Please sign in to comment.