Running constraint on a Specific Node of the MPC Horizon

Hi,
I’m trying to implement an MPC using the Python interface. I would like to include a neural network function as constraint, using L4Casadi library. This running constraint should be active only on a specific node along the horizon, and this node can change during the MPC iterations. However, from the Python interface, running constraints are defined for all the nodes along the horizon. I have found three possible work around:

  1. Use soft constraints, with nonzero weight associated to the slack variables only for the interested node (zero weight on all the others)
  2. Hard constraints, lower and upper bounds only on the desired node, -1e6 and 1e6 for the others
  3. Multiply the NN constraint by the parameter p, which will be set 1 on the desired node, 0 on the others to mimic constraint deactivation

All these are valid alternatives. The problem is that the solver requires an high computation times, since in the practice it still evaluate the constraints on all the nodes. Do you know if there exist a way to evaluate constraint only on one specific node?

Best,
Gianni

Hi Gianni,

I think the only way to avoid linearization of the constraints which you do not need is to implement your OCP as a multi-phase formulation, i.e. those stages which should include the constraint would be a different phase of your OCP, cf. this example:

And the publication:

Best, Katrin

Ah sorry, I missed the point that the stage for which you want to include the constraint can change. This is not possible with the multi-phase formulation.

In this case, I think it is not possible to avoid the extra constraint linearization at the moment. This could be implemented using masked constraints. I opened an issue, but I cant promise that this will be implemented soon :see_no_evil:

Hi Katrin,
Thank you for your fast answer! Indeed, multiphase OCP can be another work around. In this scenario, given N as the MPC horizon, we can generate N multiphase OCPs, each of it with different position of the constraint along the horizon. However, we will have a large overhead on setter and getter during the MPC iterations when the stage of the constraint change. In addition, we will generate the C code for N times, which requires additional time. Thus, the possibility to avoid extra constraint lineraization could be very helpfull and I think it would be interesting also for other users!

Best,
Gianni

Maybe it’s possible to do it on CasADi side. Did you try to add an additional if-else to the constraint function which checks the 0-1 parameter and then either returns the constraint function or a constant zero vector? The if else construct in CasADi hast an optional argument short_circuit which should allow you to evaluate only one of the two branches.

I believe that we can implement the feature described in the issue, especially if the CasADi workaround does not work well.
Could you report on that @gianni96 ?

Regarding my comment from the Github issue:

This only works if all nonlinear constraints are fully masked out (at a stage of the OCP).
The nonlinear functions are evaluated and linearized in one external C function.

Is this the case for your problem formulation?

Best,
Jonathan

Hi,
I will try the CasADi if else in the next days, I was not aware of the short_circuit option. I will be back there asap.
Concerning the comment from the Github issue, I agree with that. My aim is to evaluate and linearize the constraint only on the needed node, avoiding all the others.

Best,
Gianni

That sounds good!

Just to clarify:
Do you have nonlinear constraint other than the one modeled by NNs in your OCP formulation?
If yes, the mask approach from the Github issue would not work for you directly.

That’s a good point. Yes, I have multiple nonlinear constraints and only the one of the NN must be active on a single node

Hi all,
the if_else with the short circuit is working properly! the computation time on the linearization part is comparable with the one of the MPC problem with NN only as terminal constraint.
Anyway, the possibility to activate constraints in specific nodes could be a potential improvement of the framework.
Thank you all for the help and suggestions!

Best,
Gianni

2 Likes