Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EHN: Add random action profile generator #107

Merged
merged 1 commit into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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