1. 介绍
APM是Advanced Process Monitor,可用于混合整数规划(LP, QP, NLP, MILP, MINLP)和微分方程求解。而GEKKO是在此之上的升级版本,使用python语言。安装很简单pip install gekko即可.
快速入门的例子看这里:https://gekko.readthedocs.io/en/latest/examples.html
这里是教程,真是太贴心了:
https://apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization
GEKKO可选的求解器有1: APOPT, 2: BPOPT, 3: IPOPT。此外,也可以调用MINOS、SNOPT等。
IPOPT一般用于问题自由度(自由度=变量个数-约束个数)很大、初始估计较好的情况;BPOPT则是在系统生物应用中使用;APOPT用于问题自由度小于2000的情形。如果有整数约束,则只能使用APOPT。
这里可以在线尝试:http://apmonitor.com/。
2. 建模
Gekko常见变量:Constants, Parameters, Variables and Intermediates,顾名思义是常数、参数、变量和显式变量。
添加约束条件:m.Equations(eqs)
添加目标函数:m.Obj(obj)
connections算是官方的化简工具,两个变量如果有直接的关系,则只显示第一个。
dt()函数表示微分。
time参数可以将所有的约束在每个时间点上都建立起来。
options参数可以设置全局参数。
Array可以方便的创建多维数组,例如x = m.Array(m.Var,(n,p))
solve(disp=True, debug=False)用于求解
max/min有各种编号,具体看文档。
sos1:specical ordered set。
下面是官方的例子:
from gekko import GEKKO
#Initialize Model
m = GEKKO()
#define parameter
eq = m.Param(value=40)
#initialize variables
x1,x2,x3,x4 = [m.Var(lb=1, ub=5) for i in range(4)]
#initial values
x1.value = 1
x2.value = 5
x3.value = 5
x4.value = 1
#Equations
m.Equation(x1*x2*x3*x4>=25)
m.Equation(x1**2+x2**2+x3**2+x4**2==eq)
#Objective
m.Obj(x1*x4*(x1+x2+x3)+x3)
#Set global options
m.options.IMODE = 3 #steady state optimization
#Solve simulation
m.solve()
#Results
print('')
print('Results')
print('x1: ' + str(x1.value))
print('x2: ' + str(x2.value))
print('x3: ' + str(x3.value))
print('x4: ' + str(x4.value))
返回结果如下:
----------------------------------------------------------------
APMonitor, Version 0.9.2
APMonitor Optimization Suite
----------------------------------------------------------------
--------- APM Model Size ------------
Each time step contains
Objects : 0
Constants : 0
Variables : 6
Intermediates: 0
Connections : 0
Equations : 3
Residuals : 3
Number of state variables: 5
Number of total equations: - 2
Number of slack variables: - 1
---------------------------------------
Degrees of freedom : 2
solver 3 not supported
using default solver: APOPT
----------------------------------------------
Steady State Optimization with APOPT Solver
----------------------------------------------
Iter Objective Convergence
0 1.67676E+01 1.87500E-01
1 2.04736E+01 4.88281E-02
2 1.75530E+01 2.17630E-02
3 1.70459E+01 9.65302E-03
4 1.70140E+01 2.04419E-04
5 1.70140E+01 4.22153E-08
6 1.70140E+01 6.48259E-13
7 1.70140E+01 6.48259E-13
Successful solution
---------------------------------------------------
Solver : IPOPT (v3.12)
Solution time : 2.0000000018626451E-002 sec
Objective : 17.014017289155863
Successful solution
---------------------------------------------------
Results
x1: [1.0]
x2: [4.742999637]
x3: [3.8211499845]
x4: [1.3794082931]
3. 深度学习模块
brain模块用于人工神经网络(ANN)的优化求解。gekko优化的原理与常见的梯度下降法不太一样,使用于中等规模的模型参数优化。这里直接简单给一个例子,蓝色的是数据点,红色曲线是神经网络的预测结果。
from gekko import brain
import numpy as np
import matplotlib.pyplot as plt
b = brain.Brain(remote=False)
b.input_layer(1)
b.layer(linear=2)
b.layer(tanh=2)
b.layer(linear=2)
b.output_layer(1)
x = np.linspace(0,2*np.pi)
y = np.sin(x)
b.learn(x,y)
xp = np.linspace(-2*np.pi,4*np.pi,100)
yp = b.think(xp)
plt.figure()
plt.plot(x,y,'bo')
plt.plot(xp,yp[0],'r-')
plt.show()
结果如下:
APMonitor, Version 0.9.2
APMonitor Optimization Suite
----------------------------------------------------------------
--------- APM Model Size ------------
Each time step contains
Objects : 0
Constants : 0
Variables : 21
Intermediates: 14
Connections : 0
Equations : 15
Residuals : 1
Number of state variables: 69
Number of total equations: - 50
Number of slack variables: - 0
---------------------------------------
Degrees of freedom : 19
solver 3 not supported
using default solver: APOPT
----------------------------------------------
Model Parameter Estimation with APOPT Solver
----------------------------------------------
Iter Objective Convergence
0 2.32734E+02 9.62187E-01
1 1.93339E+02 5.00197E-01
2 1.11518E+02 1.09380E+00
3 1.11104E+03 9.86073E-01
4 3.77756E+02 1.36852E-01
5 8.80346E+02 1.98341E-01
6 4.10029E+02 1.27699E-01
7 3.47998E+02 7.34888E-03
8 1.49302E+02 4.53653E-02
9 1.93712E+02 8.73271E-02
Iter Objective Convergence
10 5.74918E+01 3.12551E-01
11 4.84043E+01 5.68927E-02
12 4.39362E+01 3.54965E-02
13 4.32555E+01 1.55739E-02
14 4.30514E+01 1.66817E-03
15 4.24926E+01 5.04354E-04
16 4.19261E+01 3.60617E-02
17 4.17156E+01 1.53822E-03
18 4.18934E+01 2.22466E-02
19 4.17898E+01 2.25828E-02
Iter Objective Convergence
20 4.17632E+01 2.25767E-03
21 4.17563E+01 1.06715E-03
22 4.17559E+01 7.26557E-05
23 4.17556E+01 8.69901E-04
24 4.17556E+01 5.07237E-04
25 4.17556E+01 3.89240E-05
26 4.17556E+01 3.89240E-05
Successful solution
---------------------------------------------------
Solver : IPOPT (v3.12)
Solution time : 7.6999999815598130E-002 sec
Objective : 41.755580638466895
Successful solution
---------------------------------------------------