-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added DisplaySimulator code * documentation, etc for display * added a few comments * more permissive NamedTupleTools compat * added display.md to git
- Loading branch information
Showing
11 changed files
with
226 additions
and
14 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,42 @@ | ||
# Display | ||
|
||
## `DisplaySimulator` | ||
|
||
The `DisplaySimulator` displays each step of a simulation in real time through a multimedia display such as a Jupyter notebook or [ElectronDisplay](https://github.com/queryverse/ElectronDisplay.jl). | ||
Specifically it uses `POMDPModelTools.render` and the built-in Julia [`display` function](https://docs.julialang.org/en/v1/base/io-network/#Base.Multimedia.display) to visualize each step. | ||
|
||
Example: | ||
```julia | ||
using POMDPs | ||
using POMDPModels | ||
using POMDPPolicies | ||
using POMDPSimulators | ||
using ElectronDisplay | ||
ElectronDisplay.CONFIG.single_window = true | ||
|
||
ds = DisplaySimulator() | ||
m = SimpleGridWorld() | ||
simulate(ds, m, RandomPolicy(m)) | ||
``` | ||
|
||
```@docs | ||
DisplaySimulator | ||
``` | ||
|
||
## Display-specific tips | ||
|
||
The following tips may be helpful when using particular displays. | ||
|
||
### Jupyter notebooks | ||
|
||
By default, in a Jupyter notebook, the visualizations of all steps are displayed in the output box one after another. To make the output animated instead, where the image is overwritten at each step, one may use | ||
```julia | ||
DisplaySimulator(predisplay=(d)->IJulia.clear_output(true)) | ||
``` | ||
|
||
### ElectronDisplay | ||
|
||
By default, ElectronDisplay will open a new window for each new step. To prevent this, use | ||
```julia | ||
ElectronDisplay.CONFIG.single_window = true | ||
``` |
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
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 |
---|---|---|
|
@@ -54,4 +54,8 @@ export | |
problem | ||
include("parallel.jl") | ||
|
||
export | ||
DisplaySimulator | ||
include("display.jl") | ||
|
||
end # module |
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,115 @@ | ||
struct DisplaySimulator | ||
display::Union{AbstractDisplay, Nothing} | ||
render_kwargs | ||
max_fps::Float64 | ||
predisplay::Function | ||
extra_initial::Bool | ||
extra_final::Bool | ||
stepsim::StepSimulator | ||
end | ||
|
||
""" | ||
DisplaySimulator(;kwargs...) | ||
Create a simulator that displays each step of a simulation. | ||
Given a POMDP or MDP model `m`, this simulator roughly works like | ||
for step in stepthrough(m, ...) | ||
display(render(m, step)) | ||
end | ||
# Keyword Arguments | ||
- `display::AbstractDisplay`: the display to use for the first argument to the `display` function. If this is `nothing`, `display(...)` will be called without an `AbstractDisplay` argument. | ||
- `render_kwargs::NamedTuple`: keyword arguments for `POMDPModelTools.render(...)` | ||
- `max_fps::Number=10`: maximum number of frames to be displayed per second - `sleep` will be used to skip extra time, so this is not designed for high precision | ||
- `predisplay::Function`: function to call before every call to `display(...)`. The only argument to this function will be the display (if it is specified) or `nothing` | ||
- `extra_initial::Bool=false`: if `true`, display an extra step at the beginning with only elements `t`, `sp`, and `bp` for POMDPs (this can be useful to see the initial state if `render` displays only `sp` and not `s`). | ||
- `extra_final`::Bool=false`: if `true`, display an extra step at the end with only elements `t`, `done`, `s`, and `b` for POMDPs (this can be useful to see the final state if `render` displays only `s` and not `sp`). | ||
- `max_steps::Integer`: maximum number of steps to run for | ||
- `spec::NTuple{Symbol}`: specification of what step elements to display (see `eachstep`) | ||
- `rng::AbstractRNG`: random number generator | ||
See the POMDPSimulators documentation for more tips about using specific displays. | ||
""" | ||
function DisplaySimulator(;display=nothing, | ||
render_kwargs=NamedTuple(), | ||
max_fps=10, | ||
predisplay=(d)->nothing, | ||
extra_initial=false, | ||
extra_final=true, | ||
max_steps=nothing, | ||
spec=CompleteSpec(), | ||
rng=Random.GLOBAL_RNG | ||
) | ||
stepsim = StepSimulator(rng, max_steps, spec) | ||
return DisplaySimulator(display, | ||
render_kwargs, | ||
max_fps, | ||
predisplay, | ||
extra_initial, | ||
extra_final, | ||
stepsim) | ||
end | ||
|
||
function simulate(sim::DisplaySimulator, m, args...) | ||
rsum = 0.0 | ||
disc = 1.0 | ||
dt = 1/sim.max_fps | ||
tm = time() | ||
isinitial = true | ||
last = NamedTuple() # for extra_final | ||
|
||
for step in simulate(sim.stepsim, m, args...) | ||
if isinitial && sim.extra_initial | ||
isinitial = false | ||
istep = initialstep(m, step) | ||
vis = render(m, istep; sim.render_kwargs...) | ||
perform_display(sim, vis) | ||
sleep_until(tm += dt) | ||
end | ||
|
||
vis = render(m, step; sim.render_kwargs...) | ||
perform_display(sim, vis) | ||
rsum += disc*get(step, :r, missing) | ||
disc *= discount(m) | ||
sleep_until(tm += dt) | ||
|
||
last = step # save for extra final | ||
end | ||
|
||
if sim.extra_final | ||
fstep = finalstep(m, last) | ||
vis = render(m, fstep; sim.render_kwargs...) | ||
perform_display(sim, vis) | ||
end | ||
|
||
if ismissing(rsum) | ||
return nothing | ||
else | ||
return rsum | ||
end | ||
end | ||
|
||
sleep_until(t) = sleep(max(t-time(), 0.0)) | ||
|
||
initialstep(m::MDP, step) = (t=0, sp=get(step, :s, missing)) | ||
initialstep(m::POMDP, step) = (t=0, | ||
sp=get(step, :s, missing), | ||
bp=get(step, :b, missing)) | ||
finalstep(m::MDP, last) = (done=true, | ||
t=get(last, :t, missing) + 1, | ||
s=get(last, :sp, missing)) | ||
finalstep(m::POMDP, last) = (done=true, | ||
t=get(last, :t, missing) + 1, | ||
s=get(last, :sp, missing), | ||
b=get(last, :bp, missing)) | ||
|
||
function perform_display(sim::DisplaySimulator, vis) | ||
sim.predisplay(sim.display) | ||
if sim.display===nothing | ||
display(vis) | ||
else | ||
display(sim.display, vis) | ||
end | ||
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
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,13 @@ | ||
ds = DisplaySimulator(max_steps=10, | ||
extra_initial=true, | ||
extra_final=true, | ||
rng=MersenneTwister(4)) | ||
m = BabyPOMDP() | ||
@test simulate(ds, m, Starve()) ≈ 0.0 | ||
|
||
ds = DisplaySimulator(max_steps=1, | ||
extra_initial=true, | ||
extra_final=true, | ||
rng=MersenneTwister(4)) | ||
m = SimpleGridWorld() | ||
@test simulate(ds, m, FunctionPolicy(s->first(actions(m)))) ≈ 0.0 |
27c515f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
27c515f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/5596
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via: