Inferensys

Glossary

Cost Function (Objective Function)

In Model Predictive Control (MPC), a cost function is a mathematical expression that quantifies the control performance to be minimized, such as tracking error, control effort, or economic cost.
ML engineer managing model versions on laptop, version history visible, technical Git-like workflow.
MODEL PREDICTIVE CONTROL (MPC)

What is a Cost Function (Objective Function)?

In Model Predictive Control (MPC), the cost function is the mathematical core that defines the controller's goal, quantifying performance to be minimized at each optimization step.

A cost function (or objective function) is a scalar mathematical expression that quantifies the performance of a control system, defining the quantity to be minimized by the Model Predictive Control (MPC) optimizer over a finite prediction horizon. It typically penalizes deviations from a desired reference (tracking error), excessive control effort, and other economic or operational objectives, translating high-level goals into a solvable Optimal Control Problem (OCP).

In practice, the function is often formulated as a quadratic sum of weighted terms, balancing competing priorities like accuracy and actuator wear. The MPC solver computes control inputs that minimize this future predicted cost while respecting state constraints and input constraints. This explicit optimization of a defined metric is what distinguishes MPC from simpler feedback controllers, enabling proactive, performance-driven behavior.

MATHEMATICAL FORMULATION

Key Components of an MPC Cost Function

The cost function, or objective function, is the mathematical core of a Model Predictive Controller. It quantifies the performance goals—such as tracking accuracy, energy use, and actuator wear—that the controller must minimize at each time step.

01

Tracking Error (Reference Deviation)

This is the primary term that penalizes deviation from a desired trajectory or setpoint. It is typically formulated as a quadratic function of the difference between the predicted system states or outputs and their target values over the prediction horizon.

  • Mathematical Form: Often expressed as (x - x_ref)^T * Q * (x - x_ref), where Q is a positive semi-definite weighting matrix.
  • Purpose: Drives the system to follow the commanded path as closely as possible.
  • Design Choice: The matrix Q allows the engineer to prioritize which states (e.g., position vs. velocity) are most critical to track accurately.
02

Control Effort (Input Cost)

This term penalizes the magnitude of the control inputs (e.g., motor torque, steering angle) applied to the system. Minimizing control effort reduces energy consumption, actuator wear, and aggressive, potentially destabilizing actions.

  • Mathematical Form: Typically u^T * R * u, where R is a positive definite weighting matrix.
  • Purpose: Promotes smooth, efficient control actions and prevents actuator saturation.
  • Trade-off: Tuned against the tracking error term. A high R weight results in slower, more conservative control, while a low weight allows for more aggressive tracking.
03

Terminal Cost

An additional cost term evaluated specifically at the final state of the prediction horizon. It is a crucial theoretical tool for ensuring closed-loop stability.

  • Mathematical Form: Often x_N^T * P * x_N, where P is computed as the solution to an Algebraic Riccati Equation (ARE) related to the infinite-horizon Linear Quadratic Regulator (LQR).
  • Purpose: Approximates the cost-to-go beyond the finite prediction horizon, encouraging the predicted terminal state to be near a desirable equilibrium.
  • Stability: When combined with a terminal constraint, it can provide formal guarantees of asymptotic stability for the MPC law.
04

Constraint Violation Penalty (Slack Variables)

A mechanism to handle soft constraints. When hard constraints (e.g., position limits) might make the optimization problem infeasible, slack variables are introduced and penalized in the cost function.

  • How it works: A slack variable ε absorbs constraint violations. The cost function includes a term like ρ * ε^2, where ρ is a large, tunable penalty weight.
  • Purpose: Ensures the online optimizer always has a feasible solution, improving controller robustness to unexpected disturbances or infeasible commands.
  • Use Case: Essential for practical deployment where temporary, minor constraint breaches are preferable to controller failure.
05

Rate of Change Penalty

Penalizes large changes in control inputs between consecutive time steps. This term directly influences the smoothness of the actuation signal.

  • Mathematical Form: Δu^T * S * Δu, where Δu = u_k - u_{k-1} and S is a weighting matrix.
  • Purpose: Reduces mechanical stress on actuators, improves passenger comfort in vehicles, and filters out high-frequency noise from the control signal.
  • Practical Impact: A well-tuned rate penalty prevents 'chattering'—rapid, oscillatory control actions that waste energy and cause wear.
06

Economic Objective

In Economic MPC (EMPC), the cost function moves beyond traditional regulation to directly encode a process economics or utility goal.

  • Examples: Minimizing energy consumption in a building's HVAC system, maximizing throughput in a chemical reactor, or minimizing fuel use in a vehicle.
  • Key Difference: The cost function is not necessarily positive definite around a setpoint. The controller seeks a dynamically optimal operating point, which may involve transient excursions to improve long-term economic performance.
  • Application: Common in process industries and energy systems where operational profit, not just setpoint tracking, is the primary goal.
CORE MECHANISM

How the Cost Function Works in MPC

The cost function is the mathematical heart of Model Predictive Control (MPC), quantifying the performance objective that the controller strives to minimize at every time step.

In Model Predictive Control (MPC), the cost function (or objective function) is a scalar mathematical expression that quantifies the controller's performance over a finite prediction horizon. It is minimized online at each control step to compute an optimal sequence of future control inputs. The function typically penalizes tracking error (deviation from a reference) and control effort (actuator movement), often using a quadratic form for computational efficiency. This transforms the control problem into a constrained optimization problem.

The design of the cost function directly dictates the closed-loop behavior. A quadratic cost on state and input error leads to a smooth, energy-efficient response. Engineers can add terms for economic objectives, like minimizing fuel consumption, or use slack variables to soften constraints. The optimization solver, such as a Quadratic Programming (QP) solver for linear MPC, finds the control sequence that minimizes this cost while respecting the system's dynamic model and state and input constraints. Only the first control action is executed before the receding horizon principle resets the calculation.

MODEL PREDICTIVE CONTROL (MPC)

Common Cost Function Types and Examples

In Model Predictive Control, the cost function quantifies the performance objective to be minimized. Different formulations prioritize tracking, energy use, or economic goals.

01

Quadratic Tracking Cost

The most common form in Linear MPC, penalizing the squared deviation of predicted states and inputs from their desired references. It leads to a convex Quadratic Programming (QP) problem.

  • Mathematical Form: ( J = \sum_{k=0}^{N-1} (x_k - x_{ref})^T Q (x_k - x_{ref}) + (u_k - u_{ref})^T R (u_k - u_{ref}) + (x_N - x_{ref})^T P (x_N - x_{ref}) )
  • Weights: ( Q ), ( R ), and ( P ) are positive (semi-)definite weighting matrices that tune the trade-off between state error and control effort.
  • Example: In autonomous vehicle path following, ( x ) represents lateral deviation and heading error, weighted by ( Q ), while ( u ) represents steering angle, weighted by ( R ) to ensure smooth driving.
02

Economic Cost Function

Directly encodes operational economics, such as profit maximization or energy cost minimization, rather than traditional reference tracking. This is central to Economic MPC.

  • Objective: Minimize a stage cost ( \ell(x_k, u_k) ) that represents instantaneous economic loss (e.g., $/hr).
  • Key Challenge: Stability is not inherent and must be enforced via additional constraints or a terminal cost, unlike tracking MPC.
  • Real-World Example: In a chemical reactor, the cost function could be ( J = \sum \text{(Price of Feedstock - Value of Product + Cost of Utilities)} ), optimized in real-time against a dynamic process model.
03

Terminal Cost & Constraint

Design elements added to the end of the prediction horizon to guarantee closed-loop stability, especially with short horizons.

  • Terminal Cost: An additional term ( V_f(x_N) ) (e.g., from a Lyapunov function or Algebraic Riccati Equation) that penalizes the final state.
  • Terminal Constraint: Requires the predicted state at the end of the horizon, ( x_N ), to lie within a terminal set ( \mathbb{X}_f ).
  • Purpose: These components ensure the optimization approximates an infinite-horizon problem, providing a stability guarantee for the Receding Horizon Control law.
04

Handling Constraints: Slack Variables

A method to implement soft constraints by adding slack variables to the cost function, preventing infeasibility of the Optimal Control Problem (OCP).

  • Mechanism: A constraint like ( y_k \leq y_{max} ) is reformulated as ( y_k \leq y_{max} + \epsilon_k ) with ( \epsilon_k \geq 0 ). A large penalty weight ( \rho ) on ( \epsilon_k ) is added to the cost: ( J = ... + \rho \sum \epsilon_k^2 ).
  • Use Case: Critical for output constraints that may become temporarily unavoidable due to large disturbances, allowing a small, penalized violation instead of controller failure.
  • Trade-off: The weight ( \rho ) creates a tunable balance between constraint satisfaction and primary objective performance.
05

Nonlinear & Non-Quadratic Costs

Used in Nonlinear MPC (NMPC) for problems where the performance metric is not adequately captured by a quadratic form.

  • Formulation: ( J = \sum \ell(x_k, u_k) ), where ( \ell(\cdot) ) is a general nonlinear function.
  • Common Types:
    • Minimum-Time: ( \ell = 1 ), minimizing the number of steps to reach a goal.
    • Obstacle Avoidance: High cost assigned to states inside obstacle regions.
    • Fuel-Optimal (L1 Norm): ( \ell = |u_k| ), minimizing absolute control effort, common in aerospace.
  • Solver Impact: Requires more complex Nonlinear Programming (NLP) solvers like Sequential Quadratic Programming (SQP) or Interior-Point Methods.
06

Cost Function in Robust & Stochastic MPC

Modified formulations that account for uncertainty in the dynamic model or disturbances.

  • Worst-Case (Robust MPC): The cost is minimized for the worst-case realization of uncertainty within a bounded set (min-max optimization).
  • Expected Value (Stochastic MPC): The cost is the expectation ( \mathbb{E}[J] ) taken over a distribution of possible disturbances or model parameters.
  • Chance Constraints: System constraints are required to hold with a probability ( 1 - \alpha ). The cost function may include terms to minimize the risk of constraint violation.
  • Result: These formulations yield controllers that are less aggressive but more reliable in the face of uncertainty, increasing robust stability.
COST FUNCTION (OBJECTIVE FUNCTION)

Frequently Asked Questions

In Model Predictive Control (MPC), the cost function is the mathematical core that defines the controller's goal. These questions address its formulation, role, and practical implementation for engineers.

A cost function (or objective function) in Model Predictive Control (MPC) is a scalar mathematical expression, typically quadratic, that quantifies the performance of the controller over a future prediction horizon, which the online optimization solver is designed to minimize at each control step.

It formally encodes the control objectives, such as:

  • Tracking Error: Minimizing the deviation of predicted system states or outputs from a desired reference trajectory.
  • Control Effort: Penalizing the magnitude or rate of change of control inputs to avoid aggressive actuation and save energy.
  • Economic Cost: Directly optimizing for a process economic metric, such as fuel consumption or production rate, in Economic MPC.

The standard form for a Linear Quadratic MPC problem is: J = Σ (x(k) - x_ref(k))^T Q (x(k) - x_ref(k)) + Σ u(k)^T R u(k), where Q and R are positive-definite weighting matrices that tune the trade-off between tracking performance and control effort.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.