Skip to content

Instantly share code, notes, and snippets.

@msaroufim
Created October 27, 2019 22:39
Show Gist options
  • Save msaroufim/38d21321a2266809930c3f78977c01f1 to your computer and use it in GitHub Desktop.
Save msaroufim/38d21321a2266809930c3f78977c01f1 to your computer and use it in GitHub Desktop.
# https://github.com/JuliaML/Reinforce.jl/blob/master/examples/mountain_car.jl
using Reinforce
using Reinforce.MountainCarEnv: MountainCar
using Plots
gr()
# Deterministic policy that is solving the problem
mutable struct BasicCarPolicy <: Reinforce.AbstractPolicy end
Reinforce.action(policy::BasicCarPolicy, r, s, A) = s.velocity < 0 ? 1 : 3
# Environment setup
env = MountainCar()
function episode!(env, π = RandomPolicy())
ep = Episode(env, π)
for (s, a, r, s′) in ep
gui(plot(env))
end
ep.total_reward, ep.niter
end
# Main part
R, n = episode!(env, BasicCarPolicy())
println("reward: $R, iter: $n")
# This one can be really long...
R, n = episode!(env, RandomPolicy())
println("reward: $R, iter: $n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment