BYU-PRISM / GEKKO

GEKKO Python for Machine Learning and Dynamic Optimization
https://machinelearning.byu.edu
Other
569 stars 102 forks source link

Linear Programming (Integer) - wrong results. #145

Closed ccebinger closed 2 years ago

ccebinger commented 2 years ago

I have troubles with a Integer Linear Programm. Is it a bug, or do I miss some parameters.

from gekko import GEKKO
for i in range(1,25,1):
    m = GEKKO(remote=False)
    x1 = m.Var(1,lb=0,integer=True,fixed_initial=True)
    x2 = m.Var(1,lb=0,integer=True)
    x3 = m.Var(1,lb=0,integer=True,fixed_initial=True)
    m.Equation(10000*x1 + 2500*x2 + 250*x3 >= i);
    selected_L = True
    selected_M = True
    selected_S = True
    if selected_L: 
        m.Equation(x1 >= 0)
    else: 
        m.Equation(x1 == 0)
    if selected_M:
        m.Equation(x2 >= 0)
    else:
        m.Equation(x2 == 0)
    if selected_S:
        m.Equation(x3 >= 0)
    else:
        m.Equation(x3 == 0)
    m.Minimize(6400*x1+1900*x2+200*x3)
    m.solve(disp=False)
    print(f"i {i}")
    print(f"x1: {x1.VALUE}")
    print(f"x2: {x2.VALUE}")
    print(f"x3: {x3.VALUE}")
i 1
x1: [0.0]
x2: [0.0]
x3: [0.0]
i 2
x1: [0.0]
x2: [0.0]
x3: [0.0]
i 3
x1: [0.0]
x2: [0.0]
x3: [0.0]
i 4
x1: [0.0]
x2: [0.0]
x3: [0.0]
i 5
x1: [0.0]
x2: [0.0]
x3: [0.0]
i 6
x1: [0.0]
x2: [0.0]
x3: [0.0]
i 7
x1: [0.0]
x2: [0.0]
x3: [0.0]
i 8
x1: [0.0]
x2: [0.0]
x3: [0.0]
i 9
x1: [0.0]
x2: [0.0]
x3: [0.0]
i 10
x1: [0.0]
x2: [0.0]
x3: [0.0]

for i 1-10 i would expect [0,0,1] instead of [0,0,0].

APMonitor commented 2 years ago

Try adding these two options:

    m.solver_options = ['minlp_integer_tol 1.0e-6']
    m.options.SOLVER=1

Switch to the MINLP solver APOPT. The default solver (IPOPT) doesn't not calculate integer solutions. The minlp_integer_tol is the tolerance for a value to be considered an integer. The default is 0.01 so if x1=0.001 then it is rounded to 0 and accepted as an integer solution.

Please post future questions to Stack Overflow with tag [gekko]: https://stackoverflow.com/questions/tagged/gekko

APMonitor commented 2 years ago

If you are still getting non-optimal results, try decreasing the minlp_gap_tol. The default is 1.0e-2. This can greatly increase the solution time, however.

minlp_gap_tol 1.0e-2 - gap is the spread between the lowest candidate leaf (obj_r=non-integer solution) and the best integer solution (obj_i). When the gap is below the minlp_gap_tol, the best integer solution is returned. The gap is defined as shown here: https://apmonitor.com/wiki/index.php/Main/OptionApmSolver

ccebinger commented 2 years ago

Thank You!

How do i can free the temporary space after one execution.

I searched for "close", "disconnect", "free", "temporary" in documentation and I found nothing

Error: forrtl: No space left on device
forrtl: severe (38): error during write, unit 10, file /tmp/tmpmcc83qssgk_model94342/APOPT.out

Stack trace terminated abnormally.

Error: 'results.json' not found. Check above for additional error details

APMonitor commented 2 years ago

Try the m.cleanup() function. Here is a link to the documentation. You can also reduce the temporary directory size with m.options.DIAGLEVEL=0.