How to terminate gurobi solver?
AnsweredHello everyone,
I'm currently working on solving a mixed-integer linear problem using Gurobi. The decision variables in this problem include both continuous and binary variables, with the binary variables representing device switching. The main focus of the problem lies in optimizing these binary variables.
In certain scenarios, Gurobi's presolve feature identifies that the initial switches are already optimal, resulting in the elimination of all binary variables. In such cases, it is unnecessary to proceed with solving the problem further. It would be more efficient to terminate the Gurobi solver if presolve reduces the number of binary variables to zero.
Set parameter MIPGap to value 0.01
Gurobi Optimizer version 10.0.0 build v10.0.0rc2 (win64)
CPU model: Intel(R) Core(TM) i7-8665U CPU @ 1.90GHz, instruction set [SSE2|AVX|AVX2]
Thread count: 4 physical cores, 8 logical processors, using up to 8 threads
Optimize a model with 419632 rows, 411245 columns and 1154749 nonzeros
Model fingerprint: 0x8b0c61e1
Variable types: 357230 continuous, 54015 integer (54015 binary)
Coefficient statistics:
Matrix range [1e-04, 4e+06]
Objective range [1e+00, 1e+00]
Bounds range [1e-04, 3e+05]
RHS range [1e-04, 4e+01]
Presolve removed 250513 rows and 208919 columns
Presolve time: 3.33s
Presolved: 169119 rows, 202326 columns, 495173 nonzeros
Variable types: 202326 continuous, 0 integer (0 binary)
Deterministic concurrent LP optimizer: primal and dual simplex
Showing first log only...
-
Hi Hussein,
It is not possible to retrieve the number of (discrete) variables removed by presolve during the optimization.
However, you could implement a MESSAGE callback, where you scan for the string "Variable types" and check whether the number of integers/binaries is 0. You can then call the terminate() method from within the callback to terminate the optimization run.
Best regards,
Jaromił1 -
This one now works! Thank you Jaromił Najman
def mycallback(model, where):
if where == GRB.Callback.MESSAGE:
if 'Variable types' in m.cbGet(GRB.Callback.MSG_STRING):
if '0 binary' in m.cbGet(GRB.Callback.MSG_STRING):
m.terminate()
m.optimize(mycallback)0
Please sign in to leave a comment.
Comments
2 comments