Skip to content

Commit 8463f68

Browse files
committed
fix flatsys "legacy" keywords + enhance legacy keyword processing
1 parent f2285e4 commit 8463f68

3 files changed

Lines changed: 19 additions & 13 deletions

File tree

control/config.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,12 @@ def use_legacy_defaults(version):
380380
# warning. If both the old and new keyword are present, a ControlArgument
381381
# exception is raised.
382382
#
383-
def _process_legacy_keyword(kwargs, oldkey, newkey, newval):
383+
def _process_legacy_keyword(kwargs, oldkey, newkey, newval, warn_oldkey=True):
384384
if oldkey in kwargs:
385-
warnings.warn(
386-
f"keyword '{oldkey}' is deprecated; use '{newkey}'",
387-
FutureWarning)
385+
if warn_oldkey:
386+
warnings.warn(
387+
f"keyword '{oldkey}' is deprecated; use '{newkey}'",
388+
FutureWarning, stacklevel=3)
388389
if newval is not None:
389390
raise ControlArgument(
390391
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)
@@ -685,7 +686,7 @@ def test_solve_flat_ocp_errors(self):
685686
# Try to optimize with insufficient degrees of freedom
686687
with pytest.raises(ValueError, match="basis set is too small"):
687688
traj = fs.solve_flat_ocp(
688-
flat_sys, timepts, x0, u0, cost=cost_fcn,
689+
flat_sys, timepts, x0, u0, trajectory_cost=cost_fcn,
689690
basis=fs.PolyFamily(2))
690691

691692
# Solve with the errors in the various input arguments
@@ -700,20 +701,21 @@ def test_solve_flat_ocp_errors(self):
700701
with pytest.raises(TypeError, match="must be a list"):
701702
traj = fs.solve_flat_ocp(
702703
flat_sys, timepts, x0, u0, cost_fcn,
703-
constraints=np.eye(2), basis=fs.PolyFamily(8))
704+
trajectory_constraints=np.eye(2), basis=fs.PolyFamily(8))
704705

705706
# Unknown constraint type
706707
with pytest.raises(TypeError, match="unknown constraint type"):
707708
traj = fs.solve_flat_ocp(
708709
flat_sys, timepts, x0, u0, cost_fcn,
709-
constraints=[(None, 0, 0, 0)], basis=fs.PolyFamily(8))
710+
trajectory_constraints=[(None, 0, 0, 0)],
711+
basis=fs.PolyFamily(8))
710712

711713
# Method arguments, parameters
712714
traj_method = fs.solve_flat_ocp(
713-
flat_sys, timepts, x0, u0, cost=cost_fcn,
715+
flat_sys, timepts, x0, u0, trajectory_cost=cost_fcn,
714716
basis=fs.PolyFamily(6), minimize_method='slsqp')
715717
traj_kwarg = fs.solve_flat_ocp(
716-
flat_sys, timepts, x0, u0, cost=cost_fcn,
718+
flat_sys, timepts, x0, u0, trajectory_cost=cost_fcn,
717719
basis=fs.PolyFamily(6), minimize_kwargs={'method': 'slsqp'})
718720
np.testing.assert_allclose(
719721
traj_method.eval(timepts)[0], traj_kwarg.eval(timepts)[0],

0 commit comments

Comments
 (0)