Skip to content

Commit e954eec

Browse files
committed
Add bridge
1 parent 9e3fe6b commit e954eec

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
struct LinearCombinationBridge{T,S,A,V,F,G} <:
8+
SetMapBridge{T,S,MOI.LinearCombinationInSet{S,A,V},F,G}
9+
constraint::MOI.ConstraintIndex{F,S}
10+
set::MOI.LinearCombinationInSet{S,A,V}
11+
end
12+
13+
function MOI.supports_constraint(
14+
::Type{<:LinearCombinationBridge},
15+
::Type{<:MOI.AbstractVectorFunction},
16+
::Type{<:MOI.LinearCombinationInSet},
17+
)
18+
return true
19+
end
20+
21+
function concrete_bridge_type(
22+
::Type{<:LinearCombinationBridge{T}},
23+
G::Type{<:MOI.AbstractVectorFunction},
24+
::Type{MOI.LinearCombinationInSet{S,A,V}},
25+
) where {T,S,A,V}
26+
U = MOI.Utilities.promote_operation(*, T, MOI.Utilities.scalar_type(G), T)
27+
F = MOI.Utilities.promote_operation(vcat, T, U)
28+
return LinearCombinationBridge{T,S,A,V,F,G}
29+
end
30+
31+
function _map_function(set::MOI.LinearCombinationInSet, func)
32+
scalars = MOI.Utilities.eachscalar(func)
33+
return MOI.Utilities.vectorize([
34+
sum(scalars[j] * set.vectors[j][i] for j in eachindex(scalars))
35+
for i in 1:MOI.dimension(set.set)
36+
])
37+
end
38+
39+
function bridge_constraint(
40+
::Type{LinearCombinationBridge{T,S,A,V,F,G}},
41+
model::MOI.ModelLike,
42+
func::G,
43+
set::MOI.LinearCombinationInSet{S,A,V},
44+
) where {T,S,A,F,G,V}
45+
mapped_func = _map_function(set, func)
46+
constraint = MOI.add_constraint(model, mapped_func, set.set)
47+
return LinearCombinationBridge{T,S,A,V,F,G}(constraint, set)
48+
end
49+
50+
function MOI.Bridges.map_set(
51+
::Type{<:LinearCombinationBridge},
52+
set::MOI.LinearCombinationInSet,
53+
)
54+
return set.set
55+
end
56+
57+
function MOI.Bridges.inverse_map_set(
58+
bridge::LinearCombinationBridge,
59+
::MOI.AbstractSet,
60+
)
61+
return bridge.set
62+
end
63+
64+
function MOI.Bridges.adjoint_map_function(
65+
bridge::LinearCombinationBridge,
66+
func,
67+
)
68+
scalars = MOI.Utilities.eachscalar(func)
69+
return MOI.Utilities.vectorize([
70+
MOI.Utilities.set_dot(vector, scalars, bridge.set.set)
71+
for vector in bridge.set.vectors
72+
])
73+
end

0 commit comments

Comments
 (0)