I don't think I fully understand the use of intermediate variables in arrays and would love some help with my code.
Along with the error this equation is posted ((-1)*((((((0.95)*(i371)))*(9))-((int_v2)*(4)))))
, it looks like my objective 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()
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. 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.
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.
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()