Skip to content

Commit 791bdbe

Browse files
committed
fix flatsys "legacy" keywords + enhance legacy keyword processing
1 parent 74533ca commit 791bdbe

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

control/config.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,12 @@ def use_legacy_defaults(version):
358358
# warning. If both the old and new keyword are present, a ControlArgument
359359
# exception is raised.
360360
#
361-
def _process_legacy_keyword(kwargs, oldkey, newkey, newval):
361+
def _process_legacy_keyword(kwargs, oldkey, newkey, newval, warn_oldkey=True):
362362
if oldkey in kwargs:
363-
warnings.warn(
364-
f"keyword '{oldkey}' is deprecated; use '{newkey}'",
365-
FutureWarning)
363+
if warn_oldkey:
364+
warnings.warn(
365+
f"keyword '{oldkey}' is deprecated; use '{newkey}'",
366+
FutureWarning, stacklevel=3)
366367
if newval is not None:
367368
raise ControlArgument(
368369
f"duplicate keywords '{oldkey}' and '{newkey}'")

control/flatsys/flatsys.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,10 @@ def point_to_point(
415415

416416
# Process keyword arguments
417417
trajectory_constraints = _process_legacy_keyword(
418-
kwargs, 'constraints', 'trajectory_constraints', trajectory_constraints)
418+
kwargs, 'constraints', 'trajectory_constraints',
419+
trajectory_constraints, warn_oldkey=False)
420+
cost = _process_legacy_keyword(
421+
kwargs, 'trajectory_cost', 'cost', cost, warn_oldkey=False)
419422

420423
minimize_kwargs = {}
421424
minimize_kwargs['method'] = kwargs.pop('minimize_method', None)

control/tests/flatsys_test.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ def test_kinematic_car_ocp(
200200
'ignore', message="unable to solve", category=UserWarning)
201201
traj_ocp = fs.solve_flat_ocp(
202202
vehicle_flat, timepts, x0, u0,
203-
cost=traj_cost, constraints=input_constraints,
203+
trajectory_cost=traj_cost,
204+
trajectory_constraints=input_constraints,
204205
terminal_cost=terminal_cost, basis=basis,
205206
initial_guess=initial_guess,
206207
minimize_kwargs={'method': method},
@@ -445,8 +446,8 @@ def test_flat_solve_ocp(self, basis):
445446
(sp.optimize.NonlinearConstraint, lambda x, u: x, lb, ub)]
446447
traj_nlconst = fs.solve_flat_ocp(
447448
flat_sys, timepts, x0, u0,
448-
cost=trajectory_cost, terminal_cost=terminal_cost,
449-
constraints=nl_constraints, basis=basis,
449+
trajectory_cost=trajectory_cost, terminal_cost=terminal_cost,
450+
trajectory_constraints=nl_constraints, basis=basis,
450451
)
451452
x_nlconst, u_nlconst = traj_nlconst.eval(timepts)
452453
np.testing.assert_almost_equal(x_const, x_nlconst)
@@ -657,7 +658,7 @@ def test_solve_flat_ocp_errors(self):
657658
# Try to optimize with insufficient degrees of freedom
658659
with pytest.raises(ValueError, match="basis set is too small"):
659660
traj = fs.solve_flat_ocp(
660-
flat_sys, timepts, x0, u0, cost=cost_fcn,
661+
flat_sys, timepts, x0, u0, trajectory_cost=cost_fcn,
661662
basis=fs.PolyFamily(2))
662663

663664
# Solve with the errors in the various input arguments
@@ -672,20 +673,21 @@ def test_solve_flat_ocp_errors(self):
672673
with pytest.raises(TypeError, match="must be a list"):
673674
traj = fs.solve_flat_ocp(
674675
flat_sys, timepts, x0, u0, cost_fcn,
675-
constraints=np.eye(2), basis=fs.PolyFamily(8))
676+
trajectory_constraints=np.eye(2), basis=fs.PolyFamily(8))
676677

677678
# Unknown constraint type
678679
with pytest.raises(TypeError, match="unknown constraint type"):
679680
traj = fs.solve_flat_ocp(
680681
flat_sys, timepts, x0, u0, cost_fcn,
681-
constraints=[(None, 0, 0, 0)], basis=fs.PolyFamily(8))
682+
trajectory_constraints=[(None, 0, 0, 0)],
683+
basis=fs.PolyFamily(8))
682684

683685
# Method arguments, parameters
684686
traj_method = fs.solve_flat_ocp(
685-
flat_sys, timepts, x0, u0, cost=cost_fcn,
687+
flat_sys, timepts, x0, u0, trajectory_cost=cost_fcn,
686688
basis=fs.PolyFamily(6), minimize_method='slsqp')
687689
traj_kwarg = fs.solve_flat_ocp(
688-
flat_sys, timepts, x0, u0, cost=cost_fcn,
690+
flat_sys, timepts, x0, u0, trajectory_cost=cost_fcn,
689691
basis=fs.PolyFamily(6), minimize_kwargs={'method': 'slsqp'})
690692
np.testing.assert_allclose(
691693
traj_method.eval(timepts)[0], traj_kwarg.eval(timepts)[0],

0 commit comments

Comments
 (0)