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

[Bridges] fix some getters of ConstraintFunction and add better tests #2328

Merged
merged 7 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/bridges/all_different.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function MOI.get(
::MOI.ConstraintFunction,
bridge::AllDifferentToCountDistinctBridge,
)
return bridge.f
return copy(bridge.f)
end

function MOI.get(
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/bridges/all_different_reif.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function MOI.get(
::MOI.ConstraintFunction,
bridge::ReifiedAllDifferentToCountDistinctBridge,
)
return bridge.f
return copy(bridge.f)
end

function MOI.get(
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/bridges/bin_packing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function MOI.get(
::MOI.ConstraintFunction,
bridge::BinPackingToMILPBridge,
)
return bridge.f
return copy(bridge.f)
end

function MOI.get(
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/bridges/circuit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function MOI.get(
::MOI.ConstraintFunction,
bridge::CircuitToMILPBridge,
)
return bridge.f
return copy(bridge.f)
end

function MOI.get(
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/bridges/count_at_least.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function MOI.get(
::MOI.ConstraintFunction,
bridge::CountAtLeastToCountBelongsBridge,
)
return bridge.f
return copy(bridge.f)
end

function MOI.get(
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/bridges/count_belongs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function MOI.get(
::MOI.ConstraintFunction,
bridge::CountBelongsToMILPBridge,
)
return bridge.f
return copy(bridge.f)
end

function MOI.get(
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/bridges/count_distinct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function MOI.get(
::MOI.ConstraintFunction,
bridge::CountDistinctToMILPBridge,
)
return bridge.f
return copy(bridge.f)
end

function MOI.get(
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/bridges/count_distinct_reif.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function MOI.get(
::MOI.ConstraintFunction,
bridge::ReifiedCountDistinctToMILPBridge,
)
return bridge.f
return copy(bridge.f)
end

function MOI.get(
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/bridges/count_greater_than.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function MOI.get(
::MOI.ConstraintFunction,
bridge::CountGreaterThanToMILPBridge,
)
return bridge.f
return copy(bridge.f)
end

function MOI.get(
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/bridges/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@
if bridge.upper !== nothing
return MOI.get(model, attr, bridge.upper)
end
return bridge.func
return copy(bridge.func)

Check warning on line 432 in src/Bridges/Constraint/bridges/interval.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/interval.jl#L432

Added line #L432 was not covered by tests
end

function MOI.get(
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/bridges/soc_to_nonconvex_quad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function MOI.get(
::MOI.ConstraintFunction,
b::_AbstractSOCtoNonConvexQuadBridge{T},
) where {T}
return MOI.VectorOfVariables(b.vars)
return MOI.VectorOfVariables(copy(b.vars))
end

function MOI.Bridges.added_constrained_variable_types(
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/bridges/table.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function MOI.get(
::MOI.ConstraintFunction,
bridge::TableToMILPBridge,
)
return bridge.f
return copy(bridge.f)
end

function MOI.get(
Expand Down
35 changes: 35 additions & 0 deletions src/Test/test_basic_constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,40 @@ function _set(::Type{T}, ::Type{MOI.HyperRectangle}) where {T}
return MOI.HyperRectangle(zeros(T, 3), ones(T, 3))
end

function _test_function_modification(
model::MOI.ModelLike,
config::Config{T},
c::MOI.ConstraintIndex{F},
f::F,
) where {T,F<:Union{MOI.ScalarAffineFunction{T},MOI.ScalarQuadraticFunction{T}}}
MOI.Utilities.modify_function!(f, MOI.ScalarConstantChange(f.constant + 1))
g = MOI.get(model, MOI.ConstraintFunction(), c)
@test !≈(f.constant, g.constant, config)
return
end

function _test_function_modification(
model::MOI.ModelLike,
config::Config{T},
c::MOI.ConstraintIndex{F},
f::F,
) where {T,F<:Union{MOI.VectorAffineFunction{T},MOI.VectorQuadraticFunction{T}}}
new_constants = f.constants .+ one(T)
MOI.Utilities.modify_function!(f, MOI.VectorConstantChange(new_constants))
g = MOI.get(model, MOI.ConstraintFunction(), c)
@test !≈(f.constants, g.constants, config)
return
end

function _test_function_modification(
::MOI.ModelLike,
::Config{T},
c::MOI.ConstraintIndex{F},
::F,
) where {T,F<:MOI.AbstractFunction}
return
end

function _basic_constraint_test_helper(
model::MOI.ModelLike,
config::Config{T},
Expand Down Expand Up @@ -233,6 +267,7 @@ function _basic_constraint_test_helper(
)
_test_attribute_value_type(model, MOI.ConstraintFunction(), c)
_test_attribute_value_type(model, MOI.CanonicalConstraintFunction(), c)
_test_function_modification(model, config, c, f)
end
###
### Test MOI.ConstraintSet
Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/vector_of_constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function MOI.get(
) where {F,S}
MOI.throw_if_not_valid(v, ci)
f, _ = v.constraints[ci]::Tuple{F,S}
return f
return copy(f)
end

function MOI.get(
Expand Down
3 changes: 2 additions & 1 deletion test/Bridges/bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ function test_recursive_model_constraint(::Type{T} = Int) where {T}
@test MOI.get(b, MOI.ConstraintFunction(), c) ≈ func
new_func = T(2) * x
MOI.set(b, MOI.ConstraintFunction(), c, new_func)
@test MOI.get(b, MOI.ConstraintFunction(), c) == new_func
@test MOI.get(b, MOI.ConstraintFunction(), c) new_func
MOI.modify(b, c, MOI.ScalarCoefficientChange(x, T(3)))
@test MOI.get(b, MOI.ConstraintFunction(), c) ≈ T(3) * x
MOI.modify(b, c, MOI.ScalarConstantChange(T(-1)))
Expand All @@ -746,6 +746,7 @@ function test_recursive_model_constraint(::Type{T} = Int) where {T}
@test MOI.is_valid(b, c)
MOI.delete(b, c)
@test !MOI.is_valid(b, c)
return
end

function test_recursive_model_objective(::Type{T} = Int) where {T}
Expand Down
2 changes: 1 addition & 1 deletion test/Utilities/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ function test_quadratic_functions()
@test MOI.Utilities.is_canonical(F1)
F3 = MOI.get(model, MOI.CanonicalConstraintFunction(), c3)
@test F3 ≈ f3
@test F3 === MOI.get(model, MOI.ConstraintFunction(), c3)
@test F3 MOI.get(model, MOI.ConstraintFunction(), c3)
@test MOI.Utilities.is_canonical(F3)
@test MOI.get(model, MOI.CanonicalConstraintFunction(), c1) ≈ f3
@test MOI.Utilities.is_canonical(
Expand Down
Loading