|
| 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 | +""" |
| 8 | + IntegerToZeroOneBridge{T} <: Bridges.Constraint.AbstractBridge |
| 9 | +
|
| 10 | +`IntegerToZeroOneBridge` implements the following reformulation: |
| 11 | +
|
| 12 | + * ``x \\in \\mathbf{Z}`` into ``y_i \\in \\{0, 1\\}``, |
| 13 | + ``x == lb + \\sum 2^{i-1} y_i``. |
| 14 | +
|
| 15 | +## Source node |
| 16 | +
|
| 17 | +`IntegerToZeroOneBridge` supports: |
| 18 | +
|
| 19 | + * `VariableIndex` in [`MOI.Integer`](@ref) |
| 20 | +
|
| 21 | +## Target nodes |
| 22 | +
|
| 23 | +`IntegerToZeroOneBridge` creates: |
| 24 | +
|
| 25 | + * [`MOI.VariableIndex`](@ref) in [`MOI.ZeroOne`](@ref) |
| 26 | + * [`MOI.ScalarAffineFunction{T}`](@ref) in [`MOI.EqualTo{T}`](@ref) |
| 27 | +
|
| 28 | +## Developer note |
| 29 | +
|
| 30 | +This bridge is implemented as a constraint bridge instead of a variable bridge |
| 31 | +because we don't want to substitute the linear combination of `y` for every |
| 32 | +instance of `x`. Doing so would be expensive and greatly reduce the sparsity of |
| 33 | +the constraints. |
| 34 | +""" |
| 35 | +mutable struct IntegerToZeroOneBridge{T} <: AbstractBridge |
| 36 | + x::MOI.VariableIndex |
| 37 | + y::Vector{MOI.VariableIndex} |
| 38 | + ci::MOI.ConstraintIndex{MOI.ScalarAffineFunction{T},MOI.EqualTo{T}} |
| 39 | + last_bounds::Union{Nothing,NTuple{2,T}} |
| 40 | + |
| 41 | + function IntegerToZeroOneBridge{T}(x::MOI.VariableIndex) where {T} |
| 42 | + return new{T}( |
| 43 | + x, |
| 44 | + MOI.VariableIndex[], |
| 45 | + MOI.ConstraintIndex{MOI.ScalarAffineFunction{T},MOI.EqualTo{T}}(0), |
| 46 | + nothing, |
| 47 | + ) |
| 48 | + end |
| 49 | +end |
| 50 | + |
| 51 | +const IntegerToZeroOne{T,OT<:MOI.ModelLike} = |
| 52 | + SingleBridgeOptimizer{IntegerToZeroOneBridge{T},OT} |
| 53 | + |
| 54 | +function bridge_constraint( |
| 55 | + ::Type{IntegerToZeroOneBridge{T}}, |
| 56 | + ::MOI.ModelLike, |
| 57 | + x::MOI.VariableIndex, |
| 58 | + ::MOI.Integer, |
| 59 | +) where {T} |
| 60 | + # !!! info |
| 61 | + # Postpone creation until final_touch. |
| 62 | + return IntegerToZeroOneBridge{T}(x) |
| 63 | +end |
| 64 | + |
| 65 | +function MOI.supports_constraint( |
| 66 | + ::Type{IntegerToZeroOneBridge{T}}, |
| 67 | + ::Type{MOI.VariableIndex}, |
| 68 | + ::Type{MOI.Integer}, |
| 69 | +) where {T} |
| 70 | + return true |
| 71 | +end |
| 72 | + |
| 73 | +function MOI.Bridges.added_constrained_variable_types( |
| 74 | + ::Type{<:IntegerToZeroOneBridge}, |
| 75 | +) |
| 76 | + return Tuple{Type}[(MOI.ZeroOne,)] |
| 77 | +end |
| 78 | + |
| 79 | +function MOI.Bridges.added_constraint_types( |
| 80 | + ::Type{IntegerToZeroOneBridge{T}}, |
| 81 | +) where {T} |
| 82 | + return Tuple{Type,Type}[(MOI.ScalarAffineFunction{T}, MOI.EqualTo{T})] |
| 83 | +end |
| 84 | + |
| 85 | +function concrete_bridge_type( |
| 86 | + ::Type{IntegerToZeroOneBridge{T}}, |
| 87 | + ::Type{MOI.VariableIndex}, |
| 88 | + ::Type{MOI.Integer}, |
| 89 | +) where {T} |
| 90 | + return IntegerToZeroOneBridge{T} |
| 91 | +end |
| 92 | + |
| 93 | +function MOI.get( |
| 94 | + ::MOI.ModelLike, |
| 95 | + ::MOI.ConstraintFunction, |
| 96 | + bridge::IntegerToZeroOneBridge, |
| 97 | +) |
| 98 | + return bridge.x |
| 99 | +end |
| 100 | + |
| 101 | +function MOI.get(::MOI.ModelLike, ::MOI.ConstraintSet, ::IntegerToZeroOneBridge) |
| 102 | + return MOI.Integer() |
| 103 | +end |
| 104 | + |
| 105 | +function MOI.delete(model::MOI.ModelLike, bridge::IntegerToZeroOneBridge) |
| 106 | + MOI.delete(model, bridge.ci) |
| 107 | + MOI.delete(model, bridge.y) |
| 108 | + return |
| 109 | +end |
| 110 | + |
| 111 | +function MOI.get(bridge::IntegerToZeroOneBridge, ::MOI.NumberOfVariables)::Int64 |
| 112 | + return length(bridge.y) |
| 113 | +end |
| 114 | + |
| 115 | +function MOI.get(bridge::IntegerToZeroOneBridge, ::MOI.ListOfVariableIndices) |
| 116 | + return copy(bridge.y) |
| 117 | +end |
| 118 | + |
| 119 | +function MOI.get( |
| 120 | + bridge::IntegerToZeroOneBridge, |
| 121 | + ::MOI.NumberOfConstraints{MOI.VariableIndex,MOI.ZeroOne}, |
| 122 | +)::Int64 |
| 123 | + return length(bridge.y) |
| 124 | +end |
| 125 | + |
| 126 | +function MOI.get( |
| 127 | + bridge::IntegerToZeroOneBridge, |
| 128 | + ::MOI.ListOfConstraintIndices{MOI.VariableIndex,MOI.ZeroOne}, |
| 129 | +) |
| 130 | + return map(bridge.y) do y |
| 131 | + return MOI.ConstraintIndex{MOI.VariableIndex,MOI.ZeroOne}(y.value) |
| 132 | + end |
| 133 | +end |
| 134 | + |
| 135 | +function MOI.get( |
| 136 | + bridge::IntegerToZeroOneBridge{T}, |
| 137 | + ::MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T},MOI.EqualTo{T}}, |
| 138 | +)::Int64 where {T} |
| 139 | + return 1 |
| 140 | +end |
| 141 | + |
| 142 | +function MOI.get( |
| 143 | + bridge::IntegerToZeroOneBridge{T}, |
| 144 | + ::MOI.ListOfConstraintIndices{MOI.ScalarAffineFunction{T},MOI.EqualTo{T}}, |
| 145 | +) where {T} |
| 146 | + return [bridge.ci] |
| 147 | +end |
| 148 | + |
| 149 | +MOI.Bridges.needs_final_touch(::IntegerToZeroOneBridge) = true |
| 150 | + |
| 151 | +function MOI.Bridges.final_touch( |
| 152 | + bridge::IntegerToZeroOneBridge{T}, |
| 153 | + model::MOI.ModelLike, |
| 154 | +) where {T} |
| 155 | + ret = MOI.Utilities.get_bounds(model, T, bridge.x) |
| 156 | + if ret === bridge.last_bounds |
| 157 | + return nothing # final_touch already called |
| 158 | + elseif ret[1] == typemin(T) || ret[2] == typemax(T) |
| 159 | + error( |
| 160 | + "Unable to use IntegerToZeroOneBridge because the variable " * |
| 161 | + "$(bridge.x) has a non-finite domain", |
| 162 | + ) |
| 163 | + end |
| 164 | + f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(T(1), bridge.x)], T(0)) |
| 165 | + lb, ub = ceil(Int, ret[1]), floor(Int, ret[2]) |
| 166 | + N = floor(Int, log2(ub - lb)) + 1 |
| 167 | + for i in 1:N |
| 168 | + y, _ = MOI.add_constrained_variable(model, MOI.ZeroOne()) |
| 169 | + push!(bridge.y, y) |
| 170 | + push!(f.terms, MOI.ScalarAffineTerm(-(T(2)^(i - 1)), y)) |
| 171 | + end |
| 172 | + bridge.ci = MOI.add_constraint(model, f, MOI.EqualTo{T}(lb)) |
| 173 | + bridge.last_bounds = ret |
| 174 | + return |
| 175 | +end |
0 commit comments