Flexible linear constraints for MPC and codegen

franckgaga
Tue, 27 Jan 2026 15:30:30 +0000
Optimization (Mathematical)

Following the subject [ANN] ModelPredictiveControl.jl with @langestefan and @darnstrom:

The expressiveness of the linear MPC framework is already very limited and the current syntax limits it further. As long as my expression is mathematically valid within the linear MPC framework I think I should be able to define it.

Agree. I would like to support more flexible linear constraints in ModelPredictiveControl.jl.

Unfortunately for commercial embedded systems every kilobyte matters so C codegen is a must. I think it’s already a modern miracle we can do MPC with a <200 kB solver (thanks Boyd!).

And that’s also why I think running MPC on traditional embedded systems (e.g. not Raspberry Pi or Nvidia Jetson) is not ideal. Even by working very hard to make C codegen fully feature-complete, there is still one unsolvable issue: it’s another form of the two language problem. A major problem that comes with it is reproducibility (the numerical results will be presumably different, and possibly drastically different for ill-posed corner cases). But that’s another subject.

This feature is not supported by code generation (i.e. there is no C code equivalent of calling setconstraint! online).

Yeah this is very tricky but I think a requirement for practical applications. Between mild and cold weather with high supply temperatures maximum output power can drop from 5 to 3 kW.

Maybe you know this already, but cvxpygen will generate a function cpg_update_ (idx, val) to update each parameter in your problem. For that to work you would first need to know where the parameters are and there are some rules as to how parameters can enter the problem (DPP compliance).

Now for LinearMPC.jl, you could say everything that is a constant in my problem is a parameter and thus could be updated and included in the codegen.

Since right now I rely on LinearMPC.jl for codegen, I need to include @darnstrom in the loop. My understanding is online updates of e.g. the lb and ub arguments of add_constraint! within the C code is not possible right now, am I right? Would it be something that is possible to add in LinearMPC.jl ?

About this @langestefan, do you think that the add_constraint! API provided by LinearMPC.jl is flexible enough for your specific constraint structure? Let’s assume that you are also able to update the value of lb and ub online in the generated C code.

Really thinking out loud here so feel free to shoot this down, but why not use JuMP as the main problem creation interface instead? There’s a DSL, and there is MOI.Parameter.

It would not help here since I’m using LinearMPC.jl for C codegen, and JuMP is not used at all in this package.

11 posts - 3 participants