-
Notifications
You must be signed in to change notification settings - Fork 3
🚀 added: C code generation via LinearMPC.jl
#307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
LinearMPC.jl
I aim to release v0.8.0 of LinearMPC.jl during this week! |
|
Btw, you should be able to set rate constraints quite easily with: add_constraint!(mpc; Au=I(nu), Aup=-I(nu), lb=dumin, ub=dumax, ks=1:mpc.Nc)where |
|
Oh thanks, I did not noticed the is that it ? |
Correct! It is not that well documented, but you can also include the reference and disturbance in the constraints with |
Ok I added to conversion of input increment constraint. Thanks for the tips. But I noticed something else. Quoting the new text above, in the first comment:
I understood the you rely on the internal constraint softening mechanism of DAQP, and the In MPC.jl I explicitly introduce a new slack variable Knowing this, I deduced that it will be simply impossible to perfectly convert @warn "The LinearMPC conversion applies an approximate conversion " *
"of the soft constraints.\n You may need to adjust the soft_weight "*
"field of the LinearMPC.MPC object to replicate behaviors."Do you feel like it's a good solution ? |
|
BTW this is from my experience: I'm able to reproduce the results of LinearMPC.jl up to the 10th digits if softening is disable. Otherwise, the results can be vastly different if a constraint is activated, and manually playing with |
|
About #307 (comment) |
|
How about turning off DAQP softness and only use the softness implemented here? |
Thanks for the idea. AFAIK, this is impossible for 2 reasons:
|
|
Are you using a single slack variable for all soft constraints @franckgaga? |
|
Yes, and that's the same approach than MATLAB (equal concern for relaxation). There is a single slack
It's entirely possible that the way you handle soft bounds is more efficient, thus more adapted to low-power embedded platform. It's another sign of the tradeoff mentionned in #124. Full reproducibility is hard with code generation, especially if the targeted platform is vastly different in term of computational power. So I think that the best solution is me trying to find a good heuristic for a conversion factor between |
|
Ok! Then the difference make sense to me. A problem with one slack variable is that some constraints can be violated even though it is not necessary: if one constraint needs to be violated, resulting in epsilon > 0, this will allow all other soft constraint to be violated without extra cost. This is corrected by having a slack variable for each soft constraint. The obvious drawback with one slack variable for each soft constraint is that a lot more extra decision variables are required (although, as mentioned above, this is not the case for DAQP due to some tricks internally.) If one is forced to add slack variables explicitly (which is the case for MPC.jl that wants to support any generic QP solver) it make sense to just use one slack variable for computational reasons! |
Super interesting and well-explained 👏 ! |
Since DAQP introduces implicitly a new variable for each softened constraints, it make sense to apply a conversion factor inversely proportional to the number of softened constraints. I tested on a simple SISO model and it works OK for various scenario of softened constraints. Note that the issue is less apparent when the only soft constraints is the output variable, which is the most common case in practice.
|
Okay some final comments:
|
The "Prioritized Constraints in Optimization-Based Control" paper indicated that the ρ weight is squared, and there is one term for each soft bound. So a `sqrt` relation is more logical here (Cwt is not squared in MPC.jl). And it is indeed a better approximate conversion factor, according to various simulations on a SISO system.
|
Quick update, by playing a little bit more with my SISO case, I found that: soft_weight = 10*sqrt(nsoft*Cwt)is a better conversion, that is, the solutions are more similar in closed-loop (up to the 3rd digits, and it was up the the 1st digit before this change). And it does make sense to include a |

Closes #124, for
LinMPCcontrollers.I implemented the conversion code as a package extension.
It comes with some limitations, as described in the docstring:
DAQP.SingleShooting.SteadyKalmanFilterwithdirect=true.Cwt=Infto disable relaxation).I also mentioned in the docstring that the reverse is also true:
LinearMPC.jloffers some exclusive features thatModelPredictiveControl.jldo not support e.g. binary manipulated inputs and constrained explicit MPC.