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

Pure strategy nash equilibrium #12

Merged
merged 7 commits into from
Nov 14, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 13 additions & 2 deletions src/Games.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
module Games

# Packages

# package code goes here
include("normal_form_game.jl")
include("nash_equilibrium.jl")

export Player, NormalFormGame, # Types

export Player, NormalFormGame,
# Type aliases
Action, MixedAction, PureAction, ActionProfile,

# Normal form game functions
best_response, best_responses, is_best_response, payoff_vector,
is_nash, pure2mixed, num_players, num_actions, num_opponents
is_nash, pure2mixed, pure_strategy_NE,

# General functions
num_players, num_actions, num_opponents

end # module
23 changes: 23 additions & 0 deletions src/nash_equilibrium.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Finds all pure strategy Nash equilibrium for a normal form
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strategy -> action
equilibrium -> equilibria
(or just "pure Nash equilibria")

game. It returns an empty array if there are no nash
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are -> is
no nash -> no pure Nash or no pure action Nash

equilibrium
"""
function pure_strategy_NE(nfg::NormalFormGame)
# Get number of players and their actions
np = num_players(nfg)
na = nfg.nums_actions

# Holder for all NE
ne = Array(ActionProfile, 0)

# For each action profile check whether it is NE
as = CartesianRange(na)
for a in as
_a = a.I::NTuple{np, Int}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having to insert type checks is never a great thing because (I believe) it incurs minor runtime cost.

It might be better to add a method is_nash{N}(::NormalFormGame{N}, ::CartesianIndex{N}) and then we can skip the _a bit all together

is_nash(nfg, _a) ? push!(ne, _a) : nothing
end

return ne
end

1 change: 1 addition & 0 deletions src/normal_form_game.jl
Original file line number Diff line number Diff line change
Expand Up @@ -594,3 +594,4 @@ function pure2mixed(num_actions::Integer, action::PureAction)
mixed_action[action] = 1
return mixed_action
end

13 changes: 13 additions & 0 deletions test/test_normal_form_game.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,19 @@
@test_throws ArgumentError g = NormalFormGame(zeros((2, 2, 1)))
end

# Pure strategy Nash equilibrium
@testset "2x2 Pure strategy Nash equilibrium" begin
A = [9.0 1.0
10.0 3.0]
p1 = Player(A)
p2 = Player(A)

nfg = NormalFormGame(p1, p2)
psne = pure_strategy_NE(nfg)

@test psne == [(2, 2)]
end

# Utility functions #

@testset "pure2mixed" begin
Expand Down