APMonitor


Advanced process monitor is a modeling language for differential algebraic equations. It is a free web-service or local server for solving representations of physical systems in the form of implicit DAE models. APMonitor is suited for large-scale problems and solves linear programming, integer programming, nonlinear programming, nonlinear mixed integer programming, dynamic simulation, moving horizon estimation, and nonlinear model predictive control. APMonitor does not solve the problems directly, but calls nonlinear programming solvers such as APOPT, BPOPT, IPOPT, MINOS, and SNOPT. The APMonitor API provides exact first and second derivatives of continuous functions to the solvers through automatic differentiation and in sparse matrix form.

Programming language integration

, MATLAB, Python are mathematical programming languages that have APMonitor integration through web-service APIs. The GEKKO Optimization Suite is a recent extension of APMonitor with complete Python integration. The interfaces are built-in optimization toolboxes or modules to both load and process solutions of optimization problems. APMonitor is an object-oriented modeling language and optimization suite that relies on programming languages to load, run, and retrieve solutions. APMonitor models and data are compiled at run-time and translated into objects that are solved by an optimization engine such as APOPT or IPOPT. The optimization engine is not specified by APMonitor, allowing several different optimization engines to be switched out. The simulation or optimization mode is also configurable to reconfigure the model for dynamic simulation, nonlinear model predictive control, moving horizon estimation or general problems in mathematical optimization.
As a first step in solving the problem, a mathematical model is expressed in terms of variables and equations such as the Hock & Schittkowski Benchmark Problem #71 used to test the performance of nonlinear programming solvers. This particular optimization problem has an objective function and subject to the inequality constraint and equality constraint. The four variables must be between a lower bound of 1 and an upper bound of 5. The initial guess values are. This mathematical model is translated into the APMonitor modeling language in the following text file.

! file saved as hs71.apm
Variables
x1 = 1, >=1, <=5
x2 = 5, >=1, <=5
x3 = 5, >=1, <=5
x4 = 1, >=1, <=5
End Variables
Equations
minimize x1*x4* + x3
x1*x2*x3*x4 > 25
x1^2 + x2^2 + x3^2 + x4^2 = 40
End Equations

The problem is then solved in Python by first installing the APMonitor package with pip install APMonitor or from the following Python code.

  1. Install APMonitor
import pip
pip.main

Installing a Python is only required once for any module. Once the APMonitor package is installed, it is imported and the apm_solve function solves the optimization problem. The solution is returned to the programming language for further processing and analysis.

  1. Python example for solving an optimization problem
from APMonitor.apm import *
  1. Solve optimization problem
sol = apm_solve
  1. Access solution
x1 = sol
x2 = sol

Similar interfaces are available for MATLAB and Julia with minor differences from the above syntax. Extending the capability of a modeling language is important because significant pre- or post-processing of data or solutions is often required when solving complex optimization, dynamic simulation, estimation, or control problems.

High Index DAEs

The highest order of a derivative that is necessary to return a DAE to ODE form is called the differentiation index. A standard way for dealing with high-index DAEs is to differentiate the equations to put them in index-1 DAE or ODE form. However, this approach can cause a number of undesirable numerical issues such as instability. While the syntax is similar to other modeling languages such as gProms, APMonitor solves DAEs of any index without rearrangement or differentiation. As an example, an index-3 DAE is shown below for the pendulum motion equations and lower index rearrangements can return this system of equations to ODE form.

Pendulum motion (index-3 DAE form)


Model pendulum
Parameters
m = 1
g = 9.81
s = 1
End Parameters
Variables
x = 0
y = -s
v = 1
w = 0
lam = m*/2*s^2
End Variables
Equations
x^2 + y^2 = s^2
$x = v
$y = w
m*$v = -2*x*lam
m*$w = -m*g - 2*y*lam
End Equations
End Model

Applications in APMonitor Modeling Language

Many physical systems are naturally expressed by differential algebraic equation. Some of these include:
Models for a direct current motor and blood glucose response of an insulin dependent patient are listed below. They are representative of differential and algebraic equations encountered in many branches of science and engineering.

Direct current (DC) motor


Parameters
! motor parameters
v = 36 ! input voltage to the motor
rm = 0.1 ! motor resistance
lm = 0.01 ! motor inductance
kb = 6.5e-4 ! back emf constant
kt = 0.1 ! torque constant
jm = 1.0e-4 ! rotor inertia
bm = 1.0e-5 ! mechanical damping
! load parameters
jl = 1000*jm ! load inertia
bl = 1.0e-3 ! load damping
k = 1.0e2 ! spring constant for motor shaft to load
b = 0.1 ! spring damping for motor shaft to load
End Parameters
Variables
i = 0 ! motor electric current
dth_m = 0 ! rotor angular velocity sometimes called omega
th_m = 0 ! rotor angle, theta
dth_l = 0 ! wheel angular velocity
th_l = 0 ! wheel angle
End Variables
Equations
lm*$i - v = -rm*i - kb *$th_m
jm*$dth_m = kt*i - *$th_m - k*th_m + b *$th_l + k*th_l
jl*$dth_l = b *$th_m + k*th_m - *$th_l - k*th_l
dth_m = $th_m
dth_l = $th_l
End Equations

Blood glucose response of an insulin dependent patient


! Model source:
! A. Roy and R.S. Parker. “Dynamic Modeling of Free Fatty
! Acids, Glucose, and Insulin: An Extended Minimal Model,”
! Diabetes Technology and Therapeutics 8, 617-626, 2006.
Parameters
p1 = 0.068 ! 1/min
p2 = 0.037 ! 1/min
p3 = 0.000012 ! 1/min
p4 = 1.3 ! mL/
p5 = 0.000568 ! 1/mL
p6 = 0.00006 ! 1/
p7 = 0.03 ! 1/min
p8 = 4.5 ! mL/
k1 = 0.02 ! 1/min
k2 = 0.03 ! 1/min
pF2 = 0.17 ! 1/min
pF3 = 0.00001 ! 1/min
n = 0.142 ! 1/min
VolG = 117 ! dL
VolF = 11.7 ! L
! basal parameters for Type-I diabetic
Ib = 0 ! Insulin
Xb = 0 ! Remote insulin
Gb = 98 ! Blood Glucose
Yb = 0 ! Insulin for Lipogenesis
Fb = 380 ! Plasma Free Fatty Acid
Zb = 380 ! Remote Free Fatty Acid
! insulin infusion rate
u1 = 3 ! µU/min
! glucose uptake rate
u2 = 300 ! mg/min
! external lipid infusion
u3 = 0 ! mg/min
End parameters
Intermediates
p9 = 0.00021 * exp ! dL/
End Intermediates
Variables
I = Ib
X = Xb
G = Gb
Y = Yb
F = Fb
Z = Zb
End variables
Equations
! Insulin dynamics
$I = -n*I + p5*u1
! Remote insulin compartment dynamics
$X = -p2*X + p3*I
! Glucose dynamics
$G = -p1*G - p4*X*G + p6*G*Z + p1*Gb - p6*Gb*Zb + u2/VolG
! Insulin dynamics for lipogenesis
$Y = -pF2*Y + pF3*I
! Plasma-free fatty acid dynamics
$F = -p7* - p8*Y*F + p9 * + u3/VolF
! Remote FFA dynamics
$Z = -k2* + k1*
End Equations