Path and Terminal Constraints

670 views
Skip to first unread message

Daniel A

unread,
Jun 28, 2016, 4:50:24 AM6/28/16
to CasADi
Hi All!

This is the first time I am using CasADi and I am impressed by its overall capabilities!

I have a few questions (hopefully trivial) concerning the way path and terminal constraints are introduced into the single shooting example problem code.

Below is the section of the code my questions are relevant to:

% Formulate the NLP
Xk = [0; 1];

for k=0:N-1   %at every node!
    % New NLP variable for the control
    Uk = MX.sym(['U_' num2str(k)]);
    w = {w{:}, Uk};
    lbw = [lbw, -1];
    ubw = [ubw,  1];
    w0 = [w0,  0];

    % Integrate till the end of the interval
    Fk = F('x0',Xk,'p', Uk);
    Xk = Fk.xf;
    J=J+Fk.qf;

    % Add inequality constraint
    g = {g{:}, Xk(1)};
    lbg = [lbg; -.25];
    ubg = [ubg;  inf];
end

How would you incorporate path constraints on the second state variable in this example? I would think you first set    g = {g{:}, Xk};

such that g is a list of 2x1 cells (two constraints). How do you then define a different set of bounds lbg and ubg for the second state variable (say lbg2 and ubg2) in a way that is in line with the solver's syntax?

A more general way of stating my problem is, how can you incorporate several different constraints (inequality or equality) into the g vector? Is it by just adding multiple rows per K value?

My last question is: how does the solver know in this example that the final state vector should have both states = 0? Where and how exactly was that imposed? If I wanted to include a non-zero terminal constraint, would I have to overwrite the state path constraint at the final node or can I incorporate both the path constraint and terminal constraint at the final node?

I'm trying to understand the overall CasADi syntax to then apply it for a multi-dimensional optimal control problem.

Any help is appreciated!

Regards,

Daniel

Joris Gillis

unread,
Jun 28, 2016, 5:18:32 AM6/28/16
to CasADi
Hi Daniel,

Hope you'll have a nice time exploring the tool..
The syntax you posted is all about putting all variables, all constraints (g), all bounds, .. etc in one tall vector.
You build up a cell of expressions, which we vertcat in the end, to obtain a tall symbolic vector.
(In fact you may do the same for lbg, ubg, its a matter of style).

To add constraints, simply add lines inside the loop. E.g.:
    % Add path constraint on second state
    g = {g{:}, Xk(2)};
    lbg = [lbg; lbg2];
    ubg = [ubg;  ubg2];


Alternatively you could say:
    % Add path constraint on second state

    g = {g{:}, Xk};
    lbg = [lbg; -inf; lbg2];
    ubg = [ubg;  inf;ubg2];

Now you added both states in the constraints, but ignore the first one by choosing inf.


The terminal state (if you're looking at direct_single_shooting.m) is in fact not enforced; the solver just decided it was good to end close to 0.
You may add a terminal constraint by a simple bound (lbw/lbu), or a full constraint.
E.g. you could just add this after the loop:


g = {g{:}, Xk};
lbg = [lbg; state_final];
ubg = [ubg; state_final];

From CasADi's point of view, there's no problem to enforce different consistent constraints on one node. Of course, you may create an LICQ violation by doing this, and not all solvers may handle such violation.

Best regards,
  Joris

Daniel A

unread,
Jun 28, 2016, 6:21:06 AM6/28/16
to CasADi
Hi Joris,

Thanks for your quick reply! I understand what you mean, that makes more sense now. Concerning the enforcement of the final constraint via:


g = {g{:}, Xk};
lbg = [lbg; state_final];
ubg = [ubg; state_final];

Will CasADi think there is an additional "node" after the final at N=20 in this example since we are adding an additional set of parameter constraints?

Regards,

Daniel

Daniel A

unread,
Jun 28, 2016, 6:30:17 AM6/28/16
to CasADi
Ok so I just tried it and I think the way to do it is by overwriting the constraints at the final node!

If you add an additional set of rows in the g vector CasADi will think there is an additional node after the last one that was specified from the discretization.

However, if you overwrite the last set of constraints, in the example the last two rows of g, the system solves my problem correctly!
(The terminal constraints would have to meet the general path constraints anyway)

Cheers,


Daniel


On Tuesday, June 28, 2016 at 11:18:32 AM UTC+2, Joris Gillis wrote:

Joris Gillis

unread,
Jun 28, 2016, 7:08:50 AM6/28/16
to Daniel A, CasADi
Hi Daniel,

If you add an additional set of rows in the g vector CasADi will there is an additional node after the last one that was specified from the discretization.

That's not true, in CasADi you transcribe the OCP by yourself to an NLP. CasADi doesn't have the notion of a shooting 'node', only decision variables and constraints; actually on the modeling level, there's only symbolic primitives and expressions built from them. As long as you do not add decision variables to the problem, there's no 'additional node' that can appear.

Best regards,
  Joris
 

--
Sent from CasADi's user forum at http://forum.casadi.org.
---
You received this message because you are subscribed to the Google Groups "CasADi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to casadi-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/casadi-users/e5d64ae8-8da6-42f3-b26d-de7503517ffc%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Daniel A

unread,
Jun 28, 2016, 10:36:39 AM6/28/16
to CasADi, daguilarm...@gmail.com
Hi Joris,

Thanks again. I'm getting a bit confused. In the single shooting method problem, the for loop for k=0:N-1 is used where I assume  N is the number of nodes? So if I just add additional rows to the g-vector at the end, the length of the new g-vector will not correspond constraints from before. So that's what I mean that it will "act" as if another node was there.

Perhaps I misunderstood, but did you mean you could just add the final constraint at every node but have it be unbounded (so not enforced) and then bound it at the final node? That would certainly keep the g-vector structure the same in the for loop?

Cheers,

Daniel- show quoted text

Joris Gillis

unread,
Jun 28, 2016, 10:49:48 AM6/28/16
to CasADi, daguilarm...@gmail.com
Hi Daniel,

CasADi or the solvers assume no particular structure of the constraints. There's no need to have the structure identical in the loop.
Didn't suggest to add "terminal constraint" at every node, you can just add them after the for loop.
Anyway, don't be scared of toying with it :p

Best,
  Joris
Reply all
Reply to author
Forward
0 new messages