Gekko class returns an expression not a value

Viewed 58

I have problems with the GEKKO class: I have to use functions like m.exp and m.log but this generates me a problem after. Here is a part of my code:

import numpy as np
from math import sinh, cosh
from scipy.integrate import quad
import csv

m = GEKKO()

def Antoine(T, C1, C2, C3, C4, C5):

    
    P = m.exp(C1 + (C2 / T) + (C3 * m.log(T)) + (C4 * (T ** C5)))

    return (P / 1000) # kPa

When I use this function (latter in the code) it returns a gekko class which I can't operate after. This is from the VSC debugger:

((((exp(((66.60897125778965+((-9.2194)*(log(393.15))))+1.07887711905)))/(1000)))/(200))

This is the type that return when I use type()

<class 'gekko.gk_operators.GK_Operators'>

I need the value of that, not the expression, I need a float type. If I try to use the method .VALUE it returns Zero 0.

I hope someone could help me with this.

1 Answers

Try using the m.solve() command to produce values. Gekko builds a symbolic version of each expression to use automatic differentiation. After the solve command, the values appear.

from gekko import GEKKO
m = GEKKO()
def Antoine(T, C1, C2, C3, C4, C5):
    P = m.exp(C1 + (C2 / T) + (C3 * m.log(T)) + (C4 * (T ** C5)))
    return (P / 1000) # kPa

T = m.Var(lb=100)
P = m.Intermediate(Antoine(T,0.1,1.0,0.2,0.3,0.4))
# find temperature where pressure is 101.325kPa
m.Equation(P==101.325)

m.solve()

print('Pressure: ', P.value[0])
print('Temperature: ', T.value[0])

The result with the arbitrary Antoine coefficients is:

Number of Iterations....: 10

                                   (scaled)                 (unscaled)
Objective...............:   0.0000000000000000e+00    0.0000000000000000e+00
Dual infeasibility......:   0.0000000000000000e+00    0.0000000000000000e+00
Constraint violation....:   2.5554220428603003e-07    2.5554220428603003e-07
Complementarity.........:   0.0000000000000000e+00    0.0000000000000000e+00
Overall NLP error.......:   2.5554220428603003e-07    2.5554220428603003e-07


Number of objective function evaluations             = 12
Number of objective gradient evaluations             = 11
Number of equality constraint evaluations            = 18
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 11
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 10
Total CPU secs in IPOPT (w/o function evaluations)   =      0.004
Total CPU secs in NLP function evaluations           =      0.001

EXIT: Optimal Solution Found.
 
 The solution was found.
 
 The final value of the objective function is   0.000000000000000E+000
 
 ---------------------------------------------------
 Solver         :  IPOPT (v3.12)
 Solution time  :   1.020000000426080E-002 sec
 Objective      :   0.000000000000000E+000
 Successful solution
 ---------------------------------------------------
 
Pressure:  101.32500026
Temperature:  5926.9919202
Related