Skip to content

Instantly share code, notes, and snippets.

View msaroufim's full-sized avatar
🤖
Putting the finishing touches on my robot army

Mark Saroufim msaroufim

🤖
Putting the finishing touches on my robot army
View GitHub Profile
# 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
# 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
# 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
# Main part
R, n = episode!(env, BasicCarPolicy())
println("reward: $R, iter: $n")
# src/Reinforce.jl
abstract type AbstractPolicy end
"""
a = action(policy, r, s, A)
Take in the last reward `r`, current state `s`,
and set of valid actions `A = actions(env, s)`,
mutable struct Episode{E<:AbstractEnvironment,P<:AbstractPolicy,F<:AbstractFloat}
env::E
policy::P
total_reward::F # total reward of the episode
last_reward::F
niter::Int # current step in this episode
freq::Int # number of steps between choosing actions
maxn::Int # max steps in an episode - should be constant during an episode
end
const min_position = -1.2
const max_position = 0.6
const max_speed = 0.07
const goal_position = 0.5
const min_start = -0.6
const max_start = 0.4
const car_width = 0.05
const car_height = car_width/2.0
const clearance = 0.2*car_height
const min_position = -1.2
const max_position = 0.6
const max_speed = 0.07
const goal_position = 0.5
const min_start = -0.6
const max_start = 0.4
const car_width = 0.05
const car_height = car_width/2.0
const clearance = 0.2*car_height
actions(env::MountainCar, s) = DiscreteSet(1:3)
finished(env::MountainCar, s′) = env.state.position >= goal_position