简体   繁体   English

Gekko 中间变量,错误:不等式或不等式的方程

[英]Gekko Intermediate Variable,error: equation without equality or inequality

I don't think I fully understand the use of intermediate variables in arrays and would love some help with my code.我认为我不完全理解 arrays 中中间变量的使用,并且希望对我的代码有所帮助。

Along with the error this equation is posted ((-1)*((((((0.95)*(i371)))*(9))-((int_v2)*(4))))) , it looks like my objective function随着这个等式的错误发布((-1)*((((((0.95)*(i371)))*(9))-((int_v2)*(4))))) ,它看起来像我的目标 function

    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()
```model.Obj(-1*(0.95*sum(r*c2)-sum(x*c1)))```

solved the issue as the objective function returns one value now not an array解决了作为目标的问题 function 现在返回一个值而不是数组

Here is a complete script that demonstrates the two issues with your current program.这是一个完整的脚本,演示了当前程序的两个问题。

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()

The first is that you are creating an array of Intermediate types and then creating them again in your loop.首先是您正在创建一个Intermediate类型数组,然后在循环中再次创建它们。 This gives the error:这给出了错误:

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

Position: 1                   
 none
 ?

because the first Intermediates that you create have blank equations.因为您创建的第一个中间体有空白方程。 You can avoid this error by just defining a list of None values.你可以通过定义一个None值列表来避免这个错误。

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

The second error is because you are using an array in the objective statement (as you already noted in your answer).第二个错误是因为您在目标陈述中使用了一个数组(正如您在回答中已经指出的那样)。 This gives the error:这给出了错误:

 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
  ?

As you correctly noted, you can add a summation to add the terms into a single term.正如您正确指出的那样,您可以添加总和以将术语添加到单个术语中。 You can also have multiple model.Obj() functions or model.Minimize() as a more descriptive version of the same function.您还可以拥有多个model.Obj()函数或model.Minimize()作为相同 function 的更具描述性的版本。

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()

Error: Intermediate variable with no equality (=) expression错误:没有相等 (=) 表达式的中间变量

m = GEKKO('Model')

variables = []
for i in crude_anames:
    variables.append(m.Var(lb = 0, ub = 1, name = f"crude_{i}"))
    

crude_density = [None] * len(variables)
for i in range(0, len(variables)):
    crude_density [i] = feed_density.iloc[:, 4 + i] * variables[i]
crude_density = m.Intermediate(m.sum(crude_density), 'crude_density')

Hi, I have the similar issue which I cannot solve.嗨,我有类似的问题,我无法解决。 I create Intermediate variable (crude_density) which will be used in further calculations.我创建了中间变量(crude_density),它将用于进一步的计算。 However, when I use the variable I get the same error as "@error: Intermediate Definition Error: Intermediate variable with no equality (=) expression".但是,当我使用该变量时,我得到与“@error:中间定义错误:没有相等 (=) 表达式的中间变量”相同的错误。 When I use just a constant value instead of variable, the model runs well.当我只使用一个常数值而不是变量时,model 运行良好。 Could you please help me on this issue?你能帮我解决这个问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM