Model predictive control (MPC)

A model of the whole plant predicts where every variable is heading; every cycle, the controller plans the next few moves of all the valves to bring every variable to its setpoint at the least weighted cost, within the valves' limits. It uses the first move and plans again next cycle.

Theory

The receding horizon. With a linear model xk+1=Φxk+Γukx_{k+1} = \Phi x_k + \Gamma u_k, y=Cxy = Cx and the present state estimate, predict the outputs PP cycles ahead for any sequence of the next MM moves. Choose the moves that minimize

J=∑i=1P∥yk+i−r∥Q2+∑j=0M−1∥Δuk+j∥R2subject toumin≤u≤umaxJ = \sum_{i=1}^{P} \lVert y_{k+i} - r\rVert^2_{Q} + \sum_{j=0}^{M-1} \lVert \Delta u_{k+j}\rVert^2_{R}\quad \text{subject to}\quad u_{min} \le u \le u_{max}

then apply only the first move, and do it all again next cycle with fresh measurements. That's a small quadratic program each cycle (here 2M2M numbers).

What the pieces do.

  • Prediction horizon PP: long enough to see where each move leads.
  • Control horizon MM: how many moves it plans; after that the valves are assumed to stay.
  • Weights QQ (per output): which variable matters more.
  • Move weight RR: how gently the valves may move. It is the main speed knob.
  • Constraints: the valve limits are part of the problem. It never plans a move it can't make, and when it can't have everything, the weights decide what gives.

Offset-free. A model is never exact. The observer (here a Kalman filter) estimates a constant load at each valve alongside the states, and the plan includes it. That does the integral action's job. (The older "offset on each output" model works too, but is slow on load changes.)

Without constraints active, MPC is a linear controller (an LQR with a finite horizon, plus the observer). The constraints are what make it different, and what make it hard to analyse with Bode and margins: judge it on tests, including model mismatch.

When it works, and when it breaks

Works
  • Several interacting variables, with limits that bind often: distillation, reactors, compressors, utilities, whole units.

  • Changing priorities: hold the quality, let the level float within its limits.

  • Slow processes with dead time and a good model, where a few % of throughput is worth an engineer's time.

Breaks
  • A poor model, or one that goes out of date when the plant changes: MPC is only as good as its model.

  • Fast loops (a flow, a pressure): the PLC cycle and the optimization don't keep up. MPC usually sits above PID loops and writes their setpoints.

  • No one on site who understands it: it gets switched off and the PIDs underneath take over.

  • Feasibility: when the setpoints can't all be met, the result depends on the weights. Choose them for that case, not just for the nominal one.

How to tune it

  1. Model first: step tests on every input, every output, at the operating points you run.
  2. Horizon: PP around the time the slowest response takes to settle (here 100 cycles, 200 s); MM of 3–10.
  3. Weights: start with equal weights in % of span; raise the one that matters more. Test the case where not everything can be met: that is where the weights really decide.
  4. Move weight RR: raise it until the valves stop chasing noise, lower it until the response is fast enough.
  5. Observer: how quickly it attributes errors to loads (here the "upsets" setting). Too fast passes noise, too slow leaves offsets that last.
The tuning rules in the lab
SIMC (Skogestad) (PI, PID)
Kc=τK(τc+θ),Ti=min⁡(τ, 4(τc+θ))K_c = \frac{\tau}{K(\tau_c + \theta)},\quad T_i = \min\big(\tau,\ 4(\tau_c + \theta)\big)

Model-based, one tuning knob: the closed-loop time constant τc. τc = θ gives a fast, still robust loop (Ms ≈ 1.6); larger τc is smoother. The min() on Ti keeps lag-dominant and integrating processes from responding too slowly to load upsets. PID uses the second-order model: Td = τ2 in series form.

Lambda (IMC) (PI)
Kc=τK(λ+θ),Ti=τK_c = \frac{\tau}{K(\lambda + \theta)},\quad T_i = \tau

The process-industry favourite: choose λ, the closed-loop time constant, and the setpoint response is first-order with no overshoot. Ti = τ cancels the process pole, which makes load rejection slow on lag-dominant processes. λ = 1–3 × τ is typical for robustness.

IMC-PID (Rivera–Morari–Skogestad) (PI, PID)
Kc=2τ+θ2Kλ,Ti=τ+θ2PID: Kc=2τ+θK(2λ+θ), Td=τθ2τ+θK_c = \frac{2\tau + \theta}{2K\lambda},\quad T_i = \tau + \frac{\theta}{2}\qquad\text{PID: } K_c = \frac{2\tau + \theta}{K(2\lambda + \theta)},\ T_d = \frac{\tau\theta}{2\tau + \theta}

Internal model control worked back into a PID (Rivera, Morari and Skogestad, 1986): the controller that makes the loop a first-order λ response, with the dead time replaced by its first-order Padé approximation. That approximation is what gives the derivative, and why Ti = τ + θ/2 rather than τ. Keep λ above about 0.8θ for the PID and 1.7θ for the PI, where the approximation stops holding. Like Lambda, it cancels the process lag, so load upsets on lag-dominant processes recover slowly. The IMC structure itself (the model running beside the plant) is the Smith predictor on the bench.

Lambda for integrating (averaging level) (P, PI)
Kc=2λ+θk′(λ+θ)2,Ti=2λ+θK_c = \frac{2\lambda + \theta}{k'(\lambda + \theta)^2},\quad T_i = 2\lambda + \theta

For integrating processes such as levels, from the ramp rate k′ (% of span per second, per % of output). λ is the arrest time: how long the level keeps moving after an inflow upset. For a step upset Δ (in % of output) the level swings about k′·Δ·λ/e before it turns round, so choose λ from the room the tank has. A long λ lets the tank absorb the upset and keeps the outflow smooth: averaging level control. P-only: Kc = 1/(k′λ), which leaves an offset that is harmless in a buffer.

Ziegler–Nichols (reaction curve) (P, PI, PID)
Kc=0.9 τKθ,Ti=3.33 θK_c = \frac{0.9\,\tau}{K\theta},\quad T_i = 3.33\,\theta

The 1942 rules, aiming for a quarter-amplitude decay. Aggressive: Ms is often above 2, so the loop rings and is sensitive to model error. Worth knowing because so many loops in the field were tuned this way.

Cohen–Coon (P, PI, PID)
Kc=τKθ(0.9+θ12τ),Ti=θ 30+3θ/τ9+20θ/τK_c = \frac{\tau}{K\theta}\Big(0.9 + \frac{\theta}{12\tau}\Big),\quad T_i = \theta\,\frac{30 + 3\theta/\tau}{9 + 20\theta/\tau}

Like Ziegler–Nichols but corrected for dead time, so it copes better when θ/τ is large. Also aimed at quarter-amplitude decay, so also aggressive.

AMIGO (Åström–Hägglund) (PI, PID)
Kc=0.15K+(0.35−θτ(θ+τ)2)τKθ,Ti=0.35θ+13θτ2τ2+12θτ+7θ2K_c = \frac{0.15}{K} + \Big(0.35 - \frac{\theta\tau}{(\theta+\tau)^2}\Big)\frac{\tau}{K\theta},\quad T_i = 0.35\theta + \frac{13\theta\tau^2}{\tau^2 + 12\theta\tau + 7\theta^2}

Derived by optimizing load rejection with a robustness limit (Ms ≈ 1.4) over a large test batch of processes. A safe modern default when you have no reason to choose a speed.

How to implement it

  • Every cycle: read the measurements, update the observer with the outputs actually applied, compute the QP's linear term, solve, write the first move.
  • The solver here is projected Gauss–Seidel (each planned move in turn, clipped to its limits): a few hundred multiply-adds per sweep, simple enough for SCL. Industrial MPCs use active-set or interior-point solvers.
  • Warm start from last cycle's plan, shifted by one.
  • Manual and bumpless: track the valves and estimate the loads that explain the readings, so auto starts from where the plant is.
  • Watchdog: if the solver doesn't finish or the model is flagged bad, fall back to the PID loops.

Siemens and elsewhere

Siemens
  • S7-1200/1500 (TIA Portal): no standard MPC block. The exported LL_Mpc is the whole controller in SCL with its matrices as LReal arrays, fine for a small problem at a cycle of seconds.
  • SIMATIC PCS 7 has a small MPC in its Advanced Process Library (ModPreCon), and larger ones as an APC option.
  • Larger MPCs usually run on a PC or an edge device, reading the measurements and writing setpoints to PID loops in the PLC over OPC UA.
Elsewhere
  • Industrial MPC packages (in every major DCS vendor's portfolio) handle dozens of variables, economic optimization layered on top, and model identification tools.
  • Embedded and fast MPC (explicit MPC, code-generated solvers) runs in drives and automotive controllers at kHz rates.
  • MPC is where control meets optimization: the same machinery schedules energy and production.

Interactive Controller Sandbox

Test and tune Model predictive control (MPC) live on a process, benchmark against baseline PI, and inspect the resulting code.

Closed-Loop Response: MPC vs Baseline PI

Simulating…
Running test scenarios…
Block Diagram
SP r₁SP r₂MPCmodel · plan M moves · limitsKalman filter: states, loadsu₁u₂FV-901hotFV-902coldProcesseach valve moves bothd: outlet demandy₁ Ty₂ hTT-901LT-902
Generated code
// Loop Lab export: Mixing tank: level and temperature, TIC-901
// TIA Portal SCL (S7-1200 / S7-1500). Generated code: review before use on a real plant.

FUNCTION_BLOCK "LL_Mpc"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   // Two valves (FV-901, FV-902), two measurements (TT-901, LT-902); 5 planned moves each.
   VAR_INPUT
      Setpoint : Real;        // °C
      Setpoint2 : Real;       // m
      Input : Real;           // TT-901, °C
      Input2 : Real;          // LT-902, m
      ManualEnable : Bool;
      ManualValue : Real;     // FV-901, %
      ManualValue2 : Real;    // FV-902, %
   END_VAR
   VAR_OUTPUT
      Output : Real;          // FV-901, %
      Output2 : Real;         // FV-902, %
   END_VAR
   VAR
      // Observer: Xo <- Phio*Xo + Gamo*[last outputs, measurements] (deviations from the design point, %).
      Phio : Array[0..7, 0..7] of LReal := [0.3612344738, -90.01744924, -1.889539758, -0.00368240269, -6.000594659, -107.4589872, 0.6301799797, -0.001147649309,
         6.409502692E-5, 0.4599335207, 0.0001283553346, 9.56607727E-5, 0.0002707577293, -0.6380593304, 4.397311361E-5, 6.581280706E-5,
         0.002797201456, 1.027087475, 0.7738435705, -0.002284639757, -0.6802548647, 0.9701890938, 0.001710609788, -0.00143120289,
         -0.003655693821, -123.5373096, 1.306067685, 0.3580315868, 4.097726142, -142.7169645, -0.001138811028, 0.6291836891,
         0.0005797984224, 0.3831747675, 0.2556781516, -0.0004571756606, 0.1499861313, 0.4502682808, 0.0002359204985, -0.0001903621914,
         2.073257793E-5, 0.2525705098, 8.113400389E-5, 3.080749126E-5, 0.0001994159979, -0.1438620839, 9.75289467E-6, 1.454331315E-5,
         -0.006704030788, -90.9008998, -1.899662019, -0.003734478258, -6.025624136, -108.1006106, 0.9980407882, -0.001164741645,
         -0.003707714229, -124.814866, 1.313406223, -0.009952648079, 4.115226141, -143.6113559, -0.001155882567, 0.9970295058];
      Gamo : Array[0..7, 0..3] of LReal := [0.6301799797, -0.001147649309, 7.899899648, 3.95470503,
         4.397311361E-5, 6.581280706E-5, -0.0003998758029, 0.02345460694,
         0.001710609788, -0.00143120289, 0.893698522, -0.04000838017,
         -0.001138811028, 0.6291836891, -5.41057996, 5.332259928,
         0.0002359204985, -0.0001903621914, 0.5920654293, -0.01669079491,
         9.75289467E-6, 1.454331315E-5, -0.0002810079192, 0.01779547579,
         -0.001959211785, -0.001164741645, 7.93510887, 3.985264163,
         -0.001155882567, -0.002970494215, -5.435460697, 5.375782815];
      // The plan: minimize Z'HZ + 2F'Z with F = Fx*X + Fd*D + Fr*R + Fu*ULast, Lo <= Z <= Hi.
      H : Array[0..9, 0..9] of LReal := [0.2033091847, 0.003159552303, -0.09670496541, 0.003144423342, 0.003269143648, 0.00311904089, 0.003235981972, 0.003088044883, 0.1438089684, 0.1430067609,
         0.003159552303, 0.2062858069, 0.003144423342, -0.09374211196, 0.00311904089, 0.006208282404, 0.003088044883, 0.006145719959, 0.1430067609, 0.2768301628,
         -0.09670496541, 0.003144423342, 0.2032951338, 0.003142286369, -0.0967191257, 0.003127046701, 0.003254873066, 0.003101552905, 0.1450181757, 0.1438187627,
         0.003144423342, -0.09374211196, 0.003142286369, 0.2062566419, 0.003127046701, -0.09377148874, 0.003101552905, 0.006178692238, 0.1438187627, 0.278906859,
         0.003269143648, 0.00311904089, -0.0967191257, 0.003127046701, 0.2032808632, 0.003124798384, -0.09673350751, 0.003109446734, 0.1462298591, 0.1446303474,
         0.00311904089, 0.006208282404, 0.003127046701, -0.09377148874, 0.003124798384, 0.2062270517, 0.003109446734, -0.0938012939, 0.1446303474, 0.2809864707,
         0.003235981972, 0.003088044883, 0.003254873066, 0.003101552905, -0.09673350751, 0.003109446734, 0.2032663693, 0.003107085795, 0.04743660048, 0.1454358311,
         0.003088044883, 0.006145719959, 0.003101552905, 0.006178692238, 0.003109446734, -0.0938012939, 0.003107085795, 0.2061970299, 0.1454358311, 0.1830558063,
         0.1438089684, 0.1430067609, 0.1450181757, 0.1438187627, 0.1462298591, 0.1446303474, 0.04743660048, 0.1454358311, 11.33374731, 10.47398547,
         0.1430067609, 0.2768301628, 0.1438187627, 0.278906859, 0.1446303474, 0.2809864707, 0.1454358311, 0.1830558063, 10.47398547, 21.27666576];
      Fx : Array[0..9, 0..5] of LReal := [0.003311401767, 18.33438169, 0.1572644016, 0.003162669101, 0.002482364429, 0.05466172646,
         0.003162669101, 27.50157253, -0.1347980585, 0.006290660979, -0.002127740939, 0.08199258967,
         0.003290682585, 18.15380486, 0.1557413799, 0.003141657867, 0.001505630135, 0.02010892538,
         0.003141657867, 27.23070728, -0.1334926113, 0.006250514809, -0.001290540115, 0.03016338807,
         0.003260524576, 17.95211181, 0.1538534541, 0.003112842902, 0.0009132108389, 0.007397660233,
         0.003112842902, 26.92816771, -0.1318743892, 0.00619321632, -0.0007827521475, 0.01109649035,
         0.003225021778, 17.7431953, 0.1517556826, 0.003080292713, 0.0005538903726, 0.002721447112,
         0.003080292712, 26.61479295, -0.1300762994, 0.006126644743, -0.0004747631765, 0.004082170668,
         0.1432063277, 804.8993363, 6.215027491, 0.1426013564, 0.0008538187318, 0.001583818828,
         0.1426013564, 1207.349004, -5.32716642, 0.2757947218, -0.0007318446272, 0.002375728242];
      Fd : Array[0..9, 0..1] of LReal := [0.1569183133, 0.1555178224,
         0.1555178224, 0.30172786,
         0.1581440915, 0.156334072,
         0.156334072, 0.3038285924,
         0.1593272328, 0.1571106801,
         0.1571106801, 0.3058490221,
         0.1604603173, 0.1578419614,
         0.1578419614, 0.3077759545,
         11.81624091, 11.05087717,
         11.05087717, 22.29644506];
      Fr : Array[0..9, 0..1] of LReal := [-0.2588325179, -0.4934907999,
         0.2218564439, -0.7402361998,
         -0.2575610799, -0.48996456,
         0.2207666399, -0.73494684,
         -0.2562718421, -0.4864137224,
         0.2196615789, -0.7296205836,
         -0.254964555, -0.4828381156,
         0.2185410471, -0.7242571734,
         -14.40065467, -25.20475441,
         12.34341829, -37.80713161];
      Fu : Array[0..9, 0..1] of LReal := [-0.1, 0.0,
         0.0, -0.1,
         0.0, 0.0,
         0.0, 0.0,
         0.0, 0.0,
         0.0, 0.0,
         0.0, 0.0,
         0.0, 0.0,
         0.0, 0.0,
         0.0, 0.0];
      Lo : Array[0..9] of LReal := [-36.4, -42.46666667, -36.4, -42.46666667, -36.4, -42.46666667, -36.4, -42.46666667,
         -36.4, -42.46666667];
      Hi : Array[0..9] of LReal := [63.6, 57.53333333, 63.6, 57.53333333, 63.6, 57.53333333, 63.6, 57.53333333,
         63.6, 57.53333333];
      // For the bumpless start: steady state for a valve change, and the outputs it gives.
      Xu : Array[0..5, 0..1] of LReal := [1.0, 0.0,
         0.01998001998, 0.02997002997,
         0.3496503497, -0.2997002997,
         0.0, 1.0,
         0.3496503497, -0.2997002997,
         0.01998001998, 0.02997002997];
      Cy : Array[0..1, 0..5] of LReal := [0.0, 0.0, 0.0, 0.0, 1.0, 0.0,
         0.0, 0.0, 0.0, 0.0, 0.0, 50.0];
      U0 : Array[0..1] of LReal := [36.4, 42.46666667];
      Y0 : Array[0..1] of LReal := [35.0, 50.0];
      Sweeps : Int := 200;
      Xo : Array[0..7] of LReal;
      XNew : Array[0..7] of LReal;
      Z : Array[0..9] of LReal;
      F : Array[0..9] of LReal;
      W : Array[0..3] of LReal;
      ULast : Array[0..1] of LReal;
      Started : Bool;
   END_VAR
   VAR_TEMP
      i : Int;
      j : Int;
      s : Int;
      acc : LReal;
      zi : LReal;
      r1 : LReal;
      r2 : LReal;
      cx : Array[0..1, 0..1] of LReal;
      det : LReal;
      v0 : LReal;
      v1 : LReal;
   END_VAR

BEGIN
    #W[2] := (#Input - 0.0) / 100.0 * 100.0 - #Y0[0];
    #W[3] := (#Input2 - 0.0) / 2.0 * 100.0 - #Y0[1];
    #r1 := (#Setpoint - 0.0) / 100.0 * 100.0 - #Y0[0];
    #r2 := (#Setpoint2 - 0.0) / 2.0 * 100.0 - #Y0[1];
    IF #ManualEnable OR NOT #Started THEN
        // Track: a steady state with the valves where they are, plus the loads that explain the readings.
        #ULast[0] := #ManualValue - #U0[0];
        #ULast[1] := #ManualValue2 - #U0[1];
        FOR #i := 0 TO 1 DO
            FOR #j := 0 TO 1 DO
                #acc := 0.0;
                FOR #s := 0 TO 5 DO
                    #acc := #acc + #Cy[#i, #s] * #Xu[#s, #j];
                END_FOR;
                #cx[#i, #j] := #acc;
            END_FOR;
        END_FOR;
        #det := #cx[0, 0] * #cx[1, 1] - #cx[0, 1] * #cx[1, 0];
        #v0 := (#cx[1, 1] * #W[2] - #cx[0, 1] * #W[3]) / #det;
        #v1 := (#cx[0, 0] * #W[3] - #cx[1, 0] * #W[2]) / #det;
        FOR #i := 0 TO 5 DO
            #Xo[#i] := #Xu[#i, 0] * #v0 + #Xu[#i, 1] * #v1;
        END_FOR;
        #Xo[6] := #v0 - #ULast[0];
        #Xo[7] := #v1 - #ULast[1];
        FOR #i := 0 TO 9 DO
            #Z[#i] := #ULast[#i MOD 2];
        END_FOR;
        #Started := TRUE;
    ELSE
        #W[0] := #ULast[0];
        #W[1] := #ULast[1];
        FOR #i := 0 TO 7 DO
            #acc := 0.0;
            FOR #j := 0 TO 7 DO
                #acc := #acc + #Phio[#i, #j] * #Xo[#j];
            END_FOR;
            FOR #j := 0 TO 3 DO
                #acc := #acc + #Gamo[#i, #j] * #W[#j];
            END_FOR;
            #XNew[#i] := #acc;
        END_FOR;
        FOR #i := 0 TO 7 DO
            #Xo[#i] := #XNew[#i];
        END_FOR;
        FOR #i := 0 TO 9 DO
            #acc := #Fd[#i, 0] * #Xo[6] + #Fd[#i, 1] * #Xo[7]
                + #Fr[#i, 0] * #r1 + #Fr[#i, 1] * #r2
                + #Fu[#i, 0] * #ULast[0] + #Fu[#i, 1] * #ULast[1];
            FOR #j := 0 TO 5 DO
                #acc := #acc + #Fx[#i, #j] * #Xo[#j];
            END_FOR;
            #F[#i] := #acc;
        END_FOR;
        // Warm start: last cycle's plan, one cycle on.
        FOR #i := 0 TO 7 DO
            #Z[#i] := #Z[#i + 2];
        END_FOR;
        // Projected Gauss-Seidel: minimize over one move at a time, clipped to the valve limits.
        FOR #s := 1 TO #Sweeps DO
            FOR #i := 0 TO 9 DO
                #acc := #F[#i];
                FOR #j := 0 TO 9 DO
                    #acc := #acc + #H[#i, #j] * #Z[#j];
                END_FOR;
                #zi := #Z[#i] - #acc / #H[#i, #i];
                #Z[#i] := LIMIT(MN := #Lo[#i], IN := #zi, MX := #Hi[#i]);
            END_FOR;
        END_FOR;
        #ULast[0] := #Z[0];
        #ULast[1] := #Z[1];
    END_IF;
    #Output := LREAL_TO_REAL(LIMIT(MN := 0.0, IN := #U0[0] + #ULast[0], MX := 100.0));
    #Output2 := LREAL_TO_REAL(LIMIT(MN := 0.0, IN := #U0[1] + #ULast[1], MX := 100.0));
END_FUNCTION_BLOCK

// OB30 "Cyclic interrupt", cycle time 2.0 s (2000 ms).
// "TT-901" is the scaled input (NORM_X/SCALE_X from the analog card), "FV-901" the output to scale back to 0-27648.
// MPC: one block plans both valves every cycle. In manual it tracks both, so auto is bumpless.
"TIC_901_DB"(Setpoint := "SP", Setpoint2 := "SP2", Input := "TT-901", Input2 := "LT-902", ManualEnable := "Manual", ManualValue := "ManualOut", ManualValue2 := "ManualOut2");
"FV-901" := "TIC_901_DB".Output;
"FV-902" := "TIC_901_DB".Output2;
// Its cycle time is part of the design: the block's matrices are for 2.0 s.