concerting legacy functions to Gekko

23 views
Skip to first unread message

ThomasR

unread,
Sep 21, 2020, 4:02:00 PM9/21/20
to apmonitor

Dear Gekko experts,

I'm trying to convert some legacy functions (often liquid properties) to Gekko in order to use them in process simulation/optimization models.
Usually, such old functions contain many helper variables, loops, if..else's etc.
I have tried to convert them to Gekko with as few changes as possible, using Gekko's 'if2' object as a replacement for the normal if..else parts.

I'm unsure if such an approach is really helpful/efficient...

1.) How does Gekko handle all those helper variables that are used inside the function definitions? Do I have to use Gekko intermediate variables (in order not to clutter the actual model with those helper variables)?

2.) Is there an elegant way in Gekko to handle conditional statements (with several expressions in each block) like this:

     def my_function(A, B):
         if A>B:
            aux1 = ....
            aux2 = .....
            return( some expression involving aux1, aux2 etc. )

         else:
            aux3 = ....
            aux4 = ....
            return( some expression involving aux3, aux4 etc. )

3.) Sometimes functions are not obviously translated into Gekko... then Gekko's cspline or bspline objects seem helpful. However, they seem to establish fixed links between variables... Is there a way to wrap these objects in a function which can be easily called several times within a model? Something like:

TempRange =  list(range(-15,145,5))
CpRange = [4.29403, 4.25688, 4.23358, 4.21944, 4.20495, 4.19545, 4.1891, 4.1848, 4.1819, 4.18002, 4.17895, 4.17886, 4.17877, 4.17956, 4.18089, 4.18277, 4.18517, 4.1881, 4.19155, 4.19552, 4.20001, 4.20502, 4.21057, 4.21664, 4.22323, 4.23036, 4.23807, 4.24637, 4.25528, 4.26484, 4.27508, 4.28604]
#cp_water_interp = scipy.interpolate.interp1d(TempRange, CpRange)

def heat_capacity_water(T_degC):
    m.cspline(T_degC, cp_water, TempRange, CpRange)
   
    return(cp_water)


Thanks for your patience and any hint...
Thomas

John Hedengren

unread,
Sep 23, 2020, 4:25:44 PM9/23/20
to APM Google Groups
Thomas,

Here is more information on how Gekko handles MPCCs and Logical conditions: https://apmonitor.com/me575/index.php/Main/LogicalConditions  I get better results with the Mixed Integer form versus the MPCC form but it can take longer to solve. If the conditional statement doesn't change with the model solution then you can dynamically define your model with regular conditional statements during the model build process. However, if the conditional statements depend on variables in your model then you'll need to use a different strategy.

Here is a question that is related to yours. I'll give some suggestions on StackOverflow in the next day or two.


-John Hedengren

--
--
APMonitor user's group e-mail list.
- To post a message, send email to apmo...@googlegroups.com
- To unsubscribe, send email to apmonitor+...@googlegroups.com
- Visit this group at http://groups.google.com/group/apmonitor
---
You received this message because you are subscribed to the Google Groups "apmonitor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to apmonitor+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/apmonitor/b7a65419-3aa8-4557-b9c9-b09f37be440fn%40googlegroups.com.


--
Best regards,

John Hedengren
GEKKO Optimization Suite
Reply all
Reply to author
Forward
0 new messages