/
debug.jl
747 lines (694 loc) · 20.3 KB
/
debug.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
# 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.
function print_node(io::IO, b::LazyBridgeOptimizer, node::VariableNode)
S, = b.variable_types[node.index]
MOI.Utilities.print_with_acronym(
io,
"[$(node.index)] constrained variables in `$S` are",
)
return
end
function print_node(io::IO, b::LazyBridgeOptimizer, node::ConstraintNode)
F, S = b.constraint_types[node.index]
MOI.Utilities.print_with_acronym(
io,
"($(node.index)) `$F`-in-`$S` constraints are",
)
return
end
function print_node(io::IO, b::LazyBridgeOptimizer, node::ObjectiveNode)
F, = b.objective_types[node.index]
MOI.Utilities.print_with_acronym(
io,
"|$(node.index)| objective function of type `$F` is",
)
return
end
function print_node_info(
io::IO,
b::LazyBridgeOptimizer,
node::AbstractNode;
debug_unsupported = false,
)
print(io, " ")
print_node(io, b, node)
d = _dist(b.graph, node)
if d == INFINITY
print(io, " not supported")
if debug_unsupported
print(io, " because")
print_unsupported(io, b, node)
else
println(io)
end
else
index = bridge_index(b.graph, node)
if iszero(index) ||
(node isa VariableNode && !is_variable_edge_best(b.graph, node))
@assert node isa VariableNode
dist = isinteger(d) ? round(Int, d) : d
println(
io,
" supported (distance $dist) by adding free variables and then constrain them, see ($(b.graph.variable_constraint_node[node.index].index)).",
)
else
dist = isinteger(d) ? round(Int, d) : d
print(io, " bridged (distance $dist) by ")
MOI.Utilities.print_with_acronym(
io,
string(_bridge_type(b, node, index)),
)
println(io, ".")
end
end
return
end
function print_graph(b::LazyBridgeOptimizer; kwargs...)
print_graph(Base.stdout, b; kwargs...)
return
end
"""
print_graph([io::IO = stdout,] b::LazyBridgeOptimizer)
Print the hyper-graph containing all variable, constraint, and objective types
that could be obtained by bridging the variables, constraints, and objectives
that are present in the model by all the bridges added to `b`.
Each node in the hyper-graph corresponds to a variable, constraint, or objective
type.
* Variable nodes are indicated by `[ ]`
* Constraint nodes are indicated by `( )`
* Objective nodes are indicated by `| |`
The number inside each pair of brackets is an index of the node in the
hyper-graph.
Note that this hyper-graph is the full list of possible transformations. When
the bridged model is created, we select the shortest hyper-path from this
graph, so many nodes may be un-used.
To see which nodes are used, call [`print_active_bridges`](@ref).
For more information, see Legat, B., Dowson, O., Garcia, J., and Lubin, M.
(2020). "MathOptInterface: a data structure for mathematical optimization
problems." [URL](https://arXiv.org/abs/2002.03447)
"""
function print_graph(io::IO, b::LazyBridgeOptimizer; kwargs...)
println(io, b.graph)
print_nodes(
io,
b,
VariableNode.(eachindex(b.graph.variable_best)),
ConstraintNode.(eachindex(b.graph.constraint_best)),
ObjectiveNode.(eachindex(b.graph.objective_best));
kwargs...,
)
return
end
function print_nodes(
io::IO,
b::LazyBridgeOptimizer,
variable_nodes,
constraint_nodes,
objective_nodes;
kwargs...,
)
for node in variable_nodes
print_node_info(io, b, node; kwargs...)
end
for node in constraint_nodes
print_node_info(io, b, node; kwargs...)
end
for node in objective_nodes
print_node_info(io, b, node; kwargs...)
end
return
end
function print_if_unsupported(
io::IO,
b::LazyBridgeOptimizer,
node::AbstractNode,
)
if _dist(b.graph, node) != INFINITY
return
end
print(io, " ")
print_node(io, b, node)
print(io, " not supported\n")
return
end
function print_unsupported(io::IO, b::LazyBridgeOptimizer, edge::Edge)
for node in edge.added_variables
print_if_unsupported(io, b, node)
end
for node in edge.added_constraints
print_if_unsupported(io, b, node)
end
return
end
function print_unsupported(io::IO, b::LazyBridgeOptimizer, edge::ObjectiveEdge)
for node in edge.added_variables
print_if_unsupported(io, b, node)
end
for node in edge.added_constraints
print_if_unsupported(io, b, node)
end
print_if_unsupported(io, b, edge.added_objective)
return
end
function print_unsupported(
io::IO,
b::LazyBridgeOptimizer,
edges::Vector,
index_to_bridge::Function,
)
no_bridge = true
for edge in edges
if no_bridge
println(io, ":")
no_bridge = false
end
MOI.Utilities.print_with_acronym(
io,
" Cannot use `$(index_to_bridge(edge.bridge_index))` because:\n",
)
print_unsupported(io, b, edge)
end
if no_bridge
println(io, " no added bridge supports bridging it.")
end
return
end
function _bridge_type(
b::LazyBridgeOptimizer,
node::VariableNode,
bridge_index::Int,
)
return Variable.concrete_bridge_type(
b.variable_bridge_types[bridge_index],
b.variable_types[node.index]...,
)
end
function _bridge_type(
b::LazyBridgeOptimizer,
node::ConstraintNode,
bridge_index::Int,
)
return Constraint.concrete_bridge_type(
b.constraint_bridge_types[bridge_index],
b.constraint_types[node.index]...,
)
end
function _bridge_type(
b::LazyBridgeOptimizer,
node::ObjectiveNode,
bridge_index::Int,
)
return Objective.concrete_bridge_type(
b.objective_bridge_types[bridge_index],
b.objective_types[node.index]...,
)
end
function print_unsupported(io::IO, b::LazyBridgeOptimizer, node::VariableNode)
print_unsupported(
io,
b,
b.graph.variable_edges[node.index],
bridge_index -> _bridge_type(b, node, bridge_index),
)
if b.variable_types[node.index] == (MOI.Reals,)
return
end
print(io, " Cannot add free variables and then constrain them because")
constraint_node = b.graph.variable_constraint_node[node.index]
if constraint_node.index == -1
println(
io,
" free variables are bridged but no functionize bridge was added.",
)
else
println(io, ":")
print_if_unsupported(io, b, constraint_node)
end
return
end
function print_unsupported(io::IO, b::LazyBridgeOptimizer, node::ConstraintNode)
print_unsupported(
io,
b,
b.graph.constraint_edges[node.index],
bridge_index -> _bridge_type(b, node, bridge_index),
)
return
end
function print_unsupported(io::IO, b::LazyBridgeOptimizer, node::ObjectiveNode)
print_unsupported(
io,
b,
b.graph.objective_edges[node.index],
bridge_index -> _bridge_type(b, node, bridge_index),
)
return
end
function add_unsupported(
graph::Graph,
edges::Vector{Edge},
variables,
constraints,
objectives,
)
for edge in edges
for node in edge.added_variables
add_unsupported(graph, node, variables, constraints, objectives)
end
for node in edge.added_constraints
add_unsupported(graph, node, variables, constraints, objectives)
end
end
return
end
function add_unsupported(
graph::Graph,
edges::Vector{ObjectiveEdge},
variables,
constraints,
objectives,
)
for edge in edges
for node in edge.added_variables
add_unsupported(graph, node, variables, constraints, objectives)
end
for node in edge.added_constraints
add_unsupported(graph, node, variables, constraints, objectives)
end
add_unsupported(
graph,
edge.added_objective,
variables,
constraints,
objectives,
)
end
return
end
function add_unsupported(
graph::Graph,
node::VariableNode,
variables,
constraints,
objectives,
)
if _dist(graph, node) != INFINITY || node in variables
return
end
push!(variables, node)
add_unsupported(
graph,
graph.variable_edges[node.index],
variables,
constraints,
objectives,
)
constraint_node = graph.variable_constraint_node[node.index]
if constraint_node.index != -1
add_unsupported(
graph,
graph.variable_constraint_node[node.index],
variables,
constraints,
objectives,
)
end
return
end
function add_unsupported(
graph::Graph,
node::ConstraintNode,
variables,
constraints,
objectives,
)
if _dist(graph, node) != INFINITY || node in constraints
return
end
push!(constraints, node)
add_unsupported(
graph,
graph.constraint_edges[node.index],
variables,
constraints,
objectives,
)
return
end
function add_unsupported(
graph::Graph,
node::ObjectiveNode,
variables,
constraints,
objectives,
)
if _dist(graph, node) != INFINITY || node in objectives
return
end
push!(objectives, node)
add_unsupported(
graph,
graph.objective_edges[node.index],
variables,
constraints,
objectives,
)
return
end
function _sort(nodes::Set)
vector = collect(nodes)
sort!(vector, by = node -> node.index)
return vector
end
function debug_unsupported(io::IO, b::LazyBridgeOptimizer, node::AbstractNode)
variables = Set{VariableNode}()
constraints = Set{ConstraintNode}()
objectives = Set{ObjectiveNode}()
add_unsupported(b.graph, node, variables, constraints, objectives)
print_nodes(
io,
b,
_sort(variables),
_sort(constraints),
_sort(objectives),
debug_unsupported = true,
)
return
end
const UNSUPPORTED_MESSAGE =
" are not supported and cannot be bridged into supported" *
" constrained variables and constraints. See details below:"
function debug(
b::LazyBridgeOptimizer,
S::Type{<:MOI.AbstractSet};
io::IO = Base.stdout,
)
if (
S <: MOI.AbstractScalarSet &&
MOI.supports_add_constrained_variable(b, S)
) || (
S <: MOI.AbstractVectorSet &&
MOI.supports_add_constrained_variables(b, S)
)
MOI.Utilities.print_with_acronym(
io,
"Constrained variables in `$S` are supported.\n",
)
else
MOI.Utilities.print_with_acronym(io, "Constrained variables in `$S`")
println(io, UNSUPPORTED_MESSAGE)
debug_unsupported(io, b, node(b, S))
end
return
end
"""
debug_supports_add_constrained_variable(
b::LazyBridgeOptimizer,
S::Type{<:MOI.AbstractSet};
io::IO = Base.stdout
)
Prints to `io` explanations for the value of
[`MOI.supports_add_constrained_variable`](@ref) with the same arguments.
"""
function debug_supports_add_constrained_variable(
b::LazyBridgeOptimizer,
S::Type{<:MOI.AbstractSet};
kwargs...,
)
debug(b, S; kwargs...)
return
end
function debug(
b::LazyBridgeOptimizer,
F::Type{<:MOI.AbstractFunction},
S::Type{<:MOI.AbstractSet};
io::IO = Base.stdout,
)
if MOI.supports_constraint(b, F, S)
MOI.Utilities.print_with_acronym(
io,
"`$F`-in-`$S` constraints are supported.\n",
)
else
MOI.Utilities.print_with_acronym(io, "`$F`-in-`$S` constraints")
println(io, UNSUPPORTED_MESSAGE)
debug_unsupported(io, b, node(b, F, S))
end
return
end
"""
debug_supports_constraint(
b::LazyBridgeOptimizer,
F::Type{<:MOI.AbstractFunction},
S::Type{<:MOI.AbstractSet};
io::IO = Base.stdout,
)
Prints to `io` explanations for the value of [`MOI.supports_constraint`](@ref)
with the same arguments.
"""
function debug_supports_constraint(
b::LazyBridgeOptimizer,
F::Type{<:MOI.AbstractFunction},
S::Type{<:MOI.AbstractSet};
kwargs...,
)
return debug(b, F, S; kwargs...)
end
function debug(
b::LazyBridgeOptimizer,
F::Type{<:MOI.AbstractFunction};
io::IO = Base.stdout,
)
MOI.Utilities.print_with_acronym(io, "Objective function of type `$F` is")
if supports_bridging_objective_function(b, F)
MOI.Utilities.print_with_acronym(io, " supported.\n")
else
MOI.Utilities.print_with_acronym(
io,
" not supported and cannot be bridged" *
" into a supported objective function by adding only supported" *
" constrained variables and constraints. See details below:\n",
)
debug_unsupported(io, b, node(b, F))
end
return
end
"""
debug_supports(
b::LazyBridgeOptimizer,
::MOI.ObjectiveFunction{F};
io::IO = Base.stdout,
) where F
Prints to `io` explanations for the value of [`MOI.supports`](@ref) with the
same arguments.
"""
function debug_supports(
b::LazyBridgeOptimizer,
::MOI.ObjectiveFunction{F};
kwargs...,
) where {F}
debug(b, F; kwargs...)
return
end
"""
print_active_bridges([io::IO=stdout,] b::MOI.Bridges.LazyBridgeOptimizer)
Print the set of bridges that are active in the model `b`.
"""
function print_active_bridges(io::IO, b::MOI.Bridges.LazyBridgeOptimizer)
F = MOI.get(b, MOI.ObjectiveFunctionType())
print_active_bridges(io, b, F)
types = MOI.get(b, MOI.ListOfConstraintTypesPresent())
# We add a sort here to make the order reproducible, and to group similar
# constraints together.
for (F, S) in sort(types; by = string)
print_active_bridges(io, b, F, S)
end
return
end
function print_active_bridges(b::MOI.Bridges.LazyBridgeOptimizer, args...)
return print_active_bridges(stdout, b, args...)
end
function _print_supported(io, arg...)
s = sprint(MOI.Utilities.print_with_acronym, arg...)
return Printf.printstyled(io, s; bold = false, color = :green)
end
function _print_unsupported(io, arg...)
s = sprint(MOI.Utilities.print_with_acronym, arg...)
return Printf.printstyled(io, s; bold = false, color = :red)
end
function _print_bridge(io, b, ::Type{BT}, offset) where {BT}
new_offset = offset * " | "
for (F, S) in MOI.Bridges.added_constraint_types(BT)
print_active_bridges(io, b, F, S, new_offset)
end
for (S,) in MOI.Bridges.added_constrained_variable_types(BT)
print_active_bridges(io, b, S, new_offset)
end
return
end
"""
print_active_bridges(
[io::IO=stdout,]
b::MOI.Bridges.LazyBridgeOptimizer,
F::Type{<:MOI.AbstractFunction}
)
Print the set of bridges required for an objective function of type `F`.
"""
function print_active_bridges(
io::IO,
b::MOI.Bridges.LazyBridgeOptimizer,
::Type{F},
offset::String = "",
) where {F<:MOI.AbstractFunction}
if !MOI.supports(b, MOI.ObjectiveFunction{F}())
throw(
MOI.UnsupportedAttribute(
MOI.ObjectiveFunction{F}(),
"The objective function cannot be bridged using the set of " *
"available bridges.",
),
)
end
if !MOI.Bridges.is_bridged(b, F)
print(io, offset, " * ")
_print_supported(io, "Supported objective: $F\n")
return
end
print(io, offset, " * ")
_print_unsupported(io, "Unsupported objective: $F\n")
index = MOI.Bridges.bridge_index(b.graph, b.objective_node[(F,)])
B = b.objective_bridge_types[index]
BT = MOI.Bridges.Objective.concrete_bridge_type(B, F)
println(io, offset, " | bridged by:")
print(io, offset, " | ")
MOI.Utilities.print_with_acronym(io, "$BT\n")
println(io, offset, " | may introduce:")
G = MOI.Bridges.set_objective_function_type(BT)
print_active_bridges(io, b, G, offset * " | ")
_print_bridge(io, b, BT, offset)
return
end
"""
print_active_bridges(
[io::IO=stdout,]
b::MOI.Bridges.LazyBridgeOptimizer,
F::Type{<:MOI.AbstractFunction},
S::Type{<:MOI.AbstractSet},
)
Print the set of bridges required for a constraint of type `F`-in-`S`.
"""
function print_active_bridges(
io::IO,
b::MOI.Bridges.LazyBridgeOptimizer,
::Type{F},
::Type{S},
offset::String = "",
) where {F<:MOI.AbstractFunction,S<:MOI.AbstractSet}
if !MOI.supports_constraint(b, F, S)
throw(
MOI.UnsupportedConstraint{F,S}(
"The constraint cannot be bridged using the set of available " *
"bridges.",
),
)
end
if !MOI.Bridges.is_bridged(b, F, S)
print(io, offset, " * ")
_print_supported(io, "Supported constraint: $F-in-$S\n")
return
end
is_constraint_bridged = true
c_node = b.constraint_node[(F, S)]
if F == MOI.VariableIndex || F == MOI.VectorOfVariables
# Call `MOI.supports_` here to build the necessary nodes in the bridging
# graph.
if F == MOI.VariableIndex
MOI.supports_add_constrained_variable(b, S)
else
MOI.supports_add_constrained_variables(b, S)
end
v_node = b.variable_node[(S,)]
if bridging_cost(b.graph, v_node) <= bridging_cost(b.graph, c_node)
is_constraint_bridged = false
end
end
if is_constraint_bridged
index = MOI.Bridges.bridge_index(b.graph, c_node)
B = b.constraint_bridge_types[index]
BT = MOI.Bridges.Constraint.concrete_bridge_type(B, F, S)
print(io, offset, " * ")
_print_unsupported(io, "Unsupported constraint: $F-in-$S\n")
println(io, offset, " | bridged by:")
print(io, offset, " | ")
MOI.Utilities.print_with_acronym(io, "$BT\n")
println(io, offset, " | may introduce:")
_print_bridge(io, b, BT, offset)
else
print_active_bridges(io, b, S, offset)
end
return
end
"""
print_active_bridges(
[io::IO=stdout,]
b::MOI.Bridges.LazyBridgeOptimizer,
S::Type{<:MOI.AbstractSet}
)
Print the set of bridges required for a variable constrained to set `S`.
"""
function print_active_bridges(
io::IO,
b::MOI.Bridges.LazyBridgeOptimizer,
::Type{S},
offset::String = "",
) where {S<:MOI.AbstractSet}
if S <: MOI.AbstractScalarSet
if !MOI.supports_add_constrained_variable(b, S)
throw(
MOI.UnsupportedConstraint{MOI.VariableIndex,S}(
"The variable cannot be bridged using the set of " *
"available bridges.",
),
)
end
else
@assert S <: MOI.AbstractVectorSet
if !MOI.supports_add_constrained_variables(b, S)
throw(
MOI.UnsupportedConstraint{MOI.VectorOfVariables,S}(
"The variables cannot be bridged using the set of " *
"available bridges.",
),
)
end
end
if !MOI.Bridges.is_bridged(b, S)
print(io, offset, " * ")
_print_supported(io, "Supported variable: $S\n")
return
end
if MOI.Bridges.is_variable_edge_best(b.graph, b.variable_node[(S,)])
index = MOI.Bridges.bridge_index(b.graph, b.variable_node[(S,)])
B = b.variable_bridge_types[index]
BT = MOI.Bridges.Variable.concrete_bridge_type(B, S)
print(io, offset, " * ")
_print_unsupported(io, "Unsupported variable: $S\n")
println(io, offset, " | bridged by:")
print(io, offset, " | ")
MOI.Utilities.print_with_acronym(io, "$BT\n")
println(io, offset, " | may introduce:")
_print_bridge(io, b, BT, offset)
else
print(io, offset, " * ")
_print_unsupported(io, "Unsupported variable: $S\n")
println(io, offset, " | adding as constraint:")
offset = offset * " | "
MOI.Bridges.print_active_bridges(io, b, MOI.Reals, offset)
F = MOI.Utilities.variable_function_type(S)
MOI.Bridges.print_active_bridges(io, b, F, S, offset)
end
return
end