Tim-Salzmann / l4casadi

Use PyTorch Models with CasADi for data-driven optimization or learning-based optimal control. Supports Acados.
MIT License
366 stars 27 forks source link

Questions about mpc_mlp_example.py #41

Closed Ucccccc closed 2 months ago

Ucccccc commented 3 months ago

Hello: I would like to ask why I need to execute several lines when solving mpc

  for i in range(N):
      x_l.append(solver.get(i, "x"))
  params = learned_dyn_model.get_params(np.stack(x_l, axis=0))
  for i in range(N):
      solver.set(i, "p", params[i])

I understand that the main purpose is to perform a first-order Taylor expansion and restore the original output of the model, but shouldn't it be in differential form, i.e. df_a? Isn't this in line with the explicit dynamics definition process,like this

image

Tim-Salzmann commented 3 months ago

Hi,

I understand that the main purpose is to perform a first-order Taylor expansion and restore the original output of the model, but shouldn't it be in differential form, i.e. df_a? Isn't this in line with the explicit dynamics definition process,like this

Yes, I think this is correct and Acados expects the dynamics model to be in differential form. However this is unrelated to the code you show.

The code does exactly what you say. It taylor approximates the model around each node and updates the model at each node. Hence the for loop. The learned dyn model should be in differential form.

Let me know if this answers your problems!

Tim

Ucccccc commented 3 months ago

Thanks for your answer! I understand what you mean. If there is a pytorch model, it should be trained in a differential way, right? Can it support discrete dynamics models (trained using pytorch)?

image

Ucccccc commented 3 months ago

Is it possible to define the dynamics model in discrete form (and train the pytorch model) and then define it in discrete form in acados

Tim-Salzmann commented 3 months ago

Can you train a discrete dynamics model in PyTorch?

Yes

Can you include it in CasADi via L4CasADi?

Yes

Can you include it in an MPC problem defined in CasADi?

Yes

Can you include it in an MPC problem defined in Acados?

I am not sure. This depends if Acados supports something like this and is a question for the Acados Forum.

Hope this helps Tim

Ucccccc commented 3 months ago

Thanks for your help!

Ucccccc commented 2 months ago

Hello, may I ask what the main purpose of this line of code is to swap the rows and columns to conform to the order defined by Casadi's notation (column priority)?

  return [a_t.cpu().numpy(), f_a.cpu().numpy(), df_a.transpose(-2, -1).cpu().numpy()

Tim-Salzmann commented 2 months ago

I think this is to bring it in the correct form for the Taylor Approximation

https://github.com/Tim-Salzmann/l4casadi/blob/b073ea1c3744bb6d43299516bdb666fee05a303b/l4casadi/realtime/realtime_l4casadi.py#L90

df_a is of shape [out_dims, in_dims] but the casadi Taylor (see above) expects [in_dims , out_dims].

Best Tim

Ucccccc commented 2 months ago

Thank you for your answer.