Skip to content

Commit

Permalink
Updated Calculation Order (#1019)
Browse files Browse the repository at this point in the history
* Updated order Calculation

Updated order of `x` and `theta` calculations so that they are no longer one timestamp behind `x_dot` and `theta_dot`.

#1018

* Added semi-implicit euler option

* Got implicit and standard euler mixed up

* switched default option
  • Loading branch information
Rampagy authored and pzhokhov committed Sep 21, 2018
1 parent b2149e9 commit 42f9e14
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions gym/envs/classic_control/cartpole.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(self):
self.polemass_length = (self.masspole * self.length)
self.force_mag = 10.0
self.tau = 0.02 # seconds between state updates
self.kinematics_integrator = 'euler'

# Angle at which to fail the episode
self.theta_threshold_radians = 12 * 2 * math.pi / 360
Expand Down Expand Up @@ -97,10 +98,16 @@ def step(self, action):
temp = (force + self.polemass_length * theta_dot * theta_dot * sintheta) / self.total_mass
thetaacc = (self.gravity * sintheta - costheta* temp) / (self.length * (4.0/3.0 - self.masspole * costheta * costheta / self.total_mass))
xacc = temp - self.polemass_length * thetaacc * costheta / self.total_mass
x = x + self.tau * x_dot
x_dot = x_dot + self.tau * xacc
theta = theta + self.tau * theta_dot
theta_dot = theta_dot + self.tau * thetaacc
if self.kinematics_integrator == 'euler':
x = x + self.tau * x_dot
x_dot = x_dot + self.tau * xacc
theta = theta + self.tau * theta_dot
theta_dot = theta_dot + self.tau * thetaacc
else: # semi-implicit euler
x_dot = x_dot + self.tau * xacc
x = x + self.tau * x_dot
theta_dot = theta_dot + self.tau * thetaacc
theta = theta + self.tau * theta_dot
self.state = (x,x_dot,theta,theta_dot)
done = x < -self.x_threshold \
or x > self.x_threshold \
Expand Down

0 comments on commit 42f9e14

Please sign in to comment.