/
bridge.jl
215 lines (181 loc) · 5.95 KB
/
bridge.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# Copyright (c) 2017: Miles Lubin and contributors
# Copyright (c) 2017: Google Inc.
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
"""
abstract type AbstractBridge <: MOI.Bridges.AbstractBridge end
Subtype of [`MOI.Bridges.AbstractBridge`](@ref) for variable bridges.
In addition to the required implementation described in
[`MOI.Bridges.AbstractBridge`](@ref), subtypes of `AbstractBridge` must
additionally implement:
* [`supports_constrained_variable`](@ref)
* [`concrete_bridge_type`](@ref)
* [`bridge_constrained_variable`](@ref)
"""
abstract type AbstractBridge <: MOI.Bridges.AbstractBridge end
"""
supports_constrained_variable(
BT::Type{<:AbstractBridge},
S::Type{<:MOI.AbstractSet},
)::Bool
Return a `Bool` indicating whether the bridges of type `BT` support bridging
constrained variables in `S`. That is, it returns `true` if the bridge of type
`BT` converts constrained variables of type `S` into a form supported by the
solver.
## Implementation notes
* This method depends only on the type of the bridge and set, not the runtime
values.
* There is a default fallback, so you need only implement this method for sets
that the bridge implements.
## Example
```jldoctest; setup=(import MathOptInterface as MOI)
julia> MOI.Bridges.Variable.supports_constrained_variable(
MOI.Bridges.Variable.NonposToNonnegBridge{Float64},
MOI.Nonpositives,
)
true
julia> MOI.Bridges.Variable.supports_constrained_variable(
MOI.Bridges.Variable.NonposToNonnegBridge{Float64},
MOI.Nonnegatives,
)
false
```
"""
function supports_constrained_variable(
::Type{<:AbstractBridge},
::Type{<:MOI.AbstractSet},
)
return false
end
"""
concrete_bridge_type(
BT::Type{<:AbstractBridge},
S::Type{<:MOI.AbstractSet},
)::Type
Return the concrete type of the bridge supporting variables in `S` constraints.
This function can only be called if `MOI.supports_constrained_variable(BT, S)`
is `true`.
## Examples
As a variable in [`MOI.GreaterThan`](@ref) is bridged into
variables in [`MOI.Nonnegatives`](@ref) by the
[`VectorizeBridge`](@ref):
```jldoctest; setup=:(import MathOptInterface as MOI)
julia> MOI.Bridges.Variable.concrete_bridge_type(
MOI.Bridges.Variable.VectorizeBridge{Float64},
MOI.GreaterThan{Float64},
)
MathOptInterface.Bridges.Variable.VectorizeBridge{Float64, MathOptInterface.Nonnegatives}
```
"""
concrete_bridge_type(::Type{BT}, ::Type{<:MOI.AbstractSet}) where {BT} = BT
function concrete_bridge_type(
b::MOI.Bridges.AbstractBridgeOptimizer,
S::Type{<:MOI.AbstractSet},
)
return concrete_bridge_type(MOI.Bridges.bridge_type(b, S), S)
end
"""
bridge_constrained_variable(
BT::Type{<:AbstractBridge},
model::MOI.ModelLike,
set::MOI.AbstractSet,
)::BT
Bridge the constrained variable in `set` using bridge `BT` to `model` and
returns a bridge object of type `BT`.
## Implementation notes
* The bridge type `BT` must be a concrete type, that is, all the type
parameters of the bridge must be set.
"""
function bridge_constrained_variable end
function MOI.get(
::MOI.ModelLike,
attr::MOI.AbstractVariableAttribute,
bridge::AbstractBridge,
)
return throw(
ArgumentError(
"Variable bridge of type `$(typeof(bridge))` does not support " *
"accessing the attribute `$attr`.",
),
)
end
function MOI.get(
::MOI.ModelLike,
attr::MOI.AbstractVariableAttribute,
bridge::AbstractBridge,
::MOI.Bridges.IndexInVector,
)
return throw(
ArgumentError(
"Variable bridge of type `$(typeof(bridge))` does not support " *
"accessing the attribute `$attr`.",
),
)
end
function MOI.supports(
::MOI.ModelLike,
::MOI.AbstractVariableAttribute,
::Type{<:AbstractBridge},
)
return false
end
function MOI.set(
model::MOI.ModelLike,
attr::MOI.AbstractVariableAttribute,
bridge::AbstractBridge,
value,
::MOI.Bridges.IndexInVector...,
)
if MOI.is_copyable(attr) && !MOI.supports(model, attr, typeof(bridge))
throw(MOI.UnsupportedAttribute(attr))
else
throw(MOI.SetAttributeNotAllowed(attr))
end
end
"""
unbridged_map(
bridge::MOI.Bridges.Variable.AbstractBridge,
vi::MOI.VariableIndex,
)
For a bridged variable in a scalar set, return a tuple of pairs mapping the
variables created by the bridge to an affine expression in terms of the
bridged variable `vi`.
```julia
unbridged_map(
bridge::MOI.Bridges.Variable.AbstractBridge,
vis::Vector{MOI.VariableIndex},
)
```
For a bridged variable in a vector set, return a tuple of pairs mapping the
variables created by the bridge to an affine expression in terms of the bridged
variable `vis`. If this method is not implemented, it falls back to calling
the following method for every variable of `vis`.
```julia
unbridged_map(
bridge::MOI.Bridges.Variable.AbstractBridge,
vi::MOI.VariableIndex,
i::MOI.Bridges.IndexInVector,
)
```
For a bridged variable in a vector set, return a tuple of pairs mapping the
variables created by the bridge to an affine expression in terms of the bridged
variable `vi` corresponding to the `i`th variable of the vector.
If there is no way to recover the expression in terms of the bridged variable(s)
`vi(s)`, return `nothing`. See [`ZerosBridge`](@ref) for an example of bridge
returning `nothing`.
"""
function unbridged_map end
function unbridged_map(bridge::AbstractBridge, vis::Vector{MOI.VariableIndex})
mappings = Pair{MOI.VariableIndex,MOI.AbstractScalarFunction}[]
for (i, vi) in enumerate(vis)
vi_mappings = unbridged_map(bridge, vi, MOI.Bridges.IndexInVector(i))
if vi_mappings === nothing
return
end
for mapping in vi_mappings
push!(mappings, mapping)
end
end
return mappings
end