How to let Gurobi stop running if the first solution was found and output that solution?
回答済みLets 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
-
Consider using the SolutionLimit parameter.
0
サインインしてコメントを残してください。
コメント
1件のコメント