Main
~~%width~~=~~400px%Attach:obj~~_~~ctrl~~_~~nomenclature.png~~
~~%width=300px%Attach:obj~~_~~ctrl~~_~~l1~~_~~norm.png~~
~~%width=300px%Attach:obj~~_~~ctrl~~_~~sq~~_~~error.png~~

Attach:download.png [[Attach:custom_traj.zip|Custom Reference Trajectory (Python and MATLAB)]]
~~!!!! Reactor Tuning Example~~

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/qH55ym1g-NM" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

(:toggle hide gekko_mpc button show="Model Predictive Control with GEKKO":)

(:div id=gekko_mpc:)

%width=500px%Attach:gekko_mpc.png

(:source lang=python:)

import numpy as np

from random import random

from gekko import GEKKO

import matplotlib.pyplot as plt

# initialize GEKKO model

m = GEKKO()

# time

m.time = np.linspace(0,20,41)

# constants

mass = 500

# Parameters

b = m.Param(value=50)

K = m.Param(value=0.8)

# Manipulated variable

p = m.MV(value=0, lb=0, ub=100)

# Controlled Variable

v = m.CV(value=0)

# Equations

m.Equation(mass*v.dt() == -v*b + K*b*p)

m.options.IMODE = 6 # control

# MV tuning

p.STATUS = 1 #allow optimizer to change

p.DCOST = 0.1 #smooth out MV

p.DMAX = 20 #slow down change of MV

# CV tuning

v.STATUS = 1 #add the CV to the objective

m.options.CV_TYPE = 1 #Dead-band

v.SPHI = 42 #set point

v.SPLO = 38 #set point

v.TR_INIT = 1 #setpoint trajectory

v.TAU = 5 #time constant of setpoint trajectory

# Solve

m.solve()

# get additional solution information

import json

with open(m.path+'//results.json') as f:

results = json.load(f)

# Plot solution

plt.figure()

plt.subplot(2,1,1)

plt.plot(m.time,p.value,'b-',LineWidth=2,label='MV')

plt.legend(loc='best')

plt.ylabel('gas')

plt.subplot(2,1,2)

plt.plot(m.time,results['v1.tr_lo'],'k-',label='SPHI')

plt.plot(m.time,results['v1.tr_hi'],'k-',label='SPLO')

plt.plot(m.time,v.value,'r--',LineWidth=2,label='CV')

plt.legend(loc='best')

plt.ylabel('velocity')

plt.xlabel('time')

plt.show()

(:sourceend:)

(:divend:)

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/dM8gX54jWW4" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

In formulating an objective function and model equations follow the following tips for improved convergence.

# Rearrange to equation in residual form to:

## Avoid divide by zero

## Minimize use of functions like sqrt, log, exp, etc.

## Have continuous first and second derivatives

## Fit the equation into a linear or quadratic form

# Bounds

## Include variable bounds to exclude infeasible solutions

## Variable bounds to avoid regions of strong nonlinearity

## Caution: watch for infeasible solutions

# Scaling:

## Scale absolute value of variables to 1e-3 to 1e3

## Scale absolute value of equation residuals to 1e-3 to 1e3

## Better that 1st derivative values are closer to 1.0

# Good initial conditions:

## Starting near a solution can improve convergence

## Try multiple initial conditions to verify global solution (non-convex problems)

## Explicitly calculate intermediate values

# Check iteration summary for improved convergence

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/qH55ym1g-NM" frameborder="0" allowfullscreen></iframe>

(:htmlend:)
~~!!!! Exercise~~

'''Objective:''' Set up and solve several [[Attach:Dynamic_Optimization_Benchmarks.pdf|dynamic optimization benchmark problems]]'^2^'. Create a program to optimize and display the results. ''Estimated Time (each): 30 minutes''

* Example 1a - Nonlinear, unconstrained, minimize final state

->Attach:dynopt_1a.png

* Example 1b - Nonlinear, unconstrained, minimize final state with terminal constraint

->Attach:dynopt_1b.png

* Example 2 - Nonlinear, constrained, minimize final state

->Attach:dynopt_2.png

* Example 3 - Tubular reactor with parallel reaction

->Attach:dynopt_3.png

* Example 4 - Batch reactor with consecutive reactions A->B->C

->Attach:dynopt_4.png

Example 5 - Catalytic reactor with A<->B->C

->Attach:dynopt_5.png

!!!! Solution

Attach:download.png [[Attach:dynamic_optimization_benchmarks.zip|Dynamic Optimization Benchmarks in MATLAB and Python]]

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/mmCFF3-6sGg" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

~~# M. Čižniar, M. Fikar, M.A. Latifi: A MATLAB Package for Dynamic Optimisation of Processes, 7th International Scientific – Technical Conference – Process Control 2006, June 13 – 16, 2006, Kouty nad Desnou, Czech Republic. [[Attach:DynOpt_Benchmarks.pdf|Article]]~~

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/mmCFF3-6sGg" frameborder="0" allowfullscreen></iframe>

(:htmlend:)
~~!!!! Exercise~~

'''Objective:''' Design a model predictive controller with a custom objective function that satisfies a specific problem criterion. Simulate and optimize a pendulum system with an adjustable overhead cart. ''Estimated time: 3 hours.''

A pendulum is described by the following dynamic equations:

Attach:pendulum_crane_equations.png

where ''epsilon'' is ''m'_2_'/(m'_1_'+m'_2_')'', ''y'' is the position of the overhead cart, ''v'' is the velocity of the overhead cart, ''theta'' is the angle of the pendulum relative to the cart, and ''q'' is the rate of angle change'^2^'.

Attach:pendulum_crane.png

The objective of the controller is to adjust the force on the cart to move the pendulum mass to a new final position. Ensure that initial and final velocities and angles of the pendulum are zero. The position of the pendulum mass is initially at -1 and it is desired to move it to the new position of 0 within 6.2 seconds. Demonstrate controller performance with changes in the pendulum position and that the final pendulum mass remains at the final position without oscillation.

!!!! Solution

Attach:download.png [[Attach:crane_control.zip|Crane Pendulum Solution Files in MATLAB and Python]]

~~# Bryson, A.E., Dynamic Optimization, Addison-Wesley, 1999.~~
~~Attach:download.png [[Attach:crane_control.zip|Crane Pendulum Source Files]]~~

# Bryson, A.E., Dynamic Optimization, Addison-Wesley, 1999.

## Dynamic Control Objectives

## Main.ControllerObjective History

Hide minor edits - Show changes to output

Changed line 141 from:

{$y,x,u=\mathrm{outputs~~\~~,~~states~~\,and\,inputs}$}

to:

{$y,x,u=\mathrm{outputs,\,states,\,and\,inputs}$}

Changed lines 91-92 from:

{$\min_{x,y,u} \Phi = \left(y-y_t\right)^T W_t \left(y-y_t\right) + y^T ~~c~~_y + u^T ~~c~~_u + \Delta u^T ~~c~~_{\Delta u} \Delta u$}

to:

{$\min_{x,y,u} \Phi = \left(y-y_t\right)^T W_t \left(y-y_t\right) + y^T w_y + u^T w_u + \Delta u^T W_{\Delta u} \Delta u$}

Changed lines 105-106 from:

{$\min_{x,y,u} \Phi = w_{hi}^T e_{hi} + w_{lo}^T e_{lo} + y^T ~~c~~_y + u^T ~~c~~_u + w_{\Delta u}^T \left(\Delta u_U+\Delta u_L\right)$}

to:

{$\min_{x,y,u} \Phi = w_{hi}^T e_{hi} + w_{lo}^T e_{lo} + y^T w_y + u^T w_u + w_{\Delta u}^T \left(\Delta u_U+\Delta u_L\right)$}

Changed lines 137-139 from:

{$~~c~~_{\Delta u}~~=\mathrm{manipulated\~~,~~variable~~\~~,movement~~\,~~penalty~~\,(DCOST)}$}

to:

{$W_{\Delta u}, w_{\Delta u}=\mathrm{manipulated\,variable\,movement\,penalty\,(DCOST)}$}

{$w_{u}, w_{y}=\mathrm{weight\,on\,input\,and\,output\,(COST)}$}

{$w_{u}, w_{y}=\mathrm{weight\,on\,input\,and\,output\,(COST)}$}

Changed lines 109-111 from:

{$0 = f\left(\frac{dx}{dt},x,y,~~p~~\right)$}

{$0 \le g\left(\frac{dx}{dt},x,y,~~p~~\right)$}

{$0 \le g\left(\frac{dx}{dt},x,y,

to:

{$0 = f\left(\frac{dx}{dt},x,y,u\right)$}

{$0 \le g\left(\frac{dx}{dt},x,y,u\right)$}

{$0 \le g\left(\frac{dx}{dt},x,y,u\right)$}

Changed line 133 from:

{$y_t,y_{t,hi},y_{t,lo}=\mathrm{reference~~ ~~trajectory~~ ~~or~~ ~~range}$}

to:

{$y_t,y_{t,hi},y_{t,lo}=\mathrm{reference\,trajectory\,or\,range}$}

Added lines 113-116:

{$\tau_c \frac{dy_{t,hi}}{dt} + y_{t,hi} = sp_{hi}$}

{$\tau_c \frac{dy_{t,lo}}{dt} + y_{t,lo} = sp_{lo}$}

{$\tau_c \frac{dy_{t,lo}}{dt} + y_{t,lo} = sp_{lo}$}

Changed lines 129-147 from:

to:

{$\Phi=\mathrm{Objective\,Function}$}

{$y=\mathrm{model\,predictions}$}

{$y_t,y_{t,hi},y_{t,lo}=\mathrm{reference trajectory or range}$}

{$W_t,w_{hi}, w_{lo}=\mathrm{penalty\,outside\,reference\,trajectory\,(WSP,WSPHI,WSPLO)}$}

{$c_{\Delta u}=\mathrm{manipulated\,variable\,movement\,penalty\,(DCOST)}$}

{$y,x,u=\mathrm{outputs\,states\,and\,inputs}$}

{$\Delta u=\mathrm{manipulated\,variable\,change}$}

{$f,g=\mathrm{equality\,and\,inequality\,constraints}$}

{$e_{hi},e_{lo}=\mathrm{upper\,and\,lower\,error\,outside\,dead-band}$}

{$\Delta u_U,\Delta u_L=\mathrm{upper\,and\,lower\,manipulated\,variable\,change}$}

{$y=\mathrm{model\,predictions}$}

{$y_t,y_{t,hi},y_{t,lo}=\mathrm{reference trajectory or range}$}

{$W_t,w_{hi}, w_{lo}=\mathrm{penalty\,outside\,reference\,trajectory\,(WSP,WSPHI,WSPLO)}$}

{$c_{\Delta u}=\mathrm{manipulated\,variable\,movement\,penalty\,(DCOST)}$}

{$y,x,u=\mathrm{outputs\,states\,and\,inputs}$}

{$\Delta u=\mathrm{manipulated\,variable\,change}$}

{$f,g=\mathrm{equality\,and\,inequality\,constraints}$}

{$e_{hi},e_{lo}=\mathrm{upper\,and\,lower\,error\,outside\,dead-band}$}

{$\Delta u_U,\Delta u_L=\mathrm{upper\,and\,lower\,manipulated\,variable\,change}$}

Changed lines 117-121 from:

{$\Delta ~~p~~_U \ge ~~p~~ - ~~\hat p$}~~

{$\Delta ~~p~~_L \ge ~~\hat p ~~- ~~p~~$}

{$e_{hi}, e_{lo}, \Delta~~p~~_U, \Delta ~~p~~_L \ge 0$}

{$e_{hi}, e_{lo}, \Delta

to:

{$\Delta u_U \ge u_i - u_{i-1}$}

{$\Delta u_L \ge u_{i-1} - u_i$}

{$e_{hi}, e_{lo}, \Delta u_U, \Delta u_L \ge 0$}

{$\Delta u_L \ge u_{i-1} - u_i$}

{$e_{hi}, e_{lo}, \Delta u_U, \Delta u_L \ge 0$}

Changed lines 105-121 from:

to:

{$\min_{x,y,u} \Phi = w_{hi}^T e_{hi} + w_{lo}^T e_{lo} + y^T c_y + u^T c_u + w_{\Delta u}^T \left(\Delta u_U+\Delta u_L\right)$}

{$\mathrm{subject\;\;to}$}

{$0 = f\left(\frac{dx}{dt},x,y,p\right)$}

{$0 \le g\left(\frac{dx}{dt},x,y,p\right)$}

{$e_{hi} \ge y - y_{t,hi}$}

{$e_{lo} \ge y_{t,lo} - y$}

{$\Delta p_U \ge p - \hat p$}

{$\Delta p_L \ge \hat p - p$}

{$e_{hi}, e_{lo}, \Delta p_U, \Delta p_L \ge 0$}

{$\mathrm{subject\;\;to}$}

{$0 = f\left(\frac{dx}{dt},x,y,p\right)$}

{$0 \le g\left(\frac{dx}{dt},x,y,p\right)$}

{$e_{hi} \ge y - y_{t,hi}$}

{$e_{lo} \ge y_{t,lo} - y$}

{$\Delta p_U \ge p - \hat p$}

{$\Delta p_L \ge \hat p - p$}

{$e_{hi}, e_{lo}, \Delta p_U, \Delta p_L \ge 0$}

Changed lines 95-99 from:

{$0 = f\left(\frac{~~d\;x~~}{~~d\;t~~},x,y,u\right)$}

{$0 \le g\left(\frac{~~d\;x~~}{~~d\;t~~},x,y,u\right)$}

{$\tau_c \frac{~~d\;y~~_t}{~~d\;t~~} + y_t = sp$}

{$0 \le g\left(\frac{

{$\tau_c \frac{

to:

{$0 = f\left(\frac{dx}{dt},x,y,u\right)$}

{$0 \le g\left(\frac{dx}{dt},x,y,u\right)$}

{$\tau_c \frac{dy_t}{dt} + y_t = sp$}

{$0 \le g\left(\frac{dx}{dt},x,y,u\right)$}

{$\tau_c \frac{dy_t}{dt} + y_t = sp$}

Changed lines 91-99 from:

to:

{$\min_{x,y,u} \Phi = \left(y-y_t\right)^T W_t \left(y-y_t\right) + y^T c_y + u^T c_u + \Delta u^T c_{\Delta u} \Delta u$}

{$\mathrm{subject\;\;to}$}

{$0 = f\left(\frac{d\;x}{d\;t},x,y,u\right)$}

{$0 \le g\left(\frac{d\;x}{d\;t},x,y,u\right)$}

{$\tau_c \frac{d\;y_t}{d\;t} + y_t = sp$}

{$\mathrm{subject\;\;to}$}

{$0 = f\left(\frac{d\;x}{d\;t},x,y,u\right)$}

{$0 \le g\left(\frac{d\;x}{d\;t},x,y,u\right)$}

{$\tau_c \frac{d\;y_t}{d\;t} + y_t = sp$}

Added lines 127-128:

Attach:download.png [[Attach:custom_traj.zip|Custom Reference Trajectory (Python and MATLAB)]]

Added lines 126-127:

Some applications require a custom reference trajectory that does not fit a standard form. A custom reference trajectory is specified by creating a new error (e) variable that is the difference between the specified trajectory (sinusoidal, sawtooth, random, etc) and the model output. This error is specified as a controlled variable (CV) with an upper and lower dead-band denoted as SPHI and SPLO. The CV can also be a value of zero with a squared error objective (e.SP=0, m.options.CV_TYPE=2) to drive to a target instead of a dead-band range.

Changed line 180 from:

e.TR_INIT = ~~1~~ #setpoint trajectory

to:

e.TR_INIT = 0 #setpoint trajectory

Changed lines 124-128 from:

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/qH55ym1g-NM" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

to:

!!!! Custom Reference Trajectory

%width=500px%Attach:gekko_traj.png

(:toggle hide gekko_traj button show="Custom Reference Trajectory with GEKKO":)

(:div id=gekko_traj:)

(:source lang=python:)

import numpy as np

from random import random

from gekko import GEKKO

import matplotlib.pyplot as plt

# initialize GEKKO model

m = GEKKO()

# time

m.time = np.linspace(0,20,41)

# constants

mass = 500

# Parameters

b = m.Param(value=50)

K = m.Param(value=0.8)

# Manipulated variable

p = m.MV(value=0, lb=-100, ub=100)

# Reference trajectory

sine = 10*np.sin(m.time/20*4*np.pi)

traj = m.Param(value=sine)

# Controlled Variable

v = m.SV(value=0,name='v')

# Error

e = m.CV(value=0,name='e')

# Equations

m.Equation(mass*v.dt() == -v*b + K*b*p)

m.Equation(e==v-traj)

m.options.IMODE = 6 # control

# MV tuning

p.STATUS = 1 #allow optimizer to change

p.DCOST = 0.1 #smooth out MV

p.DMAX = 50 #slow down change of MV

# CV tuning

e.STATUS = 1 #add the CV to the objective

m.options.CV_TYPE = 1 #Dead-band

db = 2

e.SPHI = db #set point

e.SPLO = -db #set point

e.TR_INIT = 1 #setpoint trajectory

e.TAU = 5 #time constant of setpoint trajectory

# Solve

m.solve()

# get additional solution information

import json

with open(m.path+'//results.json') as f:

results = json.load(f)

# Plot solution

plt.figure()

plt.subplot(3,1,1)

plt.plot(m.time,p.value,'b-',LineWidth=2,label='MV')

plt.legend(loc='best')

plt.ylabel('MV')

plt.subplot(3,1,2)

plt.plot(m.time,sine+db,'k-',label='SPHI')

plt.plot(m.time,sine-db,'k-',label='SPLO')

plt.plot(m.time,v.value,'r--',LineWidth=2,label='CV')

plt.legend(loc='best')

plt.ylabel('CV')

plt.subplot(3,1,3)

plt.plot(m.time,results['e.tr_hi'],'k-',label='SPHI')

plt.plot(m.time,results['e.tr_lo'],'k-',label='SPLO')

plt.plot(m.time,e.value,'r--',LineWidth=2,label='Error')

plt.legend(loc='best')

plt.ylabel('Error')

plt.xlabel('time')

plt.show()

(:sourceend:)

(:divend:)

%width=500px%Attach:gekko_traj.png

(:toggle hide gekko_traj button show="Custom Reference Trajectory with GEKKO":)

(:div id=gekko_traj:)

(:source lang=python:)

import numpy as np

from random import random

from gekko import GEKKO

import matplotlib.pyplot as plt

# initialize GEKKO model

m = GEKKO()

# time

m.time = np.linspace(0,20,41)

# constants

mass = 500

# Parameters

b = m.Param(value=50)

K = m.Param(value=0.8)

# Manipulated variable

p = m.MV(value=0, lb=-100, ub=100)

# Reference trajectory

sine = 10*np.sin(m.time/20*4*np.pi)

traj = m.Param(value=sine)

# Controlled Variable

v = m.SV(value=0,name='v')

# Error

e = m.CV(value=0,name='e')

# Equations

m.Equation(mass*v.dt() == -v*b + K*b*p)

m.Equation(e==v-traj)

m.options.IMODE = 6 # control

# MV tuning

p.STATUS = 1 #allow optimizer to change

p.DCOST = 0.1 #smooth out MV

p.DMAX = 50 #slow down change of MV

# CV tuning

e.STATUS = 1 #add the CV to the objective

m.options.CV_TYPE = 1 #Dead-band

db = 2

e.SPHI = db #set point

e.SPLO = -db #set point

e.TR_INIT = 1 #setpoint trajectory

e.TAU = 5 #time constant of setpoint trajectory

# Solve

m.solve()

# get additional solution information

import json

with open(m.path+'//results.json') as f:

results = json.load(f)

# Plot solution

plt.figure()

plt.subplot(3,1,1)

plt.plot(m.time,p.value,'b-',LineWidth=2,label='MV')

plt.legend(loc='best')

plt.ylabel('MV')

plt.subplot(3,1,2)

plt.plot(m.time,sine+db,'k-',label='SPHI')

plt.plot(m.time,sine-db,'k-',label='SPLO')

plt.plot(m.time,v.value,'r--',LineWidth=2,label='CV')

plt.legend(loc='best')

plt.ylabel('CV')

plt.subplot(3,1,3)

plt.plot(m.time,results['e.tr_hi'],'k-',label='SPHI')

plt.plot(m.time,results['e.tr_lo'],'k-',label='SPLO')

plt.plot(m.time,e.value,'r--',LineWidth=2,label='Error')

plt.legend(loc='best')

plt.ylabel('Error')

plt.xlabel('time')

plt.show()

(:sourceend:)

(:divend:)

Changed lines 40-41 from:

v = m.CV(value=0)

to:

v = m.CV(value=0,name='v')

Changed lines 75-76 from:

plt.plot(m.time,results['~~v1~~.tr_lo'],'k-',label='SPHI')

plt.plot(m.time,results['~~v1~~.tr_hi'],'k-',label='SPLO')

plt.plot(m.time,results['

to:

plt.plot(m.time,results['v.tr_lo'],'k-',label='SPHI')

plt.plot(m.time,results['v.tr_hi'],'k-',label='SPLO')

plt.plot(m.time,results['v.tr_hi'],'k-',label='SPLO')

Added lines 12-83:

(:toggle hide gekko_mpc button show="Model Predictive Control with GEKKO":)

(:div id=gekko_mpc:)

%width=500px%Attach:gekko_mpc.png

(:source lang=python:)

import numpy as np

from random import random

from gekko import GEKKO

import matplotlib.pyplot as plt

# initialize GEKKO model

m = GEKKO()

# time

m.time = np.linspace(0,20,41)

# constants

mass = 500

# Parameters

b = m.Param(value=50)

K = m.Param(value=0.8)

# Manipulated variable

p = m.MV(value=0, lb=0, ub=100)

# Controlled Variable

v = m.CV(value=0)

# Equations

m.Equation(mass*v.dt() == -v*b + K*b*p)

m.options.IMODE = 6 # control

# MV tuning

p.STATUS = 1 #allow optimizer to change

p.DCOST = 0.1 #smooth out MV

p.DMAX = 20 #slow down change of MV

# CV tuning

v.STATUS = 1 #add the CV to the objective

m.options.CV_TYPE = 1 #Dead-band

v.SPHI = 42 #set point

v.SPLO = 38 #set point

v.TR_INIT = 1 #setpoint trajectory

v.TAU = 5 #time constant of setpoint trajectory

# Solve

m.solve()

# get additional solution information

import json

with open(m.path+'//results.json') as f:

results = json.load(f)

# Plot solution

plt.figure()

plt.subplot(2,1,1)

plt.plot(m.time,p.value,'b-',LineWidth=2,label='MV')

plt.legend(loc='best')

plt.ylabel('gas')

plt.subplot(2,1,2)

plt.plot(m.time,results['v1.tr_lo'],'k-',label='SPHI')

plt.plot(m.time,results['v1.tr_hi'],'k-',label='SPLO')

plt.plot(m.time,v.value,'r--',LineWidth=2,label='CV')

plt.legend(loc='best')

plt.ylabel('velocity')

plt.xlabel('time')

plt.show()

(:sourceend:)

(:divend:)

Added lines 8-11:

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/dM8gX54jWW4" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

Changed lines 5-9 from:

The dynamic control objective function is a mathematical statement that is minimized or maximized to find a best solution among all possible feasible solutions for a controller. The form of this objective function is critical to give desirable solutions for driving a system to a desirable state or along a desired trajectory. Common objective statements relate to economic, safety, operability, environmental, or related objectives. Two common objective functions are shown below as squared error and l'_1_'-norm forms'^1^'.

to:

The dynamic control objective function is a mathematical statement that is minimized or maximized to find a best solution among all possible feasible solutions for a controller. The form of this objective function is critical to give desirable solutions for driving a system to a desirable state or along a desired trajectory. Common objective statements relate to economic, safety, operability, environmental, or related objectives.

Attach:download.png [[Attach:mpc_objectives.zip|MPC Objective Exercise (Python and MATLAB)]]

Two common objective functions are shown below as squared error and l'_1_'-norm forms'^1^'.

Attach:download.png [[Attach:mpc_objectives.zip|MPC Objective Exercise (Python and MATLAB)]]

Two common objective functions are shown below as squared error and l'_1_'-norm forms'^1^'.

Changed line 21 from:

%width=~~300px~~%Attach:obj_ctrl_nomenclature.png

to:

%width=400px%Attach:obj_ctrl_nomenclature.png

Changed lines 11-12 from:

%width=~~200px~~%Attach:obj_ctrl_sq_error.png

to:

%width=300px%Attach:obj_ctrl_sq_error.png

Changed lines 17-18 from:

%width=~~200px~~%Attach:obj_ctrl_l1_norm.png

to:

%width=300px%Attach:obj_ctrl_l1_norm.png

Changed line 21 from:

Attach:obj_ctrl_nomenclature.png

to:

%width=300px%Attach:obj_ctrl_nomenclature.png

Changed lines 11-12 from:

Attach:obj_ctrl_sq_error.png

to:

%width=200px%Attach:obj_ctrl_sq_error.png

Changed lines 15-17 from:

The l'_1_'-norm objective is the absolute value of the difference between the current measured value of the controlled variable and the desired target value. The absolute value operator ~~is ~~

Attach:obj_ctrl_l1_norm.png

Attach

to:

The l'_1_'-norm objective is the absolute value of the difference between the current measured value of the controlled variable and the desired target value. The absolute value operator (abs(x)) does not have a continuous first and second derivative at x=0 (see [[https://apmonitor.com/me575/index.php/Main/LogicalConditions|absolute value function alternatives]]). [[https://apmonitor.com/wiki/index.php/Main/SlackVariables|Slack variables]] give the same absolute value function but with continuous first and second derivatives.

%width=200px%Attach:obj_ctrl_l1_norm.png

%width=200px%Attach:obj_ctrl_l1_norm.png

Added lines 9-10:

The squared error objective is the most common form used in the literature. It penalizes deviation of the current measured value of the controlled variable from the desired target value.

Added lines 15-16:

The l'_1_'-norm objective is the absolute value of the difference between the current measured value of the controlled variable and the desired target value. The absolute value operator is

Added lines 22-42:

In formulating an objective function and model equations follow the following tips for improved convergence.

# Rearrange to equation in residual form to:

## Avoid divide by zero

## Minimize use of functions like sqrt, log, exp, etc.

## Have continuous first and second derivatives

## Fit the equation into a linear or quadratic form

# Bounds

## Include variable bounds to exclude infeasible solutions

## Variable bounds to avoid regions of strong nonlinearity

## Caution: watch for infeasible solutions

# Scaling:

## Scale absolute value of variables to 1e-3 to 1e3

## Scale absolute value of equation residuals to 1e-3 to 1e3

## Better that 1st derivative values are closer to 1.0

# Good initial conditions:

## Starting near a solution can improve convergence

## Try multiple initial conditions to verify global solution (non-convex problems)

## Explicitly calculate intermediate values

# Check iteration summary for improved convergence

Added lines 18-21:

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/qH55ym1g-NM" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

Deleted lines 18-42:

'''Objective:''' Set up and solve several [[Attach:Dynamic_Optimization_Benchmarks.pdf|dynamic optimization benchmark problems]]'^2^'. Create a program to optimize and display the results. ''Estimated Time (each): 30 minutes''

* Example 1a - Nonlinear, unconstrained, minimize final state

->Attach:dynopt_1a.png

* Example 1b - Nonlinear, unconstrained, minimize final state with terminal constraint

->Attach:dynopt_1b.png

* Example 2 - Nonlinear, constrained, minimize final state

->Attach:dynopt_2.png

* Example 3 - Tubular reactor with parallel reaction

->Attach:dynopt_3.png

* Example 4 - Batch reactor with consecutive reactions A->B->C

->Attach:dynopt_4.png

Example 5 - Catalytic reactor with A<->B->C

->Attach:dynopt_5.png

!!!! Solution

Attach:download.png [[Attach:dynamic_optimization_benchmarks.zip|Dynamic Optimization Benchmarks in MATLAB and Python]]

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/mmCFF3-6sGg" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

Deleted line 22:

Added lines 39-42:

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/mmCFF3-6sGg" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

Changed line 33 from:

Example 5 - Catalytic ~~plug flow reactor with ~~A->B->C

to:

Example 5 - Catalytic reactor with A<->B->C

Added lines 19-39:

!!!! Exercise

'''Objective:''' Set up and solve several [[Attach:Dynamic_Optimization_Benchmarks.pdf|dynamic optimization benchmark problems]]'^2^'. Create a program to optimize and display the results. ''Estimated Time (each): 30 minutes''

* Example 1a - Nonlinear, unconstrained, minimize final state

->Attach:dynopt_1a.png

* Example 1b - Nonlinear, unconstrained, minimize final state with terminal constraint

->Attach:dynopt_1b.png

* Example 2 - Nonlinear, constrained, minimize final state

->Attach:dynopt_2.png

* Example 3 - Tubular reactor with parallel reaction

->Attach:dynopt_3.png

* Example 4 - Batch reactor with consecutive reactions A->B->C

->Attach:dynopt_4.png

Example 5 - Catalytic plug flow reactor with A->B->C

->Attach:dynopt_5.png

!!!! Solution

Attach:download.png [[Attach:dynamic_optimization_benchmarks.zip|Dynamic Optimization Benchmarks in MATLAB and Python]]

'''Objective:''' Set up and solve several [[Attach:Dynamic_Optimization_Benchmarks.pdf|dynamic optimization benchmark problems]]'^2^'. Create a program to optimize and display the results. ''Estimated Time (each): 30 minutes''

* Example 1a - Nonlinear, unconstrained, minimize final state

->Attach:dynopt_1a.png

* Example 1b - Nonlinear, unconstrained, minimize final state with terminal constraint

->Attach:dynopt_1b.png

* Example 2 - Nonlinear, constrained, minimize final state

->Attach:dynopt_2.png

* Example 3 - Tubular reactor with parallel reaction

->Attach:dynopt_3.png

* Example 4 - Batch reactor with consecutive reactions A->B->C

->Attach:dynopt_4.png

Example 5 - Catalytic plug flow reactor with A->B->C

->Attach:dynopt_5.png

!!!! Solution

Attach:download.png [[Attach:dynamic_optimization_benchmarks.zip|Dynamic Optimization Benchmarks in MATLAB and Python]]

Changed line 44 from:

to:

# M. Čižniar, M. Fikar, M.A. Latifi: A MATLAB Package for Dynamic Optimisation of Processes, 7th International Scientific – Technical Conference – Process Control 2006, June 13 – 16, 2006, Kouty nad Desnou, Czech Republic. [[Attach:DynOpt_Benchmarks.pdf|Article]]

Deleted lines 18-35:

'''Objective:''' Design a model predictive controller with a custom objective function that satisfies a specific problem criterion. Simulate and optimize a pendulum system with an adjustable overhead cart. ''Estimated time: 3 hours.''

A pendulum is described by the following dynamic equations:

Attach:pendulum_crane_equations.png

where ''epsilon'' is ''m'_2_'/(m'_1_'+m'_2_')'', ''y'' is the position of the overhead cart, ''v'' is the velocity of the overhead cart, ''theta'' is the angle of the pendulum relative to the cart, and ''q'' is the rate of angle change'^2^'.

Attach:pendulum_crane.png

The objective of the controller is to adjust the force on the cart to move the pendulum mass to a new final position. Ensure that initial and final velocities and angles of the pendulum are zero. The position of the pendulum mass is initially at -1 and it is desired to move it to the new position of 0 within 6.2 seconds. Demonstrate controller performance with changes in the pendulum position and that the final pendulum mass remains at the final position without oscillation.

!!!! Solution

Attach:download.png [[Attach:crane_control.zip|Crane Pendulum Solution Files in MATLAB and Python]]

Deleted line 22:

Changed lines 27-28 from:

where ''epsilon'' is ''m'_2_'/(m'_1_'+m'_2_')'', ''y'' is the position of the overhead cart, ''v'' is the velocity of the overhead cart, ''theta'' is the angle of the pendulum relative to the cart, and ''q'' is the rate of angle change.

to:

where ''epsilon'' is ''m'_2_'/(m'_1_'+m'_2_')'', ''y'' is the position of the overhead cart, ''v'' is the velocity of the overhead cart, ''theta'' is the angle of the pendulum relative to the cart, and ''q'' is the rate of angle change'^2^'.

Deleted lines 30-31:

Added lines 35-36:

Attach:download.png [[Attach:crane_control.zip|Crane Pendulum Solution Files in MATLAB and Python]]

Added lines 40-41:

# Bryson, A.E., Dynamic Optimization, Addison-Wesley, 1999.

Added lines 19-36:

!!!! Exercise

'''Objective:''' Design a model predictive controller with a custom objective function that satisfies a specific problem criterion. Simulate and optimize a pendulum system with an adjustable overhead cart. ''Estimated time: 3 hours.''

A pendulum is described by the following dynamic equations:

Attach:pendulum_crane_equations.png

where ''epsilon'' is ''m'_2_'/(m'_1_'+m'_2_')'', ''y'' is the position of the overhead cart, ''v'' is the velocity of the overhead cart, ''theta'' is the angle of the pendulum relative to the cart, and ''q'' is the rate of angle change.

Attach:pendulum_crane.png

Attach:download.png [[Attach:crane_control.zip|Crane Pendulum Source Files]]

The objective of the controller is to adjust the force on the cart to move the pendulum mass to a new final position. Ensure that initial and final velocities and angles of the pendulum are zero. The position of the pendulum mass is initially at -1 and it is desired to move it to the new position of 0 within 6.2 seconds. Demonstrate controller performance with changes in the pendulum position and that the final pendulum mass remains at the final position without oscillation.

!!!! Solution

'''Objective:''' Design a model predictive controller with a custom objective function that satisfies a specific problem criterion. Simulate and optimize a pendulum system with an adjustable overhead cart. ''Estimated time: 3 hours.''

A pendulum is described by the following dynamic equations:

Attach:pendulum_crane_equations.png

where ''epsilon'' is ''m'_2_'/(m'_1_'+m'_2_')'', ''y'' is the position of the overhead cart, ''v'' is the velocity of the overhead cart, ''theta'' is the angle of the pendulum relative to the cart, and ''q'' is the rate of angle change.

Attach:pendulum_crane.png

Attach:download.png [[Attach:crane_control.zip|Crane Pendulum Source Files]]

The objective of the controller is to adjust the force on the cart to move the pendulum mass to a new final position. Ensure that initial and final velocities and angles of the pendulum are zero. The position of the pendulum mass is initially at -1 and it is desired to move it to the new position of 0 within 6.2 seconds. Demonstrate controller performance with changes in the pendulum position and that the final pendulum mass remains at the final position without oscillation.

!!!! Solution

Added line 40:

Changed line 21 from:

# Hedengren, J. D. and Asgharzadeh Shishavan, R., Powell, K.M., and Edgar, T.F., Nonlinear Modeling, Estimation and Predictive Control in APMonitor, Computers and Chemical Engineering, Volume 70, pg. 133–148, 2014. [[~~Article|~~https://dx.doi.org/10.1016/j.compchemeng.2014.04.013]]

to:

# Hedengren, J. D. and Asgharzadeh Shishavan, R., Powell, K.M., and Edgar, T.F., Nonlinear Modeling, Estimation and Predictive Control in APMonitor, Computers and Chemical Engineering, Volume 70, pg. 133–148, 2014. [[https://dx.doi.org/10.1016/j.compchemeng.2014.04.013|Article]]

Changed lines 5-21 from:

The dynamic control objective function is a mathematical statement that is minimized or maximized to find a best solution among all possible feasible solutions for a controller. The form of this objective function is critical to give desirable solutions for driving a system to a desirable state or along a desired trajectory. Common objective statements relate to economic, safety, operability, environmental, or related objectives.

to:

The dynamic control objective function is a mathematical statement that is minimized or maximized to find a best solution among all possible feasible solutions for a controller. The form of this objective function is critical to give desirable solutions for driving a system to a desirable state or along a desired trajectory. Common objective statements relate to economic, safety, operability, environmental, or related objectives. Two common objective functions are shown below as squared error and l'_1_'-norm forms'^1^'.

!!!! Squared Error Objective

Attach:obj_ctrl_sq_error.png

!!!! l'_1_'-norm Objective

Attach:obj_ctrl_l1_norm.png

!!!! Nomenclature

Attach:obj_ctrl_nomenclature.png

!!!! References

# Hedengren, J. D. and Asgharzadeh Shishavan, R., Powell, K.M., and Edgar, T.F., Nonlinear Modeling, Estimation and Predictive Control in APMonitor, Computers and Chemical Engineering, Volume 70, pg. 133–148, 2014. [[Article|https://dx.doi.org/10.1016/j.compchemeng.2014.04.013]]

!!!! Squared Error Objective

Attach:obj_ctrl_sq_error.png

!!!! l'_1_'-norm Objective

Attach:obj_ctrl_l1_norm.png

!!!! Nomenclature

Attach:obj_ctrl_nomenclature.png

!!!! References

# Hedengren, J. D. and Asgharzadeh Shishavan, R., Powell, K.M., and Edgar, T.F., Nonlinear Modeling, Estimation and Predictive Control in APMonitor, Computers and Chemical Engineering, Volume 70, pg. 133–148, 2014. [[Article|https://dx.doi.org/10.1016/j.compchemeng.2014.04.013]]

Added lines 1-5:

(:title Dynamic Control Objectives:)

(:keywords objective function, MATLAB, Simulink, predictive control, time horizon, tutorial:)

(:description Objective function forms for improved control performance with nonlinear models:)

The dynamic control objective function is a mathematical statement that is minimized or maximized to find a best solution among all possible feasible solutions for a controller. The form of this objective function is critical to give desirable solutions for driving a system to a desirable state or along a desired trajectory. Common objective statements relate to economic, safety, operability, environmental, or related objectives.

(:keywords objective function, MATLAB, Simulink, predictive control, time horizon, tutorial:)

(:description Objective function forms for improved control performance with nonlinear models:)

The dynamic control objective function is a mathematical statement that is minimized or maximized to find a best solution among all possible feasible solutions for a controller. The form of this objective function is critical to give desirable solutions for driving a system to a desirable state or along a desired trajectory. Common objective statements relate to economic, safety, operability, environmental, or related objectives.