-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_algorithm_comparison.jl
More file actions
68 lines (60 loc) · 3.09 KB
/
Copy pathgame_algorithm_comparison.jl
File metadata and controls
68 lines (60 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
include("game_algorithm_definition.jl")
using DataFrames, CSV, Gurobi, Distributions, Random, MDPs, ProgressBars
function make_markov_game(num_states::Int64,num_actions_x::Vector{Int64},num_actions_y::Vector{Int64},r_lower::Float64,r_upper::Float64, η::Float64)
num_next = Int(round(η*num_states))
P = [zeros(num_actions_y[s],num_actions_x[s],num_states) for s ∈ 1:num_states]
R = [rand(Uniform(r_lower,r_upper),num_actions_y[s],num_actions_x[s]) for s ∈ 1:num_states]
for s ∈ 1:num_states
for a ∈ 1:num_actions_y[s]
for b ∈ 1:num_actions_x[s]
P[s][a,b,shuffle(1:num_states)[1:num_next]] = normalize(rand(Exponential(1),num_next),1)
end
end
end
return (transition = P, rewards = R)
end
function time_algorithm_games(alg,P,R,γ,ϵ,env,time_limit)
if alg.name == "VI"
return VI(P,R,γ,ϵ,env,time_limit)
elseif alg.name == "PAI"
return PAI(P,R,γ,ϵ,env,time_limit)
elseif alg.name == "HK"
return HoffKarp(P,R,γ,ϵ,env,time_limit)
elseif alg.name == "FT"
return Filar(P,R,γ,ϵ,env,time_limit,alg.η,alg.β)
elseif alg.name == "MP"
return Mareks(P,R,γ,ϵ,env,time_limit,alg.β)
elseif alg.name == "RCPI∞"
return Keiths(P,R,γ,ϵ,env,time_limit)
elseif alg.name == "RCPI₀"
return RCPI(P,R,γ,ϵ,env,time_limit)
elseif alg.name == "WS"
return Winnicki(P,R,γ,ϵ,env,time_limit,alg.H,alg.m)
elseif alg.name == "PPI"
return PPI(P,R,γ,ϵ,env,time_limit,alg.ϵ₂,alg.β)
else error("algorithm name must be one of: \n
VI, PAI, HK, PPI, FT, MP, KB, RCPI, WIN")
end
end
function benchmark_run_games(state_nums::Vector{Int64}, Γ::Vector{Float64}, algs, action_nums::Vector{Int64}, r_lower::Float64, r_upper::Float64, η::Number, ϵ::Number, maxtime::Float64)
results = DataFrame(runtime = Vector{Float64}(undef,0),game_id = Vector{Int64}(undef,0), state_number = Vector{Int64}(undef,0), algorithm = Vector{String}(undef,0),
γ = Vector{Float64}(undef,0), times = Vector{Vector{Float64}}(undef,0), errors = Vector{Vector{Float64}}(undef,0))
G_ENV = Gurobi.Env()
thread_lock = ReentrantLock()
pbar = ProgressBar(total=length(state_nums)*length(Γ)*length(algs))
Threads.@threads for id ∈ eachindex(state_nums)
nₛ = state_nums[id]
G = make_markov_game(nₛ,rand(action_nums,nₛ),rand(action_nums,nₛ),r_lower,r_upper,η)
for disc ∈ Γ
for alg ∈ algs
out = time_algorithm_games(alg,G.transition,G.rewards,disc,ϵ,G_ENV,maxtime)
@lock thread_lock (push!(results, [out.times[end],id,nₛ,alg.name,disc,out.times,out.errors]); update(pbar))
end
end
end
return results
end
#= algorithms = [(name = "VI",), (name = "PAI",), (name = "HK",), (name = "FT", η = 1e-3, β = .5),
(name = "KB",), (name = "RCPI",), (name = "WIN", H = 2, m = 4),
(name = "PPI", ϵ₂ = .1, β = .5)] =#
#= algorithms = [(name = "PAI",), (name = "KB",), (name = "RCPI",), (name = "VI",)] =#