Parameteric OCP with path constraint and LS cost

Hi everyone,
Thanks a lot for developing such a wonderful toolbox. I am very new to OCP and trying to setup one in python interface but face some issues regarding following points.

  1. My model is defined by nonlinear equation f(x,u,p) where parameter p needs an update at each stage. Currently I defined them as P = MX(‘p’, np, 1) put them in model.p following the guidelines of the example of race cars. Further I also set = model.p.size()[0] in but it throws me an error,
    Error: Failed to render ‘’
    Reason: Variable constraints.p not found in context while rendering ‘’

So I also defined,

throws me another error saying ‘Code generation of ‘filename_expl_ode_fun’ is not possible since variables [p] are free’
How do I properly implement this in the toolbox?

  1. I have nu inputs in the nonlinear model and some these inputs are set to be equal to zero at each stage. I am doing it by updating ‘lbu’ and ‘ubu’ for entire input vector u at each stage. How can I choose some entries of vector u to be set to zero over each stage?

  2. I have certain constraints on u like constant*u1 - u2 <=0. I believe it goes in path constraint as per the problem_formulation_ocp_mex.pdf but still it is not clear which path constraint.

  3. I have linear least square cost for state reference tracking in my formulation ||x-x_ref||^2. I understand that W and W_e are the panalties for the reference tracking. However, I don’t understant how V_x, V_u and V_z should be defined. I see that from race cars example that they are set to Identity matrices. Does it mean that by setting certain diagonal entries in V_x/V_u/V_z to zero I can define which of variables has no tracking objective?

I am sorry if some of the issues raised are trivial but wanted to be clear with the implementation I am doing. Thanks a lot in advance!

Hi @jay101,

First, welcome to the acados forum :tada:

These are the values with which the parameters are initialized, so a np array, not CasADi symbolics.

Setting control bounds via lbu, ubu is the correct way.

You can implement this by using the general linear constraints lg <=C*x + D*u <= ug.

Exactly, you want to track y which in the linear leas square case is defined by y= Vx * x + Vu * u + Vz * z.

Since you seem to get started with acados in Python, please use this branch:

There are among other changes, some checks on all the dimensions in Python which should be helpful for you.


Hi @FreyJo,
Thanks a lot for your prompt reply.

  1. But to define a parameter in the model f(x,u,p) I will have to use CasADi symbolics right? Could you please tell me the way I should do it starting from i)ow to define it in the model of CasADi symbolics ii)include in the acados settings and iii)then update at each stage?

  2. Thanks for confirming the implementation zeroing some elements of u.
    I also wanted to know if there is a way to update bounds on only first two elements (for example) of u without updating bounds on entire u array. Currently, ‘lbu’ and ‘ubu’ has the size of u and I am unable to choose first two element to update the bounds.

Thanks a lot again!


this is an example with nonempty parameter vector:

Here the parameter values are set/updated stagewise:

This is not possible. I dont think this would simplify things really.


Hi @FreyJo,
Thanks a lot again for the answers. I was able to run the code successfully.
Can you please let me know if there is a way to get the Cost value after the solution? I see that acados_solver.get_stats() provide other values but not the cost.

I recently added the option to access the residuals of each SQP iteration from Python.
The cost value is not as easily available internally.
You could simulate a system with an extra cost state to obtain the cost value with high accuracy.

Thanks for you answer!
I have one more doubt. How can I add delta_u (input regulation) and penalty related to it for LINEAR_LS cost type? where delta_u = u_{k+1}-u_{k} and k denotes certain stage.

To do so, I would modify the model, and define the derivative of your u as the control.
Such that you have:
\dot{u_{\textrm{old}}} = u_{\textrm{new}}
making u_{\textrm{old}} part of x and penalizing u_{\textrm{new}}.

I will try your suggestion and let you know if I succeed.
Although, I was able to run the OCP I am not getting same results with one iteration of SQP_RTI algorithm and the same implementation in Matlab with Yalmip. The only difference I can see in two implementations is that, in Matlab I use exact hessian and I can’t find the an option to use ‘EXACT’ with the current installation of acados. I could find some example where the exact hessian was used

Can you please let me know if the master branch includes the feature to use exact hessian?

Hi @jay101,

the exact hessian for the Python interface is not merged into master yet.
Still waiting for some feedback.

But you can already use the corresponding branch.

Hi @FreyJo,
Thanks I have it working! Apparently Hessian approximation is not an issue so I wanted to check the sensitivities. I found in following script example for simulation

But after running this example I found that it gives me zeros for acados_integrator(“Sx”) whereas I get some values for (“Su”). Can you please tell me how to check them properly?

Also is it possible to check the references and parameter values passed to OCP problem formulation after compiling the AcadosOcpSolver object?

Outputting sensitivities from Python was not really implemented somehow.

I just fixed it on the following branch:

The ones from the formulation can only be checked by getting it from the object again, and it can also be double checked that they are correct in the generated C code.
Also, you can just update the parameters and references after compiling.


Hi @FreyJo,
Thanks for the information.

Does this branch allow to check the sensitivities from AcadosOcpSolver object?

How can you get the values from the AcadosOcpSolver object before compiling? When I run AcadosOcpSolver object.get() method I can only query “x” and “u”. How can I query “yref”, “p”, etc?

After compiling acados_solver_xxx_model.c file has only the value of the parameters, yref, etc only for stage one if I am not wrong. Once I update after compiling it for every stage how can I check if the values are updated correctly for each stage?

Thanks a lot,

Hi @jay101,

sorry for the late response.

Are you asking for the sensitivities of the optimal solution of the OCP here?
I thought you are interested in the sensitivities of the integrator, which you can get in Python using this branch: (sorry, the branch I pointed to before is where I push also other stuff these days…)

This is not be possible, the AcadosOcpSolver instance compiles in its constructor.

There are no getters for these quantities available, since they are just supposed to be set from outside.
Setting them can be done like here:

Do you think there is something wrong when updating these quantities? Or why do you think they are not set correctly?
Maybe you can make a minimal example.

I see, here you mean rendering.
Indeed, when rendering the template, y_ref and parameter values are initialized with the same value for every shooting node.


Hi @FreyJo,
Please don’t be sorry and I am very grateful for you patience to answer all of my questions.

No I was referring to the integrator sensitivities used to build OCP problem. I wanted to be sure that all the values of references and parameters are updated after compiling the OCP object and the integrator sensitivities used by OCP object are right. Just to be sure that I have properly configured the OCP.

I followed this example to construct OCP with only difference that I also have parameters in my formulation. For parameter updates I referred this example

However, I’m not sure about this but it seems like the parameters are not properly updated at every stage in my formulation. It gives me meaningful values when I don’t update the parameters at every stage (passing only initial values of parameters while building OCP object) whereas, when I update them at every stage the solution is completely different.

I know it’s difficult for you tell without looking at the formulation and I will try to make a minimal formulation that I might be able to share.

I have written an SQP_RTI algorithm in MATLAB using Yalmip and Gurobi solver with “Gauss-Newton” hessian approx. method without any condensing. In Acados, however I have to choose partial or full condensing for HPIPM and QPOASES. How can I use this solvers without any condensing to do fare comparison with MATLAB solution?

I know that I should compare with same solvers in both the formulations but I am unable to install QPOASES in MATLAB and HPIPM is not supported by Yalmip.

On top of that I also compared the solution of “SQP” (fully converged solution) solver in acados with Ipopt obtained in Matlab and they differ.

You are looking for this option:
If you set it to N, there is no condensing happening.

I guess if they differ much more than the solver precision, it is quite likely that the formulations are not equivalent.
It is hard to say more without looking into the details.

Indeed it is.
This would be great, please open another thread for this to keep things structured.

1 Like

@FreyJo @jay101
I also got the same error in matlab interface ,when parameter p needs an update at each stage.

.../casadi/core/sx_function.cpp:147: Code generation is not possible since variables [P_0, P_1] are free.

1.this is an example with nonempty parameter vector:

2.Here the parameter values are set/updated stagewise:

I tried the solution you mentioned above . But its not work.

Maybe, its different between Python and Matlab.

So, Are there any examples of MATLAB?

Thank you in advance.


Sorry, but this is not the same error at all.
If you are not sure that it is, please open a new topic instead of posting somewhere.

This is how to update parameter values stage wise in Matlab.