11
11
12
12
* ``x \\ in \\ textsf{CountAtLeast}(n, d, set)`` to
13
13
``(n_i, x_{d_i}) \\ in \\ textsf{CountBelongs}(1+d)``
14
- and ``\\ sum \\ limits n_i \\ ge n``
14
+ and ``n_i \\ ge n``
15
15
16
16
## Source node
17
17
@@ -27,7 +27,7 @@ where `F` is [`MOI.VectorOfVariables`](@ref) or
27
27
`CountAtLeastToCountBelongsBridge` creates:
28
28
29
29
* `F` in [`MOI.CountBelongs`](@ref)
30
- * [`MOI.ScalarAffineFunction{T} `](@ref) in [`MOI.GreaterThan{T}`](@ref)
30
+ * [`MOI.VariableIndex `](@ref) in [`MOI.GreaterThan{T}`](@ref)
31
31
"""
32
32
mutable struct CountAtLeastToCountBelongsBridge{
33
33
T,
@@ -37,7 +37,19 @@ mutable struct CountAtLeastToCountBelongsBridge{
37
37
s:: MOI.CountAtLeast
38
38
variables:: Vector{MOI.VariableIndex}
39
39
ci:: Vector{MOI.ConstraintIndex{F,MOI.CountBelongs}}
40
- count:: MOI.ConstraintIndex{MOI.ScalarAffineFunction{T},MOI.GreaterThan{T}}
40
+ # We need an explicit inner constructor to avoid the unbound type parameter
41
+ # T (it doesn't appear in the fields).
42
+ function CountAtLeastToCountBelongsBridge {T} (
43
+ f:: Union{MOI.VectorOfVariables,MOI.VectorAffineFunction{T}} ,
44
+ s:: MOI.CountAtLeast ,
45
+ ) where {T}
46
+ return new {T,typeof(f)} (
47
+ f,
48
+ s,
49
+ MOI. VariableIndex[],
50
+ MOI. ConstraintIndex{typeof (f),MOI. CountBelongs}[],
51
+ )
52
+ end
41
53
end
42
54
43
55
const CountAtLeastToCountBelongs{T,OT<: MOI.ModelLike } =
@@ -50,25 +62,21 @@ function bridge_constraint(
50
62
s:: MOI.CountAtLeast ,
51
63
) where {T,F<: Union{MOI.VectorOfVariables,MOI.VectorAffineFunction{T}} }
52
64
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))
65
+ bridge = CountAtLeastToCountBelongsBridge {T} (f, s)
56
66
offset = 0
57
67
for p in s. partitions
58
68
indices = offset .+ (1 : p)
59
- y = MOI. add_variable (model)
60
- push! (variables, y)
61
- push! (count_f. terms, MOI. ScalarAffineTerm (one (T), y))
69
+ y, _ = MOI. add_constrained_variable (model, MOI. GreaterThan (T (s. n)))
70
+ push! (bridge. variables, y)
62
71
ci = MOI. add_constraint (
63
72
model,
64
73
MOI. Utilities. operate (vcat, T, y, x[indices]. .. ),
65
74
MOI. CountBelongs (1 + p, s. set),
66
75
)
67
- push! (cis , ci)
76
+ push! (bridge . ci , ci)
68
77
offset += p
69
78
end
70
- count = MOI. add_constraint (model, count_f, MOI. GreaterThan (T (s. n)))
71
- return CountAtLeastToCountBelongsBridge {T,F} (f, s, variables, cis, count)
79
+ return bridge
72
80
end
73
81
74
82
function MOI. supports_constraint (
@@ -80,18 +88,15 @@ function MOI.supports_constraint(
80
88
end
81
89
82
90
function MOI. Bridges. added_constrained_variable_types (
83
- :: Type{<:CountAtLeastToCountBelongsBridge} ,
84
- )
85
- return Tuple{Type}[]
91
+ :: Type{<:CountAtLeastToCountBelongsBridge{T} } ,
92
+ ) where {T}
93
+ return Tuple{Type}[(MOI . GreaterThan{T},) ]
86
94
end
87
95
88
96
function MOI. Bridges. added_constraint_types (
89
97
:: Type{CountAtLeastToCountBelongsBridge{T,F}} ,
90
98
) where {T,F}
91
- return Tuple{Type,Type}[
92
- (F, MOI. CountBelongs),
93
- (MOI. ScalarAffineFunction{T}, MOI. GreaterThan{T}),
94
- ]
99
+ return Tuple{Type,Type}[(F, MOI. CountBelongs)]
95
100
end
96
101
97
102
function concrete_bridge_type (
@@ -125,7 +130,6 @@ function MOI.delete(
125
130
for ci in bridge. ci
126
131
MOI. delete (model, ci)
127
132
end
128
- MOI. delete (model, bridge. count)
129
133
for x in bridge. variables
130
134
MOI. delete (model, x)
131
135
end
@@ -161,18 +165,18 @@ function MOI.get(
161
165
end
162
166
163
167
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
+ bridge :: CountAtLeastToCountBelongsBridge{T} ,
169
+ :: MOI.NumberOfConstraints{MOI.VariableIndex ,MOI.GreaterThan{T}} ,
170
+ ):: Int64 where {T}
171
+ return length (bridge . variables)
168
172
end
169
173
170
174
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 ]
175
+ bridge:: CountAtLeastToCountBelongsBridge{T} ,
176
+ :: MOI.ListOfConstraintIndices{MOI.VariableIndex,MOI.GreaterThan{T}} ,
177
+ ) where {T}
178
+ return [
179
+ MOI . ConstraintIndex {MOI.VariableIndex,MOI.GreaterThan{T}} (x . value) for
180
+ x in bridge . variables
181
+ ]
178
182
end
0 commit comments