-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathbridge_optimizer.jl
1935 lines (1762 loc) · 56.8 KB
/
bridge_optimizer.jl
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
AbstractBridgeOptimizer
A bridge optimizer applies given constraint bridges to a given optimizer thus
extending the types of supported constraints. The attributes of the inner
optimizer are automatically transformed to make the bridges transparent, e.g.
the variables and constraints created by the bridges are hidden.
By convention, the inner optimizer should be stored in a `model` field and
the dictionary mapping constraint indices to bridges should be stored in a
`bridges` field. If a bridge optimizer deviates from these conventions, it
should implement the functions `MOI.optimize!` and `bridge` respectively.
"""
abstract type AbstractBridgeOptimizer <: MOI.AbstractOptimizer end
# AbstractBridgeOptimizer interface
function recursive_model end
function supports_constraint_bridges end
"""
is_bridged(
b::AbstractBridgeOptimizer,
F::Type{<:MOI.AbstractFunction},
S::Type{<:MOI.AbstractSet},
)::Bool
Return a `Bool` indicating whether `b` tries to bridge `F`-in-`S` constraints
instead of passing it as is to its internal model.
is_bridged(b::AbstractBridgeOptimizer, S::Type{<:MOI.AbstractSet})::Bool
Return a `Bool` indicating whether `b` tries to bridge constrained variables in
`S` instead of passing it as is to its internal model.
is_bridged(
b::AbstractBridgeOptimizer,
F::Type{<:MOI.AbstractFunction},
)::Bool
Return a `Bool` indicating whether `b` tries to bridge objective functions of
type `F` instead of passing it as is to its internal model.
"""
function is_bridged end
"""
is_bridged(b::AbstractBridgeOptimizer, vi::MOI.VariableIndex)
Return a `Bool` indicating whether `vi` is bridged. The variable is said to be
bridged if it is a variable of `b` but not a variable of `b.model`.
"""
is_bridged(::AbstractBridgeOptimizer, vi::MOI.VariableIndex) = vi.value < 0
"""
is_bridged(b::AbstractBridgeOptimizer, ci::MOI.ConstraintIndex)
Return a `Bool` indicating whether `ci` is bridged. The constraint is said to be
bridged if it is a constraint of `b` but not a constraint of `b.model`.
"""
function is_bridged(
b::AbstractBridgeOptimizer,
ci::MOI.ConstraintIndex{F,S},
) where {F,S}
return is_bridged(b, F, S)
end
function is_bridged(
b::AbstractBridgeOptimizer,
ci::MOI.ConstraintIndex{MOI.VariableIndex,S},
) where {S}
# There are a few cases for which we should return `false`:
# 1) It was added as variables constrained on creation to `b.model`,
# In this case, `is_bridged(b, S)` is `false` and `ci.value >= 0`.
# 2) It was added as constraint on a non-bridged variable to `b.model`,
# In this case, `is_bridged(b, F, S)` is `false` and `ci.value >= 0`.
# and a few cases for which we should return `true`:
# 3) It was added with a variable bridge,
# In this case, `is_bridged(b, S)` is `true` and `ci.value < 0`.
# 4) It was added as constraint on a bridged variable so it was force-bridged,
# In this case, `ci.value < 0`.
# 5) It was added with a constraint bridge,
# In this case, `is_bridged(b, F, S)` is `true` and `ci.value >= 0` (the variable is non-bridged, otherwise, the constraint would have been force-bridged).
# So
# * if, `ci.value < 0` then it is case 3) or 4) and we return `true`.
# * Otherwise,
# - if `is_bridged(b, S)` and `is_bridged(b, F, S)` then 1) and 2) are
# not possible so we are in case 5) and we return `true`.
# - if `!is_bridged(b, F, S)`, then 5) is not possible and we return `false`.
# - if `!is_bridged(b, S)` and `is_bridged(b, F, S)`, then it is either case 1)
# or 5). They cannot both be the cases as one cannot add two `VariableIndex`
# with the same set type on the same variable (this is ensured by
# `_check_double_single_variable`). Therefore, we can safely determine
# whether it is bridged with `haskey(Constraint.bridges(b), ci)`.
return ci.value < 0 || (
is_bridged(b, MOI.VariableIndex, S) &&
(is_bridged(b, S) || haskey(Constraint.bridges(b), ci))
)
end
function is_bridged(
::AbstractBridgeOptimizer,
ci::MOI.ConstraintIndex{MOI.VectorOfVariables,S},
) where {S}
return ci.value < 0
end
"""
is_bridged(b::AbstractBridgeOptimizer, attr::MOI.ObjectiveFunction)
Return a `Bool` indicating whether `attr` is bridged. The objective function is
said to be bridged the objective function attribute passed to `b.model` is
different to `attr`.
"""
function is_bridged(b::AbstractBridgeOptimizer, attr::MOI.ObjectiveFunction)
return haskey(Objective.bridges(b), attr)
end
const ObjectiveAttribute =
Union{MOI.ObjectiveSense,MOI.ObjectiveFunction,MOI.ObjectiveFunctionType}
"""
supports_bridging_constrained_variable(
::AbstractBridgeOptimizer,
::Type{<:MOI.AbstractSet},
)
Return a `Bool` indicating whether `b` supports bridging constrained variable in
`S`.
"""
function supports_bridging_constrained_variable(
::AbstractBridgeOptimizer,
::Type{<:MOI.AbstractSet},
)
return false
end
"""
is_variable_bridged(
b::AbstractBridgeOptimizer,
S::Type{<:MOI.AbstractSet},
)
Return a `Bool` indicating whether `b` bridges constrained variable in
`S` using a variable bridge, assuming `is_bridged(b, S)`. If it returns `false`,
it means that free variables and a constraint on these variables should be
added instead. The constraint is then bridged by a constraint bridge.
"""
function is_variable_bridged(
::AbstractBridgeOptimizer,
::Type{<:MOI.AbstractSet},
)
return false
end
"""
is_variable_bridged(b::AbstractBridgeOptimizer, ci::MOI.ConstraintIndex)
Returns whether `ci` is the constraint of a bridged constrained variable. That
is, if it was returned by `Variable.add_key_for_bridge` or
`Variable.add_keys_for_bridge`. Note that it is not equivalent to
`ci.value < 0` as, it can also simply be a constraint on a bridged variable.
"""
is_variable_bridged(::AbstractBridgeOptimizer, ::MOI.ConstraintIndex) = false
function is_variable_bridged(
b::AbstractBridgeOptimizer,
ci::MOI.ConstraintIndex{<:Union{MOI.VariableIndex,MOI.VectorOfVariables}},
)
# It can be a constraint corresponding to bridged constrained variables so
# we `check` with `haskey(Constraint.bridges(b), ci)` whether this is the
# case.
return ci.value < 0 && !haskey(Constraint.bridges(b), ci)
end
"""
supports_bridging_constraint(
b::AbstractBridgeOptimizer,
F::Type{<:MOI.AbstractFunction},
S::Type{<:MOI.AbstractSet},
)::Bool
Return a `Bool` indicating whether `b` supports bridging `F`-in-`S` constraints.
"""
function supports_bridging_constraint(
::AbstractBridgeOptimizer,
::Type{<:MOI.AbstractFunction},
::Type{<:MOI.AbstractSet},
)
return false
end
"""
supports_bridging_objective_function(
b::AbstractBridgeOptimizer,
F::Type{<:MOI.AbstractScalarFunction},
)::Bool
Return a `Bool` indicating whether `b` supports bridging objective functions of
type `F`.
"""
function supports_bridging_objective_function(
::AbstractBridgeOptimizer,
::Type{<:MOI.AbstractScalarFunction},
)
return false
end
"""
bridge_type(
b::AbstractBridgeOptimizer,
F::Type{<:MOI.AbstractFunction},
S::Type{<:MOI.AbstractSet},
)
Return the `AbstractBridge` type to be used to bridge `F`-in-`S` constraints.
This function should only be called if `is_bridged(b, F, S)`.
"""
function bridge_type end
"""
bridge(b::AbstractBridgeOptimizer, vi::MOI.VariableIndex)
Return the `Variable.AbstractBridge` used to bridge the variable with index
`vi`.
"""
function bridge(b::AbstractBridgeOptimizer, vi::MOI.VariableIndex)
return Variable.bridges(b)[vi]
end
"""
bridge(b::AbstractBridgeOptimizer, ci::MOI.ConstraintIndex)
Return the `AbstractBridge` used to bridge the constraint with index `ci`.
"""
function bridge(b::AbstractBridgeOptimizer, ci::MOI.ConstraintIndex)
if is_variable_bridged(b, ci)
return bridge(b, MOI.VariableIndex(ci.value))
else
return Constraint.bridges(b)[ci]
end
end
"""
bridge(b::AbstractBridgeOptimizer, attr::MOI.ObjectiveFunction)
Return the `Objective.AbstractBridge` used to bridge the objective function
`attr`.
"""
function bridge(b::AbstractBridgeOptimizer, attr::MOI.ObjectiveFunction)
return Objective.bridges(b)[attr]
end
"""
call_in_context(
b::AbstractBridgeOptimizer,
vi::MOI.VariableIndex,
f::Function,
)
Call `f(bridge)` where `vi` is bridged by `bridge` in its context, see
[`Variable.call_in_context`](@ref).
"""
function call_in_context(
b::AbstractBridgeOptimizer,
vi::MOI.VariableIndex,
f::Function,
)
return Variable.call_in_context(Variable.bridges(b), vi, f)
end
"""
call_in_context(
b::AbstractBridgeOptimizer,
ci::MOI.ConstraintIndex,
f::Function,
)
Call `f(bridge)` where `ci` is bridged by `bridge` in its context, see
[`Variable.call_in_context`](@ref).
"""
function call_in_context(
b::AbstractBridgeOptimizer,
ci::MOI.ConstraintIndex,
f::Function,
)
if is_variable_bridged(b, ci)
return call_in_context(b, MOI.VariableIndex(ci.value), f)
else
return Variable.call_in_context(
Variable.bridges(b),
ci,
() -> f(Constraint.bridges(b)[ci]),
)
end
end
function call_in_context(
f::F,
b::AbstractBridgeOptimizer,
index::MOI.Index,
attr::MOI.AnyAttribute,
args::Vararg{Any,N},
) where {F<:Union{typeof(MOI.get),typeof(MOI.set)},N}
return call_in_context(
b,
index,
bridge -> f(recursive_model(b), attr, bridge, args...),
)
end
function call_in_context(
f::F,
b::AbstractBridgeOptimizer,
index::MOI.Index,
args::Vararg{Any,N},
) where {F<:Function,N}
return call_in_context(
b,
index,
bridge -> f(recursive_model(b), bridge, args...),
)
end
function _functionize_bridge(b::AbstractBridgeOptimizer, bridge_type)
func, name = _func_name(bridge_type)
return error(
"Need to apply a `$bridge_type` to a `$func` $name because the",
" variable is bridged but $name bridges are not supported by",
" `$(typeof(b))`.",
)
end
function constraint_scalar_functionize_bridge(b::AbstractBridgeOptimizer)
return _functionize_bridge(b, Constraint.ScalarFunctionizeBridge)
end
function constraint_vector_functionize_bridge(b::AbstractBridgeOptimizer)
return _functionize_bridge(b, Constraint.VectorFunctionizeBridge)
end
function objective_functionize_bridge(b::AbstractBridgeOptimizer)
return _functionize_bridge(b, Objective.FunctionizeBridge)
end
# Implementation of the MOI interface for AbstractBridgeOptimizer
MOI.optimize!(b::AbstractBridgeOptimizer) = MOI.optimize!(b.model)
# By convention, the model should be stored in a `model` field
function MOI.is_empty(b::AbstractBridgeOptimizer)
return isempty(Variable.bridges(b)) &&
isempty(Constraint.bridges(b)) &&
MOI.is_empty(b.model)
end
function MOI.empty!(b::AbstractBridgeOptimizer)
MOI.empty!(b.model)
if Variable.has_bridges(Variable.bridges(b))
empty!(b.var_to_name)
b.name_to_var = nothing
end
if Variable.has_bridges(Variable.bridges(b)) ||
Constraint.has_bridges(Constraint.bridges(b))
empty!(b.con_to_name)
b.name_to_con = nothing
end
empty!(Variable.bridges(b))
empty!(Constraint.bridges(b))
empty!(Objective.bridges(b))
return
end
function MOI.supports(
b::AbstractBridgeOptimizer,
attr::Union{MOI.AbstractModelAttribute,MOI.AbstractOptimizerAttribute},
)
return MOI.supports(b.model, attr)
end
function MOIU.pass_nonvariable_constraints(
dest::AbstractBridgeOptimizer,
src::MOI.ModelLike,
idxmap::MOIU.IndexMap,
constraint_types,
)
if Variable.has_bridges(Variable.bridges(dest))
# The functions may contained bridged variables which needs to be
# substituted so we use the fallback.
return MOIU.pass_nonvariable_constraints_fallback(
dest,
src,
idxmap,
constraint_types,
)
end
not_bridged_types = eltype(constraint_types)[]
bridged_types = eltype(constraint_types)[]
for (F, S) in constraint_types
if is_bridged(dest, F, S)
push!(bridged_types, (F, S))
else
push!(not_bridged_types, (F, S))
end
end
MOIU.pass_nonvariable_constraints(
dest.model,
src,
idxmap,
not_bridged_types,
)
MOIU.pass_nonvariable_constraints_fallback(dest, src, idxmap, bridged_types)
return
end
function MOI.copy_to(
dest::AbstractBridgeOptimizer,
src::MOI.ModelLike;
kwargs...,
)
return MOIU.default_copy_to(dest, src; kwargs...)
end
function MOI.supports_incremental_interface(b::AbstractBridgeOptimizer)
return MOI.supports_incremental_interface(b.model)
end
function MOIU.final_touch(uf::AbstractBridgeOptimizer, index_map)
return MOIU.final_touch(uf.model, index_map)
end
# References
function MOI.is_valid(b::AbstractBridgeOptimizer, vi::MOI.VariableIndex)
if is_bridged(b, vi)
return haskey(Variable.bridges(b), vi)
else
return MOI.is_valid(b.model, vi)
end
end
function MOI.is_valid(
b::AbstractBridgeOptimizer,
ci::MOI.ConstraintIndex{F,S},
) where {F,S}
if is_bridged(b, ci)
if is_variable_bridged(b, ci)
vi = MOI.VariableIndex(ci.value)
return MOI.is_valid(b, vi) &&
Variable.constrained_set(Variable.bridges(b), vi) == S
else
return haskey(Constraint.bridges(b), ci)
end
else
return MOI.is_valid(b.model, ci)
end
end
function _delete_variables_in_vector_of_variables_constraint(
b::AbstractBridgeOptimizer,
vis::Vector{MOI.VariableIndex},
ci::MOI.ConstraintIndex{MOI.VectorOfVariables,S},
) where {S}
func = MOI.get(b, MOI.ConstraintFunction(), ci)
variables = copy(func.variables)
if vis == variables
MOI.delete(b, ci)
else
for vi in vis
i = findfirst(isequal(vi), variables)
if i !== nothing
if MOI.supports_dimension_update(S)
call_in_context(MOI.delete, b, ci, IndexInVector(i))
else
MOIU.throw_delete_variable_in_vov(vi)
end
end
end
end
end
function _delete_variables_in_variables_constraints(
b::AbstractBridgeOptimizer,
vis::Vector{MOI.VariableIndex},
)
# Delete all `MOI.VectorOfVariables` constraints of these variables.
# We reverse for the same reason as for `VariableIndex` below.
# As the iterators are lazy, when the inner bridge constraint is deleted,
# it won't be part of the iteration.
for ci in Iterators.reverse(
Constraint.vector_of_variables_constraints(Constraint.bridges(b)),
)
_delete_variables_in_vector_of_variables_constraint(b, vis, ci)
end
# Delete all `MOI.VariableIndex` constraints of these variables.
for vi in vis
# If a bridged `VariableIndex` constraints creates a second one,
# then we will delete the second one when deleting the first one hence we
# should not delete it again in this loop.
# For this, we reverse the order so that we encounter the first one first
# and we won't delete the second one since `MOI.is_valid(b, ci)` will be `false`.
for ci in Iterators.reverse(
Constraint.variable_constraints(Constraint.bridges(b), vi),
)
if MOI.is_valid(b, ci)
MOI.delete(b, ci)
end
end
end
end
function MOI.delete(b::AbstractBridgeOptimizer, vis::Vector{MOI.VariableIndex})
if Constraint.has_bridges(Constraint.bridges(b))
_delete_variables_in_variables_constraints(b, vis)
end
if any(vi -> is_bridged(b, vi), vis)
for vi in vis
MOI.throw_if_not_valid(b, vi)
end
if all(vi -> is_bridged(b, vi), vis) &&
Variable.has_keys(Variable.bridges(b), vis)
call_in_context(MOI.delete, b, first(vis))
b.name_to_var = nothing
for vi in vis
delete!(b.var_to_name, vi)
end
ci = Variable.constraint(Variable.bridges(b), first(vis))
b.name_to_con = nothing
delete!(b.con_to_name, ci)
delete!(Variable.bridges(b), vis)
else
for vi in vis
MOI.delete(b, vi)
end
end
else
MOI.delete(b.model, vis)
end
end
function MOI.delete(b::AbstractBridgeOptimizer, vi::MOI.VariableIndex)
if Constraint.has_bridges(Constraint.bridges(b))
_delete_variables_in_variables_constraints(b, [vi])
end
if is_bridged(b, vi)
MOI.throw_if_not_valid(b, vi)
if Variable.length_of_vector_of_variables(Variable.bridges(b), vi) > 1
if MOI.supports_dimension_update(
Variable.constrained_set(Variable.bridges(b), vi),
)
call_in_context(MOI.delete, b, vi, _index(b, vi)...)
else
MOIU.throw_delete_variable_in_vov(vi)
end
else
call_in_context(MOI.delete, b, vi)
ci = Variable.constraint(Variable.bridges(b), vi)
b.name_to_con = nothing
delete!(b.con_to_name, ci)
end
delete!(Variable.bridges(b), vi)
b.name_to_var = nothing
delete!(b.var_to_name, vi)
else
MOI.delete(b.model, vi)
end
end
function MOI.delete(b::AbstractBridgeOptimizer, ci::MOI.ConstraintIndex)
if is_bridged(b, ci)
MOI.throw_if_not_valid(b, ci)
br = bridge(b, ci)
if is_variable_bridged(b, ci)
error(
"Cannot delete constraint index of bridged constrained",
" variables. Delete the scalar variable or the vector of",
" variables instead.",
)
else
delete!(Constraint.bridges(b), ci)
end
Variable.call_in_context(
Variable.bridges(b),
ci,
() -> MOI.delete(recursive_model(b), br),
)
b.name_to_con = nothing
delete!(b.con_to_name, ci)
else
MOI.delete(b.model, ci)
end
end
# Attributes
"""
function reduce_bridged(
b::AbstractBridgeOptimizer,
args,
F::Type{<:MOI.AbstractFunction},
S::Type{<:MOI.AbstractSet},
value::T,
operate_variable_bridges!,
operate_constraint_bridges!,
)::T where {T}
If `F`-in-`S` constraints may be added to `b.model`, starts with
`value = MOI.get(b.model, args...)`, otherwise, starts with `value`.
Then:
* if `F`-in-`S` constraints may correspond to bridged variables, modify it with
`operate_variable_bridges!`
* if `F`-in-`S` constraints may correspond to bridged constraints, modify it
with `operate_constraint_bridges!`
then return the final `value`.
For example, [`MOI.supports`](@ref) calls this function with
* `value = true`
* `operate_variable_bridges(ok) = ok && MOI.supports(b, attr, Variable.concrete_bridge_type(b, S))`
* `operate_constraint_bridges(ok) = ok && MOI.supports(b, attr, Constraint.concrete_bridge_type(b, F, S))`.
"""
function reduce_bridged(
b::AbstractBridgeOptimizer,
F::Type{<:MOI.AbstractFunction},
S::Type{<:MOI.AbstractSet},
value::T,
model_value::Function,
operate_variable_bridges!::Function,
operate_constraint_bridges!::Function,
)::T where {T}
variable_function = F == MOIU.variable_function_type(S)
# A `F`-in-`S` could be added to the model either if it this constraint
# is not bridged or if variables constrained on creations to `S` are not
# bridged and `F` is `VariableIndex` or `VectorOfVariables`.
if !is_bridged(b, F, S) || (variable_function && !is_bridged(b, S))
value = model_value()
end
if variable_function && is_bridged(b, S) && is_variable_bridged(b, S)
value = operate_variable_bridges!(value)
end
# Even if it is not bridged, it may have been force-bridged because one of
# the variable in the function was bridged.
if is_bridged(b, F, S) ||
(variable_function && supports_constraint_bridges(b))
value = operate_constraint_bridges!(value)
end
return value
end
# List of indices of all constraints, including those bridged
function get_all_including_bridged(
b::AbstractBridgeOptimizer,
attr::MOI.ListOfVariableIndices,
)
list = MOI.get(b.model, attr)
if !isempty(Variable.bridges(b))
# The conversion from `keys` into a `Vector` happens inside `append!`.
append!(list, keys(Variable.bridges(b)))
end
return list
end
function get_all_including_bridged(
b::AbstractBridgeOptimizer,
attr::MOI.ListOfConstraintIndices{F,S},
) where {F,S}
return reduce_bridged(
b,
F,
S,
MOI.ConstraintIndex{F,S}[],
() -> MOI.get(b.model, attr),
list -> append!(
list,
Variable.constraints_with_set(Variable.bridges(b), S),
),
list -> append!(
list,
Constraint.keys_of_type(
Constraint.bridges(b),
MOI.ConstraintIndex{F,S},
),
),
)
end
# Remove constraints bridged by `bridge` from `list`
function _remove_bridged(list, bridge, attr)
for c in MOI.get(bridge, attr)
i = findfirst(isequal(c), list)
if i !== nothing
MOI.deleteat!(list, i)
end
end
return
end
function MOI.get(
b::AbstractBridgeOptimizer,
attr::Union{MOI.ListOfConstraintIndices,MOI.ListOfVariableIndices},
)
list = get_all_including_bridged(b, attr)
for bridge in values(Variable.bridges(b))
_remove_bridged(list, bridge, attr)
end
for bridge in values(Constraint.bridges(b))
_remove_bridged(list, bridge, attr)
end
for bridge in values(Objective.bridges(b))
_remove_bridged(list, bridge, attr)
end
return list
end
function MOI.get(b::AbstractBridgeOptimizer, attr::MOI.NumberOfVariables)::Int64
s =
MOI.get(b.model, attr) +
Variable.number_of_variables(Variable.bridges(b))
for bridge in values(Variable.bridges(b))
s -= MOI.get(bridge, attr)
end
for bridge in values(Constraint.bridges(b))
s -= MOI.get(bridge, attr)
end
for bridge in values(Objective.bridges(b))
s -= MOI.get(bridge, attr)
end
return s
end
# Number of all constraints, including those bridged
function get_all_including_bridged(
b::AbstractBridgeOptimizer,
attr::MOI.NumberOfConstraints{F,S},
) where {F,S}
return reduce_bridged(
b,
F,
S,
Int64(0),
() -> MOI.get(b.model, attr),
num -> num + Variable.number_with_set(Variable.bridges(b), S),
num ->
num + Constraint.number_of_type(
Constraint.bridges(b),
MOI.ConstraintIndex{F,S},
),
)
end
function MOI.get(
b::AbstractBridgeOptimizer,
attr::MOI.NumberOfConstraints{F,S},
)::Int64 where {F,S}
s = get_all_including_bridged(b, attr)
# The constraints counted in `s` may have been added by bridges
for bridge in values(Variable.bridges(b))
s -= MOI.get(bridge, attr)
end
for bridge in values(Constraint.bridges(b))
s -= MOI.get(bridge, attr)
end
for bridge in values(Objective.bridges(b))
s -= MOI.get(bridge, attr)
end
return s
end
function MOI.get(
b::AbstractBridgeOptimizer,
attr::MOI.ListOfConstraintTypesPresent,
)
list_of_types = MOI.get(b.model, attr)
if Constraint.has_bridges(Constraint.bridges(b))
append!(
list_of_types,
Constraint.list_of_key_types(Constraint.bridges(b)),
)
end
if Variable.has_bridges(Variable.bridges(b))
append!(
list_of_types,
Variable.list_of_constraint_types(Variable.bridges(b)),
)
end
unique!(list_of_types)
# Some constraint types show up in `list_of_types` including when all the
# constraints of that type have been created by bridges and not by the user.
# The code in `NumberOfConstraints` takes care of removing these constraints
# from the counter so we can rely on it to remove these constraint types.
filter!(list_of_types) do (F, S)
return MOI.get(b, MOI.NumberOfConstraints{F,S}()) > 0
end
return list_of_types
end
# Model an optimizer attributes
function MOI.get(b::AbstractBridgeOptimizer, attr::MOI.ListOfModelAttributesSet)
list = MOI.get(b.model, attr)
if is_objective_bridged(b)
list = copy(list)
# There should be a `MOI.ObjectiveFunction` in `list` otherwise
# `is_objective_bridged` would return `false`.
deleteat!(list, findfirst(attr -> attr isa MOI.ObjectiveFunction, list))
push!(
list,
MOI.ObjectiveFunction{MOI.get(b, MOI.ObjectiveFunctionType())}(),
)
end
return unbridged_function(b, list)
end
function MOI.get(
b::AbstractBridgeOptimizer,
attr::Union{MOI.AbstractModelAttribute,MOI.AbstractOptimizerAttribute},
)
return unbridged_function(b, MOI.get(b.model, attr))
end
function MOI.set(
b::AbstractBridgeOptimizer,
attr::Union{MOI.AbstractModelAttribute,MOI.AbstractOptimizerAttribute},
value,
)
MOI.set(b.model, attr, bridged_function(b, value))
return
end
"""
bridging_cost(b::AbstractBridgeOptimizer, S::Type{<:MOI.AbstractSet}})
Return the cost of bridging variables constrained in `S` on creation,
`is_bridged(b, S)` is assumed to be `true`.
bridging_cost(
b::AbstractBridgeOptimizer,
F::Type{<:MOI.AbstractFunction},
S::Type{<:MOI.AbstractSet},
)
Return the cost of bridging `F`-in-`S` constraints.
`is_bridged(b, S)` is assumed to be `true`.
"""
function bridging_cost end
function MOI.get(
b::AbstractBridgeOptimizer,
attr::MOI.VariableBridgingCost{S},
) where {S}
if is_bridged(b, S)
return bridging_cost(b, S)
else
return MOI.get(b.model, attr)
end
end
function MOI.get(
b::AbstractBridgeOptimizer,
attr::MOI.ConstraintBridgingCost{F,S},
) where {F,S}
if is_bridged(b, F, S)
return bridging_cost(b, F, S)
else
return MOI.get(b.model, attr)
end
end
# Objective
"""
is_objective_bridged(b::AbstractBridgeOptimizer)
Return a `Bool` indicating whether the objective is bridged. The objective is
said to be bridged if the value of `MOI.ObjectiveFunctionType` is different for
`b` and `b.model`.
"""
is_objective_bridged(b) = !isempty(Objective.bridges(b))
function _delete_objective_bridges(b)
MOI.delete(b, Objective.root_bridge(Objective.bridges(b)))
empty!(Objective.bridges(b))
return
end
function MOI.supports(
b::AbstractBridgeOptimizer,
attr::MOI.ObjectiveFunction{F},
) where {F}
if is_bridged(b, F)
return supports_bridging_objective_function(b, F)
else
return MOI.supports(b.model, attr)
end
end
"""
struct ObjectiveFunctionValue{F<:MOI.AbstractScalarFunction} end
Attribute for the value of the objective function of type `F`. If the objective
of the objective function does not depend on `F`, the type `F` determines
whether the computation is redirected to an objective bridge or to the
underlying model.
"""
struct ObjectiveFunctionValue{F<:MOI.AbstractScalarFunction}
result_index::Int
end
# `recursive_model(b::Objective.SingleBridgeOptimizer)` returns
# `b.model` so any model should implement `ObjectiveFunctionValue`.
function MOI.get(model::MOI.ModelLike, attr::ObjectiveFunctionValue)
return MOI.get(model, MOI.ObjectiveValue(attr.result_index))
end
function MOI.get(
b::AbstractBridgeOptimizer,
attr::ObjectiveFunctionValue{F},
) where {F<:MOI.AbstractScalarFunction} # Need `<:` to avoid ambiguity
obj_attr = MOI.ObjectiveFunction{F}()
if is_bridged(b, obj_attr)
return MOI.get(recursive_model(b), attr, bridge(b, obj_attr))
else
return MOI.get(b.model, MOI.ObjectiveValue(attr.result_index))
end
end
function MOI.get(b::AbstractBridgeOptimizer, attr::MOI.ObjectiveValue)
if is_objective_bridged(b)
F = Objective.function_type(Objective.bridges(b))
return MOI.get(b, ObjectiveFunctionValue{F}(attr.result_index))
else
return MOI.get(b.model, attr)
end
end
function MOI.get(b::AbstractBridgeOptimizer, attr::MOI.ObjectiveFunctionType)
if is_objective_bridged(b)
return Objective.function_type(Objective.bridges(b))
else
return MOI.get(b.model, attr)
end
end
function MOI.get(b::AbstractBridgeOptimizer, attr::MOI.ObjectiveSense)
return MOI.get(b.model, attr)
end
function MOI.get(b::AbstractBridgeOptimizer, attr::MOI.ObjectiveFunction)
value = if is_bridged(b, attr)
MOI.get(recursive_model(b), attr, bridge(b, attr))
else
MOI.get(b.model, attr)
end
return unbridged_function(b, value)
end
function MOI.set(
b::AbstractBridgeOptimizer,
attr::MOI.ObjectiveSense,
value::MOI.OptimizationSense,
)
MOI.set(b.model, attr, value)
if is_objective_bridged(b)
if value == MOI.FEASIBILITY_SENSE
_delete_objective_bridges(b)
else
for bridge in values(Objective.bridges(b))
MOI.set(recursive_model(b), attr, bridge, value)
end
end
end
return
end
function _bridge_objective(b, BridgeType, func)
bridge = Objective.bridge_objective(BridgeType, recursive_model(b), func)
Objective.add_key_for_bridge(Objective.bridges(b), bridge, func)
return
end
function MOI.set(
b::AbstractBridgeOptimizer,
attr::MOI.ObjectiveFunction,
func::MOI.AbstractScalarFunction,
)
if is_objective_bridged(b)
# Clear objective function by setting sense to `MOI.FEASIBILITY_SENSE`
# first. This is needed if the objective function of `b.model` is
# `slack` where `slack` is the slack variable
# created by `Objective.SlackBridge`.
sense = MOI.get(b.model, MOI.ObjectiveSense())
MOI.set(b.model, MOI.ObjectiveSense(), MOI.FEASIBILITY_SENSE)
_delete_objective_bridges(b)
if sense != MOI.FEASIBILITY_SENSE
MOI.set(b.model, MOI.ObjectiveSense(), sense)
end
end
if Variable.has_bridges(Variable.bridges(b))
if func isa MOI.VariableIndex
if is_bridged(b, func)
BridgeType = Objective.concrete_bridge_type(
objective_functionize_bridge(b),