Skip to content

Commit

Permalink
Merge pull request #107 from QuantEcon/random_actions
Browse files Browse the repository at this point in the history
EHN: Add random action profile generator
  • Loading branch information
oyamad authored Mar 6, 2019
2 parents d3e2067 + 29efb32 commit 600b146
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Games.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export

# Random Games
random_game, covariance_game,
random_pure_actions, random_mixed_actions,

# Support Enumeration
support_enumeration, support_enumeration_task
Expand Down
42 changes: 42 additions & 0 deletions src/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,45 @@ end

covariance_game(nums_actions::NTuple{N,Int}, rho::Real) where {N} =
covariance_game(Random.GLOBAL_RNG, nums_actions, rho)

#
# Random action profile
#
"""
random_pure_actions([rng=GLOBAL_RNG], nums_actions)
Return a tuple of random pure actions (integers).
# Arguments
- `rng::AbstractRNG=GLOBAL_RNG`: Random number generator used.
- `nums_actions::NTuple{N,Int}`: N-tuple of the numbers of actions,
one for each player.
# Returns
- `::NTuple{N,Int}`: N-tuple of random pure actions.
"""
random_pure_actions(rng::AbstractRNG, nums_actions::NTuple{N,Int}) where {N} =
ntuple(i -> rand(rng, 1:nums_actions[i]), Val(N))

random_pure_actions(nums_actions::NTuple{N,Int}) where {N} =
random_pure_actions(Random.GLOBAL_RNG, nums_actions)

"""
random_mixed_actions(nums_actions)
Return a tuple of random mixed actions (vectors of floats).
# Arguments
- `nums_actions::NTuple{N,Int}`: N-tuple of the numbers of actions,
one for each player.
# Returns
- `::NTuple{N,Vector{Float64}}`: N-tuple of random mixed actions.
"""
random_mixed_actions(nums_actions::NTuple{N,Int}) where {N} =
ntuple(i -> vec(QuantEcon.random_probvec(nums_actions[i], 1)), Val(N))
17 changes: 17 additions & 0 deletions test/test_random.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Random

@testset "Testing Random Games Generating" begin

@testset "test random game" begin
Expand Down Expand Up @@ -60,4 +62,19 @@

end

@testset "random_pure_actions" begin
nums_actions = (2, 3, 4)
seed = 1234
action_profiles = [random_pure_actions(MersenneTwister(seed), nums_actions)
for i in 1:2]
@test action_profiles[1] <= nums_actions
@test action_profiles[2] == action_profiles[1]
end

@testset "random_mixed_actions" begin
nums_actions = (2, 3, 4)
action_profile = random_mixed_actions(nums_actions)
@test length.(action_profile) == nums_actions
end

end

0 comments on commit 600b146

Please sign in to comment.