Obtaining the value of a 'GK_Operators' class

Viewed 26

The main purpose of this question is to figure out how to obtain the value of what seems to be a 'GK.Operator'. I am new in the gekko environment and I may be not specific enough but I will try my best.

Here it is an example code

from gekko import GEKKO
from numpy import triu_indices, diag_indices, full

#Create a gekko model
model=GEKKO()

n=2
idx=triu_indices(n,1)

#Create a 1xn array
T=model.Array(model.CV, n)

#Create a nxn array
R = full((n, n), 0, dtype=object)

#Fill only the upper and lower triangle of the R matrix
if n>1:
    R[idx] = model.FV(name='R')
    R[idx[::-1]] = R[idx]
    
#Same as R array
Q = full((n, n), 0, dtype=object)
Q[diag_indices(n)] = model.Const(0)
if n>1:
    Q[idx] = (T[idx[1]] - T[idx[0]]) / R[idx]
    Q[idx[::-1]] = -Q[idx]

#Assign values to T and R
T[1].VALUE,T[0].VALUE=25,12
R[0,1].VALUE=4

print(R)

model.solve(disp=False)

print(R)

print(Q[0,1].VALUE)
print(T[1].MODEL,T[0].MODEL,R[0,1].VALUE)
print(type(R[0,1]),
type(Q[0,1]))

First of all, I think that it would be useful to say that this code comes from a bigger one that simulates a dynamic system, there is not an objective function and therefore is not an optimization problem.

The problem here, is that I can not obtain the value of any of the items of the Q array. As long as I am concerned, the attribute .VALUE or .MODEL will return the value of a gekko variable. However, it seems like it is not working with the variable Q[0,1] which is supposed to be equal to the result of the operation (T[1]-T[0])/R[0,1] or (25-12)/4. When asking for that value with Q[0,1].VALUE a 0 is returned, but that is clearly not the result of that operation. I thought that the class of that variable could have something to do with this, and when checking with type(Q[0,1]), it returns a GK.Operator. I am not sure about what really a GK.Operator is, but I guess that using the attributes .VALUE or .MODEL will not work. It is quite important for me to be able to obtain the values of Q.

In addition, there is another problem with the array R. Before solving the model, that array is what it is supposed to be. However, after solving the model some items of that array become themselves arrays.

I hope that you can help me understanding these doubts. Thanks for reading.

1 Answers

The Q value needs to be a Gekko Var, Param, or Intermediate to use .value. Here is a simplified version of the code that works:

from gekko import GEKKO
from numpy import triu_indices, diag_indices, full

#Create a gekko model 
model=GEKKO()

#Create a 1xn array
T=model.Array(model.Var,2)

#Create a nxn array
R = full((2,2), 0, dtype=object)

#Fill only the upper and lower triangle of the R matrix
R[0,1] = model.FV(name='R')
R[1,0] = R[0,1]

#Same as R array               
Q = full((2,2), 0, dtype=object)
Q[0,0] = 0
Q[1,1] = 0
Q[0,1] = model.Intermediate((T[1] - T[0]) / R[0,1])
Q[1,0] = model.Intermediate(-Q[0,1])

#Assign values to T and R                                                                               
T[1].VALUE,T[0].VALUE=25,12
R[0,1].VALUE=4

print('R before solve: ',R)

model.solve(disp=False)

print('R after solve: ',R)

print('Q[0,1]:',Q[0,1].value)
print('Q[1,0]:',Q[1,0].value)
print('T[1],T[0]:',T[1].value,T[0].value)
print('R[0,1] type:',type(R[0,1]))
print('Q[0,1] type:',type(Q[0,1]))

Results:

R before solve:  [[0 4]
 [4 0]]
R after solve:  [[0 [4.0]]
 [[4.0] 0]]
Q[0,1]: [3.25]
Q[1,0]: [-3.25]
T[1],T[0]: [25.0] [12.0]
R[0,1] type: <class 'gekko.gk_parameter.GK_FV'>
Q[0,1] type: <class 'gekko.gk_operators.GK_Intermediate'>
Related