# MINLP with GEKKO - Modeling discrete variables

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?

Below is an example of using Gekko (v0.2.4+) to define a SOS1 variable with an objective function to find the minimum in that sequence of values.

from gekko import GEKKO
m = GEKKO()
y = m.sos1([19.05, 25.0, 29.3, 30.2])
m.Obj(y) # select the minimum value
m.solve()
print(y.value)


Additional information on model building functions is available in the Gekko documentation (see Model Building Functions).

• Thank you so much John, that's a good approach because I'm more familiar with GEKKO modelling!! – srgam Aug 19 '19 at 8:26
• Nice work, @John! (Are binary variables and SOS2 forthcoming?) – Richard Aug 19 '19 at 22:36
• I didn't add SOS2 because from what I could gather, it is primarily used for Piecewise linear approximations to Nonlinear functions. I already have a PWL model in Gekko. Let me know if there are other use cases and I can add it. I should also add a binary variable. I added those as a Feature request: github.com/BYU-PRISM/GEKKO/issues/66 – John Hedengren Aug 20 '19 at 3:45

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.

• Thank you so much, it's useful!! Which package able me to develop that??? – srgam Aug 16 '19 at 18:18
• I'd suggest looking into Julia's JuMP, Python's Gurobi solver (if you can get an academic license), and CVXPY. – Richard Aug 16 '19 at 20:09
• If this answer was helpful, feel free to upvote it by clicking the gray arrows on the left-hand side. If it turns out to solve your problem or be the best of several answers, you can click the gray check mark to accept it. – Richard Aug 16 '19 at 20:10
• Thank you, Richard, I will review them. Can Pyomo also model this kind of problem? – srgam Aug 17 '19 at 15:04
• Richard, I just added an SOS1 object to Gekko at github.com/BYU-PRISM/GEKKO/blob/master/gekko/gekko.py SOS1 constraints will be available with the next release. Thanks for your suggestions. I agree that there are many very nice modeling language platforms and would also recommend that Sergio look at those for his problem. – John Hedengren Aug 18 '19 at 6:55