Skip to content

Commit

Permalink
Fix bug modifying a constraint after deletion (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Aug 15, 2023
1 parent fbe5044 commit 38464c2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Gurobi"
uuid = "2e9cd046-0924-5485-92f1-d5272153d98b"
repo = "https://github.com/jump-dev/Gurobi.jl"
version = "1.0.1"
version = "1.0.2"

[deps]
LazyArtifacts = "4af54fe1-eca0-43a8-85a7-787d91b784e3"
Expand Down
4 changes: 4 additions & 0 deletions src/MOI_wrapper/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3459,6 +3459,7 @@ function MOI.modify(
c::MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},<:Any},
chg::MOI.ScalarCoefficientChange{Float64},
)
_update_if_necessary(model)
ret = GRBchgcoeffs(
model,
1,
Expand All @@ -3476,6 +3477,7 @@ function MOI.modify(
cis::Vector{MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},S}},
changes::Vector{MOI.ScalarCoefficientChange{Float64}},
) where {S}
_update_if_necessary(model)
nels = length(cis)
@assert nels == length(changes)
rows = Vector{Cint}(undef, nels)
Expand All @@ -3497,6 +3499,7 @@ function MOI.modify(
::MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}},
chg::MOI.ScalarCoefficientChange{Float64},
)
_update_if_necessary(model)
ret = GRBsetdblattrelement(
model,
"Obj",
Expand All @@ -3514,6 +3517,7 @@ function MOI.modify(
::MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}},
changes::Vector{MOI.ScalarCoefficientChange{Float64}},
)
_update_if_necessary(model)
nels = length(changes)
cols = Vector{Cint}(undef, nels)
coefs = Vector{Cdouble}(undef, nels)
Expand Down
46 changes: 46 additions & 0 deletions test/MOI/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,52 @@ function test_dual_qcp_failure()
return
end

function test_modify_after_delete()
model = Gurobi.Optimizer(GRB_ENV)
x = MOI.add_variable(model)
c = [MOI.add_constraint(model, i * x, MOI.LessThan(i)) for i in [1.0, 2.0]]
MOI.delete(model, c[1])
MOI.modify(model, c[2], MOI.ScalarCoefficientChange(x, 4.0))
@test MOI.get(model, MOI.ConstraintFunction(), c[2]) 4.0 * x
return
end

function test_modify_after_delete_plural()
model = Gurobi.Optimizer(GRB_ENV)
x = MOI.add_variable(model)
c = [MOI.add_constraint(model, i * x, MOI.LessThan(i)) for i in [1.0, 2.0]]
MOI.delete(model, c[1])
MOI.modify(model, [c[2]], [MOI.ScalarCoefficientChange(x, 4.0)])
@test MOI.get(model, MOI.ConstraintFunction(), c[2]) 4.0 * x
return
end

function test_modify_after_delete_objective()
model = Gurobi.Optimizer(GRB_ENV)
x = MOI.add_variables(model, 2)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
f = 1.0 * x[1] + 2.0 * x[2]
attr = MOI.ObjectiveFunction{typeof(f)}()
MOI.set(model, attr, f)
MOI.delete(model, x[1])
MOI.modify(model, attr, MOI.ScalarCoefficientChange(x[2], 3.0))
@test MOI.get(model, attr) 3.0 * x[2]
return
end

function test_modify_after_delete_objective_plural()
model = Gurobi.Optimizer(GRB_ENV)
x = MOI.add_variables(model, 2)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
f = 1.0 * x[1] + 2.0 * x[2]
attr = MOI.ObjectiveFunction{typeof(f)}()
MOI.set(model, attr, f)
MOI.delete(model, x[1])
MOI.modify(model, attr, [MOI.ScalarCoefficientChange(x[2], 3.0)])
@test MOI.get(model, attr) 3.0 * x[2]
return
end

end

TestMOIWrapper.runtests()

2 comments on commit 38464c2

@odow
Copy link
Member Author

@odow odow commented on 38464c2 Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/89657

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.2 -m "<description of version>" 38464c27b96e9818257b22c61d2458890957d0fe
git push origin v1.0.2

Please sign in to comment.