如何在 GEKKO 中间使用 np.log 或 np.exp

问题描述

我正在使用 gekko 来求解方程组。作为中间步骤,我使用将 MV 温度插入以下函数的中间体:

def riedelVP(T,const):
    '''Returns Vapor Pressure
    INPUTS
    :T - Temperature (K)
    :const - A,B,C,D,E constants for eqn
    OUTPUTS
    :Y - Pressure in Pascals'''
    # unpack constants
    a,b,c,d,e = const
    
    # plug into equation
    Y = np.exp(a+b/T+c*np.log(T) + d*T**e)
    return Y

当我这样做时,我收到以下错误

我尝试使用 T.valueT.value[0] 作为函数的参数而不是 T。
TypeError: loop of ufunc does not support argument 0 of type GKVariable which has no callable log method
如何使用带有 exp 的函数登录 Gekko 中间

解决方法

这种情况下的问题是 np.exp() 函数和 np.log 函数都需要一个浮点型参数,而 gekko 变量是它们自己的类型。如果您要使用 gekko 的方法求幂并在函数中取对数,它将起作用。为此,请使用 exp 或 log 方法。在这个例子中,我已经使用 m = GEKKO() 创建了一个 gekko 模型,然后我可以创建函数:

def riedelVP(T,const):
    '''Returns Vapor Pressure
    INPUTS
    :T - Temperature (K)
    :const - A,B,C,D,E constants for eqn
    OUTPUTS
    :Y - Pressure in Pascals'''
    # unpack constants
    a,b,c,d,e = const
    
    # plug into equation
    Y = m.exp(a+b/T+c*m.log(T) + d*T**e)
    return Y

当函数中出现类型错误时,使用 gekko 的类似方法。