def test_spy(self): ocp, _, _ = integrator_control_problem() ocp.solve() ocp.spy() import matplotlib.pylab as plt self.assertEqual(plt.gca().title._text, "Lagrange Hessian: 101x101,0nz")
def test_show_infeasibilities(self): for method in [MultipleShooting(), DirectCollocation()]: ocp, x, u = integrator_control_problem(stage_method=method, x0=0) ocp.subject_to(ocp.at_t0(x) == 2) with self.assertRaises(Exception): sol = ocp.solve() with StringIO() as buf, redirect_stdout(buf): ocp.show_infeasibilities(1e-4) out = buf.getvalue() self.assertIn("ocp.subject_to(ocp.at_t0(x)==2)", out)
def test_all(self): T = 1 M = 1 b = 1 t0 = 0 x0 = 0 for scheme in [MultipleShooting(N=40, M=1, intg='rk'), DirectCollocation(N=40)]: (ocp, x, u) = integrator_control_problem(T, b, x0, scheme, t0) sol = ocp.solve() ts, xs = sol.sample(x, grid='control') self.assertAlmostEqual(xs[0],x0,places=6) self.assertAlmostEqual(xs[-1],x0-b*T,places=6)
def test_initial(self): for stage_method in [MultipleShooting(), DirectCollocation()]: ocp, x, u = integrator_control_problem(x0=None, stage_method=stage_method) v = ocp.variable() ocp.subject_to(ocp.at_t0(x) == v) ocp.subject_to(0 == sin(v)) sol = ocp.solve() ts, xs = sol.sample(x, grid='control') self.assertAlmostEqual(xs[0], 0, places=6) ocp.set_initial(v, 2 * pi) sol = ocp.solve() ts, xs = sol.sample(x, grid='control') self.assertAlmostEqual(xs[0], 2 * pi, places=6)
def test_grid_integrator(self): N, T, u_max, x0 = 10, 10, 2, 1 tolerance = 1e-6 ocp, x, u = integrator_control_problem(T, u_max, x0, MultipleShooting(N=N,M=3,intg='rk')) sol = ocp.solve() ts, xs = sol.sample(x, grid='integrator') ts, us = sol.sample(u, grid='integrator') ts, uxs = sol.sample(u * x, grid='integrator') t_exact = np.linspace(0, T, N * 3 + 1) x_exact = np.linspace(1, x0 - 10 * u_max, N * 3 + 1) u_exact = np.ones(N * 3 + 1) * (-u_max) assert_allclose(ts, t_exact, atol=tolerance) assert_allclose(xs, x_exact, atol=tolerance) assert_allclose(us, u_exact, atol=tolerance) assert_allclose(uxs, u_exact * x_exact, atol=tolerance) tsa, tsb = sol.sample(ocp.t, grid='integrator') assert_allclose(tsa, tsb, atol=tolerance)
def test_basic(self): for T in [1, 3.2]: for M in [1, 2]: for u_max in [1, 2]: for t0 in [0, 1]: for x0 in [0, 1]: for method in [ MultipleShooting(N=4, M=M, intg='rk'), MultipleShooting(N=4, M=M, intg='cvodes'), MultipleShooting(N=4, M=M, intg='idas'), DirectCollocation(N=4, M=M) ]: ocp, x, u = integrator_control_problem( T, u_max, x0, method, t0) sol = ocp.solve() ts, xs = sol.sample(x, grid='control') self.assertAlmostEqual(xs[0], x0, places=6) self.assertAlmostEqual(xs[-1], x0 - u_max * T, places=6) self.assertAlmostEqual(ts[0], t0) self.assertAlmostEqual(ts[-1], t0 + T)