Skip to content

Commit 2ef6709

Browse files
committed
Add test for bridge
1 parent c80fd01 commit 2ef6709

File tree

4 files changed

+103
-32
lines changed

4 files changed

+103
-32
lines changed
Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
struct DotProductsBridge{T,S,V} <: SetMapBridge{T,S,MOI.SetWithDotProducts{S,V}}
1+
struct DotProductsBridge{T,S,A,V} <: SetMapBridge{T,S,MOI.SetWithDotProducts{S,A,V}}
22
variables::Vector{MOI.VariableIndex}
33
constraint::MOI.ConstraintIndex{MOI.VectorOfVariables,S}
4-
set::MOI.SetWithDotProducts{S,V}
4+
set::MOI.SetWithDotProducts{S,A,V}
55
end
66

77
function supports_constrained_variable(
@@ -13,23 +13,23 @@ end
1313

1414
function concrete_bridge_type(
1515
::Type{<:DotProductsBridge{T}},
16-
::Type{MOI.SetWithDotProducts{S,V}},
17-
) where {T,S,V}
18-
return DotProductsBridge{T,S,V}
16+
::Type{MOI.SetWithDotProducts{S,A,V}},
17+
) where {T,S,A,V}
18+
return DotProductsBridge{T,S,A,V}
1919
end
2020

2121
function bridge_constrained_variable(
22-
BT::Type{DotProductsBridge{T,S,V}},
22+
BT::Type{DotProductsBridge{T,S,A,V}},
2323
model::MOI.ModelLike,
24-
set::MOI.SetWithDotProducts{S,V},
25-
) where {T,S,V}
24+
set::MOI.SetWithDotProducts{S,A,V},
25+
) where {T,S,A,V}
2626
variables, constraint =
2727
_add_constrained_var(model, MOI.Bridges.inverse_map_set(BT, set))
2828
return BT(variables, constraint, set)
2929
end
3030

3131
function MOI.Bridges.map_set(bridge::DotProductsBridge{T,S}, set::S) where {T,S}
32-
return MOI.SetWithDotProducts(set, bridge.vectors)
32+
return bridge.set
3333
end
3434

3535
function MOI.Bridges.inverse_map_set(
@@ -45,28 +45,23 @@ function MOI.Bridges.map_function(
4545
i::MOI.Bridges.IndexInVector,
4646
) where {T}
4747
scalars = MOI.Utilities.eachscalar(func)
48-
if i.value in eachindex(bridge.set.vectors)
49-
return MOI.Utilities.set_dot(
50-
bridge.set.vectors[i.value],
51-
scalars,
52-
bridge.set.set,
53-
)
54-
else
55-
return convert(
56-
MOI.ScalarAffineFunction{T},
57-
scalars[i.value-length(bridge.vectors)],
58-
)
59-
end
48+
return MOI.Utilities.set_dot(
49+
bridge.set.vectors[i.value],
50+
scalars,
51+
bridge.set.set,
52+
)
6053
end
6154

62-
function MOI.Bridges.inverse_map_function(
63-
bridge::DotProductsBridge{T},
64-
func,
65-
) where {T}
66-
m = length(bridge.set.vectors)
67-
return MOI.Utilities.operate(
68-
vcat,
69-
T,
70-
MOI.Utilities.eachscalar(func)[(m+1):end],
71-
)
55+
# This returns `true` by default for `SetMapBridge`
56+
# but is is not supported for this bridge because `inverse_map_function`
57+
# is not implemented
58+
function MOI.supports(::MOI.ModelLike, ::MOI.VariablePrimalStart, ::Type{<:DotProductsBridge})
59+
return false
60+
end
61+
62+
function unbridged_map(
63+
::DotProductsBridge,
64+
::Vector{MOI.VariableIndex},
65+
)
66+
return nothing
7267
end

src/Bridges/Variable/set_map.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function MOI.get(
129129
bridge::SetMapBridge,
130130
)
131131
set = MOI.get(model, attr, bridge.constraint)
132-
return MOI.Bridges.map_set(typeof(bridge), set)
132+
return MOI.Bridges.map_set(bridge, set)
133133
end
134134

135135
function MOI.set(

src/sets.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,10 @@ struct SetWithDotProducts{S,A,V<:AbstractVector{A}} <: AbstractVectorSet
18141814
vectors::V
18151815
end
18161816

1817+
function Base.:(==)(s1::SetWithDotProducts, s2::SetWithDotProducts)
1818+
return s1.set == s2.set && s1.vectors == s2.vectors
1819+
end
1820+
18171821
function Base.copy(s::SetWithDotProducts)
18181822
return SetWithDotProducts(copy(s.set), copy(s.vectors))
18191823
end

test/Bridges/Variable/set_dot.jl

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright (c) 2017: Miles Lubin and contributors
2+
# Copyright (c) 2017: Google Inc.
3+
#
4+
# Use of this source code is governed by an MIT-style license that can be found
5+
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
6+
7+
module TestVariableDotProducts
8+
9+
using Test
10+
11+
import MathOptInterface as MOI
12+
13+
function runtests()
14+
for name in names(@__MODULE__; all = true)
15+
if startswith("$(name)", "test_")
16+
@testset "$(name)" begin
17+
getfield(@__MODULE__, name)()
18+
end
19+
end
20+
end
21+
return
22+
end
23+
24+
include("../utilities.jl")
25+
26+
function test_psd()
27+
MOI.Bridges.runtests(
28+
MOI.Bridges.Variable.DotProductsBridge,
29+
model -> begin
30+
x, _ = MOI.add_constrained_variables(model,
31+
MOI.SetWithDotProducts(
32+
MOI.PositiveSemidefiniteConeTriangle(2),
33+
MOI.TriangleVectorization.([
34+
[1 2.0
35+
2 3],
36+
[4 5.0
37+
5 6],
38+
]),
39+
)
40+
)
41+
MOI.add_constraint(
42+
model,
43+
1.0x[1],
44+
MOI.EqualTo(0.0),
45+
)
46+
MOI.add_constraint(
47+
model,
48+
1.0x[2],
49+
MOI.LessThan(0.0),
50+
)
51+
end,
52+
model -> begin
53+
Q, _ = MOI.add_constrained_variables(model, MOI.PositiveSemidefiniteConeTriangle(2))
54+
MOI.add_constraint(
55+
model,
56+
1.0 * Q[1] + 4.0 * Q[2] + 3.0 * Q[3],
57+
MOI.EqualTo(0.0),
58+
)
59+
MOI.add_constraint(
60+
model,
61+
4.0 * Q[1] + 10.0 * Q[2] + 6.0 * Q[3],
62+
MOI.LessThan(0.0),
63+
)
64+
end;
65+
cannot_unbridge = true,
66+
)
67+
return
68+
end
69+
70+
end # module
71+
72+
TestVariableDotProducts.runtests()

0 commit comments

Comments
 (0)