Modifying the OCP without recompling it in python interface

Hi acados team!

Thank you again for all your work on acados.
My question is, what should I do to create a new ocp problem (new initial conditions and new parameters) without recompiling the solver with the python interface?

I am trying to solve a multi-robot motion planning problem with an MPC. Each robot has an initial position and a goal to reach. I would like to solve the MPC for different couples (initial position,goal). For this, I set the goal as a parameter. I export the solver once, solve the first ocp, and then to solve a new ocp, I update the parameter (goal) with ocp_solver.acados_ocp.parameter_values = goal, and I use ocp_solver.solve_for_x0(x0) to use the new initial position. However, the results I get are still different than the ones I get if I recompile the solver.

At each time step, I also initialize the solver and the integrator using set():
ocp_solver.set(0, “lbx”, xcurrent)
ocp_solver.set(0, “ubx”, xcurrent)
ocp_solver.set(0,‘x’,xcurrent)
ocp_solver.set(0,‘u’,np.zeros((nu,)))
ocp_solver.set(0,‘p’, goal)

What instructions do you think I should add? Does the ocp_solver keep the previous solution in memory and is it possible to delete it? I tried the function reset(), but then I get errors of status 4. I have also tried to use store_iterate() and load_iterate() but the problem is still here. I probably do not use these functions correctly? I do store_iterate(“json_file.json”) once after exporting/compiling the solver. And then, during the simulation I do load_iterate(“json_file.json”) at each time step, before initializing the solver, before calling .solve_for_x0. Should I create different json files for each different ocp?

Thank you very much in advance for your help!

why do you think the reset() is the issue?
Can you problem just be infeasible?

Hi,

Thank you for your quick reply!

I don’t think reset() is the issue because when I solve the problem exporting the solver for the specific initial positions and goals, then it works well. The problem occurs when I want to solve multiple ocp without re-exporting the solver.

In this case, reset() should indeed work.
Maybe you can do some debugging:

  • You can use store_iterate() once with your reset and once after exporting the solver for the same initial position and compare the files.
  • If this is indeed the same. You can also carry out a single SQP iteration and run dump_last_qp_to_json() and compare the two.

Let me know what you find!

Indeed the first debugging you propose actually reveals the problem!

  • When I apply store_iterate() after exporting the solver, every values are zeros except the x_i values that are equal to the my initial conditions x0. I guess these values correspond to the initial condition constraint.

  • When I apply store_iterate() after reset() then all the values in the json file are equal to zero, the x_i values too.

How could I set again the initial condition?

Thank you again for your help!

If I set my initial condition using ocp_solver.set(i,'x',x0) before store_iterate() and now both json files are the same, but the problem is still not solved.

I will try your second suggestion for debugging.

I have finally found the solution!

Here are the steps I had to do:

  • ocp_solver.reset()

  • Initialize the solver, so the initial constraint, initial values of my state, and initial value of the goal parameter:

 for i in range(Npred+1):
        ocp_solver.set(i,'x',x0)
        ocp_solver.set(i,'p', goal)
 ocp_solver.set(0, "lbx", x0)
 ocp_solver.set(0, "ubx", x0)

I especially needed to do it until Npred+1 and not Npred. Otherwise, my cost was NaN at the end of the horizon, since my goal is directly used in my cost function.

  • ocp_solver.store_iterate(json_file)

  • Then, in the closed-loop simulation, at each timestep:

ocp_solver.load_iterate(json_file)
ocp_solver.set(0,'x',xcurrent)
ocp_solver.set(0,'u',np.zeros((nu,)))
ocp_solver.set(0,'p', goal)
ocp_solver.solve_for_x0(xcurrent)

I was actually stuck on this problem for a long time, so thank you so much for your precious help and amazing responsiveness!
Your suggested tricks will definitely be useful for any problem I face in the future.

Best,
Manohari

1 Like

Hi Manohari,

I am happy it helped and you were able to solve your issue! :slight_smile:

I guess only this line should not be necessary, if the control values in the iterate you load are already 0.

ocp_solver.set(0,'u',np.zeros((nu,)))

Best,
Jonathan