Close Menu
    Trending
    • Creating AI that matters | MIT News
    • Scaling Recommender Transformers to a Billion Parameters
    • Hidden Gems in NumPy: 7 Functions Every Data Scientist Should Know
    • Is RAG Dead? The Rise of Context Engineering and Semantic Layers for Agentic AI
    • ChatGPT Gets More Personal. Is Society Ready for It?
    • Why the Future Is Human + Machine
    • Why AI Is Widening the Gap Between Top Talent and Everyone Else
    • Implementing the Fourier Transform Numerically in Python: A Step-by-Step Guide
    ProfitlyAI
    • Home
    • Latest News
    • AI Technology
    • Latest AI Innovations
    • AI Tools & Technologies
    • Artificial Intelligence
    ProfitlyAI
    Home » Model Predictive-Control Basics | Towards Data Science
    Artificial Intelligence

    Model Predictive-Control Basics | Towards Data Science

    ProfitlyAIBy ProfitlyAIAugust 12, 2025No Comments10 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Fast

    On this article we’ll:

    1. Introduction

    Mannequin predictive management (MPC) is a well-liked suggestions management methodology the place a finite-horizon optimum management drawback (OCP) is iteratively solved with an up to date measured state on every iteration.

    The MPC loop. Picture by writer.

    Within the OCP one makes use of a mannequin of the plant to search out the optimum open loop management over the finite horizon thought-about. As a result of the mannequin can not seize the true plant’s behaviour 100%, and since in the true world the system is subjected to noise and disturbances, one solely applies the primary portion of the optimum open loop management, and commonly measures the state to re-solve the OCP. This closes the loop and creates a suggestions.

    The maths behind it’s comparatively easy and intuitive (particularly when in comparison with issues like sturdy management) and it’s simple to code up an MPC controller. Different execs are that it will possibly successfully deal with exhausting and gentle constraints on the state and management (exhausting constraints are strict, whereas gentle constraints are enforced by way of penalisation in the fee perform) and it will possibly typically be used on nonlinear methods with nonconvex constraints (relying on how nasty these are in fact!). The one con is that it is advisable clear up a optimization issues “on-line” in actual time, which could be a drawback in the event you’re controlling a quick system or have restricted computing sources.

    1.2 Working Instance

    All through the article I’ll think about the double integrator with a zero-order maintain management (i.e. a piecewise fixed management) because the working instance within the code. The continual time system reads:

    [
    begin{align*}
    dot x _1(t) & = x_2(t),
    dot x _2(t) & = u(t),
    end{align*}
    ]

    with (t in mathbb{R} ) denoting time. Right here ( x_1(t) in mathbb{R} ) is the place whereas ( x_2(t) in mathbb{R} ) is the velocity. You may consider this method as a 1kg block sliding on a frictionless desk, with (u(t)) the utilized pressure.

    Working instance: the double integrator. Picture by writer.

    If we constrain the management to be piecewise fixed over intervals of size 0.1 seconds, we get the discrete-time system:

    [
    begin{align*}
    x_{k+1} &= A x_k + Bu_k,
    end{align*}
    ]

    with (okay in mathbb{Z}), the place,

    [
    begin{align*}
    A :=
    left(
    begin{array}{cc}
    1 & 0.1
    0 & 1
    end{array}
    right), ,,
    B :=
    left(
    begin{array}{c}
    0.05
    1
    end{array}
    right)
    end{align*}
    ]

    and (x_k in mathbb{R}^2 ), (u_k in mathbb{R} ). (Notice that I exploit (x_k) and (u_k) to check with the discrete-time system’s state and management to make the notation neater in the remainder of the article.)

    You need to use the scipy packages’s cont2discrete perform to get this discrete time system, as follows:

    import numpy as np
    from scipy.sign import cont2discrete
    
    A = np.array([[0, 1],[0, 0]])
    B = np.array([[0],[1]])
    C = np.array([[1, 0],[0, 1]])
    D = np.array([[0, 0],[0, 0]])
    dt = 0.1 # in seconds
    discrete_system = cont2discrete((A, B, C, D), dt, methodology='zoh')
    A_discrete, B_discrete, *_ = discrete_system

    2. Optimum Management Downside

    We’ll think about the next discrete-time optimum management drawback (OCP):

    [
    begin{equation}
    {mathrm{OCP}(bar x):}begin{cases}
    minlimits_{mathbf{u},mathbf{x}} quad & sum_{k=0}^{K-1} left(x_k^{top}Qx_k + u_k^top R u_k right)+ x_{K}^top Q_K x_{K}
    mathrm{subject to:}quad & x_{k+1} = Ax_k + Bu_k, &k in[0:K-1], & dots(1)
    quad & x_0 = bar x , & & dots(2)
    quad & x_kin [-1,1]occasions(-infty,infty),& okay in[1:K], & dots(3)
    quad & u_kin [-1,1],& okay in[0:K-1], & dots(4)
    finish{instances}
    finish{equation}
    ]

    the place,

    • (Kinmathbb{R}_{geq 0}) denotes the finite horizon over which we clear up the OCP,
    • (kinmathbb{Z}) denotes the discrete time step,
    • ([p:q]), with (p,qinmathbb{Z}), denotes the set of integers ({p,p+1,…,q}),
    • (bar x in mathbb{R}^2 ) denotes the preliminary situation of the dynamical system,
    • (x_kinmathbb{R}^2 ) denotes the state at step (okay),
    • (uinmathbb{R}) denotes the management at step (okay),
    • (Qinmathbb{R}^{2times 2}), (Rinmathbb{R}) and (Q_K in mathbb{R}^{2times 2}) are sq. optimistic particular matrices that specify the fee perform ((R) is scalar right here as a result of (u) is scalar).

    Furthermore, we’ll let,

    • (mathbf{u}:= (u_0,u_1,…,u_{Ok−1})inmathbb{R}^Ok ) denote the management sequence,
    • (mathbf{x}:= (x_0,x_1,…,x_{Ok})inmathbb{R}^{2(Ok+1)} ) denote the state sequence.

    To be rigorous, we’ll say that the pair ((mathbf{u}^{*}, mathbf{x}^{*})inmathbb{R}^Ok occasions mathbb{R}^{2(Ok+1)}) is a answer to ( mathrm{OCP}(bar{x})) supplied that it minimises the fee perform over all admissible pairs, that’s,

    [
    begin{equation*}
    J(mathbf{u}^{*}, mathbf{x}^{*}) leq J(mathbf{u}, mathbf{x}),quad forall (mathbf{u},mathbf{x})in Omega
    end{equation*}
    ]

    the place (J:mathbb{R}^Ok occasions mathbb{R}^{2(Ok+1)}rightarrow mathbb{R}_{geq 0} ) is,

    [
    begin{equation*}
    J(mathbf{u},mathbf{x}) :=left( sum_{k=0}^{K-1 }x_k^top Q x_k + u_k^top R u_k right) + x_K^top Q_K x_K
    end{equation*}
    ]

    and (Omega ) denotes all admissible pairs,

    [
    Omega :={(mathbf{u},mathbf{x})in mathbb{R}^{K}times mathbb{R}^{2(K+1)} : (1)-(4),, mathrm{hold}}.
    ]

    Thus, the optimum management drawback is to discover a management and state sequence, (( mathbf{u}^{*},mathbf{x}^{*})), that minimises the fee perform, topic to the dynamics, (f), in addition to constraints on the state and management, (x_kin[-1,1]occasions(-infty,infty)), (u_k in [-1,1] ), for all (okay). The price perform is important to the controller’s efficiency. Not solely within the sense of making certain the controller behaves properly (for instance, to forestall erratic alerts) but in addition in specifying the equilibrium level the closed loop state will settle at. Extra on this in Part 4.

    Notice how (mathrm{OCP}(bar x) ) is parametrised with respect to the preliminary state, (bar x ). This comes from the elemental concept behind MPC: that the optimum management drawback is iteratively solved with an up to date measured state.

    2.1 Coding an OCP solver

    CasADi’s opti stack makes it very easy to arrange and clear up the OCP.

    First, some preliminaries:

    from casadi import *
    
    n = 2 # state dimension
    m = 1 # management dimension
    Ok = 100 # prediction horizon
    
    # an arbitrary preliminary state
    x_bar = np.array([[0.5],[0.5]]) # 2 x 1 vector
    
    # Linear value matrices (we'll simply use identities)
    Q = np.array([[1. , 0],
                [0. , 1. ]])
    R = np.array([[1]])
    Q_K = Q
    
    # Constraints for all okay
    u_max = 1
    x_1_max = 1
    x_1_min = -1

    Now we outline the issue’s choice variables:

    opti = Opti()
    
    x_tot = opti.variable(n, Ok+1)  # State trajectory
    u_tot = opti.variable(m, Ok)    # Management trajectory

    Subsequent, we impose the dynamic constraints and arrange the fee perform:

    # Specify the preliminary situation
    opti.subject_to(x_tot[:, 0] == x_bar)
    
    value = 0
    for okay in vary(Ok):
        # add dynamic constraints
        x_tot_next = get_x_next_linear(x_tot[:, k], u_tot[:, k])
        opti.subject_to(x_tot[:, k+1] == x_tot_next)
    
        # add to the fee
        value += mtimes([x_tot[:,k].T, Q, x_tot[:,k]]) +      
                               mtimes([u_tot[:,k].T, R, u_tot[:,k]])
    
    # terminal value
    value += mtimes([x_tot[:,K].T, Q_K, x_tot[:,K]])
    def get_x_next_linear(x, u):
        # Linear system
        A = np.array([[1. , 0.1],
                    [0. , 1. ]])
        B = np.array([[0.005],
                      [0.1  ]])
        
        return mtimes(A, x) + mtimes(B, u)

    The code mtimes([x_tot[:,k].T, Q, x_tot[:,k]]) implements matrix multiplication, (x_k^{prime} Q x_k ).

    We now add the management and state constraints,

    # constrain the management
    opti.subject_to(opti.bounded(-u_max, u_tot, u_max))
    
    # constrain the place solely
    opti.subject_to(opti.bounded(x_1_min, x_tot[0,:], x_1_max))

    and clear up:

    # Say we wish to minimise the fee and specify the solver (ipopt)
    opts = {"ipopt.print_level": 0, "print_time": 0}
    opti.decrease(value)
    opti.solver("ipopt", opts)
        
    answer = opti.clear up()
    
    # Get answer
    x_opt = answer.worth(x_tot)
    u_opt = answer.worth(u_tot)

    We are able to plot the answer with the repo’s plot_solution() perform.

    from MPC_tutorial import plot_solution
    
    plot_solution(x_opt, u_opt.reshape(1,-1)) # should reshape u_opt to (1,Ok)
    OCP answer (open loop). Picture by writer.

    3. Mannequin Predictive Management

    The answer to ( mathrm{OCP}(bar x) ), ( (mathbf{x}^{*},mathbf{u}^{*}) ), for a given (bar x), offers us with an open loop management, ( mathbf{u}^{*} ). We now shut the loop by iteratively fixing ( mathrm{OCP}(bar x) ) and updating the preliminary state (that is the MPC algorithm).

    [
    begin{aligned}
    &textbf{Input:} quad mathbf{x}^{mathrm{init}}inmathbb{R}^2
    &quad bar x gets mathbf{x}^{mathrm{init}}
    &textbf{for } k in [0:infty) textbf{:}
    &quad (mathbf{x}^{*}, mathbf{u}^{*}) gets argmin mathrm{OCP}(bar x)
    &quad mathrm{apply} u_0^{*} mathrm{ to the system}
    &quad bar x gets mathrm{measured state at } k+1
    &textbf{end for}
    end{aligned}
    ]

    3.1 Coding the MPC algorithm

    The remainder is kind of simple. First, I’ll put all our earlier code in a perform:

    def solve_OCP(x_bar, Ok):
        ....
    
        return x_opt, u_opt

    Notice that I’ve parametrised the perform with the preliminary state, (bar x), in addition to the prediction horizon, (Ok). The MPC loop is then carried out with:

    x_init = np.array([[0.5],[0.5]]) # 2 x 1 vector
    Ok = 10
    number_of_iterations = 150 # should in fact be finite!
    
    # matrices of zeros with the proper sizes to retailer the closed loop
    u_cl = np.zeros((1, number_of_iterations))
    x_cl = np.zeros((2, number_of_iterations + 1))
    
    x_cl[:, 0] = x_init[:, 0]
    
    x_bar = x_init
    for i in vary(number_of_iterations):
        _, u_opt = solve_OCP(x_bar, Ok)
        u_opt_first_element = u_opt[0]
    
        # save closed loop x and u
        u_cl[:, i] = u_opt_first_element
        x_cl[:, i+1] = np.squeeze(get_x_next_linear(x_bar,   
                                                    u_opt_first_element))
    
        # replace preliminary state
        x_bar = get_x_next_linear(x_bar, u_opt_first_element)

    Once more, we will plot the closed loop answer.

    plot_solution(x_cl, u_cl)
    MPC closed loop. Picture by writer.

    Notice that I’ve “measured” the plant’s state through the use of the perform get_x_next_linear(). In different phrases, I’ve assumed that our mannequin is 100% right.

    Right here’s a plot of the closed loop from a bunch of preliminary states.

    MPC closed loop from numerous preliminary states. Picture by writer.

    4. Additional matters

    4.1 Stability and Recursive Feasibility

    Two of a very powerful elements of an MPC controller are recursive feasibility of the iteratively invoked OCP and stability of the closed loop. In different phrases, if I’ve solved the OCP at time (okay), will there exist an answer to the OCP at time (okay+1)? If there exists an answer to the OCP at each time step, will the closed-loop state asymptotically settle at an equilibrium level (i.e. will or not it’s steady)?

    Guaranteeing that an MPC controller reveals these two properties entails rigorously designing the fee perform and constraints, and selecting a protracted sufficient prediction horizon. Going again to our instance, recall that the matrices in the fee perform have been merely chosen to be:

    [
    Q = left( begin{array}{cc}
    1 & 0
    0 & 1
    end{array}
    right),, Q_K = left( begin{array}{cc}
    1 & 0
    0 & 1
    end{array}
    right),, R = 1.
    ]

    In different phrases, the OCP penalises the space of the state to the origin and thus drives it there. As you’ll be able to most likely guess, if the prediction horizon, (Ok), may be very brief and the preliminary state is positioned very near the constraints at (x_1=pm 1), the OCP will discover options with inadequate “foresight” and the issue shall be infeasible at some future iteration of the MPC loop. (You may as well experiment with this by making (Ok) small within the code.)

    4.2 Some Different Subjects

    MPC is an lively area of analysis and there are various attention-grabbing matters to discover.

    What if the total state can’t be measured? This pertains to observability and output MPC. What if I don’t care about asymptotic stability? This (typically) has to do with financial MPC. How do I make the controller sturdy to noise and disturbances? There are just a few methods to cope with this, with tube MPC most likely being the best-known.

    Future articles may deal with a few of these matters.

    5. Additional studying

    Listed here are some commonplace and excellent textbooks on MPC.

    [1] Grüne, L., & Pannek, J. (2016). Nonlinear mannequin predictive management.

    [2] Rawlings, J. B., Mayne, D. Q., & Diehl, M. (2020). Mannequin predictive management: principle, computation, and design.

    [3] Kouvaritakis, B., & Cannon, M. (2016). Mannequin predictive management: Basic, sturdy and stochastic.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleFine-Tune Your Topic Modeling Workflow with BERTopic
    Next Article Reducing Time to Value for Data Science Projects: Part 4
    ProfitlyAI
    • Website

    Related Posts

    Artificial Intelligence

    Creating AI that matters | MIT News

    October 21, 2025
    Artificial Intelligence

    Scaling Recommender Transformers to a Billion Parameters

    October 21, 2025
    Artificial Intelligence

    Hidden Gems in NumPy: 7 Functions Every Data Scientist Should Know

    October 21, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    The Good-Enough Truth | Towards Data Science

    April 17, 2025

    How do AI models generate videos?

    September 12, 2025

    Can LangExtract Turn Messy Clinical Notes into Structured Data?

    August 19, 2025

    Where are we with Shor’s algorithm?

    July 7, 2025

    How a Research Lab Made Entirely of LLM Agents Developed Molecules That Can Block a Virus

    August 5, 2025
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    Most Popular

    Meta MoCha genererar talande animerade karaktärer

    April 7, 2025

    Googles framtidsvision är att Gemini utför googling åt användarna

    May 23, 2025

    Smarter Model Tuning: An AI Agent with LangGraph + Streamlit That Boosts ML Performance

    August 20, 2025
    Our Picks

    Creating AI that matters | MIT News

    October 21, 2025

    Scaling Recommender Transformers to a Billion Parameters

    October 21, 2025

    Hidden Gems in NumPy: 7 Functions Every Data Scientist Should Know

    October 21, 2025
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2025 ProfitlyAI All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.