2

I am trying to solve a MINLP optimization problem in Python GEKKO. There are two things that I would like to ask you.

Firstly; I examined the link below thoroughly and tried to understand changes made by Mr. Hedengren.

Python GEKKO MINLP optimization of energy system: How to build intermediates that are 2D arrays

In this example’s .apm model file with a text editor; there are " connections " . I understood the operation there, however I do not get which part in the code brings those connections. I have tried to follow this example; I used the similar intermediate structure but never got something like those connections in my own model.

The other thing; in my code with intermediates I have sum functions. Again in the text file version of the model I see that summation always starts with 0 in the beginning, then it adds variables to each other. For example:

.....[(((((((((((0+(((((((((1+i5))^(-9)))(2711)))(v230)))*(int_v340)))+

in the model; T=10 G=11 and the equation above is so:

INV=m.Intermediate ([[sum (pow((1+r), -t)*IC[g]*Z[g,t]*Y[g,t] for g in range(G) )]for t in range(T) ] )

Operations look correct however I don't understand why there is 0 (zero) in every brackets ? It gives invalid element error and it points the variable with the last t value...

*** Error in syntax of function string: Invalid element: v230

This variable (v230) stands for Z[11,10]. I figured out that it always gives an error for the last year which means the highest t value.

Also, if I use m.sum instead of sum; it gives this error: " x must be a python list of GEKKO parameters, variables, or expressions"

I would be very thankful if you could give me a hint. It might not be such a big problem however I could not find any solution by myself.

Thank you for your time and understanding.

Best regards

mormery
  • 21
  • 1
  • Thanks for posting the question. Could you post a Minimal, Reproducible example that shows the error? https://stackoverflow.com/help/minimal-reproducible-example It helps us to suggest a fix. You can include the code with three Grave Accents https://en.wikipedia.org/wiki/Grave_accent at the beginning and end. – John Hedengren Feb 06 '21 at 23:09

1 Answers1

0

The Intermediates always put a zero at the beginning just in case there are no arguments in the expression. Here is a simple Gekko script that shows the use of Intermediates and m.sum().

from gekko import GEKKO
m = GEKKO()
x = m.Array(m.Var,3)
m.Equation(m.sum(x)==3)
y = m.Intermediate(sum(x))
m.Minimize(y)
for i,xi in enumerate(x):
    m.Minimize((xi-i)**2)
m.solve()
m.open_folder()
print(x)

This is the gk_model0.apm model file that is produced by Gekko and compiled by APMonitor into byte-code.

Model
Variables
    v1 = 0
    v2 = 0
    v3 = 0
End Variables
Intermediates
    i0=(((0+v1)+v2)+v3)
End Intermediates
Equations
    ((v1+v2)+v3)=3
    minimize i0
    minimize (((v1-0))^(2))
    minimize (((v2-1))^(2))
    minimize (((v3-2))^(2))
End Equations
End Model

If the x numpy array is converted to a list with x=list(x) then it creates an object and connections to those objects as shown in the documentation. This is the APMonitor model.

Model
Variables
    v1 = 0
    v2 = 0
    v3 = 0
    v4 = 0
End Variables
Intermediates
    i0=(((0+v1)+v2)+v3)
End Intermediates
Equations
    v4=3
    minimize i0
    minimize (((v1-0))^(2))
    minimize (((v2-1))^(2))
    minimize (((v3-2))^(2))
End Equations
Connections
    v1 = sum_1.x[1]
    v2 = sum_1.x[2]
    v3 = sum_1.x[3]
    v4 = sum_1.y
End Connections
Objects
    sum_1 = sum(3)
End Objects
End Model
TexasEngineer
  • 684
  • 6
  • 15
  • 1
    Thank you @TexasEngineer I have been trying to shorten my code. At least I know now that zero is not the cause of error I ended up. Also thank you for the link. I had missed that part about connetcions. – mormery Feb 08 '21 at 07:20