-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathInequalityToComplementsBridge.jl
More file actions
152 lines (126 loc) · 4.44 KB
/
InequalityToComplementsBridge.jl
File metadata and controls
152 lines (126 loc) · 4.44 KB
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
# 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.
"""
InequalityToComplementsBridge{T,F,S,G} <: Bridges.Constraint.AbstractBridge
`InequalityToComplementsBridge` implements the following reformulations:
* ``f(x) \\ge b`` into ``\\exists y`` such that ``f(x) - b \\perp y \\ge 0``
* ``f(x) \\le b`` into ``f(x) - b \\perp y \\le 0``
* ``f(x) = b`` into ``f(x) - b \\perp y``
## Source node
`InequalityToComplementsBridge` supports:
* `F` in [`MOI.GreaterThan{T}`](@ref)
* `F` in [`MOI.LessThan{T}`](@ref)
* `F` in [`MOI.EqualTo`](@ref)
## Target nodes
`InequalityToComplementsBridge` creates:
* [`MOI.VariableIndex`](@ref) in [`MOI.LessThan{T}`](@ref)
* [`MOI.VariableIndex`](@ref) in [`MOI.GreaterThan{T}`](@ref)
* `G` in [`MOI.Complements`](@ref)
"""
mutable struct InequalityToComplementsBridge{
T,
F<:MOI.AbstractScalarFunction,
S<:Union{MOI.GreaterThan{T},MOI.LessThan{T},MOI.EqualTo{T}},
G<:MOI.AbstractVectorFunction,
} <: AbstractBridge
y::MOI.VariableIndex
set::S
ci::MOI.ConstraintIndex{G,MOI.Complements}
end
const InequalityToComplements{T,OT<:MOI.ModelLike} =
SingleBridgeOptimizer{InequalityToComplementsBridge{T},OT}
function _add_y_variable(model, ::MOI.GreaterThan{T}) where {T}
return MOI.add_constrained_variable(model, MOI.GreaterThan(zero(T)))[1]
end
function _add_y_variable(model, ::MOI.LessThan{T}) where {T}
return MOI.add_constrained_variable(model, MOI.LessThan(zero(T)))[1]
end
_add_y_variable(model, ::MOI.EqualTo) = MOI.add_variable(model)
function bridge_constraint(
::Type{InequalityToComplementsBridge{T,F,S,G}},
model::MOI.ModelLike,
f::F,
set::S,
) where {T,F,S<:Union{MOI.GreaterThan{T},MOI.LessThan{T},MOI.EqualTo{T}},G}
y = _add_y_variable(model, set)
f_set = MOI.Utilities.operate(-, T, f, MOI.constant(set))
g = MOI.Utilities.operate(vcat, T, f_set, y)
ci = MOI.add_constraint(model, g, MOI.Complements(2))
return InequalityToComplementsBridge{T,F,S,G}(y, set, ci)
end
function MOI.supports_constraint(
::Type{<:InequalityToComplementsBridge{T}},
::Type{F},
::Type{<:Union{MOI.GreaterThan{T},MOI.LessThan{T},MOI.EqualTo{T}}},
) where {T,F<:MOI.AbstractScalarFunction}
return MOI.Utilities.is_coefficient_type(F, T) &&
!MOI.Utilities.is_complex(F)
end
function MOI.Bridges.added_constrained_variable_types(
::Type{InequalityToComplementsBridge{T,F,S,G}},
) where {T,F,S<:Union{MOI.GreaterThan{T},MOI.LessThan{T}},G}
return Tuple{Type}[(S,)]
end
function MOI.Bridges.added_constrained_variable_types(
::Type{InequalityToComplementsBridge{T,F,MOI.EqualTo{T},G}},
) where {T,F,G}
return Tuple{Type}[(MOI.Reals,)]
end
function MOI.Bridges.added_constraint_types(
::Type{InequalityToComplementsBridge{T,F,S,G}},
) where {T,F,S,G}
return Tuple{Type,Type}[(G, MOI.Complements)]
end
function concrete_bridge_type(
::Type{<:InequalityToComplementsBridge},
F::Type{<:MOI.AbstractScalarFunction},
S::Type{<:Union{MOI.GreaterThan{T},MOI.LessThan{T},MOI.EqualTo{T}}},
) where {T}
G = MOI.Utilities.promote_operation(vcat, T, F, MOI.VariableIndex)
return InequalityToComplementsBridge{T,F,S,G}
end
function MOI.get(::InequalityToComplementsBridge, ::MOI.NumberOfVariables)
return Int64(1)
end
function MOI.get(
bridge::InequalityToComplementsBridge,
::MOI.ListOfVariableIndices,
)
return [bridge.y]
end
function MOI.get(
::InequalityToComplementsBridge{T,F,S,G},
::MOI.NumberOfConstraints{G,MOI.Complements},
)::Int64 where {T,F,S,G}
return 1
end
function MOI.get(
bridge::InequalityToComplementsBridge{T,F,S,G},
::MOI.ListOfConstraintIndices{G,MOI.Complements},
) where {T,F,S,G}
return [bridge.ci]
end
function MOI.delete(model::MOI.ModelLike, bridge::InequalityToComplementsBridge)
MOI.delete(model, bridge.y)
MOI.delete(model, bridge.ci)
return
end
function MOI.get(
model::MOI.ModelLike,
attr::MOI.ConstraintFunction,
bridge::InequalityToComplementsBridge{T},
) where {T}
g = MOI.get(model, attr, bridge.ci)
f_set = first(MOI.Utilities.scalarize(g))
return MOI.Utilities.operate(+, T, f_set, MOI.constant(bridge.set))
end
function MOI.get(
::MOI.ModelLike,
::MOI.ConstraintSet,
bridge::InequalityToComplementsBridge,
)
return bridge.set
end