ModelPredictiveControl.jl icon indicating copy to clipboard operation
ModelPredictiveControl.jl copied to clipboard

Direct collocation transcription

Open franckgaga opened this issue 11 months ago • 3 comments

Implement a trapezoidal ODE solver first.

franckgaga avatar Feb 23 '25 22:02 franckgaga

@1-Bart-1 still interested in this feature ?

franckgaga avatar Apr 05 '25 14:04 franckgaga

Thank you for asking! Difficult to say yet, improving the kite simulator took a couple of months more than expected 😆 I will be experimenting with MPC.jl in the coming weeks, so I will come back to this.

1-Bart-1 avatar Apr 06 '25 09:04 1-Bart-1

Alright I'll prioritize #177 in the next weeks for now, it's been a while that I want to implement MV blocking.

franckgaga avatar Apr 06 '25 14:04 franckgaga

Hello @baggepinnen! I'm working on the trapezoidal method and I'm stuck at a detail. You said this:

The most efficient way to implement trapezoidal discretization tends to be to implement it as constraints to IPOPT. This way, you don't need any external root finder, Ipopt will handle everything through the constraints

$$F(x, u) = 0$$

This can also cut down the number of function evaluations by a factor of 2 (provided that you accept FOH instead of ZOH interpolations of the inputs), since you only need to evaluate the dynamics once per time step, whereas if you implement it as a time-stepping integrator with ZOH interpolation, you cannot exploit the fact that you have already evaluated $f(x_k, u_k)$ when you compute the step between $f(x_k, u_k)$ and $f(x_{k+1}, u_{k+1})$ when you computed the step $f(x_{k-1}, u_{k-1})$ to $f(x_k, u_k)$

Originally posted by @baggepinnen in #140

It feels weird to assume piece-wise linear (or FOH) manipulated inputs in the transcription method when the user will presumably implement the optimal solution on the real plant as piece-wise constant inputs (or ZOH). Or even in simulation, we can assume that the user will simulate the plant with a stepper like RK4, and they almost always assume piece-wise constant manipulated inputs. Two questions:

  1. Do you think that this discrepancy between the transcription method and the reality will affect the closed-loop response? What's your experience on this?
  2. If I would like to implement a trapezoidal method that assumes piece-wise constant manipulated inputs, to be a little bit more realistic, would it consist in computing the defects as :
x_{k-1}-x_k-0.5\Delta T[ f(x_k, u_k) + f(x_{k+1}, u_k) ]

instead of:

x_{k-1}-x_k+-0.5\Delta T[ f(x_k, u_k) + f(x_{k+1}, u_{k+1}) ]

franckgaga avatar Aug 19 '25 20:08 franckgaga

the user will presumably implement the optimal solution on the real plant as piece-wise constant inputs (or ZOH)

Ideally, both options would be available. If the MPC controller is the lowest-level controller then yes, ZoH is typically how the control signal will be implemented. On the other hand, if the MPC controller has a lower-level controller inside of it, FoH (or even higher order) is not uncommon, and if possible to implement this way, reduces higher-order harmonics from step changes and may improve performance quite a lot. The only difference between the two interpolation options for $u$ lies in what $u$ you use fort the end point of each interval, for ZoH, you cannot reuse the evaluation of $f$ between intervals, but it's otherwise equally simple to implement.

would it consist in computing the defects as...

correct, except for the indices of k-1, k for the x outside of f but k, k+1 inside of f? I would have expected the same indices inside and outside of f.

baggepinnen avatar Aug 20 '25 02:08 baggepinnen

Yes, I meant computing the defects as:

x_{k+1}-x_{k}-0.5\Delta T[ f(x_{k+1}, u_k) + f(x_k, u_k)  ]

instead of:

x_{k+1}-x_k+-0.5\Delta T[f(x_{k+1}, u_{k+1}) +  f(x_k, u_k)]

Thanks!

P.S. The more I think of it, the more I find hidden complexities in collocation methods. In the package, I define my stochastic model for the unmeasured disturbances as discrete state-space. The $f$ function above should be the continuous state-space model of the plant model augmented with the unmeasured disturbance model. Do I need to call d2c on my stochastic model ? I'm not sure, but it feels like a dirty solution to me.

franckgaga avatar Aug 20 '25 12:08 franckgaga

I'm not sure what the best approach is in this case, a simple approach to start out with could perhaps be to require all dynamics in continuous time for continuous-time transcriptions?

baggepinnen avatar Aug 20 '25 13:08 baggepinnen

The refactoring to convert all the dynamics continuous in time would be huuuge, I would like to avoid that.

Hummmm I think it's feasible by keeping my stochastic model discrete in time. The collocation constraint will be applied only for the deterministic states, while the stochastic states would be trivial equality constraints. I will explore this avenue.

franckgaga avatar Aug 21 '25 12:08 franckgaga