Skip to content

MINLP with GEKKO - Modeling discrete variables

An answer to this question on the Scientific Computing Stack Exchange.

Question

I'm trying to define a MINLP optimization problem with GEKKO in Python, and I want to use some variables with fixed values.

For my first variable, x1, I need to define the following values (as would be defined in a list): x1 = [19.05, 25.0, 29.3, 30.2]

How can I define these type of variables in my model?

Answer

If they don't have a variable that is constrained to a discrete set you can formulate it like so:

b1 = m.Var(lb=0,ub=1,integer=True) #Binary variable
b2 = m.Var(lb=0,ub=1,integer=True) #Binary variable
b3 = m.Var(lb=0,ub=1,integer=True) #Binary variable
b4 = m.Var(lb=0,ub=1,integer=True) #Binary variable
a  = m.Var()                       #Variable indicating which choice from the set was made
m.Equation(b1+b2+b3+b4==1)
m.Equation(a==19.05*b1+25.0*b2+29.3*b3+30.2*b4)

In reality, you want an SOS1 constraint, but Gekko doesn't appear to use those.

Gekko might not be the best software for this problem! No binary variables, no SOS constraints, no high-level way of specifying a set of values.