gpt4 book ai didi

python-3.x - Gekko 中间变量,错误 : equation without equality or inequality

转载 作者:行者123 更新时间:2023-12-04 15:25:53 26 4
gpt4 key购买 nike

我不认为我完全理解数组中中间变量的使用,希望对我的代码有所帮助。

这个等式连同错误一起发布 ((-1)*((((((0.95)*(i371)))*(9))-((int_v2)*(4))) )),看起来像我的目标函数

    yh = model.Array(model.Intermediate,(10),equation=None)
for i in range(10):
yh[i] = model.Intermediate(x[i]*f[i]*0.1) #x,f are variable arrays of size 10
y1 = model.Array(model.if3, (10), x1=1, x2=0, condition=sum(yh)-d) #d is a constant array of size 10

y2 = model.Array(model.if3, (10), x1=1, x2=0, condition=-1*(sum(yh)-lb)) #lb is a constant array of size 10

model.Equation(sum(x)==10)
model.options.IMODE = 3
model.options.SOLVER = 1
m2 = model.Array(model.Intermediate,(10,10),equation=None)

for i in range(10):
for j in range(10):
m2[i][j] = model.Intermediate(m[i][j]*x[i]*0.1*y1[j]*y2[j]) #m is a 10x10 constant array, i'm trying to multiply every element in a row
#with the corresponding x value, and every element in a column with the corresponding y value
r = model.Array(model.Intermediate,(10),equation=None)

for i in range(10):
r[i]= model.Intermediate(sum(m2[j][i] for j in range(10))) #im trying to get the sum of each column

model.Obj(-1*(0.95*r*c2-x*c1)) #c1,c2 are constant arrays; x is a variable array

model.solve()

最佳答案

这是一个完整的脚本,它演示了您当前程序的两个问题。

from gekko import GEKKO
model = GEKKO()
x = model.Array(model.Var,10)
yh = model.Array(model.Intermediate,10,equation=None)
for i in range(10):
yh[i] = model.Intermediate(x[i]**2)
model.Equation(sum(x)==10)
model.Obj(yh)
model.solve()

首先是您正在创建一个 Intermediate 类型的数组,然后在您的循环中再次创建它们。这给出了错误:

 @error: Model Expression
*** Error in syntax of function string: Invalid element: none

Position: 1
none
?

因为您创建的第一个中间体具有空白方程式。您可以通过定义 None 值列表来避免此错误。

yh = [None]*10
for i in range(10):
yh[i] = model.Intermediate(x[i]**2)

第二个错误是因为您在目标陈述中使用了数组(正如您在回答中已经指出的那样)。这给出了错误:

 Warning: there is insufficient data in CSV file 136.36.211.159_gk_model0.csv
@error: Model Expression
*** Error in syntax of function string: Missing operator

Position: 2
0,0,0,0,0,0,0,0,0,0
?

正如您正确指出的那样,您可以添加总和以将项添加到单个项中。您还可以拥有多个 model.Obj() 函数或 model.Minimize() 作为同一函数的更具描述性的版本。

from gekko import GEKKO
model = GEKKO()
x = model.Array(model.Var,10)
yh = [None]*10
for i in range(10):
yh[i] = model.Intermediate(x[i]**2)
model.Equation(sum(x)==10)
model.Minimize(model.sum(yh))
model.solve()

关于python-3.x - Gekko 中间变量,错误 : equation without equality or inequality,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62244082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com