-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathvariables_container.jl
More file actions
503 lines (440 loc) · 13.4 KB
/
variables_container.jl
File metadata and controls
503 lines (440 loc) · 13.4 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# 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 AbstractVectorBounds end
function set_from_constants(
b::AbstractVectorBounds,
::Type{<:MOI.EqualTo},
index,
)
return MOI.EqualTo(b.lower[index])
end
function set_from_constants(
b::AbstractVectorBounds,
S::Type{<:Union{MOI.GreaterThan,MOI.EqualTo}},
index,
)
# Lower and upper bounds are equal for `EqualTo`, we can take either of them.
return S(b.lower[index])
end
function set_from_constants(
b::AbstractVectorBounds,
S::Type{<:MOI.LessThan},
index,
)
return S(b.upper[index])
end
function set_from_constants(
b::AbstractVectorBounds,
S::Type{<:Union{MOI.Interval,MOI.Semicontinuous,MOI.Semiinteger}},
index,
)
return S(b.lower[index], b.upper[index])
end
function set_from_constants(
::AbstractVectorBounds,
S::Type{<:Union{MOI.Integer,MOI.ZeroOne}},
index,
)
return S()
end
function set_from_constants(
b::AbstractVectorBounds,
::Type{<:MOI.Parameter},
index,
)
return MOI.Parameter(b.lower[index])
end
"""
SUPPORTED_VARIABLE_SCALAR_SETS{T}
The union of scalar sets for `VariableIndex` constraints supported by
`Utilities.Hyperrectangle` and `Utilities.VariablesContainer`.
"""
const SUPPORTED_VARIABLE_SCALAR_SETS{T} = Union{
MOI.EqualTo{T},
MOI.GreaterThan{T},
MOI.LessThan{T},
MOI.Interval{T},
MOI.Integer,
MOI.ZeroOne,
MOI.Semicontinuous{T},
MOI.Semiinteger{T},
MOI.Parameter{T},
}
# 0x01cb = 0x0080 | 0x0040 | 0x0008 | 0x0002 | 0x0001 | 0x0100
const _LOWER_BOUND_MASK = 0x01cb
# 0x01cd = 0x0080 | 0x0040 | 0x0008 | 0x0004 | 0x0001 | 0x0100
const _UPPER_BOUND_MASK = 0x01cd
const _DELETED_VARIABLE = 0x8000
_single_variable_flag(::Type{<:MOI.EqualTo}) = 0x0001
_single_variable_flag(::Type{<:MOI.GreaterThan}) = 0x0002
_single_variable_flag(::Type{<:MOI.LessThan}) = 0x0004
_single_variable_flag(::Type{<:MOI.Interval}) = 0x0008
_single_variable_flag(::Type{MOI.Integer}) = 0x0010
_single_variable_flag(::Type{MOI.ZeroOne}) = 0x0020
_single_variable_flag(::Type{<:MOI.Semicontinuous}) = 0x0040
_single_variable_flag(::Type{<:MOI.Semiinteger}) = 0x0080
_single_variable_flag(::Type{<:MOI.Parameter}) = 0x0100
function _flag_to_set_type(flag::UInt16, ::Type{T}) where {T}
if flag == 0x0001
return MOI.EqualTo{T}
elseif flag == 0x0002
return MOI.GreaterThan{T}
elseif flag == 0x0004
return MOI.LessThan{T}
elseif flag == 0x0008
return MOI.Interval{T}
elseif flag == 0x0010
return MOI.Integer
elseif flag == 0x0020
return MOI.ZeroOne
elseif flag == 0x0040
return MOI.Semicontinuous{T}
elseif flag == 0x0080
return MOI.Semiinteger{T}
else
@assert flag == 0x0100
return MOI.Parameter{T}
end
end
# Julia doesn't infer `S1` correctly, so we use a function barrier to improve
# inference.
function _throw_if_lower_bound_set_inner(variable, S2, mask, T)
S1 = _flag_to_set_type(mask, T)
return throw(MOI.LowerBoundAlreadySet{S1,S2}(variable))
end
function _throw_if_lower_bound_set(variable, S2, mask, T)
lower_mask = mask & _LOWER_BOUND_MASK
if iszero(lower_mask)
return # No lower bound set.
elseif iszero(_single_variable_flag(S2) & _LOWER_BOUND_MASK)
return # S2 isn't related to the lower bound.
end
return _throw_if_lower_bound_set_inner(variable, S2, lower_mask, T)
end
# Julia doesn't infer `S1` correctly, so we use a function barrier to improve
# inference.
function _throw_if_upper_bound_set_inner(variable, S2, mask, T)
S1 = _flag_to_set_type(mask, T)
return throw(MOI.UpperBoundAlreadySet{S1,S2}(variable))
end
function _throw_if_upper_bound_set(variable, S2, mask, T)
upper_mask = mask & _UPPER_BOUND_MASK
if iszero(upper_mask)
return # No upper bound set.
elseif iszero(_single_variable_flag(S2) & _UPPER_BOUND_MASK)
return # S2 isn't related to the upper bound.
end
return _throw_if_upper_bound_set_inner(variable, S2, upper_mask, T)
end
function _lower_bound(
set::Union{MOI.GreaterThan,MOI.Interval,MOI.Semicontinuous,MOI.Semiinteger},
)
return set.lower
end
_lower_bound(set::Union{MOI.EqualTo,MOI.Parameter}) = set.value
function _upper_bound(
set::Union{MOI.LessThan,MOI.Interval,MOI.Semicontinuous,MOI.Semiinteger},
)
return set.upper
end
_upper_bound(set::Union{MOI.EqualTo,MOI.Parameter}) = set.value
# Use `-Inf` and `Inf` for `AbstractFloat` subtypes.
_no_lower_bound(::Type{T}) where {T} = zero(T)
_no_lower_bound(::Type{T}) where {T<:AbstractFloat} = typemin(T)
_no_upper_bound(::Type{T}) where {T} = zero(T)
_no_upper_bound(::Type{T}) where {T<:AbstractFloat} = typemax(T)
###
### VariablesContainer
###
### For use in MOI.Utilities.Model
###
"""
struct VariablesContainer{T} <: AbstractVectorBounds
set_mask::Vector{UInt16}
lower::Vector{T}
upper::Vector{T}
end
A struct for storing variables and VariableIndex-related constraints. Used in
`MOI.Utilities.Model` by default.
"""
mutable struct VariablesContainer{T} <: AbstractVectorBounds
set_mask::Vector{UInt16}
lower::Vector{T}
upper::Vector{T}
end
function VariablesContainer{T}() where {T}
return VariablesContainer{T}(UInt16[], T[], T[])
end
function Base.:(==)(a::VariablesContainer, b::VariablesContainer)
return a.set_mask == b.set_mask && a.lower == b.lower && a.upper == b.upper
end
function MOI.empty!(b::VariablesContainer)
empty!(b.set_mask)
empty!(b.lower)
empty!(b.upper)
return b
end
function MOI.is_empty(b::VariablesContainer)
if length(b.set_mask) == 0
return true
end
return all(isequal(_DELETED_VARIABLE), b.set_mask)
end
function Base.resize!(b::VariablesContainer, n)
resize!(b.set_mask, n)
resize!(b.lower, n)
resize!(b.upper, n)
return
end
function MOI.add_variable(b::VariablesContainer{T}) where {T}
push!(b.set_mask, 0x0000)
push!(b.lower, _no_lower_bound(T))
push!(b.upper, _no_upper_bound(T))
x = MOI.VariableIndex(length(b.set_mask))
return x
end
function MOI.get(b::VariablesContainer, ::MOI.ListOfVariableIndices)
return MOI.VariableIndex[
MOI.VariableIndex(i) for
i in 1:length(b.set_mask) if b.set_mask[i] != _DELETED_VARIABLE
]
end
function MOI.is_valid(b::VariablesContainer, x::MOI.VariableIndex)
mask = get(b.set_mask, x.value, _DELETED_VARIABLE)
return mask != _DELETED_VARIABLE
end
function MOI.get(b::VariablesContainer, ::MOI.NumberOfVariables)::Int64
if length(b.set_mask) == 0
return 0
end
return sum(x != _DELETED_VARIABLE for x in b.set_mask)
end
function MOI.supports_constraint(
::VariablesContainer{T},
::Type{MOI.VariableIndex},
::Type{<:SUPPORTED_VARIABLE_SCALAR_SETS{T}},
) where {T}
return true
end
function MOI.add_constraint(
b::VariablesContainer{T},
f::MOI.VariableIndex,
set::S,
) where {T,S<:SUPPORTED_VARIABLE_SCALAR_SETS}
flag = _single_variable_flag(S)
mask = b.set_mask[f.value]
_throw_if_lower_bound_set(f, S, mask, T)
_throw_if_upper_bound_set(f, S, mask, T)
if !iszero(flag & _LOWER_BOUND_MASK)
b.lower[f.value] = _lower_bound(set)
end
if !iszero(flag & _UPPER_BOUND_MASK)
b.upper[f.value] = _upper_bound(set)
end
b.set_mask[f.value] = mask | flag
return MOI.ConstraintIndex{MOI.VariableIndex,S}(f.value)
end
function MOI.delete(
b::VariablesContainer{T},
ci::MOI.ConstraintIndex{MOI.VariableIndex,S},
) where {T,S<:SUPPORTED_VARIABLE_SCALAR_SETS{T}}
MOI.throw_if_not_valid(b, ci)
flag = _single_variable_flag(S)
b.set_mask[ci.value] &= ~flag
if !iszero(flag & _LOWER_BOUND_MASK)
b.lower[ci.value] = _no_lower_bound(T)
end
if !iszero(flag & _UPPER_BOUND_MASK)
b.upper[ci.value] = _no_upper_bound(T)
end
return
end
function MOI.delete(b::VariablesContainer, x::MOI.VariableIndex)
MOI.throw_if_not_valid(b, x)
b.set_mask[x.value] = _DELETED_VARIABLE
return
end
function MOI.is_valid(
b::VariablesContainer{T},
ci::MOI.ConstraintIndex{MOI.VariableIndex,S},
) where {T,S<:SUPPORTED_VARIABLE_SCALAR_SETS{T}}
if !(1 <= ci.value <= length(b.set_mask))
return false
end
return !iszero(b.set_mask[ci.value] & _single_variable_flag(S))
end
MOI.is_valid(::VariablesContainer, ::MOI.ConstraintIndex) = false
function MOI.get(
model::VariablesContainer,
::Union{UnsafeConstraintFunction,MOI.ConstraintFunction},
ci::MOI.ConstraintIndex{MOI.VariableIndex},
)
MOI.throw_if_not_valid(model, ci)
return MOI.VariableIndex(ci.value)
end
function MOI.set(
::VariablesContainer,
::MOI.ConstraintFunction,
::MOI.ConstraintIndex{MOI.VariableIndex},
::MOI.VariableIndex,
)
return throw(MOI.SettingVariableIndexNotAllowed())
end
function MOI.get(
model::VariablesContainer{T},
::MOI.ConstraintSet,
ci::MOI.ConstraintIndex{MOI.VariableIndex,S},
) where {T,S<:SUPPORTED_VARIABLE_SCALAR_SETS{T}}
MOI.throw_if_not_valid(model, ci)
return set_from_constants(model, S, ci.value)
end
function MOI.set(
model::VariablesContainer{T},
::MOI.ConstraintSet,
ci::MOI.ConstraintIndex{MOI.VariableIndex,S},
set::S,
) where {T,S<:SUPPORTED_VARIABLE_SCALAR_SETS{T}}
MOI.throw_if_not_valid(model, ci)
flag = _single_variable_flag(S)
if !iszero(flag & _LOWER_BOUND_MASK)
model.lower[ci.value] = _lower_bound(set)
end
if !iszero(flag & _UPPER_BOUND_MASK)
model.upper[ci.value] = _upper_bound(set)
end
return
end
function MOI.get(
b::VariablesContainer{T},
::MOI.NumberOfConstraints{MOI.VariableIndex,S},
)::Int64 where {T,S<:SUPPORTED_VARIABLE_SCALAR_SETS{T}}
flag = _single_variable_flag(S)
return count(mask -> !iszero(flag & mask), b.set_mask)
end
function _add_constraint_type(
list,
b::VariablesContainer,
S::Type{<:MOI.AbstractScalarSet},
)
flag = _single_variable_flag(S)::UInt16
if any(mask -> !iszero(flag & mask), b.set_mask)
push!(list, (MOI.VariableIndex, S))
end
return
end
function MOI.get(
b::VariablesContainer{T},
::MOI.ListOfConstraintTypesPresent,
) where {T}
list = Tuple{Type,Type}[]
_add_constraint_type(list, b, MOI.EqualTo{T})
_add_constraint_type(list, b, MOI.GreaterThan{T})
_add_constraint_type(list, b, MOI.LessThan{T})
_add_constraint_type(list, b, MOI.Interval{T})
_add_constraint_type(list, b, MOI.Semicontinuous{T})
_add_constraint_type(list, b, MOI.Semiinteger{T})
_add_constraint_type(list, b, MOI.Integer)
_add_constraint_type(list, b, MOI.ZeroOne)
_add_constraint_type(list, b, MOI.Parameter{T})
return list
end
function MOI.get(
b::VariablesContainer{T},
::MOI.ListOfConstraintIndices{MOI.VariableIndex,S},
) where {T,S<:SUPPORTED_VARIABLE_SCALAR_SETS{T}}
list = MOI.ConstraintIndex{MOI.VariableIndex,S}[]
flag = _single_variable_flag(S)
for (index, mask) in enumerate(b.set_mask)
if !iszero(mask & flag)
push!(list, MOI.ConstraintIndex{MOI.VariableIndex,S}(index))
end
end
return list
end
# These `MOI.ModelLike` fallbacks have to be redefined here as
# `VariablesContainer` is not a subtype of `MOI.ModelLike`
function MOI.throw_if_not_valid(b::VariablesContainer, index)
if !MOI.is_valid(b, index)
throw(MOI.InvalidIndex(index))
end
end
function MOI.set(
::VariablesContainer,
::MOI.ConstraintFunction,
ci::MOI.ConstraintIndex,
func,
)
return throw(MOI.FunctionTypeMismatch{MOI.func_type(ci),typeof(func)}())
end
function MOI.set(
::VariablesContainer,
::MOI.ConstraintSet,
ci::MOI.ConstraintIndex{MOI.VariableIndex},
set,
)
return throw(MOI.SetTypeMismatch{MOI.set_type(ci),typeof(set)}())
end
function MOI.supports_constraint(
::VariablesContainer,
::Type{<:MOI.AbstractFunction},
::Type{<:MOI.AbstractSet},
)
return false
end
function MOI.add_constraint(
::VariablesContainer,
func::MOI.AbstractFunction,
set::MOI.AbstractSet,
)
return throw(MOI.UnsupportedConstraint{typeof(func),typeof(set)}())
end
###
### Hyperrectangle
###
"""
struct Hyperrectangle{T} <: AbstractVectorBounds
lower::Vector{T}
upper::Vector{T}
end
A struct for the .constants field in MatrixOfConstraints.
"""
struct Hyperrectangle{T} <: AbstractVectorBounds
lower::Vector{T}
upper::Vector{T}
end
Hyperrectangle{T}() where {T} = Hyperrectangle{T}(T[], T[])
function Base.:(==)(a::Hyperrectangle, b::Hyperrectangle)
return a.lower == b.lower && a.upper == b.upper
end
function Base.empty!(b::Hyperrectangle)
empty!(b.lower)
empty!(b.upper)
return b
end
function Base.resize!(b::Hyperrectangle, n)
resize!(b.lower, n)
resize!(b.upper, n)
return
end
function load_constants(
b::Hyperrectangle{T},
offset,
set::SUPPORTED_VARIABLE_SCALAR_SETS{T},
) where {T}
flag = _single_variable_flag(typeof(set))
if iszero(flag & _LOWER_BOUND_MASK)
b.lower[offset+1] = _no_lower_bound(T)
else
b.lower[offset+1] = _lower_bound(set)
end
if iszero(flag & _UPPER_BOUND_MASK)
b.upper[offset+1] = _no_upper_bound(T)
else
b.upper[offset+1] = _upper_bound(set)
end
return
end
function_constants(::Hyperrectangle{T}, row) where {T} = zero(T)