In Julia, pressing Ctrl+C usually kills the entire Julia process instead of just stopping Gurobi.
My goal is:
Stop the optimization at any arbitrary time (not using MIPGap, TimeLimit, or NodeLimit).
Still retrieve the best feasible solution found so far.
I’ve seen that Gurobi supports callbacks via JuMP/MOI. Is it possible to have something like a user interrupt (e.g., pressing a key) that triggers a callback to terminate the solver gracefully?
If yes, could someone show me the minimal working example of how to set this up in Julia?
I think the requirement is somewhat unusual.
But you can use GRBterminate(backend(model)) within a callback, as written in GitHub - jump-dev/Gurobi.jl: A Julia interface to the Gurobi Optimizer.
As for achieving your “arbitrary” goal…
Since I have no idea what a cloud solve is like… But in julia maybe you can try some asynchronous programming thing:
import JuMP, Gurobi, SparseArrays
N = 50 # 754 seconds
Q = 1. * SparseArrays.dropzeros(
SparseArrays.spdiagm(
[i => rand(-9:9, N - i) for i in 0:div(N, 2)]...
)
);
m = JuMP.direct_model(Gurobi.Optimizer())
JuMP.@variable(m, rand(-7:-3) <= y[1:N] <= rand(3:7));
JuMP.@variable(m, rand(-9:-5) <= x[1:N] <= rand(5:9));
JuMP.@objective(m, Min, (x'Q)y);
# Run the solve async-ly
Threads.@spawn JuMP.optimize!(m)
# when you want to interrupt, execute this command in the REPL
Gurobi.GRBterminate(JuMP.backend(m))
# you can check the current status
JuMP.solution_summary(m)
JuMP.value.(x)
i’m using server base gurobi license, not on local machine.
Do you mean the Gurobi instant cloud? Or just the WLS license?
If you’re using the Gurobi instant cloud, the Gurobi documentation sounds like it should support termination, so if CTRL+C isn’t working, then that is a bug we should fix: Callbacks - Gurobi Remote Services Guide
Do you have a reproducible example of your model? What options did you set? (You shouldn’t are any sensitive API keys, etc.)
Hello @odow, thank you for your response. However, I’m experiencing an issue specifically with VS Code and its integrated terminal. When I run the code through the VS Code terminal, it doesn’t accept any input or respond until Gurobi finishes solving. In contrast, when I execute the same code via PowerShell, the “Ctrl + C” interrupt command works as expected.