-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'garnet' of github.com:RiskAverseRL/MDPs.jl into garnet
- Loading branch information
Showing
6 changed files
with
59 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using JuMP | ||
|
||
# ---------------------------------------------------------------- | ||
# Linear Program Solver | ||
# ---------------------------------------------------------------- | ||
|
||
|
||
""" | ||
lp_solve(model, γ, lpm) | ||
Implements the linear program primal problem for an MDP `model` with a discount factor `γ`. | ||
It uses the JuMP model `lpm` as the linear program solver and returns the state values | ||
found by `lpm`. | ||
""" | ||
|
||
function lp_solve(model::TabMDP, γ::Number, lpm) | ||
0 ≤ γ < 1 || error("γ must be between 0 and 1") | ||
set_silent(lpm) | ||
n = state_count(model) | ||
@variable(lpm, v[1:n]) | ||
@objective(lpm,Min, sum(v[1:n])) | ||
for s in 1:n | ||
m = action_count(model,s) | ||
for a in 1:m | ||
snext = transition(model,s,a) | ||
@constraint(lpm, v[s] ≥ sum(sp[2]*(sp[3]+γ*v[sp[1]]) for sp in snext)) | ||
end | ||
end | ||
optimize!(lpm) | ||
return value.(v) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,30 @@ | ||
using Main.MDPs | ||
using MDPs.Domains | ||
import HiGHS | ||
|
||
@testset "Solve Garnet" begin | ||
|
||
g = Domains.Garnet.GarnetMDP([[1,1],[2,0]],[[[1,0],[0,1]],[[0,1],[1,0]]],2,[2,2]) | ||
""" | ||
g = Garnet.GarnetMDP([[1,1],[2,0]],[[[1,0],[0,1]],[[0,1],[1,0]]],2,[2,2]) | ||
simulate(g, random_π(g), 1, 10000, 500) | ||
g1 = make_int_mdp(g; docompress=false) | ||
g2 = make_int_mdp(g; docompress=true) | ||
|
||
v1 = value_iteration(g, InfiniteH(0.99); ϵ=1e-7) | ||
v2 = value_iteration(g1, InfiniteH(0.99); ϵ=1e-7) | ||
v3 = value_iteration(g2, InfiniteH(0.99); ϵ=1e-7) | ||
v1 = value_iteration(g, InfiniteH(0.95); ϵ=1e-10) | ||
v2 = value_iteration(g1, InfiniteH(0.95); ϵ=1e-10) | ||
v3 = value_iteration(g2, InfiniteH(0.95); ϵ=1e-10) | ||
v4 = policy_iteration(g2, 0.95) | ||
#v5 = lp_solve(g, .95, HiGHS.Optimizer) | ||
|
||
# Ensure value functions are close | ||
V = hcat(v1.value, v2.value[1:end-1], v3.value[1:end-1], v4.value[1:end-1]) | ||
V = hcat(v1.value, v2.value[1:end-1], v3.value[1:end-1], v4.value[1:end-1], v5) | ||
@test map(x -> x[2] - x[1], mapslices(extrema, V; dims=2)) |> maximum ≤ 1e-6 | ||
|
||
# Ensure policies are identical | ||
p1 = greedy(model, InfiniteH(0.95), v1.value) | ||
p2 = greedy(model_g, InfiniteH(0.95), v2.value) | ||
p3 = greedy(model_gc, InfiniteH(0.95), v3.value) | ||
p1 = greedy(g, InfiniteH(0.95), v1.value) | ||
p2 = greedy(g1, InfiniteH(0.95), v2.value) | ||
p3 = greedy(g2, InfiniteH(0.95), v3.value) | ||
p4 = v4.policy | ||
p5 = greedy(g, InfiniteH(0.95), v5) | ||
|
||
P = hcat(p1, p2[1:end-1], p3[1:end-1], p4[1:end-1]) | ||
@test all(mapslices(allequal, P; dims=2)) | ||
""" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters