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

Bug when objective changed with different constant #492

Closed
odow opened this issue Dec 4, 2022 · 2 comments
Closed

Bug when objective changed with different constant #492

odow opened this issue Dec 4, 2022 · 2 comments
Labels

Comments

@odow
Copy link
Member

odow commented Dec 4, 2022

Reported on https://discourse.julialang.org/t/bug-in-detecting-changed-constant-in-objective-function/91235

julia> using Gurobi

julia> const MOI = Gurobi.MOI
MathOptInterface

julia> model = Gurobi.Optimizer();

julia> x = MOI.add_variable(model)
MathOptInterface.VariableIndex(1)

julia> MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)

julia> attr = MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}()
MathOptInterface.ObjectiveFunction{MathOptInterface.ScalarAffineFunction{Float64}}()

julia> MOI.set(model, attr, 1.0 * x - 1.0)

julia> MOI.set(model, attr, 1.0 * x + 1.0)

julia> MOI.get(model, attr)
MathOptInterface.ScalarAffineFunction{Float64}(MathOptInterface.ScalarAffineTerm{Float64}[MathOptInterface.ScalarAffineTerm{Float64}(1.0, MathOptInterface.VariableIndex(1))], -1.0)

Final constant should be +1.0, not -1.0.

@odow odow added the bug label Dec 4, 2022
@odow
Copy link
Member Author

odow commented Dec 5, 2022

Issue is upstream:

julia> using Gurobi

julia> p = Ref{Cdouble}(NaN)
Base.RefValue{Float64}(NaN)

julia> envP, modelP = Ref{Ptr{Cvoid}}(), Ref{Ptr{Cvoid}}()
(Base.RefValue{Ptr{Nothing}}(Ptr{Nothing} @0x0000000000000006), Base.RefValue{Ptr{Nothing}}(Ptr{Nothing} @0x000000000000000c))

julia> GRBemptyenv(envP)
0

julia> GRBstartenv(envP[])
0

julia> GRBnewmodel(envP[], modelP, "", 0, C_NULL, C_NULL, C_NULL, C_NULL, C_NULL)
0

julia> model = modelP[]
Ptr{Nothing} @0x00007faf975b4e80

julia> GRBsetintattr(model, "ModelSense", -1)
0

julia> GRBsetdblattr(model, "ObjCon", -1.0)
0

julia> GRBupdatemodel(model)
0

julia> GRBgetdblattr(model, "ObjCon", p)
0

julia> p[]
-1.0

julia> GRBsetdblattr(model, "ObjCon", 1.0)
0

julia> GRBupdatemodel(model)
0

julia> GRBgetdblattr(model, "ObjCon", p)
0

julia> p[]
-1.0

julia> # Last result should be +1.0!

@odow
Copy link
Member Author

odow commented Dec 5, 2022

Closing because it is fixed in Gurobi v10:

julia> using Gurobi

julia> const MOI = Gurobi.MOI
MathOptInterface

julia> model = Gurobi.Optimizer();

julia> x = MOI.add_variable(model)
MathOptInterface.VariableIndex(1)

julia> MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)

julia> attr = MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}()
MathOptInterface.ObjectiveFunction{MathOptInterface.ScalarAffineFunction{Float64}}()

julia> MOI.set(model, attr, 1.0 * x - 1.0)
(f.constant, ret) = (-1.0, 0)

julia> MOI.set(model, attr, 1.0 * x + 1.0)
(f.constant, ret) = (1.0, 0)

julia> MOI.get(model, attr)
MathOptInterface.ScalarAffineFunction{Float64}(MathOptInterface.ScalarAffineTerm{Float64}[MathOptInterface.ScalarAffineTerm{Float64}(1.0, MathOptInterface.VariableIndex(1))], 1.0)

julia> Gurobi._GUROBI_VERSION
v"10.0.0"

@odow odow closed this as completed Dec 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant