Skip to content

Commit d735f79

Browse files
committed
add unit tests for additional coverage
1 parent df91cac commit d735f79

2 files changed

Lines changed: 33 additions & 11 deletions

File tree

control/tests/optimal_test.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from control.tests.conftest import slycotonly
1616
from numpy.lib import NumpyVersion
1717

18+
1819
def test_finite_horizon_simple():
1920
# Define a linear system with constraints
2021
# Source: https://www.mpt3.org/UI/RegulationProblem
@@ -108,6 +109,7 @@ def test_discrete_lqr():
108109
# Make sure we got a different solution
109110
assert np.any(np.abs(res1.inputs - res2.inputs) > 0.1)
110111

112+
111113
def test_mpc_iosystem():
112114
# model of an aircraft discretized with 0.2s sampling time
113115
# Source: https://www.mpt3.org/UI/RegulationProblem
@@ -212,6 +214,7 @@ def test_constraint_specification(constraint_list):
212214
np.testing.assert_almost_equal(
213215
u_openloop, [-1, -1, 0.1393, 0.3361, -5.204e-16], decimal=3)
214216

217+
215218
@pytest.mark.parametrize("sys_args", [
216219
pytest.param(
217220
([[1, 0], [0, 1]], np.eye(2), np.eye(2), 0, True),
@@ -319,6 +322,7 @@ def test_terminal_constraints(sys_args):
319322
res = optctrl.compute_trajectory(x0, squeeze=True, return_x=True)
320323
assert not res.success
321324

325+
322326
def test_optimal_logging(capsys):
323327
"""Test logging functions (mainly for code coverage)"""
324328
sys = ct.ss2io(ct.ss(np.eye(2), np.eye(2), np.eye(2), 0, 1))
@@ -435,14 +439,31 @@ def test_optimal_basis_simple():
435439
cost = opt.quadratic_cost(sys, Q, R)
436440

437441
# Set up the optimal control problem
438-
time = np.arange(0, 5, 1)
442+
Tf = 5
443+
time = np.arange(0, Tf, 1)
439444
x0 = [4, 0]
440445

441446
# Basic optimal control problem
442-
res = opt.solve_ocp(sys, time, x0, cost, constraints, return_x=True)
443-
assert res.success
447+
res1 = opt.solve_ocp(
448+
sys, time, x0, cost, constraints,
449+
basis=flat.BezierFamily(4, Tf), return_x=True)
450+
assert res1.success
444451

445452
# Make sure the constraints were satisfied
446-
np.testing.assert_array_less(np.abs(res.states[0]), 5 + 1e-6)
447-
np.testing.assert_array_less(np.abs(res.states[1]), 5 + 1e-6)
448-
np.testing.assert_array_less(np.abs(res.inputs[0]), 1 + 1e-6)
453+
np.testing.assert_array_less(np.abs(res1.states[0]), 5 + 1e-6)
454+
np.testing.assert_array_less(np.abs(res1.states[1]), 5 + 1e-6)
455+
np.testing.assert_array_less(np.abs(res1.inputs[0]), 1 + 1e-6)
456+
457+
# Pass an initial guess and rerun
458+
res2 = opt.solve_ocp(
459+
sys, time, x0, cost, constraints, initial_guess=0.99*res1.inputs,
460+
basis=flat.BezierFamily(4, Tf), return_x=True)
461+
assert res2.success
462+
np.testing.assert_almost_equal(res2.inputs, res1.inputs, decimal=3)
463+
464+
# Run with logging turned on for code coverage
465+
res3 = opt.solve_ocp(
466+
sys, time, x0, cost, constraints,
467+
basis=flat.BezierFamily(4, Tf), return_x=True, log=True)
468+
assert res3.success
469+
np.testing.assert_almost_equal(res3.inputs, res1.inputs, decimal=3)

examples/steering-optimal.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ def plot_results(t, y, u, figure=None, yf=None):
107107
print("Approach 1: standard quadratic cost")
108108

109109
# Set up the cost functions
110-
Q = np.diag([.1, 10, .1]) # keep lateral error low
111-
R = np.diag([.1, 1]) # minimize applied inputs
110+
Q = np.diag([.1, 10, .1]) # keep lateral error low
111+
R = np.diag([.1, 1]) # minimize applied inputs
112112
quad_cost = opt.quadratic_cost(vehicle, Q, R, x0=xf, u0=uf)
113113

114114
# Define the time horizon (and spacing) for the optimization
@@ -209,9 +209,9 @@ def plot_results(t, y, u, figure=None, yf=None):
209209
start_time = time.process_time()
210210
result3 = opt.solve_ocp(
211211
vehicle, horizon, x0, cost3, constraints,
212-
terminal_constraints=terminal, initial_guess=u2, log=True,
213-
# solve_ivp_kwargs={'atol': 1e-3, 'rtol': 1e-2},
214-
solve_ivp_kwargs={'method': 'RK23', 'atol': 1e-4, 'rtol': 1e-2},
212+
terminal_constraints=terminal, initial_guess=u2, log=False,
213+
solve_ivp_kwargs={'atol': 1e-4, 'rtol': 1e-2},
214+
# solve_ivp_kwargs={'method': 'RK23', 'atol': 1e-4, 'rtol': 1e-2},
215215
minimize_options={'eps': 0.01})
216216
print("* Total time = %5g seconds\n" % (time.process_time() - start_time))
217217

@@ -246,6 +246,7 @@ def plot_results(t, y, u, figure=None, yf=None):
246246
solve_ivp_kwargs={'method': 'RK45', 'atol': 1e-2, 'rtol': 1e-2},
247247
minimize_method='trust-constr', minimize_options={'disp': True},
248248
# method='SLSQP', options={'eps': 0.01}
249+
log=True
249250
)
250251
print("* Total time = %5g seconds\n" % (time.process_time() - start_time))
251252

0 commit comments

Comments
 (0)