首页 > 解决方案 > Gekko 类返回一个表达式而不是一个值

问题描述

我对 GEKKO 类有问题:我必须使用 m.exp 和 m.log 之类的函数,但这会给我带来一个问题。这是我的代码的一部分:

import numpy as np
from math import sinh, cosh
from scipy.integrate import quad
import csv

m = GEKKO()

def Antoine(T, C1, C2, C3, C4, C5):

    
    P = m.exp(C1 + (C2 / T) + (C3 * m.log(T)) + (C4 * (T ** C5)))

    return (P / 1000) # kPa

当我使用这个函数(代码后面)时,它返回一个我无法操作的 gekko 类。这是来自 VSC 调试器:

((((exp(((66.60897125778965+((-9.2194)*(log(393.15))))+1.07887711905)))/(1000)))/(200))

这是我使用 type() 时返回的类型

<class 'gekko.gk_operators.GK_Operators'>

我需要那个值,而不是表达式,我需要一个浮点类型。如果我尝试使用 .VALUE 方法,它会返回零 0。

我希望有人可以帮助我解决这个问题。

标签: pythongekko

解决方案


尝试使用该m.solve()命令生成值。Gekko 构建每个表达式的符号版本以使用自动微分。在求解命令之后,将出现值。

from gekko import GEKKO
m = GEKKO()
def Antoine(T, C1, C2, C3, C4, C5):
    P = m.exp(C1 + (C2 / T) + (C3 * m.log(T)) + (C4 * (T ** C5)))
    return (P / 1000) # kPa

T = m.Var(lb=100)
P = m.Intermediate(Antoine(T,0.1,1.0,0.2,0.3,0.4))
# find temperature where pressure is 101.325kPa
m.Equation(P==101.325)

m.solve()

print('Pressure: ', P.value[0])
print('Temperature: ', T.value[0])

具有任意 Antoine 系数的结果是:

Number of Iterations....: 10

                                   (scaled)                 (unscaled)
Objective...............:   0.0000000000000000e+00    0.0000000000000000e+00
Dual infeasibility......:   0.0000000000000000e+00    0.0000000000000000e+00
Constraint violation....:   2.5554220428603003e-07    2.5554220428603003e-07
Complementarity.........:   0.0000000000000000e+00    0.0000000000000000e+00
Overall NLP error.......:   2.5554220428603003e-07    2.5554220428603003e-07


Number of objective function evaluations             = 12
Number of objective gradient evaluations             = 11
Number of equality constraint evaluations            = 18
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 11
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 10
Total CPU secs in IPOPT (w/o function evaluations)   =      0.004
Total CPU secs in NLP function evaluations           =      0.001

EXIT: Optimal Solution Found.
 
 The solution was found.
 
 The final value of the objective function is   0.000000000000000E+000
 
 ---------------------------------------------------
 Solver         :  IPOPT (v3.12)
 Solution time  :   1.020000000426080E-002 sec
 Objective      :   0.000000000000000E+000
 Successful solution
 ---------------------------------------------------
 
Pressure:  101.32500026
Temperature:  5926.9919202

推荐阅读