Commit aead00e0 authored by Stefan Ecklebe's avatar Stefan Ecklebe
Browse files

Fix issue with kwargs in constraint check

parent d8f1f36b
......@@ -594,7 +594,7 @@ class OptimalControlProblem:
assert ocp.data_hash == ocp_data_hash
return ocp
def check_constraints(self, **kwargs):
def check_constraints(self, x=None, u=None):
"""
Check whether the given state and input trajectories satisfy the
constraints.
......@@ -609,9 +609,13 @@ class OptimalControlProblem:
Returns: Figure object that shows the constraint violations.
None if all constraints were satisfied.
"""
u = kwargs.get("u", None)
kwargs = {}
if x is not None:
kwargs["x"] = x
if u is not None:
kwargs["u"] = u
if u is not None and u.ndim > 1:
kwargs["Du"] = np.diff(kwargs["u"], axis=1)
kwargs["Du"] = np.diff(u, axis=1)
# constant bounds
for lbl, var in kwargs.items():
......@@ -654,10 +658,10 @@ class OptimalControlProblem:
logger.exception(e)
logger.error("Step constraint function depends on variables "
"that were not provided, constraint check failed.")
return f
raise
e_vals = self.e_func(*e_args).full().squeeze()
if e_args[0].ndim == 2:
e_vals = self.e_func(*e_args).full()
if e_vals.ndim == 2:
err_idxs = np.where(np.any(e_vals >= 0, axis=1))[0]
else:
err_idxs = np.where(e_vals > 0)[0]
......@@ -665,12 +669,13 @@ class OptimalControlProblem:
logger.error("Step constraint function 'e' violated "
f"at indexes {err_idxs}")
f, ax = plt.subplots()
ax.axhline(0, lw=2, ls="--", c="r")
for idx in err_idxs:
if var.ndim == 2:
if e_vals.ndim == 2 and e_vals.shape[1] > 1:
ax.plot(e_vals[idx].T, label=f"e_{idx}")
else:
ax.axhline(e_vals[idx], label=f"e_{idx}")
ax.axhline(0, lw=2, ls="--", c="r")
ax.autoscale()
ax.legend()
ax.set_title("Step Constraint violations "
"(values should not be positive)")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment