test_cartpole_diff.py
Go to the documentation of this file.
1 # import autograd.numpy as np
2 # from autograd import grad, jacobian, hessian
3 import numpy as np
4 import pyexotica as exo
5 import unittest
6 from numpy import testing as nptest
7 sin = np.sin
8 cos = np.cos
9 
10 class TestCartpoleDiff(unittest.TestCase):
11  """Tests cartpole derivatives against python's autograd."""
12  @staticmethod
13  def dynamics(state, u):
14  # Analytical dynamics from underactuated robotics
15  # http://underactuated.mit.edu
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)
19 
20  return np.array([
21  xdot,
22  thetadot,
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]
27  ])
28 
29  def setUp(self):
30  # set up exotica with the test configuration
31  problem = exo.Setup.load_problem('{exotica_cartpole_dynamics_solver}/test/test_cartpole_diff.xml')
32 
33  # alias derivatives and dynamic solver
34  self.dynamics_solver = problem.get_scene().get_dynamics_solver()
35  # self.fx = jacobian(TestCartpoleDiff.dynamics, argnum=0)
36  # self.fu = jacobian(TestCartpoleDiff.dynamics, argnum=1)
37 
38  # NOTE: We need autograd for this to work.
39  # PR link: https://github.com/ros/rosdistro/pull/21332
40  # def test_diff_autograd(self):
41  # # set the seed so this is deterministic
42  # np.random.seed(42)
43 
44  # # check against 100 state,control pairs
45  # for i in range(100):
46  # x = np.random.uniform(size=(1, 4))[0]
47  # u = np.array([np.random.uniform()])
48  # auto_diff = self.fx(x, u)
49  # solver_diff = self.dynamics_solver.fx(x, u)
50  # nptest.assert_allclose(auto_diff, solver_diff, err_msg="Derivative w.r.t. state test failed")
51 
52  # auto_diff = self.fu(x, u)
53  # solver_diff = self.dynamics_solver.fu(x, u)
54  # nptest.assert_allclose(auto_diff, solver_diff, err_msg="Derivative w.r.t. controls test failed")
55 
57  # set the seed so this is deterministic
58  np.random.seed(42)
59  eps = 1e-5
60 
61  # check against 100 state,control pairs
62  for _ in range(100):
63  x = np.random.uniform(size=(1, 4))[0]
64  u = np.array([np.random.uniform()])
65  fx_fd = []
66  for i in range(4):
67  x_low, x_high = np.copy(x), np.copy(x)
68  x_low[i] -= eps/2
69  x_high[i] += eps/2
70  fx_ = (TestCartpoleDiff.dynamics(x_high, u) - \
71  TestCartpoleDiff.dynamics(x_low, u)) / eps
72  fx_fd.append(fx_)
73 
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")
77 
78  fu_fd = []
79  for i in range(1):
80  u_low, u_high = np.copy(u), np.copy(u)
81  u_low[i] -= eps/2
82  u_high[i] += eps/2
83  fu_ = (TestCartpoleDiff.dynamics(x, u_high) - \
84  TestCartpoleDiff.dynamics(x, u_low)) / eps
85  fu_fd.append(fu_)
86 
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")
90 
91 
92 if __name__ == '__main__':
93  unittest.main()


exotica_cartpole_dynamics_solver
Author(s): Traiko Dinev
autogenerated on Sat Apr 10 2021 02:36:04