我正在使用一个函数创建一个包含Gekko变量的字典.有人能告诉我是什么原因造成语法错误吗?

下面是我正在运行的代码:

from gekko import GEKKO
m = GEKKO(remote = False)
def tank(tank_id, area, gm): #gm = gekko model
    tank_dict = {}
    tank_dict['id'] = tank_id
    tank_dict['area'] = gm.Param(value = area, name = tank_id + '_area')
    tank_dict['volume'] = gm.FV(value = 10, name = tank_id + '_volume')
    tank_dict['height'] = gm.Var(name = tank_id + '_height')
    
    gm.Equation(tank_dict['height'] == tank_dict['volume']/tank_dict['area'])
    
    return(tank_dict)

tank_1 = tank('tank_1', area = 5, gm = m)
m.solve(disp = True)

以下是我收到的错误消息:

 ----------------------------------------------------------------
 APMonitor, Version 1.0.0
 APMonitor Optimization Suite
 ----------------------------------------------------------------
 
 
 --------- APM Model Size ------------
 Each time step contains
   Objects      :  0
   Constants    :  0
   Variables    :  3
   Intermediates:  0
   Connections  :  0
   Equations    :  1
   Residuals    :  1
 
 @error: Model Expression
 *** Error in syntax of function string: Missing opening parenthesis

Position: 4                   
 tank_1_height-(((tank_1_volume)/(tank_1_area)))
    ?

我希望TANK_1词典中的‘Height’键包含值2.

推荐答案

由于底层APMonitor模型是如何编写和编译成字节码的,所以在gekko中有用于命名变量的保留关键字.这些关键字包括在变量定义的名称tank中使用的tan()函数.变量的命名是可选的,但如果您需要读取运行目录m._path中的gk0_model.apm文件,则会有所帮助.在变量名前加上类似x_这样的前缀可以克服这个错误.

tank_1 = tank('x_tank_1', area = 5, gm = m)

以下是完整的 playbook .

from gekko import GEKKO
m = GEKKO(remote = False)
def tank(tank_id, area, gm): #gm = gekko model
    tank_dict = {}
    tank_dict['id'] = tank_id
    tank_dict['area'] = gm.Param(value = area, name = tank_id + '_area')
    tank_dict['volume'] = gm.FV(value = 10, name = tank_id + '_volume')
    tank_dict['height'] = gm.Var(name = tank_id + '_height')
    
    gm.Equation(tank_dict['height'] == tank_dict['volume']/tank_dict['area'])
    
    return(tank_dict)

tank_1 = tank('x_tank_1', area = 5, gm = m)
m.solve(disp = False)

print('Height: ', tank_1['height'].value[0])

或者,让gekko处理内部变量命名:

from gekko import GEKKO
m = GEKKO(remote = False)
def tank(tank_id, area, gm):
    tank_dict = {}
    tank_dict['id'] = tank_id
    tank_dict['area'] = gm.Param(value = area)
    tank_dict['volume'] = gm.FV(value = 10)
    tank_dict['height'] = gm.Var()
    
    gm.Equation(tank_dict['height'] == tank_dict['volume']/tank_dict['area'])
    
    return(tank_dict)

tank_1 = tank('tank_1', area = 5, gm = m)
m.solve(disp = False)

print('Height: ', tank_1['height'].value[0])

以下是gekko中用于命名变量的其他保留关键字的列表:

abs(x) absolute value |x|
abs2(x) absolute value with MPCC
abs3(x) absolute value with binary variable for switch
acos(x) inverse cosine, cos^-1(x)
acosh(x) inverse hyperbolic cosine, cosh^-1(x)
Array(type,size) array of GEKKO objects
arx auto-regressive exogenous input (time series) model
asin(x) inverse sine, sin^-1(x)
asinh(x) inverse hyperbolic sine, sinh^-1(x)
atan(x) inverse tangent, tan^-1(x)
atanh(x) inverse hyperbolic tangent, tanh^-1(x)
bspline bspline for 2D data
cos(x) cosine
cspline cubic spline for 1D data
erf(x) error function
erfc(x) complementary error function
exp(x) e^x
if3(cond,x1,x2) switch between x1 (cond<0) and x2 (cond>=0)
log(x) log_e (x), natural log
log10(x) log_10 (x), log base 10
max2(x1,x2) maximum value with MPCC
max3(x1,x2) maximum value with binary variable for switch
min2(x1,x2) minimum value with MPCC
min3(x1,x2) minimum value with binary variable for switch
periodic periodic (initial=final) for dynamic problems
pwl piece-wise linear function
sign2(x) signum operator with MPCC
sign3(x) signum operator with binary variable for switch
sin(x) sine
sinh(x) hyperbolic sine
sqrt(x) square root
state_space continuous/discrete and dense/sparse state space
sum summation of elements in a list or numpy array
tan(x) tangent
tanh(x) hyperbolic tangent
vsum(x) vertical sum of a single variable in the data direction

这份名单也在APMonitor Documentation中找到.

Python相关问答推荐

如何在Deliveryter笔记本中从同步上下文正确地安排和等待Delivercio代码中的结果?

如何在msgraph.GraphServiceClient上进行身份验证?

acme错误-Veritas错误:模块收件箱没有属性linear_util'

抓取rotowire MLB球员新闻并使用Python形成表格

追溯(最近最后一次调用):文件C:\Users\Diplom/PycharmProject\Yolo01\Roboflow-4.py,第4行,在模块导入roboflow中

如何在虚拟Python环境中运行Python程序?

Python虚拟环境的轻量级使用

如何使用scipy的curve_fit与约束,其中拟合的曲线总是在观测值之下?

提取相关行的最快方法—pandas

可以bcrypts AES—256 GCM加密损坏ZIP文件吗?

LocaleError:模块keras._' tf_keras. keras没有属性__internal_'''

如何将数据帧中的timedelta转换为datetime

从一个df列提取单词,分配给另一个列

如何在Gekko中处理跨矢量优化

将数字数组添加到Pandas DataFrame的单元格依赖于初始化

对于数组中的所有元素,Pandas SELECT行都具有值

如何在不遇到IndexError的情况下将基数10的整数转换为基数80?

按最大属性值Django对对象进行排序

为什么这个正则表达式没有捕获最后一次输入?

Sknowled线性回归()不需要迭代和学习率作为参数