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?


2 Answers 2


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

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

  • 2
    $\begingroup$ Thank you so much John, that's a good approach because I'm more familiar with GEKKO modelling!! $\endgroup$
    – srgam
    Aug 19, 2019 at 8:26
  • $\begingroup$ Nice work, @John! (Are binary variables and SOS2 forthcoming?) $\endgroup$
    – Richard
    Aug 19, 2019 at 22:36
  • 1
    $\begingroup$ 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 $\endgroup$ Aug 20, 2019 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

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.

  • $\begingroup$ Thank you so much, it's useful!! Which package able me to develop that??? $\endgroup$
    – srgam
    Aug 16, 2019 at 18:18
  • 1
    $\begingroup$ I'd suggest looking into Julia's JuMP, Python's Gurobi solver (if you can get an academic license), and CVXPY. $\endgroup$
    – Richard
    Aug 16, 2019 at 20:09
  • $\begingroup$ 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. $\endgroup$
    – Richard
    Aug 16, 2019 at 20:10
  • $\begingroup$ Thank you, Richard, I will review them. Can Pyomo also model this kind of problem? $\endgroup$
    – srgam
    Aug 17, 2019 at 15:04
  • 3
    $\begingroup$ 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. $\endgroup$ Aug 18, 2019 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.