How to let Gurobi stop running if the first solution was found and output that solution?
AnsweredLets say i have a MILP for neural network verification. I just want to quickly check if there exists a solution that satisfies all my constraints without having to find the optimal solution.
I read some articles about this problem and write a function like this trying to output all the feasible solutions but it still outputs nothing and leads to timeout:
def mycallback(model, where):
time_interval = 5
if where == gp.GRB.Callback.MIPSOL:
current_time = time.time()
if not hasattr(model, '_last_output_time') or current_time - model._last_output_time > time_interval:
obj = model.cbGet(gp.GRB.Callback.MIPSOL_OBJ)
sol = model.cbGetSolution(model.getVars())
print(f"Current OBJ: {obj}")
print("SOL:", sol)
model._last_output_time = current_time
model_1.optimize(mycallback)
time_interval = 5
if where == gp.GRB.Callback.MIPSOL:
current_time = time.time()
if not hasattr(model, '_last_output_time') or current_time - model._last_output_time > time_interval:
obj = model.cbGet(gp.GRB.Callback.MIPSOL_OBJ)
sol = model.cbGetSolution(model.getVars())
print(f"Current OBJ: {obj}")
print("SOL:", sol)
model._last_output_time = current_time
model_1.optimize(mycallback)
0
-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Consider using the SolutionLimit parameter.
0
Post is closed for comments.
Comments
2 comments