Fehu_envs.CartpoleSourceClassic cart-pole balancing environment.
ID: CartPole-v1
Observation Space: Fehu.Space.Box with shape [4] in range:
Action Space: Fehu.Space.Discrete with 2 choices:
0: Push cart to the left1: Push cart to the rightRewards: +1.0 for each step the pole remains upright
Episode Termination:
Rendering: Text output showing cart position, velocity, pole angle, and angular velocity
Train an agent to balance the pole:
let rng = Rune.Rng.create () in
let env = Fehu_envs.Cartpole.make ~rng () in
let obs, _ = Fehu.Env.reset env () in
let rec run_episode total_reward =
let action = (* DQN or policy gradient decision *) in
let t = Fehu.Env.step env action in
let new_total = total_reward +. t.reward in
if t.terminated || t.truncated then
Printf.printf "Episode reward: %.0f\n" new_total
else
run_episode new_total
in
run_episode 0.0type state = {mutable x : float;mutable x_dot : float;mutable theta : float;mutable theta_dot : float;mutable steps : int;rng : Rune.Rng.key ref;}val reset :
'a ->
?options:'b ->
unit ->
state ->
(float, Rune.float32_elt) Rune.t * Fehu.Info.tval step :
'a ->
(Int32.t, 'b) Rune.t ->
state ->
((float, Rune.float32_elt) Rune.t, 'c, 'd) Fehu.Env.transitionval make :
rng:Rune.Rng.key ->
unit ->
(Fehu.Space.Box.element, Fehu.Space.Discrete.element, string) Fehu.Env.t