Python 3.x Gekko中间变量,误差:无等式或不等式的方程
我不认为我完全理解数组中中间变量的使用,我希望我的代码能得到一些帮助 这个方程连同误差一起被公布在Python 3.x Gekko中间变量,误差:无等式或不等式的方程,python-3.x,gekko,Python 3.x,Gekko,我不认为我完全理解数组中中间变量的使用,我希望我的代码能得到一些帮助 这个方程连同误差一起被公布在(-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[
(-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()
解决了这个问题,因为目标函数现在返回一个值而不是一个数组。下面是一个完整的脚本,演示了当前程序中的两个问题
从gekko导入gekko
模型=GEKKO()
x=model.Array(model.Var,10)
yh=model.Array(model.Intermediate,10,等式=无)
对于范围(10)内的i:
yh[i]=中间型(x[i]**2)
模型方程(和(x)==10)
模型Obj(yh)
model.solve()
首先,您正在创建一个中间类型的数组,然后在循环中再次创建它们。这会产生以下错误:
@error: Model Expression
*** Error in syntax of function string: Invalid element: none
Position: 1
none
?
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
?
因为您创建的第一个中间产物具有空白方程式。只需定义一个None
值列表,就可以避免此错误
yh=[None]*10
对于范围(10)内的i:
yh[i]=中间型(x[i]**2)
第二个错误是因为您在目标语句中使用了数组(正如您在回答中已经指出的)。这会产生以下错误:
@error: Model Expression
*** Error in syntax of function string: Invalid element: none
Position: 1
none
?
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()
作为同一函数的更具描述性的版本
从gekko导入gekko
模型=GEKKO()
x=model.Array(model.Var,10)
yh=[无]*10
对于范围(10)内的i:
yh[i]=中间型(x[i]**2)
模型方程(和(x)==10)
模型最小化(模型和(yh))
model.solve()