Difference between if2() and if3() functions

286 views
Skip to first unread message

Muhammad Ali

unread,
May 4, 2020, 7:14:25 AM5/4/20
to apmonitor
I am new to Gekko and I am trying to solve a MILP problem using Gekko. I am having trouble understanding how if conditions are being implemented.

I don't understand how to implement an if condition, for example

if SolarPower > LoadPower, then SolarShedding = 0
if SolarPower = LoadPower, then SolarShedding = 0 and LoadShedding = 0
if SolarPower < Load Power, then LoadShedding =0

where SolarShedding and LoadShedding are both variables and their outputs should be determined by Gekko. But if any condition is true, then these values should be fixed by the user.

I am unsure how to implement this, and what the difference is between if2 and if3 and how it can affect my model

John Hedengren

unread,
May 4, 2020, 7:37:23 AM5/4/20
to APM Google Groups
Muhammad,

You could use an if statement:

switch = m.if3(SolarPower-LoadPower,0,1)

and then use switch or (1-switch) in your model to turn on or off different parts.

SolarShedding = m.Intermediate(1-switch)
LoadShedding = m.Intermediate(switch)

If SolarPower and LoadPower are continuous variables then there is no difference between SolarPower>LoadPower and SolarPower>=LoadPower because 1e-6 differences are within solver solution tolerance. For the differences between if3 (integer switching) and if2 (MPCC switching), I recommend this page for more information: https://apmonitor.com/me575/index.php/Main/LogicalConditions

You may also want to look at slack variables instead of integer switches. Here is a related question but for thermal and electrical storage: https://stackoverflow.com/questions/61411129/having-optimizer-switch-power-storage-based-on-capacities-and-net-load

Please consider posting this or future questions to StackOverflow with tag [gekko].

Best regards,

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/be9132da-f547-4df4-b818-8820ba462f45%40googlegroups.com.


--
Best regards,

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