[英]Use APMonitor model in Python Gekko
Is there a way to use an APMonitor model directly within Gekko?有没有办法直接在 Gekko 中使用 APMonitor model? For example, could I solve the Hock Schittkowski benchmark problems without re-writing them in Gekko but still solve them with Gekko?
例如,我是否可以解决 Hock Schittkowski 基准测试问题而无需在 Gekko 中重写它们,但仍然使用 Gekko 解决它们? Example is problem 43, the Rosen-Suzuki problem.
示例是问题 43,Rosen-Suzuki 问题。 In APMonitor it is:
在 APMonitor 中是:
File: hs043.apm文件:hs043.apm
Model hs43
Variables
x[1:4] = 0
obj
End Variables
Equations
x[1]^2 +x[2]^2 +x[3]^2 +x[4]^2 +x[1] -x[2] +x[3] -x[4] <= 8
x[1]^2 +2*x[2]^2 +x[3]^2 +2*x[4]^2 -x[1] -x[4] <= 10
2*x[1]^2 +x[2]^2 +x[3]^2 +2*x[1] -x[2] -x[4] <= 5
! best known objective = -44
! best known solution
! x[1] = 0
! x[2] = 1
! x[3] = 2
! x[4] = -1
obj = x[1]^2 + x[2]^2 + 2*x[3]^2 + x[4]^2 - 5*x[1] - 5*x[2] - 21*x[3] + 7*x[4]
End Equations
End Model
The model in Gekko is similar: Gekko中的model类似:
Python Gekko Python 壁虎
from gekko import GEKKO
m = GEKKO(remote=False)
x1,x2,x3,x4 = m.Array(m.Var,4)
m.Equation(x1**2+x2**2+x3**2+x4**2+x1-x2+x3-x4<=8)
m.Equation(x1**2+2*x2**2+x3**2+2*x4**2-x1-x4<=10)
m.Equation(2*x1**2+x2**2+x3**2+2*x1-x2-x4<=5)
m.Minimize(x1**2+x2**2+2*x3**2+x4**2-5*x1-5*x2-21*x3+7*x4)
m.solve()
print(x1,x2,x3,x4)
print('Objective: ',m.options.OBJFCNVAL)
When I open the run folder with m.open_folder()
with the Gekko application, I see that Gekko is writing an APMonitor file gk_model0.apm
.当我使用 Gekko 应用程序使用
m.open_folder()
打开运行文件夹时,我看到 Gekko 正在编写一个 APMonitor 文件gk_model0.apm
。
Model
Variables
v1 = 0
v2 = 0
v3 = 0
v4 = 0
End Variables
Equations
(((((((((v1)^(2))+((v2)^(2)))+((v3)^(2)))+((v4)^(2)))+v1)-v2)+v3)-v4)<=8
(((((((v1)^(2))+((2)*(((v2)^(2)))))+((v3)^(2)))+((2)*(((v4)^(2)))))-v1)-v4)<=10
(((((((2)*(((v1)^(2))))+((v2)^(2)))+((v3)^(2)))+((2)*(v1)))-v2)-v4)<=5
minimize (((((((((v1)^(2))+((v2)^(2)))+((2)*(((v3)^(2)))))+((v4)^(2)))-((5)*(v1)))-((5)*(v2)))-((21)*(v3)))+((7)*(v4)))
End Equations
End Model
Could I use hs043.apm
instead?我可以改用
hs043.apm
吗?
The m.Raw()
function allows you to use APMonitor
code in Gekko, although it isn't recommended except for advanced users. m.Raw()
function 允许您在 Gekko 中使用APMonitor
代码,但不建议高级用户使用。 Here is hs043.apm
in gekko.这是
hs043.apm
中的hs043.apm。
model = '''
Model hs43
Variables
x[1:4] = 0
obj
End Variables
Equations
x[1]^2 +x[2]^2 +x[3]^2 +x[4]^2 +x[1] -x[2] +x[3] -x[4] <= 8
x[1]^2 +2*x[2]^2 +x[3]^2 +2*x[4]^2 -x[1] -x[4] <= 10
2*x[1]^2 +x[2]^2 +x[3]^2 +2*x[1] -x[2] -x[4] <= 5
! best known objective = -44
! best known solution
! x[1] = 0
! x[2] = 1
! x[3] = 2
! x[4] = -1
obj = x[1]^2 + x[2]^2 + 2*x[3]^2 + x[4]^2 &
- 5*x[1] - 5*x[2] - 21*x[3] + 7*x[4]
End Equations
End Model
'''
from gekko import GEKKO
m = GEKKO(remote=False)
m.Raw(model)
m.solve()
print('Objective: ', m.options.OBJFCNVAL)
It solves with Objective: -44.00000003
, the same as the Python gekko application.它用
Objective: -44.00000003
求解,与 Python gekko 应用程序相同。 One drawback to using an APMonitor model is that you'll need to retrieve the results because they won't be loaded back into Python variables.使用 APMonitor model 的一个缺点是您需要检索结果,因为它们不会被加载回 Python 变量。
import json
with open(m.path+'//results.json') as f:
results = json.load(f)
print(results)
Here is the results dictionary:这是结果字典:
{'time': [0.0], 'hs43.x[1]': [1.1795097003e-09], \
'hs43.x[2]': [1.0000000007], 'hs43.x[3]': [2.0000000018], \
'hs43.x[4]': [-0.99999999967], 'hs43.obj': [-44.00000003], \
'hs43.slk_1': [0.0], 'hs43.slk_2': [0.99999999287], \
'hs43.slk_3': [0.0]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.