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

Use of ProgressMeter.jl and added a reset_callback to DPWSolver #69

Merged
merged 15 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ POMDPPolicies = "182e52fb-cfd0-5e46-8c26-fd0667c990f4"
POMDPSimulators = "e0d0a172-29c6-5d4e-96d0-f262df5d01fd"
POMDPs = "a93abf59-7444-517b-a68a-c42f96afdd7d"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
Expand Down
1 change: 1 addition & 0 deletions src/MCTS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using POMDPSimulators
using CPUTime
using Random
using Printf
using ProgressMeter

export
MCTSSolver,
Expand Down
11 changes: 9 additions & 2 deletions src/dpw.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ POMDPs.action(p::DPWPlanner, s) = first(action_info(p, s))
"""
Construct an MCTSDPW tree and choose the best action. Also output some information.
"""
function POMDPModelTools.action_info(p::DPWPlanner, s; tree_in_info=false)
function POMDPModelTools.action_info(p::DPWPlanner, s; tree_in_info=false, show_progress=false)

Choose a reason for hiding this comment

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

I'd suggest using an Union{Progress, Nothing} here. So that @zsunberg will have no concern about performance😁

local a::actiontype(p.mdp)
info = Dict{Symbol, Any}()
dt = show_progress ? 0.1 : Inf
try
if isterminal(p.mdp, s)
error("""
Expand Down Expand Up @@ -48,13 +49,16 @@ function POMDPModelTools.action_info(p::DPWPlanner, s; tree_in_info=false)

nquery = 0
start_us = CPUtime_us()
for i = 1:p.solver.n_iterations
@showprogress dt for i = 1:p.solver.n_iterations
nquery += 1
simulate(p, snode, p.solver.depth) # (not 100% sure we need to make a copy of the state here)
if CPUtime_us() - start_us >= p.solver.max_time * 1e6
break
end
end
if !isnothing(p.solver.reset_callback)
p.solver.reset_callback(p.mdp, s) # Optional: used to reset/reinitialize MDP to a given state.
end
info[:search_time_us] = CPUtime_us() - start_us
info[:tree_queries] = nquery
if p.solver.tree_in_info || tree_in_info
Expand Down Expand Up @@ -89,6 +93,9 @@ function simulate(dpw::DPWPlanner, snode::Int, d::Int)
sol = dpw.solver
tree = dpw.tree
s = tree.s_labels[snode]
if !isnothing(sol.reset_callback)
sol.reset_callback(dpw.mdp, s) # Optional: used to reset/reinitialize MDP to a given state.
end
if isterminal(dpw.mdp, s)
return 0.0
elseif d == 0
Expand Down
11 changes: 9 additions & 2 deletions src/dpw_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ Fields:
If this is a Policy `p`, `action(p, belief)` will be called.
If it is an object `a`, `default_action(a, pomdp, belief, ex)` will be called, and if this method is not implemented, `a` will be returned directly.
default: `ExceptionRethrow()`

reset_callback::Function
Function used to reset/reinitialize the MDP to a given state `s`.
`f(mdp, s)` will be called.
default: `nothing`
"""
mutable struct DPWSolver <: AbstractMCTSSolver
depth::Int
Expand All @@ -108,6 +113,7 @@ mutable struct DPWSolver <: AbstractMCTSSolver
init_N::Any
next_action::Any
default_action::Any
reset_callback::Any
end

"""
Expand All @@ -134,9 +140,10 @@ function DPWSolver(;depth::Int=10,
init_Q::Any = 0.0,
init_N::Any = 0,
next_action::Any = RandomActionGenerator(rng),
default_action::Any = ExceptionRethrow()
default_action::Any = ExceptionRethrow(),
reset_callback::Any = nothing,
)
DPWSolver(depth, exploration_constant, n_iterations, max_time, k_action, alpha_action, k_state, alpha_state, keep_tree, enable_action_pw, enable_state_pw, check_repeat_state, check_repeat_action, tree_in_info, rng, estimate_value, init_Q, init_N, next_action, default_action)
DPWSolver(depth, exploration_constant, n_iterations, max_time, k_action, alpha_action, k_state, alpha_state, keep_tree, enable_action_pw, enable_state_pw, check_repeat_state, check_repeat_action, tree_in_info, rng, estimate_value, init_Q, init_N, next_action, default_action, reset_callback)
end

#=
Expand Down