由于底层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中找到.