2 from __future__
import print_function, division
6 import pyexotica
as exo
10 PKG =
'exotica_examples' 11 roslib.load_manifest(PKG)
14 np.set_printoptions(2, threshold=sys.maxsize, suppress=
True, linewidth=500)
17 scene = problem.get_scene()
18 ds = scene.get_dynamics_solver()
21 u = np.random.random((ds.nu,))
23 if t == problem.T - 1:
24 problem.update_terminal_state(x)
26 problem.update(x, u, t)
27 J_solver = problem.get_state_cost_jacobian(t).copy()
28 J_numdiff = np.zeros_like(J_solver)
29 np.testing.assert_equal(J_numdiff.shape[0], ds.ndx)
31 original_integrator = ds.integrator
33 for i
in range(ds.ndx):
34 dx = np.zeros((ds.ndx,))
37 ds.integrator = exo.Integrator.RK1
38 x_plus = ds.integrate(x, dx, 1.0)
39 x_minus = ds.integrate(x, -dx, 1.0)
40 ds.integrator = original_integrator
42 if t == problem.T - 1:
43 problem.update_terminal_state(x_plus)
45 problem.update(x_plus, u, t)
46 cost_high = problem.get_state_cost(t)
48 if t == problem.T - 1:
49 problem.update_terminal_state(x_minus)
51 problem.update(x_minus, u, t)
52 cost_low = problem.get_state_cost(t)
54 J_numdiff[i] = (cost_high - cost_low) / eps
55 np.testing.assert_allclose(J_solver, J_numdiff, rtol=3e-5,
56 atol=1e-3, err_msg=
'StateCostJacobian does not match!')
60 scene = problem.get_scene()
61 ds = scene.get_dynamics_solver()
64 u = np.random.random((ds.nu,))
66 if t == problem.T - 1:
67 problem.update_terminal_state(x)
69 problem.update(x, u, t)
70 H_solver = problem.get_state_cost_hessian(t).copy()
71 H_numdiff = np.zeros_like(H_solver)
72 np.testing.assert_equal(H_numdiff.shape[0], ds.ndx)
73 np.testing.assert_equal(H_numdiff.shape[1], ds.ndx)
75 original_integrator = ds.integrator
77 for i
in range(ds.ndx):
78 dx = np.zeros((ds.ndx,))
81 ds.integrator = exo.Integrator.RK1
82 x_plus = ds.integrate(x, dx, 1.0)
83 x_minus = ds.integrate(x, -dx, 1.0)
84 ds.integrator = original_integrator
86 if t == problem.T - 1:
87 problem.update_terminal_state(x_plus)
89 problem.update(x_plus, u, t)
90 jacobian_plus = problem.get_state_cost_jacobian(t)
92 if t == problem.T - 1:
93 problem.update_terminal_state(x_minus)
95 problem.update(x_minus, u, t)
96 jacobian_minus = problem.get_state_cost_jacobian(t)
98 H_numdiff[:,i] = (jacobian_plus - jacobian_minus) / eps
99 if np.linalg.norm(H_solver - H_numdiff) > 1e-2:
100 print(
"Hessian does not match at t=", t)
103 print((H_solver-H_numdiff)<1e-3)
104 print(H_solver-H_numdiff)
105 np.testing.assert_allclose(H_solver, H_numdiff, rtol=1e-5,
106 atol=1e-2, err_msg=
'StateCostHessian does not match!')
110 scene = problem.get_scene()
111 ds = scene.get_dynamics_solver()
114 u = np.random.random((ds.nu,))
116 problem.update(x, u, t)
117 J_solver = problem.get_control_cost_jacobian(t).copy()
118 J_numdiff = np.zeros_like(J_solver)
119 np.testing.assert_equal(J_numdiff.shape[0], ds.nu)
122 for i
in range(ds.nu):
124 u_plus[i] += eps / 2.0
126 u_minus[i] -= eps / 2.0
128 problem.update(x, u_plus, t)
129 cost_high = problem.get_control_cost(t)
131 problem.update(x, u_minus, t)
132 cost_low = problem.get_control_cost(t)
134 J_numdiff[i] = (cost_high - cost_low) / eps
135 np.testing.assert_allclose(J_solver, J_numdiff, rtol=1e-5,
136 atol=1e-5, err_msg=
'ControlCostJacobian does not match!')
140 scene = problem.get_scene()
141 ds = scene.get_dynamics_solver()
144 u = np.random.random((ds.nu,))
146 problem.update(x, u, t)
147 H_solver = problem.get_control_cost_hessian(t).copy()
148 H_numdiff = np.zeros_like(H_solver)
149 np.testing.assert_equal(H_numdiff.shape[0], ds.nu)
150 np.testing.assert_equal(H_numdiff.shape[1], ds.nu)
153 for i
in range(ds.nu):
155 u_plus[i] += eps / 2.0
157 u_minus[i] -= eps / 2.0
159 problem.update(x, u_plus, t)
160 jacobian_plus = problem.get_control_cost_jacobian(t)
162 problem.update(x, u_minus, t)
163 jacobian_minus = problem.get_control_cost_jacobian(t)
165 H_numdiff[:,i] = (jacobian_plus - jacobian_minus) / eps
166 np.testing.assert_allclose(H_solver, H_numdiff, rtol=1e-5,
167 atol=1e-5, err_msg=
'ControlCostHessian does not match!')
171 if __name__ ==
"__main__":
174 '{exotica_examples}/resources/configs/dynamic_time_indexed/01_ilqr_cartpole.xml',
175 '{exotica_examples}/resources/configs/dynamic_time_indexed/02_lwr_task_maps.xml',
176 '{exotica_examples}/resources/configs/dynamic_time_indexed/03_ilqr_valkyrie.xml',
177 '{exotica_examples}/resources/configs/dynamic_time_indexed/04_analytic_ddp_cartpole.xml',
178 '{exotica_examples}/resources/configs/dynamic_time_indexed/05_analytic_ddp_lwr.xml',
179 '{exotica_examples}/resources/configs/dynamic_time_indexed/06_analytic_ddp_valkyrie.xml',
180 '{exotica_examples}/resources/configs/dynamic_time_indexed/07_control_limited_ddp_cartpole.xml',
181 '{exotica_examples}/resources/configs/dynamic_time_indexed/08_control_limited_ddp_lwr.xml',
182 '{exotica_examples}/resources/configs/dynamic_time_indexed/09_control_limited_ddp_valkyrie.xml',
183 '{exotica_examples}/resources/configs/dynamic_time_indexed/10_ilqg_cartpole.xml',
184 '{exotica_examples}/resources/configs/dynamic_time_indexed/11_ilqg_lwr.xml',
185 '{exotica_examples}/resources/configs/dynamic_time_indexed/12_ilqg_valkyrie.xml',
186 '{exotica_examples}/resources/configs/dynamic_time_indexed/13_control_limited_ddp_quadrotor.xml',
187 '{exotica_examples}/resources/configs/dynamic_time_indexed/14_rrt_cartpole.xml',
188 '{exotica_examples}/resources/configs/dynamic_time_indexed/15_rrt_quadrotor.xml',
189 '{exotica_examples}/resources/configs/dynamic_time_indexed/16_kpiece_cartpole.xml',
190 '{exotica_examples}/resources/configs/dynamic_time_indexed/17_quadrotor_collision_avoidance.xml',
191 '{exotica_examples}/resources/configs/dynamic_time_indexed/18_ilqr_pendulum.xml',
192 '{exotica_examples}/resources/configs/dynamic_time_indexed/19_ddp_quadrotor_sphere.xml',
193 '{exotica_examples}/resources/configs/dynamic_time_indexed/20_sparse_fddp_pendulum.xml',
194 '{exotica_examples}/resources/configs/dynamic_time_indexed/21_sparse_fddp_cartpole.xml',
195 '{exotica_examples}/resources/configs/dynamic_time_indexed/22_boxfddp_cartpole.xml',
198 for config
in configs:
199 print(
"Testing", config)
200 _, problem_init = exo.Initializers.load_xml_full(config)
201 problem = exo.Setup.create_problem(problem_init)
202 scene = problem.get_scene()
203 ds = problem.get_scene().get_dynamics_solver()
209 for t
in range(problem.T):
214 for t
in range(problem.T):
218 for t
in range(problem.T - 1):
222 for t
in range(problem.T - 1):
def check_control_cost_hessian_at_t(problem, t)
def check_state_cost_jacobian_at_t(problem, t)
def check_control_cost_jacobian_at_t(problem, t)
def check_state_cost_hessian_at_t(problem, t)