**Objective:** Solve an optimal control problem with a minimal final time. Set up and solve the Jennings optimal control benchmark problem. Create a program to optimize and display the results. *Estimated Time: 30 minutes*

import numpy as np

from gekko import GEKKO

import matplotlib.pyplot as plt

m = GEKKO()

nt = 501

tm = np.linspace(0,1,nt)

m.time = tm

# Variables

x1 = m.Var(value=np.pi/2.0)

x2 = m.Var(value=4.0)

x3 = m.Var(value=0.0)

p = np.zeros(nt)

p[-1] = 1.0

final = m.Param(value=p)

# FV

tf = m.FV(value=1.0,lb=0.1,ub=100.0)

tf.STATUS = 1

# MV

u = m.MV(value=0,lb=-2,ub=2)

u.STATUS = 1

m.Equation(x1.dt()==u*tf)

m.Equation(x2.dt()==m.cos(x1)*tf)

m.Equation(x3.dt()==m.sin(x1)*tf)

m.Equation(x2*final<=0)

m.Equation(x3*final<=0)

m.Obj(tf)

m.options.IMODE = 6

m.solve()

print('Final Time: ' + str(tf.value[0]))

tm = tm * tf.value[0]

plt.figure(1)

plt.plot(tm,x1.value,'k-',LineWidth=2,label=r'$x_1$')

plt.plot(tm,x2.value,'b-',LineWidth=2,label=r'$x_2$')

plt.plot(tm,x3.value,'g--',LineWidth=2,label=r'$x_3$')

plt.plot(tm,u.value,'r--',LineWidth=2,label=r'$u$')

plt.legend(loc='best')

plt.xlabel('Time')

plt.ylabel('Value')

plt.show()

from gekko import GEKKO

import matplotlib.pyplot as plt

m = GEKKO()

nt = 501

tm = np.linspace(0,1,nt)

m.time = tm

# Variables

x1 = m.Var(value=np.pi/2.0)

x2 = m.Var(value=4.0)

x3 = m.Var(value=0.0)

p = np.zeros(nt)

p[-1] = 1.0

final = m.Param(value=p)

# FV

tf = m.FV(value=1.0,lb=0.1,ub=100.0)

tf.STATUS = 1

# MV

u = m.MV(value=0,lb=-2,ub=2)

u.STATUS = 1

m.Equation(x1.dt()==u*tf)

m.Equation(x2.dt()==m.cos(x1)*tf)

m.Equation(x3.dt()==m.sin(x1)*tf)

m.Equation(x2*final<=0)

m.Equation(x3*final<=0)

m.Obj(tf)

m.options.IMODE = 6

m.solve()

print('Final Time: ' + str(tf.value[0]))

tm = tm * tf.value[0]

plt.figure(1)

plt.plot(tm,x1.value,'k-',LineWidth=2,label=r'$x_1$')

plt.plot(tm,x2.value,'b-',LineWidth=2,label=r'$x_2$')

plt.plot(tm,x3.value,'g--',LineWidth=2,label=r'$x_3$')

plt.plot(tm,u.value,'r--',LineWidth=2,label=r'$u$')

plt.legend(loc='best')

plt.xlabel('Time')

plt.ylabel('Value')

plt.show()

A differential equation is time-scaled by adding a final time parameter `t_f` that scales a simulation from time 0 to 1 to a range of 0 to `t_f`. The differential equation simulation time is adjusted by dividing each derivative term by `t_f`. A general expression for a differential equation is `0=(\frac{dx}{dt},x,u)` for simulation time 0.0 to 1.0. The `dt` term is adjusted from the current time derivative to a modified time derivative by adding the final time `t_f` as `t_f \, dt`. The new time scaled differential equation is

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

In semi-explicit form `\dot x=f(x,u)`, this equation is time scaled

$$\frac{\frac{dx}{dt}}{t_f}=f(x,u)$$

and rearranged to the final form as applied to the Jennings optimal control problem `\frac{dx}{dt}=t_f f(x,u)`.

The Jennings optimal control problem minimizes the final time subject to differential constraints. Minimizing final time is found in many areas such as manufacturing, transportation, and energy systems. In each case, it is the desired end-state that must be met while the optimizer seeks to arrive at those conditions in the shortest time.

$$\min_{u(t)} \; t_f$$ $$\mathrm{subject \; to}$$ $$\frac{dx_1}{dt}=u$$ $$\frac{dx_2}{dt}=\cos\left(x_1(t)\right)$$ $$\frac{dx_3}{dt}=\sin\left(x_1(t)\right)$$ $$x(0) = \left[\pi/2,4,0\right]$$ $$x_2\left(t_f\right)=0$$ $$x_3\left(t_f\right)=0$$ $$-2 \le u(t) \le 2$$

The Jennings problem is solved by specifying a time horizon from 0.0 to 1.0 with the additional `t_f` parameter that scales the final time. The conditions `x_2(t_f)=0` and `x_3(t_f)=0` are binding at the end and limit the system from having a lower final time.

$$\min_{u(t), t_f} \; t_f$$ $$\mathrm{subject \; to}$$ $$\frac{dx_1}{dt}=t_f \, u$$ $$\frac{dx_2}{dt}=t_f \, \cos\left(x_1(t)\right)$$ $$\frac{dx_3}{dt}=t_f \, \sin\left(x_1(t)\right)$$ $$x(0) = \left[\pi/2,4,0\right]$$ $$x_2\left(t_f\right)=0$$ $$x_3\left(t_f\right)=0$$ $$-2 \le u(t) \le 2$$

The parameter `t_f` is declared as a Feedforward Variable/Fixed Value so that it has only one value over the time horizon. The adjustable value `u` is declared as a Manipulated Variable that is adjustable at all time points in the time horizon.

- Beal, L., GEKKO for Dynamic Optimization, 2018, URL: http://gekko.readthedocs.io/en/latest/
- 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

Retrieved from http://apmonitor.com/do/index.php/Main/MinimizeFinalTime

Page last modified on March 01, 2018, at 04:51 PM