Skip to content

Commit 3c068d0

Browse files
committed
Add constraints tests
1 parent b2ea4bf commit 3c068d0

File tree

6 files changed

+47
-4
lines changed

6 files changed

+47
-4
lines changed

src/Bridges/detbridge.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ Constrains ``t \\le l_1 + \\cdots + l_n`` where `n` is the length of `l` and ret
108108
function subsum(model, t::MOI.ScalarAffineFunction, l::Vector{MOI.VariableIndex}, ::Type{T}) where T
109109
n = length(l)
110110
f = MOIU.operate!(-, T, t, MOIU.operate(sum, T, l))
111-
return MOIU.add_scalar_constraint(model, f, MOI.LessThan(zero(T)))
111+
return MOIU.add_scalar_constraint(model, f, MOI.LessThan(zero(T)),
112+
own_function=true)
112113
end
113114

114115
# Attributes, Bridge acting as an model

src/Bridges/geomeanbridge.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ function GeoMeanBridge{T, F, G}(model, f::MOI.AbstractVectorFunction,
7070
# With sqrt(2)^l*t - xl1, we should scale both the ConstraintPrimal and ConstraintDual
7171
tubc = MOIU.add_scalar_constraint(model,
7272
MOIU.operate!(+, T, t, -sN * xl1),
73-
MOI.LessThan(zero(T)))
73+
MOI.LessThan(zero(T)),
74+
own_function=true)
7475

7576
socrc = Vector{CI{G, MOI.RotatedSecondOrderCone}}(undef, N-1)
7677
offset = offsetnext = 0

src/Bridges/squarepsdbridge.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ function SquarePSDBridge{T, F, G}(model::MOI.ModelLike, f::F,
9595
# functions at entries (i, j) and (j, i) are almost identical
9696
if !MOIU.isapprox_zero(diff, 1e-10)
9797
push!(sym, (i, j) => MOIU.add_scalar_constraint(model, diff,
98-
MOI.EqualTo(zero(T))))
98+
MOI.EqualTo(zero(T)),
99+
own_function=true))
99100
end
100101
end
101102
k += dim - j

src/Utilities/constraints.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1+
"""
2+
add_scalar_constraint(model::MOI.ModelLike,
3+
func::MOI.AbstractScalarFunction,
4+
set::MOI.AbstractScalarSet;
5+
own_function::Bool=false)
6+
7+
Adds the scalar constraint obtained by moving the constant term in `func` to
8+
the set in `model`. If `own_function` is `true` then the function `func`, can
9+
be modified.
10+
"""
11+
function add_scalar_constraint end
12+
113
function add_scalar_constraint(model::MOI.ModelLike, func::MOI.SingleVariable,
214
set::MOI.AbstractScalarSet)
315
return MOI.addconstraint!(model, func, set)
416
end
517
function add_scalar_constraint(model::MOI.ModelLike,
618
func::Union{MOI.ScalarAffineFunction{T},
719
MOI.ScalarQuadraticFunction{T}},
8-
set::MOI.AbstractScalarSet) where T
20+
set::MOI.AbstractScalarSet;
21+
own::Bool=false) where T
922
set = shift_constant(set, -func.constant)
23+
if !own
24+
func = copy(func)
25+
end
1026
func.constant = zero(T)
1127
return MOI.addconstraint!(model, func, set)
1228
end

test/constraints.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@testset "Scalar constraints" begin
2+
model = Model{Float64}()
3+
x = MOI.addvariable!(model)
4+
@testset "SingleVariable" begin
5+
f = MOI.SingleVariable(x)
6+
ci = MOIU.add_scalar_constraint(model, f, MOI.EqualTo(1.0))
7+
@test MOI.get(model, MOI.ConstraintFunction(), ci) == f
8+
@test MOI.get(model, MOI.ConstraintSet(), ci) == MOI.EqualTo(1.0)
9+
end
10+
@testset "ScalarAffineFunction" begin
11+
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 2.0)
12+
g = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.0)
13+
ci = MOIU.add_scalar_constraint(model, f, MOI.EqualTo(3.0))
14+
@test f.constant == 2.0
15+
@test MOI.get(model, MOI.ConstraintFunction(), ci) g
16+
@test MOI.get(model, MOI.ConstraintSet(), ci) == MOI.EqualTo(1.0)
17+
ci = MOIU.add_scalar_constraint(model, f, MOI.Interval(-1.0, 1.0))
18+
@test f.constant == 2.0
19+
@test MOI.get(model, MOI.ConstraintFunction(), ci) g
20+
@test MOI.get(model, MOI.ConstraintSet(), ci) == MOI.Interval(-3.0,
21+
-1.0)
22+
end
23+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ MOIU.@model(Model,
3535
@testset "MOI.Utilities" begin
3636
include("functions.jl")
3737
include("sets.jl")
38+
include("constraints.jl")
3839
include("model.jl")
3940
include("universalfallback.jl")
4041
include("parser.jl")

0 commit comments

Comments
 (0)