Gekko Python: Any way to export the model for debugging purposes?

Viewed 92

I have a gekko model that is not producing the solution I expect. It would be great to print the model in a readable form for debugging purposes (much like printing an LP format from CPLEX).

Is this possible?

My model solves perfectly in Matlab.

1 Answers

Use m.open_folder() to open the run directory where gk0_model.apm is located. This is a text version of the model that Gekko produces. Here is a simple application:

from gekko import GEKKO

m = GEKKO()                 # create GEKKO model

print('--------- Follow local path to view files --------------')
print(m.path)               # show source file path
m.open_folder()
print('-'*56)

u = m.Param(value=5,name='u')  # define parameter
x = m.Var(name='s')            # define variable
m.Equation(x==u)               # define equation
m.options.SOLVER = 1           # change solver (1=APOPT,3=IPOPT)
m.solve(disp=False)
print('x: ' + str(x.value))    # print variable value

The following output is produced by the script:

--------- Follow local path to view files --------------
C:\Users\johnh\AppData\Local\Temp\tmp3oqof67rgk_model0
--------------------------------------------------------
x: [5.0]

Opening gk0_model.apm with a text editor shows the model file in APMonitor format:

Model
Parameters
    u = 5
End Parameters
Variables
    s = 0
End Variables
Equations
    s=u
End Equations
End Model

The APM model can be solved with APM Matlab or through a web-browser interface.

Related