# Model Predictive Control (MPC) in Julia

**URL:** <https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [May 15, 2019, 2:48pm UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240 "2019-05-15T14:48:57Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![uwechsler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwechsler/32/5223_2.png) [@uwechsler](https://discourse.julialang.org/u/uwechsler)\
**Post date:** [May 15, 2019, 2:48pm UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/1 "2019-05-15T14:48:58Z")

</div>

I am considering using Julia and JuMP for my Master Thesis in _learning based robust economic Model Predictive Control_. I hope to get some feedback from the community on what packages to use or on what functionalities are still missing, where I could possibly help contributing to julia.

In principle, implementing an MPC Controller means solving a constraint finite horizon Optimal Control Problem repeatedly. Additionally, it is important to be able to calculate invariant sets, i.e. computing the Minkowski Sum of two sets repeatedly (e.g. the sum of polyhedral and an ellipsoidal set).

At my University, we have a Matlab heavy control lab, and therefore we are also using Matlab for MPC. Specifically the Yalmip toolbox (for optimization) and the Multi-Parametric Toolbox ([https://www.mpt3.org/](https://www.mpt3.org/)) for the set calculations. I’d like to replace these toolboxes and Matlab by a Julia framework.  
From my experience, with JuMP I should be able to define and solve the optimization problems. But for the set manipulations, I haven’t found any packages suitable for my needs.

So I was wondering, if there is already some work done on MPC in general and invariant set calculation in special or if there are plans to do so. Or if there are general caveats on using JuMP for MPC?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 15, 2019, 2:56pm UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/2 "2019-05-15T14:56:33Z")

</div>

Sounds exciting! For polyhedron calculations you can use [GitHub - JuliaPolyhedra/Polyhedra.jl: Polyhedral Computation Interface](https://github.com/JuliaPolyhedra/Polyhedra.jl) . What else is missing?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 15, 2019, 2:59pm UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/3 "2019-05-15T14:59:17Z")

</div>

Ok, I see, positive invariant set calculation. The Matlab code has GPL license, so you could convert it directly to julia if you know exactly which functions you need. Not so nice, LGPL or MIT license is more common for Julia libraries.

---

<div class="post-metadata">

**Author:** ![uwechsler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwechsler/32/5223_2.png) [@uwechsler](https://discourse.julialang.org/u/uwechsler)\
**Post date:** [May 15, 2019, 3:07pm UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/4 "2019-05-15T15:07:49Z")

</div>

Thanks 🙂 . Yes, I saw the **Polyhedra.jl** package, but as you mentioned, the functionality of calculating the Minkowsky sum is missing (as far as I know). And most likely, I also need to do the calculations with ellipsoidal sets.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 15, 2019, 4:32pm UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/5 "2019-05-15T16:32:42Z")

</div>

You can certainly do MPC in JuMP (see [https://github.com/rdeits/DynamicWalking2018.jl/blob/master/notebooks/6.%20Optimization%20with%20JuMP.ipynb](https://github.com/rdeits/DynamicWalking2018.jl/blob/master/notebooks/6.%20Optimization%20with%20JuMP.ipynb) for a very simple example), and if your baseline is Yalmip in MATLAB then the performance should be good.

An alternative to JuMP is [GitHub - tkoolen/Parametron.jl: Efficiently solving instances of a parameterized family of (possibly mixed-integer) linear/quadratic optimization problems in Julia](https://github.com/tkoolen/Parametron.jl) , which focuses on making it easy to update a problem in-place very efficiently. That can make your problem setup much faster than it would be with JuMP. Parametron.jl and JuMP both use the same solver interface tools, so it should be relatively easy to switch between them.

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [May 16, 2019, 12:24pm UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/6 "2019-05-16T12:24:55Z")

</div>

[SetProg.jl](https://github.com/blegat/SetProg.jl) is designed for computing invariant sets, it can compute ellipsoidal or polynomial controlled invariant sets that can be used for MPC, Polyhedral invariant sets is not implemented yet but it’s a few lines with Polyhedra.jl: [https://github.com/JuliaPolyhedra/Polyhedra.jl/blob/master/examples/Computing%20controlled%20invariant%20sets.ipynb](https://github.com/JuliaPolyhedra/Polyhedra.jl/blob/master/examples/Computing%20controlled%20invariant%20sets.ipynb)  
Could you describe what form of invariant set (polyhedra, ellipsoidal ?) you are looking form and what is the system (do you want it to be controlled invariant or just invariant ?)

---

<div class="post-metadata">

**Author:** ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)\
**Post date:** [May 16, 2019, 2:03pm UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/7 "2019-05-16T14:03:34Z")

</div>

Is Parametron.jl expected to be more efficient for MPC than ParameterJuMP ([GitHub - JuliaStochOpt/ParameterJuMP.jl: A JuMP extension to use parameter in constraints RHS](https://github.com/JuliaStochOpt/ParameterJuMP.jl))?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 16, 2019, 2:08pm UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/8 "2019-05-16T14:08:06Z")

</div>

Perhaps, but we haven’t tested it. The real difference is that they’re focused on different things: ParameterJuMP focuses on scalar parameters and lets you extract the duals corresponding to those parameters, while Parametron.jl takes a much more broad view of what a “parameter” is. In Parametron.jl, a `Parameter` can be literally anything as long as you can provide a value for it and a way to update that value. For example, the entire state of your robot might be a `Parameter` in Paramteron.jl. Parametron.jl also provides tools to construct affine expressions that can reference parameters, including the ability to update those expressions in-place when your parameters change. On the other hand, Parametron.jl doesn’t provide any ability to query duals of parameters.

---

<div class="post-metadata">

**Author:** ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)\
**Post date:** [May 16, 2019, 2:10pm UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/9 "2019-05-16T14:10:16Z")

</div>

Ah, thank you for describing the different intended uses of these two packages.

---

<div class="post-metadata">

**Author:** ![uwechsler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwechsler/32/5223_2.png) [@uwechsler](https://discourse.julialang.org/u/uwechsler)\
**Post date:** [May 18, 2019, 8:52am UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/10 "2019-05-18T08:52:34Z")

</div>

Thanks, I will have a closer look at SetProg.jl.

Since I am still working on the theoretical description of my system which will be an economic MPC scheme with a learned model, I haven’t figured out everything concerning the implementation yet.  
However, I intended to work with a polyhedral invariant set if it is computationally feasible. In MPC term, this would be the minimal robust positive invariant set (mRPI), respectively an invariant approximation of it as e.g. shown in [Invariant Approximation of mRPI](https://www.semanticscholar.org/paper/Invariant-approximations-of-the-minimal-robust-set-Rakovic-Kerrigan/87848d476d19d3253e8a294c8adac0b1171541cb).

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [May 20, 2019, 1:56pm UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/11 "2019-05-20T13:56:34Z")

</div>

See [this example](https://github.com/JuliaPolyhedra/Polyhedra.jl/blob/master/examples/Minimal%20Robust%20Positively%20Invariant%20Set.ipynb) for how to compute a minimal robust positive invariant set with [Polyhedra.jl](https://github.com/JuliaPolyhedra/Polyhedra.jl).

---

<div class="post-metadata">

**Author:** ![uwechsler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwechsler/32/5223_2.png) [@uwechsler](https://discourse.julialang.org/u/uwechsler)\
**Post date:** [June 25, 2019, 12:02pm UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/12 "2019-06-25T12:02:36Z")

</div>

Thanks for your example. I was trying to run it for a 3D system (i.e. A as 3D matrix) and the performance was really bad. However, I did not use the `CDDLib`, since I could not make it build on Windows. For the 2D system, it worked without `CDDLib`, but for the 3D system, it just stalls.  
Is there a way to make that computation faster? Or approach the problem differently?

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [July 1, 2019, 8:39pm UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/13 "2019-07-01T20:39:35Z")

</div>

The V-redundancy removal using LP [is now implemented](https://github.com/JuliaPolyhedra/Polyhedra.jl/pull/174) and used by default if the H-representation is has not been computed. Therefore you should now get similar performance between CDDLib and other libraries with Polyhedra v0.5.2.

---

<div class="post-metadata">

**Author:** ![timueh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timueh/32/12379_2.png) [@timueh](https://discourse.julialang.org/u/timueh)\
**Post date:** [July 2, 2019, 5:47am UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/14 "2019-07-02T05:47:36Z")

</div>

Just out of curiousity @uwechsler: since you are using the mpt toolbox I suppose your are solving linear-quadratic MPC, i.e. positive definite quadratic cost + linear time-invariant system dynamics + polytopic state/input constraints?

Have you played with nonlinear MPC formulations in Julia, i.e. nonlinear system dynamics of the form \dot{x} = f(x,u) with some initial condition and x \in \mathbb{R}^n, u \in \mathbb{R}^m ?

---

<div class="post-metadata">

**Author:** ![uwechsler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwechsler/32/5223_2.png) [@uwechsler](https://discourse.julialang.org/u/uwechsler)\
**Post date:** [July 3, 2019, 12:01pm UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/15 "2019-07-03T12:01:46Z")

</div>

> [@timueh](#):
>
> Have you played with nonlinear MPC formulations in Julia, i.e. nonlinear system dynamics of the form \dot{x} = f(x,u) with some initial condition and x \in \mathbb{R}^n, u \in \mathbb{R}^m?

So far, I was mostly working on theoretical analysis such as performance guarantees and convergence proofs. But I have implemented some low dimension nonlinear examples from academia such as Example 2 from [Robust Economic MPC](https://www.sciencedirect.com/science/article/pii/S0959152414001693) which has a piecewise quadratic cost and a “mild” nonlinearity. To do this, I used `LazySets`, `Ipopt` and `JuMP`.

Since I am working on economic MPC, I can use arbitrary continuous objective functions but mostly focus on linear objectives. Over the next months, I plan to implement “my” MPC scheme for an industrial application. First, a linear cost with the linearized model and subsequently, a linear cost (or something crazier) with the full nonlinear model.

The biggest challenge so far in the implementation was to compute the polytopic robust invariant sets for higher dimensions (on Windows 😅). But with the great support from @blegat and the guys from `LazySets` I could make it work. However, I hope to be able to implement an ellipsoidal set representation in the future.

I see that you do research in a similar area. Are you able to use Julia for your applications or do you mostly use Matlab?

---

<div class="post-metadata">

**Author:** ![timueh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timueh/32/12379_2.png) [@timueh](https://discourse.julialang.org/u/timueh)\
**Post date:** [July 5, 2019, 6:17am UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/16 "2019-07-05T06:17:35Z")

</div>

We do a lecture on nonlinear MPC for which we use Matlab together with [casadi](https://web.casadi.org/). The great thing about casadi is that it lets you formulate vector-valued nonlinear equality constraints, i.e. h: \mathbb{R}^{n\_x} \rightarrow \mathbb{R}^{n\_h}. As far as I know (_and I’d love to be corrected here_), the current version of `JuMP` does not have a native support for vector-valued user-defined nonlinear functions. ☹ Sure, for small nonlinear systems there are workarounds and so on, but still…

that’s why I asked about your dynamics 😉

---

<div class="post-metadata">

**Author:** ![uwechsler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwechsler/32/5223_2.png) [@uwechsler](https://discourse.julialang.org/u/uwechsler)\
**Post date:** [July 5, 2019, 7:58am UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/17 "2019-07-05T07:58:00Z")

</div>

That sounds very interesting 🙂 . Is there an official website to see a detailed course description?

I don’t think that there is such a nice functionality for nonlinear vector-valued function as with the linear-quadratic MPC case, like this

```julia
N = 20
f(x,u) = rand(3,3)*x + rand(3,2)*u
l(x,u) = x'*x + u'*u
m = Model(with_optimizer(Ipopt.Optimizer))
@variable(m, z[1:3,1:N])
@variable(m, v[1:2,1:N])
@constraint(m, [i=1:N-1], z[:,i+1] .== f(z[:,i],v[:,i]))
@objective(m, Min, sum(l(z,v)))

```

The workaround for non-linear systems that I used so far and the only method I know, looks something like that

```julia
m = Model(with_optimizer(Ipopt.Optimizer))
@variable(m, z[1:3])
@variable(m, v[1:2])
f1(x1,x2,x3,u1,u2) = x1^3 + x2^3 + x3^3 + u1^2 + u2^4
JuMP.register(m,:f,5,f, autodiff=true)
@NLconstraint(m, z[1] == f1(z[1],z[2],z[3],v[1],v[2]))

```

but I admit that it feels hacky and could be really annoying up to practically infeasible for problems with a large state-space.

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [July 5, 2019, 3:14pm UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/18 "2019-07-05T15:14:02Z")

</div>

We have no current plans to add support for vector-valued expressions to JuMP. I’d recommend supporting the effort to get casadi running in Julia: [Using CasADi from Julia · Issue #1105 · casadi/casadi · GitHub](https://github.com/casadi/casadi/issues/1105).

---

<div class="post-metadata">

**Author:** ![timueh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timueh/32/12379_2.png) [@timueh](https://discourse.julialang.org/u/timueh)\
**Post date:** [July 8, 2019, 5:43am UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/19 "2019-07-08T05:43:40Z")

</div>

thanks for the pointer! I wasn’t aware of these efforts!

---

<div class="post-metadata">

**Author:** ![bienpierre](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bienpierre](https://discourse.julialang.org/u/bienpierre)\
**Post date:** [July 30, 2019, 3:21pm UTC](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240/20 "2019-07-30T15:21:02Z")

</div>

Hello,  
I am curious, I have also some interest about robust EMPC.  
What lead you to tube-based EMPC with non linear systems. Because in [robust EMPC](https://www.sciencedirect.com/science/article/pii/S0005109817305484), it is written that it brings some difficulties in finding the robust control invariant sets?  
Thanks a lot.  
Regards

[Next page](https://discourse.julialang.org/t/model-predictive-control-mpc-in-julia/24240.md?page=2)
