Model Predictive Control (MPC)
Model predictive control (MPC) plans a short sequence of actions with a model, applies only the first, and replans each step. Covers parts, uses, and code.
Model predictive control (MPC) is a control method that uses a model of a system to predict its behavior over a short future horizon, chooses the sequence of actions that best meets a goal while respecting constraints, applies only the first action, and then repeats the whole process at the next time step with fresh measurements. This plan, act, and replan cycle is called receding-horizon control, and it lets the controller anticipate what is coming instead of reacting only to the current error. MPC grew out of oil refining and chemical processing in the late 1970s, where Shell's Dynamic Matrix Control was among the first industrial versions, and it now steers robots, autonomous vehicles, and building climate systems.
The Parts of an MPC Controller#
Every MPC controller combines four pieces:
- Dynamics model: Predicts the next state from the current state and an action, such as how a car's position and heading change for a given steering angle. It can come from physics equations, from system identification on logged data, or from a neural network trained on recorded trajectories.
- Cost function: Scores a predicted trajectory, usually penalizing distance from a target, energy use, and abrupt changes in the controls.
- Constraints: Limits every plan must satisfy, such as maximum motor torque, speed limits, and keep-out zones around obstacles. Handling constraints explicitly is MPC's main advantage over classic PID control, which reacts to errors only after they appear and cannot look ahead to a limit.
- Optimizer: Searches for the best action sequence within the horizon at every control step.
The model decides how hard the optimization is. A linear model with a quadratic cost becomes a quadratic program that dedicated solvers finish in milliseconds or less, while nonlinear models need nonlinear MPC solvers, usually paired with an extended Kalman filter to estimate the state, or sampling-based search. At every control step, the optimizer runs an inner loop of predicting and improving candidate plans, and the controller runs an outer loop that executes one action and starts again from the newly measured state:
The horizon length is the central tuning choice. A longer horizon lets a vehicle brake early for a sharp turn, but each extra step adds computation and compounds the model's prediction error, and the whole optimization must finish within one control period.
Receding Horizon in Code#
The snippet below controls a cart that must travel to position 5 and stop, with acceleration limited to plus or minus 1. At each step it samples 500 random acceleration plans, predicts every plan's trajectory with the cart's dynamics, executes the first action of the cheapest plan, and replans from the new state:
import numpy as np
rng = np.random.default_rng(0)
position, velocity, target = 0.0, 0.0, 5.0
dt, horizon = 0.1, 30
for _ in range(100):
# Sample 500 candidate plans of accelerations within the actuator limit of +/-1
plans = rng.uniform(-1, 1, size=(500, horizon))
# Predict every plan's trajectory with the cart's dynamics model
v = velocity + dt * plans.cumsum(axis=1)
p = position + dt * v.cumsum(axis=1)
# Cost: distance from the target at each step plus speed at the end of the horizon
cost = ((p - target) ** 2).sum(axis=1) + 10 * v[:, -1] ** 2
# Apply only the first action of the cheapest plan, then replan from the new state
velocity += dt * plans[cost.argmin(), 0]
position += dt * velocity
print(f"position: {position:.2f}, velocity: {velocity:.2f}")After 10 simulated seconds the cart sits at position 5.04 with a velocity of 0.04. No single random plan is good on its own, yet executing one step and replanning from the measured state corrects the errors continuously. This approach, called random shooting, is the simplest member of the sampling-based MPC family, which also includes the cross-entropy method and model predictive path integral (MPPI) control, which Georgia Tech researchers used to race a one-fifth-scale rally car around a dirt track.
MPC in Plants, Robots, Cars, and Buildings#
- Process industries: Refineries, chemical plants, and power stations use MPC to hold temperatures, pressures, and product quality close to the limits that maximize throughput, the setting where the method began.
- Legged robots: MIT's Cheetah 3 ran and balanced using a convex MPC that recomputed its ground reaction forces many times per second, and MIT later released that controller as open-source software for its Mini Cheetah.
- Driving: Path-tracking controllers in self-driving cars use MPC to follow a planned trajectory smoothly within steering, acceleration, and comfort limits.
- Buildings: Heating and cooling controllers predict room temperatures hours ahead from weather and occupancy forecasts, pre-cooling or pre-heating when energy is cheapest.
MPC vs. Related Approaches#
- PID control: A PID controller computes its output from the current error, its integral, and its rate of change. It is simple and cheap, but it has no model of the future and no notion of constraints.
- Linear quadratic regulator (LQR): LQR finds the optimal feedback law for a linear system with a quadratic cost over an infinite horizon, in closed form. MPC solves a finite-horizon version online, which is what lets it enforce constraints and handle nonlinear models.
- Reinforcement learning: RL learns a policy from experience before deployment, after which choosing an action takes a single fast forward pass. MPC solves a fresh optimization online using an explicit model.
The two increasingly merge. Model-based deep reinforcement learning learns a world model and plans through it with MPC, and methods such as TD-MPC2 plan in a learned latent space while a learned value function scores what happens beyond the end of the short horizon. Learned dynamics models are a form of predictive modeling, and their accuracy over the horizon sets how far ahead such a controller can usefully plan.
Perception for MPC#
An MPC controller plans only as well as it perceives, and for a physical AI system such as a robot moving among people, its state must include where those people are and where they are heading. A camera running Ultralytics YOLO26 in tracking mode supplies a bounding box and a persistent track ID for each person in every frame. Combined with depth estimation or camera calibration and a Kalman filter, those tracks become the obstacle positions and velocities that the controller's constraints keep clear of.










