Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I terminate model in the callback function? #381

Closed
Wenbo11 opened this issue Dec 23, 2020 · 3 comments · Fixed by #382
Closed

How can I terminate model in the callback function? #381

Wenbo11 opened this issue Dec 23, 2020 · 3 comments · Fixed by #382

Comments

@Wenbo11
Copy link

Wenbo11 commented Dec 23, 2020

Hey,

I want to create multiple-terminating criteria in Gurobi like this example {https://support.gurobi.com/hc/en-us/articles/360047717291} in gurobipy.
I tried using GRBterminate(model) in callback function but an error raised
MethodError: no method matching unsafe_convert(::Type{Ptr{Nothing}}, ::UnitCommitmentModel) Closest candidates are: unsafe_convert(::Type{Ptr{Nothing}}, !Matched::Base.RefValue{T}) where T at refvalue.jl:30 unsafe_convert(::Type{Ptr{Nothing}}, !Matched::Base.RefArray{T,A,R} where R where A<:(AbstractArray{T,N} where N)) where T at refpointer.jl:90 unsafe_convert(::Type{Ptr{Nothing}}, !Matched::Base.CFunction) at c.jl:36 ...
How can I terminate model in the callback function?

Thanks!

@odow
Copy link
Member

odow commented Dec 23, 2020

Please follow the example: https://github.com/jump-dev/Gurobi.jl#callbacks

Here's an example calling GRBterminate:

function test_GRBterminate()
model = Gurobi.Optimizer(GRB_ENV)
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variable(model)
MOI.set(model, Gurobi.CallbackFunction(), (cb_data, cb_where) -> begin
GRBterminate(model)
end)
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == MOI.INTERRUPTED
end

Note that if you are using JuMP, you need to use direct_model. I don't know what ::UnitCommitmentModel is, so it appears you are trying to use something other than a direct model via JuMP, or a Gurobi.Optimizer.

@Wenbo11
Copy link
Author

Wenbo11 commented Dec 23, 2020

Thanks for your replying, I attach my toy example:

function stop_criteria(cb_data, cb_where::Cint)
    if cb_where == GRB_CB_MIP
        
        primal_bound = Ref{Cdouble}() # a pointer
        GRBcbget(cb_data, cb_where, GRB_CB_MIP_OBJBST, primal_bound)
        
        dual_bound = Ref{Cdouble}()
        GRBcbget(cb_data, cb_where, GRB_CB_MIP_OBJBND, dual_bound)
        
        gap = abs((primal_bound[] - dual_bound[]) / primal_bound[])
        
        if gap < 0.03
            println("Terminate the solving ...")
            MOI.set(jump_model, Gurobi.CallbackFunction(), (cb_data, cb_where) -> begin
                GRBterminate(jump_model)
            end)
#             GRBterminate(jump_model)
        end
    end
end
MOI.set(jump_model, Gurobi.CallbackFunction(), stop_criteria)
JuMP.optimize!(jump_model)

Then I got the error:

MethodError: no method matching unsafe_convert(::Type{Ptr{Nothing}}, ::Model)
Closest candidates are:
  unsafe_convert(::Type{Ptr{Nothing}}, !Matched::Base.RefValue{T}) where T at refvalue.jl:30
  unsafe_convert(::Type{Ptr{Nothing}}, !Matched::Base.RefArray{T,A,R} where R where A<:(AbstractArray{T,N} where N)) where T at refpointer.jl:90
  unsafe_convert(::Type{Ptr{Nothing}}, !Matched::Base.CFunction) at c.jl:36
  ...

Looks that the error will raise even I use

     MOI.set(model, Gurobi.CallbackFunction(), (cb_data, cb_where) -> begin 
         GRBterminate(model) 
     end) 

in the example.

Any idea to solve it?
Many thanks!

@odow
Copy link
Member

odow commented Dec 23, 2020

See #382

@odow odow closed this as completed in #382 Dec 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants