-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from JuliaPOMDP/policy-evaluation
Policy evaluation
- Loading branch information
Showing
8 changed files
with
112 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Policy Evaluation | ||
|
||
The [`evaluate`](@ref) function provides a policy evaluation tool for MDPs: | ||
|
||
```@docs | ||
evaluate | ||
``` |
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,83 @@ | ||
""" | ||
Value function for a policy on an MDP. | ||
If `v` is a `DiscreteValueFunction`, access the value for a state with `v(s)` | ||
""" | ||
struct DiscreteValueFunction{M<:MDP} <: Function | ||
m::M | ||
values::Vector{Float64} | ||
end | ||
|
||
(v::DiscreteValueFunction)(s) = v.values[stateindex(v.m, s)] | ||
|
||
""" | ||
evaluate(m::MDP, p::Policy) | ||
evaluate(m::MDP, p::Policy; rewardfunction=POMDPs.reward) | ||
Calculate the value for a policy on an MDP using the approach in equation 4.2.2 of Kochenderfer, *Decision Making Under Uncertainty*, 2015. | ||
Returns a DiscreteValueFunction, which maps states to values. | ||
# Example | ||
``` | ||
using POMDPModelTools, POMDPPolicies, POMDPModels | ||
m = SimpleGridWorld() | ||
u = evaluate(m, FunctionPolicy(x->:left)) | ||
u([1,1]) # value of always moving left starting at state [1,1] | ||
``` | ||
""" | ||
function evaluate(m::MDP, p::Policy; rewardfunction=POMDPs.reward) | ||
t = policy_transition_matrix(m, p) | ||
r = policy_reward_vector(m, p, rewardfunction=rewardfunction) | ||
u = (I-discount(m)*t)\r | ||
return DiscreteValueFunction(m, u) | ||
end | ||
|
||
""" | ||
policy_transition_matrix(m::Union{MDP, POMDP}, p::Policy) | ||
Create an |S|x|S| sparse transition matrix for a given policy. | ||
The row corresponds to the current state and column to the next state. Corresponds to ``T^π`` in equation (4.7) in Kochenderfer, *Decision Making Under Uncertainty*, 2015. | ||
""" | ||
function policy_transition_matrix(m::Union{MDP,POMDP}, p::Policy) | ||
ns = n_states(m) | ||
rows = Int[] | ||
cols = Int[] | ||
probs = Float64[] | ||
|
||
for s in states(m) | ||
if !isterminal(m, s) # if terminal, the transition probabilities are all just zero | ||
si = stateindex(m, s) | ||
a = action(p, s) | ||
td = transition(m, s, a) | ||
for (sp, p) in weighted_iterator(td) | ||
if p > 0.0 | ||
spi = stateindex(m, sp) | ||
push!(rows, si) | ||
push!(cols, spi) | ||
push!(probs, p) | ||
end | ||
end | ||
end | ||
end | ||
|
||
return sparse(rows, cols, probs, ns, ns) | ||
end | ||
|
||
function policy_reward_vector(m::Union{MDP,POMDP}, p::Policy; rewardfunction=POMDPs.reward) | ||
r = zeros(n_states(m)) | ||
for s in states(m) | ||
if !isterminal(m, s) # if terminal, the transition probabilities are all just zero | ||
si = stateindex(m, s) | ||
a = action(p, s) | ||
td = transition(m, s, a) | ||
for (sp, p) in weighted_iterator(td) | ||
if p > 0.0 | ||
r[si] += p*rewardfunction(m, s, a, sp) | ||
end | ||
end | ||
end | ||
end | ||
return r | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
POMDPModels | ||
POMDPSimulators | ||
POMDPPolicies |
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,8 @@ | ||
let | ||
m = SimpleGridWorld(rewards=Dict([3,1]=>10.0), tprob=1.0) | ||
u = evaluate(m, FunctionPolicy(x->:right)) | ||
@test u([2,1]) == 9.5 | ||
@test u([3,1]) == 10.0 | ||
@test u([4,1]) == 0.0 | ||
@test u([3,2]) == 0.0 | ||
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