4 import pyexotica
as exo
6 from numpy
import testing
as nptest
11 """Tests cartpole derivatives against python's autograd.""" 16 _, theta, xdot, thetadot = state
17 l, m_c, m_p, g = 1, 1, 1, 9.81
18 s, c = np.sin(theta), np.cos(theta)
23 ((u + m_p * s * (l * (thetadot ** 2) + g * c)) /\
24 (m_c + m_p * (s ** 2)))[0],
25 (- (l * m_p * c * s * (thetadot ** 2) + u * c + (m_c + m_p) * g * s ) /\
26 (l * m_c + l * m_p * (s ** 2)))[0]
31 problem = exo.Setup.load_problem(
'{exotica_cartpole_dynamics_solver}/test/test_cartpole_diff.xml')
63 x = np.random.uniform(size=(1, 4))[0]
64 u = np.array([np.random.uniform()])
67 x_low, x_high = np.copy(x), np.copy(x)
70 fx_ = (TestCartpoleDiff.dynamics(x_high, u) - \
71 TestCartpoleDiff.dynamics(x_low, u)) / eps
74 fx_fd = np.array(fx_fd).T
75 fx_solver = self.dynamics_solver.fx(x, u)
76 nptest.assert_allclose(fx_fd, fx_solver, err_msg=
"Derivative w.r.t. state test failed")
80 u_low, u_high = np.copy(u), np.copy(u)
83 fu_ = (TestCartpoleDiff.dynamics(x, u_high) - \
84 TestCartpoleDiff.dynamics(x, u_low)) / eps
87 fu_fd = np.array(fu_fd).T
88 fu_solver = self.dynamics_solver.fu(x, u)
89 nptest.assert_allclose(fu_fd, fu_solver, err_msg=
"Derivative w.r.t. controls test failed")
92 if __name__ ==
'__main__':
def test_diff_finite_differences(self)