Search code examples
linear-programminggekko

Linear Programming - Question - Python Gekko


I am recently learning Python Gekko and I am very very new to linear programming, so excuse my ignorance in certain topics.

I have a variable which should have a value of either 0 or should be greater than 20.

I later learnt that this is called a semi-continuous variable. My questions are as below

  1. Is it possible to convert the above condition into a linear equation
  2. By any chance does Gekko support the semi-continuous variables as I could not find anything about it in the documentation.

Solution

  • You can use the if3() function to enforce that constraint. That function uses a binary variable for the switch condition so it transforms the problem from a linear programming (LP) problem to a mixed integer linear programming (MILP) problem.

    result

    from gekko import GEKKO
    import numpy as np
    import matplotlib.pyplot as plt
    
    m = GEKKO()
    
    p = m.Param(np.linspace(0,50))
    y = m.if3(p-20,0,p)
    
    m.options.IMODE=2
    m.solve()
    
    # plot solution
    plt.plot(p.value,'r-',lw=3)
    plt.plot(y.value,'b.-')
    plt.show()