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

ListOfModelAttributesSet does not include callbacks #579

Closed
bachdavi opened this issue Oct 24, 2024 · 9 comments · Fixed by #584
Closed

ListOfModelAttributesSet does not include callbacks #579

bachdavi opened this issue Oct 24, 2024 · 9 comments · Fixed by #584
Labels
bug Wrapper: MathOptInterface Issue is specific to MOI wrapper

Comments

@bachdavi
Copy link

bachdavi commented Oct 24, 2024

Hey!

As the title says, we get an error when printing in LP format with Gurobi:

ERROR: MathOptInterface.UnsupportedAttribute{Gurobi.CallbackFunction}: Attribute Gurobi.CallbackFunction() is not supported by the model.
Stacktrace:

Here is a MRE on the latest version 1.3.1 of Gurobi.jl.

import Gurobi
import MathOptInterface as MOI

model = MOI.instantiate(() -> Gurobi.Optimizer(); with_cache_type = Float64, with_bridge_type = Float64)
MOI.set(model, Gurobi.CallbackFunction(), () -> nothing)
MOI.Utilities.loadfromstring!(
           model,
           """
       variables: x, y, z
       minobjective: 1.0 * x + 1.0 * y + 1.0 * z
       c1: x + y == 2.0
       c2: x + y + z >= 0.0
       c3: 1.0 * x * x + -1.0 * y * y + -1.0 * z * z >= 0.0
       x >= 0.0
       y >= 0.0
       z >= 0.0
       """)
print_options = (; format = MOI.FileFormats.FORMAT_LP)
dest = MOI.FileFormats.Model(; print_options...)
MOI.copy_to(dest, model)

If I omit the line that sets the Gurobi.CallbackFunction, printing works. Also using the Gurobi.Optimizer() directly instead of a cached optimizer works.

Edit: I thought we could set CallbackFunction to nothing before printing but that unfortunately resets the optimizer state. Printing works correctly in that case.

Thank you!

@odow
Copy link
Member

odow commented Oct 24, 2024

This error is expected behavior. The LP writer does not support serializing Gurobi.CallbackFunction to disk.

I think this is just asking for a way to unset the CallbackFunction.

Gurobi.Optimizer works because we don't properly report that a callback is set (this is a separate bug):

function MOI.get(model::Optimizer, ::MOI.ListOfModelAttributesSet)
if MOI.is_empty(model)
return Any[]
end
attributes = Any[]
if model.objective_sense !== nothing
push!(attributes, MOI.ObjectiveSense())
end
if model.is_objective_set
F = MOI.get(model, MOI.ObjectiveFunctionType())
push!(attributes, MOI.ObjectiveFunction{F}())
end
if MOI.get(model, MOI.Name()) != ""
push!(attributes, MOI.Name())
end
return attributes
end

@odow
Copy link
Member

odow commented Oct 24, 2024

Why do you need this?

@bachdavi
Copy link
Author

bachdavi commented Oct 25, 2024

Thanks for the quick answer :)

We need this because we use Gurobi callbacks to store some state while solving, and depending on the user query we will also print the problem. Disabling the callback without resetting the optimizer state would solve our issue. I've tried a bunch of things, but no luck so far. Do you have any tips?

@odow
Copy link
Member

odow commented Oct 25, 2024

If you know that you're using Gurobi

Gurobi.GRBwrite(JuMP.unsafe_backend(model), "model.lp")

@bachdavi
Copy link
Author

That works! Thanks! Closing this.

@odow odow reopened this Oct 28, 2024
@odow odow changed the title Gurobi.CallbackFunction() breaks printing in LP format for a cached optimizer ListOfModelAttributesSet does not include callbacks Oct 28, 2024
@odow odow added bug Wrapper: MathOptInterface Issue is specific to MOI wrapper labels Oct 28, 2024
@odow
Copy link
Member

odow commented Oct 29, 2024

Perhaps there is an argument to exclude. I'll discuss this at our next monthly meeting

@odow
Copy link
Member

odow commented Oct 31, 2024

Call says:

  • Check/make set to nothing support. We might need to make UniversalFallback support this.
  • otherwise check if the ModelFilter works
  • We should error because the callback is part of the model

@odow
Copy link
Member

odow commented Nov 5, 2024

Just to follow up on this. The next release of MOI will support MOI.set(model, Gurobi.CallbackFunction(), nothing) as a way to remove the callback.

@bachdavi
Copy link
Author

bachdavi commented Nov 6, 2024

Just to follow up on this. The next release of MOI will support MOI.set(model, Gurobi.CallbackFunction(), nothing) as a way to remove the callback.

Thanks @odow ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Wrapper: MathOptInterface Issue is specific to MOI wrapper
Development

Successfully merging a pull request may close this issue.

2 participants