Skip to content

Commit 7583404

Browse files
committed
(#2) Inroduce GameProblem for game solver and implement solve to multiple dispatch
1 parent b317daf commit 7583404

File tree

4 files changed

+42
-24
lines changed

4 files changed

+42
-24
lines changed

docs/src/game.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# Zero-Sum Games
22

33

4+
## Game Problem
5+
6+
```@docs
7+
OperationsResearchModels.GameProblem
8+
```
9+
410
## Game solver for the row player
511

612
```@docs
7-
OperationsResearchModels.game
13+
OperationsResearchModels.solve(g::GameProblem)
814
```
915

1016

src/OperationsResearchModels.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ import .Network: Connection, nodes
8383
import .MaximumFlow: MaximumFlowResult, MaximumFlowProblem
8484
import .MinimumCostFlow: MinimumCostFlowProblem, MinimumCostFlowResult
8585
import .Assignment: AssignmentProblem, AssignmentResult, isbalanced, balance
86-
import .Game: game, GameResult, game_solver
86+
import .Game: GameProblem, GameResult
8787
import .MinimumSpanningTree: hasloop, MstResult, MstProblem
8888
import .PMedian: pmedian, pmedian_with_distances, PMedianResult
8989
import .CPM: CpmActivity, earliestfinishtime, longestactivity, CpmProblem, CpmResult
@@ -99,7 +99,7 @@ export TransportationProblem, TransportationResult, balance, isbalanced, northwe
9999
export Connection, ShortestPathResult, MaximumFlowResult, MinimumCostFlowResult, nodes
100100
export ShortestPathProblem, MaximumFlowProblem, MinimumCostFlowProblem
101101
export AssignmentProblem, AssignmentResult, isbalanced, balance
102-
export game, GameResult, game_solver
102+
export GameProblem, GameResult
103103
export hasloop, MstResult, MstProblem
104104
export pmedian, pmedian_with_distances, PMedianResult
105105
export CpmActivity, earliestfinishtime, longestactivity, CpmProblem, CpmResult

src/game.jl

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@ module Game
33
using JuMP, HiGHS
44

55

6+
import ..OperationsResearchModels: solve
7+
8+
9+
"""
10+
GameProblem
11+
12+
# Description
13+
14+
Defines the problem of a zero-sum game.
15+
16+
# Fields
17+
18+
- `decisionMatrix::Matrix{<:Real}`: The payoff matrix of the game designed for the row player.
19+
"""
20+
struct GameProblem
21+
decisionMatrix::Matrix{<:Real}
22+
end
23+
24+
625
"""
726
GameResult
827
@@ -24,40 +43,27 @@ end
2443

2544

2645
"""
27-
game(decisionMatrix::Matrix{<:Real}; verbose::Bool = false)::Vector{GameResult}
46+
solve(p::GameProblem; verbose::Bool = false)::Vector{GameResult}
2847
2948
Solves a zero-sum game using the simplex method.
3049
3150
# Arguments
3251
33-
- `decisionMatrix`: The payoff matrix of the game.
52+
- `p::GameProblem`: The problem instance containing the decision matrix.
3453
- `verbose`: If true, prints the model information.
3554
3655
# Returns
3756
- An array of `GameResult` objects containing the probabilities and value of the game.
3857
"""
39-
function game(decisionMatrix::Matrix{<:Real}; verbose::Bool = false)::Vector{GameResult}
40-
rowplayers_result = game_solver(decisionMatrix, verbose = verbose)
41-
columnplayers_result = game_solver(Matrix(decisionMatrix') * -1.0, verbose = verbose)
58+
function solve(p::GameProblem; verbose::Bool = false)::Vector{GameResult}
59+
rowplayers_result = game_solver(p.decisionMatrix, verbose = verbose)
60+
columnplayers_result = game_solver(Matrix(p.decisionMatrix') * -1.0, verbose = verbose)
4261
return [rowplayers_result, columnplayers_result]
4362
end
4463

4564

4665

47-
"""
48-
game_solver(gamematrix::Matrix{<:Real}; verbose::Bool = false)::GameResult
49-
50-
Solves a zero-sum game using the simplex method.
51-
52-
# Arguments
5366

54-
- `gamematrix`: The payoff matrix of the game.
55-
- `verbose`: If true, prints the model information.
56-
57-
# Returns
58-
59-
- A `GameResult` object containing the probabilities and value of the game.
60-
"""
6167
function game_solver(gamematrix::Matrix{<:Real}; verbose::Bool = false)::GameResult
6268

6369
nrow, ncol = size(gamematrix)

test/testgame.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
-1 2 4
99
]
1010

11-
result = game(mat)
11+
problem = GameProblem(mat)
12+
13+
result = solve(problem)
1214

1315
@test isa(result, Vector{GameResult})
1416
@test length(result) == 2
@@ -32,7 +34,9 @@
3234
-1 1 0
3335
]
3436

35-
result = game(mat)
37+
problem = GameProblem(mat)
38+
39+
result = solve(problem)
3640

3741
@test isa(result, Vector{GameResult})
3842
@test length(result) == 2
@@ -54,7 +58,9 @@
5458
5 4
5559
]
5660

57-
result = game(mat)
61+
problem = GameProblem(mat)
62+
63+
result = solve(problem)
5864

5965
@test isa(result, Vector{GameResult})
6066

0 commit comments

Comments
 (0)