Proper setting of the Initial guess

Hi :wave:

I am just getting started with acados and how to set the initial guess. For me, there seems to be a discrepancy in the amount of state samples connected with specific N.

In the python example time-optimal-crane, the state-trajectory is set for N samples:

for i, tau in enumerate(np.linspace(0, 1, N)):
    ocp_solver.set(i, 'x', (1-tau)*x0 + tau*xf)

In the generated code c code, the state-trajectory is set for N+1 samples, so one more!:

// initialize solution
for (int i = 0; i <= nlp_dims->N; i++)
    ocp_nlp_out_set(nlp_config, nlp_dims, nlp_out, i, "x", x_init);
    ocp_nlp_out_set(nlp_config, nlp_dims, nlp_out, i, "u", u0);

Which initialization is in general correct, e.g. if the final state has no box constraints but some algebraic constraint h(xf)?

I came up with this topic, because my generated c code is not finding a solution. I figured out that the setting of the initial guess is not part of the c generated code (Makes sense).
Is there more I need to set manually?

Thanks for your help in advance and for publishing acados in the first place! :smiley:

Best regards

Hi Ludwig,

Thanks for your post! Well spotted!
Indeed both of the parts you mentioned are not fully correct.
I made this PR in response to your post:

The state x is typically available at all N+1 shooting nodes (0, …, N).
While the control input u is available at the N shooting nodes (0,…,N-1) and applied for the time between this shooting node and the next one.
So N shooting intervals, defined through the N+1 nodes.

That’s the way to go!
Does it work in your Python prototyping workflow?
You can maybe also initialize multipliers.

Maybe that helps too Is there any way to reset solver in Python? - #2 by FreyJo


Hi Jonathan,

The crane initialization should be than something like this, right?:

for i, tau in enumerate(np.linspace(0, 1, N+1)):
    ocp_solver.set(i, 'x', (1-tau)*x0 + tau*xf)

for i, tau in enumerate(np.linspace(0, 1, N)):
    ocp_solver.set(i, 'u', np.array([0.1, 0.5]))

In the changes of your pull request you will have xf in the last two samples.

  • In the python script the problem is solved just nicely.
  • With the multipliers you mean the upper and lower limits for the equality/inequality constraints?

My specific problem is an implementation bug! I copy pasted the generated code into my existing project where it failed as described above.
If I implemented the initial guess setting in the generated code itself and that works.
So, while copy pasting and doing “some minor changes” I apparently introduces some bug.

Right, I changed it to that in the PR.

No, I mean the Lagrange multipliers for the [in]equality constraints. See Python API — acados documentation

But, if just initializing the primal variables makes it work for you in Python, this should also be sufficient in C. So better keep looking at your “minor changes” :stuck_out_tongue_winking_eye: