Model Predictive Control

Optimal control is a method to use model predictions to plan an optimized future trajectory for time-varying systems. It is often referred to as Model Predictive Control (MPC) or Dynamic Optimization. The following is an introductory video from the Dynamic Optimization Course

A method to solve dynamic control problems is by numerically integrating the dynamic model at discrete time intervals, much like measuring a physical system at particular time points. The numerical solution is compared to a desired trajectory and the difference is minimized by adjustable parameters in the model that may change at every time step. The first control action is taken and then the entire process is repeated at the next time instance. The process is repeated because objective targets may change or updated measurements may have adjusted parameter or state estimates.


Objective: Implement a model predictive controller that automatically regulates vehicle velocity. Implement the controller in Python and tune the controller for acceptable performance. Discuss factors that may be important for evaluating controller performance.

The dynamic relationship between a vehicle gas pedal position (MV) and velocity (CV) is given by the following set of conditions and a single dynamic equation.

   m = 500 ! Mass (kg)
   b = 50  ! Resistive coefficient (N-s/m)  
   K = 0.8 ! Gain (m/s-%pedal)
   p = 0 >= 0 <= 100  ! Gas pedal position (%)
   v = 0 ! initial condition
   m * $v = -v * b + K * b * p

Implement a model predictive controller that adjusts gas pedal position to regulate velocity. Start at an initial vehicle velocity of 0 m/s and accelerate to a velocity of 40 m/s.

Discuss the controller performance and how it could be tuned to meet multiple objectives including:

  • minimize travel time
  • remain within speed limits
  • improve vehicle fuel efficiency
  • discourage excessive gas pedal adjustments
  • do not accelerate excessively

There is no need to implement these advanced objectives in simulation for this second part of the exercise, only discuss the possible competing objectives.


The Model Predictive Control both solves the differential equations that describe the velocity of a vehicle as well as minimizes the control objective function.

The multiple objectives can be implemented with variable constraints or alternative objective functions. For example, if the only objective is to minimize travel time, the solution would be to use full gas pedal position (100%) for the duration of the simulation. If the objective is to minimize travel time but stay within the speed limit, the solution would be to reach 40 m/s as fast as possible without overshoot. Each additional objective has the potential to adapt the solution to achieve an optimal tradeoff. It is a tradeoff because some of the desirable outcomes have conflicting objectives.

See the Dynamic Optimization Course for Excel, MATLAB, and Simulink source files to this problem. The Python source is shown below.

import numpy as np
   from APMonitor import *
   # Automatically install APMonitor
   import pip
   from APMonitor import *

s = ''
a = 'velocity'

# clear prior application
apm(s,a,'clear all')

# time points
t = np.linspace(0,30,31)

# write model file (model.apm)
fid = open('model.apm','w')
fid.write('Constants \n')
fid.write('  m = 500 ! Mass (kg) \n')
fid.write('Parameters \n')
fid.write('  b = 50  ! Resistive coefficient (N-s/m) \n')
fid.write('  K = 0.8 ! Gain (m/s-%pedal) \n')
fid.write('  p = 0 >= 0 <= 100  ! Gas pedal position (%) \n')
fid.write(' \n')
fid.write('Variables \n')
fid.write('  v = 0 \n')
fid.write('  tau = m/b \n')
fid.write(' \n')
fid.write('Equations \n')
fid.write('  m * $v = -v * b + K * b * p \n')
fid.write('  tau * b = m \n')

# write data file (data.csv)
header = 'time'
data = t.T # transpose to column vector

# load model and data

# specify MV / CV

# configuration parameters

# turn on MV / CV

# tune controller

# solve and retrieve results
output = apm(s,a,'solve')

# open web-viewer