8000 add error for using solve_ivp for discrete time + formatting tweaks · python-control/python-control@2860d89 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2860d89

Browse files
committed
add error for using solve_ivp for discrete time + formatting tweaks
1 parent efca9ca commit 2860d89

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

benchmarks/optimal_bench.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ def time_optimal_lq_basis(basis_name, basis_size, npoints):
8989
basis = get_basis(basis_name, basis_size, Tf)
9090

9191
res = opt.solve_ocp(
92-
sys, timepts, x0, traj_cost, constraints, terminal_cost=term_cost,
92+
sys, timepts, x0, traj_cost, constraints, terminal_cost=term_cost,
93+
basis=basis,
9394
)
9495
# Only count this as a benchmark if we converged
9596
assert res.success
@@ -130,7 +131,7 @@ def time_optimal_lq_methods(integrator_name, minimizer_name):
130131
basis = get_basis('poly', 12, Tf)
131132

132133
res = opt.solve_ocp(
133-
sys, timepts, x0, traj_cost, constraints, terminal_cost=term_cost,
134+
sys, timepts, x0, traj_cost, constraints, terminal_cost=term_cost,
134135
solve_ivp_method=integrator[0], solve_ivp_kwargs=integrator[1],
135136
minimize_method=minimizer[0], minimize_options=minimizer[1],
136137
)
@@ -179,21 +180,21 @@ def time_optimal_lq_size(nstates, ninputs, npoints):
179180
timepts = np.linspace(0, Tf, npoints)
180181

181182
res = opt.solve_ocp(
182-
sys, timepts, x0, traj_cost, constraints, terminal_cost=term_cost,
183+
sys, timepts, x0, traj_cost, constraints, terminal_cost=term_cost,
183184
)
184185
# Only count this as a benchmark if we converged
185186
assert res.success
186187

187188
# Parameterize the test against different choices of integrator and minimizer
188189
time_optimal_lq_size.param_names = ['nstates', 'ninputs', 'npoints']
189190
time_optimal_lq_size.params = ([1, 2, 4], [1, 2, 4], [5, 10, 20])
190-
191+
191192

192193
#
193194
# Aircraft MPC example (from multi-parametric toolbox)
194195
#
195196

196-
def time_aircraft_mpc():
197+
def time_discrete_aircraft_mpc(minimizer_name):
197198
# model of an aircraft discretized with 0.2s sampling time
198199
# Source: https://www.mpt3.org/UI/RegulationProblem
199200
A = [[0.99, 0.01, 0.18, -0.09, 0],
@@ -228,9 +229,18 @@ def time_aircraft_mpc():
228229
R = np.diag([3, 2])
229230
cost = opt.quadratic_cost(model, Q, R, x0=xd, u0=ud)
230231

232+
# Set the time horizon and time points
233+
Tf = 3
234+
timepts = np.arange(0, 6) * 0.2
235+
236+
# Get the minimizer parameters to use
237+
minimizer = minimizer_table[minimizer_name]
238+
231239
# online MPC controller object is constructed with a horizon 6
232240
ctrl = opt.create_mpc_iosystem(
233-
model, np.arange(0, 6) * 0.2, cost, constraints)
241+
model, timepts, cost, constraints,
242+
minimize_method=minimizer[0], minimize_options=minimizer[1],
243+
)
234244

235245
# Define an I/O system implementing model predictive control
236246
loop = ct.feedback(sys, ctrl, 1)
@@ -245,3 +255,8 @@ def time_aircraft_mpc():
245255
# Make sure the system converged to the desired state
246256
np.testing.assert_allclose(
247257
xout[0:sys.nstates, -1], xd, atol=0.1, rtol=0.01)
258+
259+
# Parameterize the test against different choices of minimizer and basis
260+
time_discrete_aircraft_mpc.param_names = ['minimizer']
261+
time_discrete_aircraft_mpc.params = (
262+
['trust', 'trust_bigstep', 'SLSQP', 'SLSQP_bigstep', 'COBYLA'])

control/optimal.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ def __init__(
154154
self.minimize_kwargs.update(kwargs.pop(
155155
'minimize_kwargs', config.defaults['optimal.minimize_kwargs']))
156156

157+
# Check to make sure arguments for discrete-time systems are OK
158+
if sys.isdtime(strict=True):
159+
if self.solve_ivp_kwargs['method'] is not None or \
160+
len(self.solve_ivp_kwargs) > 1:
161+
raise TypeError(
162+
"solve_ivp method, kwargs not allowed for"
163+
" discrete time systems")
164+
157165
# Make sure there were no extraneous keywords
158166
if kwargs:
159167
raise TypeError("unrecognized keyword(s): ", str(kwargs))

control/tests/optimal_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,15 @@ def test_ocp_argument_errors():
471471
res = opt.solve_ocp(
472472
sys, time, x0, cost, terminal_constraints=constraints)
473473

474+
# Discrete time system checks: solve_ivp keywords not allowed
475+
sys = ct.rss(2, 1, 1, dt=True)
476+
with pytest.raises(TypeError, match="solve_ivp method, kwargs not allowed"):
477+
res = opt.solve_ocp(
478+
sys, time, x0, cost, solve_ivp_method='LSODA')
479+
with pytest.raises(TypeError, match="solve_ivp method, kwargs not allowed"):
480+
res = opt.solve_ocp(
481+
sys, time, x0, cost, solve_ivp_kwargs={'eps': 0.1})
482+
474483

475484
@pytest.mark.parametrize("basis", [
476485
flat.PolyFamily(4), flat.PolyFamily(6),

0 commit comments

Comments
 (0)
0