|
| 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 | + CountAtLeastToCountBelongsBridge{T,F} <: Bridges.Constraint.AbstractBridge |
| 9 | +
|
| 10 | +`CountAtLeastToCountBelongsBridge` implements the following reformulation: |
| 11 | +
|
| 12 | + * ``x \\in \\textsf{CountAtLeast}(n, d, set)`` to |
| 13 | + ``(n_i, x_{d_i}) \\in \\textsf{CountBelongs}(1+d)`` |
| 14 | + and ``\\sum\\limits n_i \\ge n`` |
| 15 | +
|
| 16 | +## Source node |
| 17 | +
|
| 18 | +`CountAtLeastToCountBelongsBridge` supports: |
| 19 | +
|
| 20 | + * `F` in [`MOI.CountAtLeast`](@ref) |
| 21 | +
|
| 22 | +where `F` is [`MOI.VectorOfVariables`](@ref) or |
| 23 | +[`MOI.VectorAffineFunction{T}`](@ref). |
| 24 | +
|
| 25 | +## Target nodes |
| 26 | +
|
| 27 | +`CountAtLeastToCountBelongsBridge` creates: |
| 28 | +
|
| 29 | + * `F` in [`MOI.CountBelongs`](@ref) |
| 30 | + * [`MOI.ScalarAffineFunction{T}`](@ref) in [`MOI.GreaterThan{T}`](@ref) |
| 31 | +""" |
| 32 | +mutable struct CountAtLeastToCountBelongsBridge{ |
| 33 | + T, |
| 34 | + F<:Union{MOI.VectorOfVariables,MOI.VectorAffineFunction{T}}, |
| 35 | +} <: AbstractBridge |
| 36 | + f::F |
| 37 | + s::MOI.CountAtLeast |
| 38 | + variables::Vector{MOI.VariableIndex} |
| 39 | + ci::Vector{MOI.ConstraintIndex{F,MOI.CountBelongs}} |
| 40 | + count::MOI.ConstraintIndex{MOI.ScalarAffineFunction{T},MOI.GreaterThan{T}} |
| 41 | +end |
| 42 | + |
| 43 | +const CountAtLeastToCountBelongs{T,OT<:MOI.ModelLike} = |
| 44 | + SingleBridgeOptimizer{CountAtLeastToCountBelongsBridge{T},OT} |
| 45 | + |
| 46 | +function bridge_constraint( |
| 47 | + ::Type{CountAtLeastToCountBelongsBridge{T,F}}, |
| 48 | + model::MOI.ModelLike, |
| 49 | + f::F, |
| 50 | + s::MOI.CountAtLeast, |
| 51 | +) where {T,F<:Union{MOI.VectorOfVariables,MOI.VectorAffineFunction{T}}} |
| 52 | + x = collect(MOI.Utilities.eachscalar(f)) |
| 53 | + variables = MOI.VariableIndex[] |
| 54 | + cis = MOI.ConstraintIndex{F,MOI.CountBelongs}[] |
| 55 | + count_f = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm{T}[], zero(T)) |
| 56 | + offset = 0 |
| 57 | + for p in s.partitions |
| 58 | + indices = offset .+ (1:p) |
| 59 | + y = MOI.add_variable(model) |
| 60 | + push!(variables, y) |
| 61 | + push!(count_f.terms, MOI.ScalarAffineTerm(one(T), y)) |
| 62 | + ci = MOI.add_constraint( |
| 63 | + model, |
| 64 | + MOI.Utilities.operate(vcat, T, y, x[indices]...), |
| 65 | + MOI.CountBelongs(1 + p, s.set), |
| 66 | + ) |
| 67 | + push!(cis, ci) |
| 68 | + offset += p |
| 69 | + end |
| 70 | + count = MOI.add_constraint(model, count_f, MOI.GreaterThan(T(s.n))) |
| 71 | + return CountAtLeastToCountBelongsBridge{T,F}(f, s, variables, cis, count) |
| 72 | +end |
| 73 | + |
| 74 | +function MOI.supports_constraint( |
| 75 | + ::Type{<:CountAtLeastToCountBelongsBridge{T}}, |
| 76 | + ::Type{<:Union{MOI.VectorOfVariables,MOI.VectorAffineFunction{T}}}, |
| 77 | + ::Type{MOI.CountAtLeast}, |
| 78 | +) where {T} |
| 79 | + return true |
| 80 | +end |
| 81 | + |
| 82 | +function MOI.Bridges.added_constrained_variable_types( |
| 83 | + ::Type{<:CountAtLeastToCountBelongsBridge}, |
| 84 | +) |
| 85 | + return Tuple{Type}[] |
| 86 | +end |
| 87 | + |
| 88 | +function MOI.Bridges.added_constraint_types( |
| 89 | + ::Type{CountAtLeastToCountBelongsBridge{T,F}}, |
| 90 | +) where {T,F} |
| 91 | + return Tuple{Type,Type}[ |
| 92 | + (F, MOI.CountBelongs), |
| 93 | + (MOI.ScalarAffineFunction{T}, MOI.GreaterThan{T}), |
| 94 | + ] |
| 95 | +end |
| 96 | + |
| 97 | +function concrete_bridge_type( |
| 98 | + ::Type{<:CountAtLeastToCountBelongsBridge{T}}, |
| 99 | + ::Type{F}, |
| 100 | + ::Type{MOI.CountAtLeast}, |
| 101 | +) where {T,F<:Union{MOI.VectorOfVariables,MOI.VectorAffineFunction{T}}} |
| 102 | + return CountAtLeastToCountBelongsBridge{T,F} |
| 103 | +end |
| 104 | + |
| 105 | +function MOI.get( |
| 106 | + ::MOI.ModelLike, |
| 107 | + ::MOI.ConstraintFunction, |
| 108 | + bridge::CountAtLeastToCountBelongsBridge, |
| 109 | +) |
| 110 | + return bridge.f |
| 111 | +end |
| 112 | + |
| 113 | +function MOI.get( |
| 114 | + ::MOI.ModelLike, |
| 115 | + ::MOI.ConstraintSet, |
| 116 | + bridge::CountAtLeastToCountBelongsBridge, |
| 117 | +) |
| 118 | + return bridge.s |
| 119 | +end |
| 120 | + |
| 121 | +function MOI.delete( |
| 122 | + model::MOI.ModelLike, |
| 123 | + bridge::CountAtLeastToCountBelongsBridge, |
| 124 | +) |
| 125 | + for ci in bridge.ci |
| 126 | + MOI.delete(model, ci) |
| 127 | + end |
| 128 | + MOI.delete(model, bridge.count) |
| 129 | + for x in bridge.variables |
| 130 | + MOI.delete(model, x) |
| 131 | + end |
| 132 | + return |
| 133 | +end |
| 134 | + |
| 135 | +function MOI.get( |
| 136 | + bridge::CountAtLeastToCountBelongsBridge, |
| 137 | + ::MOI.NumberOfVariables, |
| 138 | +)::Int64 |
| 139 | + return length(bridge.variables) |
| 140 | +end |
| 141 | + |
| 142 | +function MOI.get( |
| 143 | + bridge::CountAtLeastToCountBelongsBridge, |
| 144 | + ::MOI.ListOfVariableIndices, |
| 145 | +)::Vector{MOI.VariableIndex} |
| 146 | + return copy(bridge.variables) |
| 147 | +end |
| 148 | + |
| 149 | +function MOI.get( |
| 150 | + bridge::CountAtLeastToCountBelongsBridge{T,F}, |
| 151 | + ::MOI.NumberOfConstraints{F,MOI.CountBelongs}, |
| 152 | +)::Int64 where {T,F} |
| 153 | + return length(bridge.ci) |
| 154 | +end |
| 155 | + |
| 156 | +function MOI.get( |
| 157 | + bridge::CountAtLeastToCountBelongsBridge{T,F}, |
| 158 | + ::MOI.ListOfConstraintIndices{F,MOI.CountBelongs}, |
| 159 | +) where {T,F} |
| 160 | + return copy(bridge.ci) |
| 161 | +end |
| 162 | + |
| 163 | +function MOI.get( |
| 164 | + ::CountAtLeastToCountBelongsBridge{T,F}, |
| 165 | + ::MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T},MOI.GreaterThan{T}}, |
| 166 | +)::Int64 where {T,F} |
| 167 | + return 1 |
| 168 | +end |
| 169 | + |
| 170 | +function MOI.get( |
| 171 | + bridge::CountAtLeastToCountBelongsBridge{T,F}, |
| 172 | + ::MOI.ListOfConstraintIndices{ |
| 173 | + MOI.ScalarAffineFunction{T}, |
| 174 | + MOI.GreaterThan{T}, |
| 175 | + }, |
| 176 | +) where {T,F} |
| 177 | + return [bridge.count] |
| 178 | +end |
0 commit comments