/
issubset.jl
1295 lines (1033 loc) · 37.9 KB
/
issubset.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
import Base.issubset
"""
issubset(X::LazySet, Y::LazySet, [witness]::Bool=false, args...)
Alias for `⊆` (inclusion check).
### Input
- `X` -- set
- `Y` -- set
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``X ⊆ Y``
* If `witness` option is activated:
* `(true, [])` iff ``X ⊆ Y``
* `(false, v)` iff ``X ⊈ Y`` and ``v ∈ X \\setminus Y``
### Notes
For more documentation see `⊆`.
"""
function issubset end
# this operation is invalid, but it is a common error, so we give a detailed
# error message
function ⊆(::AbstractVector, ::LazySet)
throw(ArgumentError("cannot make an inclusion check if the left-hand side " *
"is a vector; either wrap it as a set with one element, as in " *
"`Singleton(v) ⊆ X`, or check for set membership, as in `v ∈ X` " *
"(they behave equivalently although the implementations may differ)"))
end
# conversion for IA types
⊆(X::LazySet, Y::IA.Interval) = ⊆(X, Interval(Y))
⊆(X::IA.Interval, Y::LazySet) = ⊆(Interval(X), Y)
⊆(X::LazySet, Y::IA.IntervalBox) = ⊆(X, convert(Hyperrectangle, Y))
⊆(X::IA.IntervalBox, Y::LazySet) = ⊆(convert(Hyperrectangle, X), Y)
"""
⊆(X::LazySet, P::LazySet, [witness]::Bool=false)
Check whether a set is contained in a polyhedral set, and if not, optionally
compute a witness.
### Input
- `X` -- inner set
- `Y` -- outer polyhedral set
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``X ⊆ P``
* If `witness` option is activated:
* `(true, [])` iff ``X ⊆ P``
* `(false, v)` iff ``X ⊈ P`` and ``v ∈ X \\setminus P``
### Notes
We require that `constraints_list(P)` is available.
### Algorithm
We check inclusion of `X` in every constraint of `P`.
"""
function ⊆(X::LazySet, P::LazySet, witness::Bool=false)
if is_polyhedral(P)
return _issubset_constraints_list(X, P, witness)
else
error("an inclusion check for the given combination of set types is " *
"not available")
end
end
"""
⊆(S::LazySet, H::AbstractHyperrectangle, [witness]::Bool=false)
Check whether a set is contained in a hyperrectangular set, and if not,
optionally compute a witness.
### Input
- `S` -- inner set
- `H` -- outer hyperrectangular set
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``S ⊆ H``
* If `witness` option is activated:
* `(true, [])` iff ``S ⊆ H``
* `(false, v)` iff ``S ⊈ H`` and ``v ∈ S ∖ H``
### Algorithm
``S ⊆ H`` iff ``\\operatorname{ihull}(S) ⊆ H``, where ``\\operatorname{ihull}``
is the interval-hull operator.
"""
function ⊆(S::LazySet, H::AbstractHyperrectangle, witness::Bool=false)
return _issubset_in_hyperrectangle(S, H, witness)
end
function _issubset_in_hyperrectangle(S, H, witness)
n = dim(S)
@assert n == dim(H)
N = promote_type(eltype(S), eltype(H))
for i in 1:n
lS, hS = extrema(S, i)
lH, hH = extrema(H, i)
if !witness && (lS < lH || hS > hH)
return false
elseif lS < lH
# outside in negative direction
v = σ(SingleEntryVector(i, n, -one(N)), S)
return (false, v)
elseif hS > hH
# outside in positive direction
v = σ(SingleEntryVector(i, n, one(N)), S)
return (false, v)
end
end
return _witness_result_empty(witness, true, N)
end
# disambiguation
function ⊆(P::AbstractPolytope, H::AbstractHyperrectangle, witness::Bool=false)
return _issubset_in_hyperrectangle(P, H, witness)
end
"""
⊆(H1::AbstractHyperrectangle, H2::AbstractHyperrectangle,
[witness]::Bool=false)
Check whether a given hyperrectangular set is contained in another
hyperrectangular set, and if not, optionally compute a witness.
### Input
- `H1` -- inner hyperrectangular set
- `H2` -- outer hyperrectangular set
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``H1 ⊆ H2``
* If `witness` option is activated:
* `(true, [])` iff ``H1 ⊆ H2``
* `(false, v)` iff ``H1 ⊈ H2`` and ``v ∈ H1 ∖ H2``
### Algorithm
``H1 ⊆ H2`` iff ``c_1 + r_1 ≤ c_2 + r_2 ∧ c_1 - r_1 ≥ c_2 - r_2`` iff
``r_1 - r_2 ≤ c_1 - c_2 ≤ -(r_1 - r_2)``, where ``≤`` is taken component-wise.
"""
function ⊆(H1::AbstractHyperrectangle, H2::AbstractHyperrectangle,
witness::Bool=false)
@assert dim(H1) == dim(H2)
N = promote_type(eltype(H1), eltype(H2))
@inbounds for i in 1:dim(H1)
c_dist = center(H1, i) - center(H2, i)
r_dist = radius_hyperrectangle(H1, i) - radius_hyperrectangle(H2, i)
# check if c_dist is not in the interval [r_dist, -r_dist]
if !_leq(r_dist, c_dist) || !_leq(c_dist, -r_dist)
if witness
# compute a witness v
v = copy(center(H1))
if c_dist >= zero(N)
v[i] += radius_hyperrectangle(H1, i)
else
v[i] -= radius_hyperrectangle(H1, i)
end
return (false, v)
else
return false
end
end
end
return _witness_result_empty(witness, true, N)
end
"""
⊆(P::AbstractPolytope, S::LazySet, [witness]::Bool=false;
[algorithm]="constraints")
Check whether a polytopic set is contained in a convex set, and if not,
optionally compute a witness.
### Input
- `P` -- inner polytopic set
- `S` -- outer convex set
- `witness` -- (optional, default: `false`) compute a witness if activated
- `algorithm` -- (optional, default: `"constraints"`) algorithm for the
inclusion check; available options are:
* `"constraints"`, using the list of constraints of `S` (requires that `S`
is polyhedral) and support-function evaluations of `S`
* `"vertices"`, using the list of vertices of `P` and membership evaluations
of `S`
### Output
* If `witness` option is deactivated: `true` iff ``P ⊆ S``
* If `witness` option is activated:
* `(true, [])` iff ``P ⊆ S``
* `(false, v)` iff ``P ⊈ S`` and ``v ∈ P ∖ S``
### Algorithm
- `"vertices"`:
Since ``S`` is convex, ``P ⊆ S`` iff ``v ∈ S`` for all vertices ``v`` of ``P``.
"""
function ⊆(P::AbstractPolytope, S::LazySet, witness::Bool=false;
algorithm=nothing)
@assert dim(P) == dim(S)
if !isconvextype(typeof(S))
error("an inclusion check for the given combination of set types is " *
"not available")
end
if isnothing(algorithm)
# TODO smarter evaluation which representation is better
if is_polyhedral(S)
algorithm = "constraints"
else
algorithm = "vertices"
end
end
if algorithm == "constraints"
return _issubset_constraints_list(P, S, witness)
elseif algorithm == "vertices"
return _issubset_vertices_list(P, S, witness)
else
error("algorithm $algorithm unknown")
end
end
# check whether P ⊆ S by testing whether each vertex of P belongs to S
function _issubset_vertices_list(P, S, witness)
for v in vertices(P)
if v ∉ S
return _witness_result(witness, false, v)
end
end
return _witness_result_empty(witness, true, P, S)
end
"""
⊆(X::LazySet, P::AbstractPolyhedron, [witness]::Bool=false)
Check whether a convex set is contained in a polyhedral set, and if not,
optionally compute a witness.
### Input
- `X` -- inner convex set
- `P` -- outer polyhedral set
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``X ⊆ P``
* If `witness` option is activated:
* `(true, [])` iff ``X ⊆ P``
* `(false, v)` iff ``X ⊈ P`` and ``v ∈ P ∖ X``
### Algorithm
Since ``X`` is convex, we can compare the support function of ``X`` and ``P`` in
each direction of the constraints of ``P``.
For witness generation, we use a support vector in the first direction where the
above check fails.
"""
function ⊆(X::LazySet, P::AbstractPolyhedron, witness::Bool=false)
if !isconvextype(typeof(X))
return _issubset_in_polyhedron_high(X, P, witness)
end
return _issubset_constraints_list(X, P, witness)
end
# S ⊆ P where P = ⟨Cx ≤ d⟩ iff y ≤ d where y is the upper corner of box(C*S)
#
# see Proposition 7 in Wetzlinger, Kochdumper, Bak, Althoff: *Fully-automated verification
# of linear systems using inner- and outer-approximations of reachable sets*. 2022.
function _issubset_in_polyhedron_high(S::LazySet, P::LazySet, witness::Bool=false)
@assert dim(S) == dim(P)
C, d = tosimplehrep(P)
x = high(C * S)
result = all(x .≤ d)
if result
return _witness_result_empty(witness, true, S, P)
elseif !witness
return false
end
throw(ArgumentError("witness production is not supported yet"))
end
function ⊆(Z::AbstractZonotope, P::AbstractPolyhedron, witness::Bool=false)
return _issubset_zonotope_in_polyhedron(Z, P, witness)
end
# implements Proposition 7 in Wetzlinger, Kochdumper, Bak, Althoff: *Fully-automated verification
# of linear systems using inner- and outer-approximations of reachable sets*. 2022.
function _issubset_zonotope_in_polyhedron(Z::AbstractZonotope, P::LazySet,
witness::Bool=false)
@assert dim(Z) == dim(P)
# corner case: no generator
if ngens(Z) == 0
c = center(Z)
result = c ∈ P
return _witness_result_empty(witness, result, Z, P, c)
end
C, d = tosimplehrep(P)
c = center(Z)
G = genmat(Z)
A = sum(abs.(C * gj) for gj in eachcol(G))
b = d - C * c
result = all(_leq.(A, b))
if result
return _witness_result_empty(witness, true, Z, P)
elseif !witness
return false
end
throw(ArgumentError("witness production is not supported yet"))
end
# for documentation see
# ⊆(X::LazySet, P::AbstractPolyhedron, witness::Bool=false)
function _issubset_constraints_list(S::LazySet, P::LazySet, witness::Bool=false)
@assert dim(S) == dim(P) "incompatible set dimensions $(dim(S)) and $(dim(P))"
@assert is_polyhedral(P) "this inclusion check requires a polyhedral set " *
"on the right-hand side"
@inbounds for H in constraints_list(P)
if !_leq(ρ(H.a, S), H.b)
return witness ? (false, σ(H.a, S)) : false
end
end
return _witness_result_empty(witness, true, S, P)
end
# disambiguations
for ST in [:AbstractPolytope, :AbstractHyperrectangle, :LineSegment]
@eval function ⊆(X::($ST), P::AbstractPolyhedron, witness::Bool=false)
return _issubset_constraints_list(X, P, witness)
end
end
"""
⊆(S::AbstractSingleton, X::LazySet, [witness]::Bool=false)
Check whether a given set with a single value is contained in another set, and
if not, optionally compute a witness.
### Input
- `S` -- inner set with a single value
- `X` -- outer set
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``S ⊆ X``
* If `witness` option is activated:
* `(true, [])` iff ``S ⊆ X``
* `(false, v)` iff ``S ⊈ X`` and ``v ∈ S ∖ X``
"""
function ⊆(S::AbstractSingleton, X::LazySet, witness::Bool=false)
return _issubset_singleton(S, X, witness)
end
function _issubset_singleton(S, X, witness)
s = element(S)
result = s ∈ X
return _witness_result_empty(witness, result, S, X, s)
end
# disambiguations
for ST in [:AbstractHyperrectangle, :AbstractPolyhedron, :UnionSetArray,
:Complement]
@eval function ⊆(X::AbstractSingleton, Y::($ST), witness::Bool=false)
return _issubset_singleton(X, Y, witness)
end
end
"""
⊆(S1::AbstractSingleton, S2::AbstractSingleton, witness::Bool=false)
Check whether a given set with a single value is contained in another set with a
single value, and if not, optionally compute a witness.
### Input
- `S1` -- inner set with a single value
- `S2` -- outer set with a single value
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``S1 ⊆ S2`` iff ``S1 == S2``
* If `witness` option is activated:
* `(true, [])` iff ``S1 ⊆ S2``
* `(false, v)` iff ``S1 ⊈ S2`` and ``v ∈ S1 ∖ S2``
"""
function ⊆(S1::AbstractSingleton, S2::AbstractSingleton, witness::Bool=false)
s1 = element(S1)
result = _isapprox(s1, element(S2))
return _witness_result_empty(witness, result, S1, S2, s1)
end
"""
⊆(B1::Ball2, B2::Ball2, [witness]::Bool=false)
Check whether a ball in the 2-norm is contained in another ball in the 2-norm,
and if not, optionally compute a witness.
### Input
- `B1` -- inner ball in the 2-norm
- `B2` -- outer ball in the 2-norm
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``B1 ⊆ B2``
* If `witness` option is activated:
* `(true, [])` iff ``B1 ⊆ B2``
* `(false, v)` iff ``B1 ⊈ B2`` and ``v ∈ B1 ∖ B2``
### Algorithm
``B1 ⊆ B2`` iff ``‖ c_1 - c_2 ‖_2 + r_1 ≤ r_2``
"""
function ⊆(B1::Ball2, B2::Ball2, witness::Bool=false)
result = norm(B1.center - B2.center, 2) + B1.radius <= B2.radius
if result
return _witness_result_empty(witness, true, B1, B2)
elseif !witness
return false
end
# compute a witness v
v = B1.center .+ B1.radius * (B1.center .- B2.center)
return (false, v)
end
"""
⊆(B::Union{Ball2, Ballp}, S::AbstractSingleton, witness::Bool=false)
Check whether a ball in the 2-norm or p-norm is contained in a set with a single
value, and if not, optionally compute a witness.
### Input
- `B` -- inner ball in the 2-norm or p-norm
- `S` -- outer set with a single value
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``B ⊆ S``
* If `witness` option is activated:
* `(true, [])` iff ``B ⊆ S``
* `(false, v)` iff ``B ⊈ S`` and ``v ∈ B ∖ S``
"""
function ⊆(B::Union{Ball2,Ballp}, S::AbstractSingleton, witness::Bool=false)
result = isapproxzero(B.radius) && _isapprox(B.center, element(S))
if result
return _witness_result_empty(witness, true, B, S)
elseif !witness
return false
end
# compute a witness v
if B.center != element(S)
v = B.center
else
v = copy(B.center)
v[1] += B.radius
end
return (false, v)
end
"""
⊆(L::LineSegment, S::LazySet, witness::Bool=false)
Check whether a line segment is contained in a convex set, and if not,
optionally compute a witness.
### Input
- `L` -- inner line segment
- `S` -- outer convex set
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``L ⊆ S``
* If `witness` option is activated:
* `(true, [])` iff ``L ⊆ S``
* `(false, v)` iff ``L ⊈ S`` and ``v ∈ L ∖ S``
### Algorithm
Since ``S`` is convex, ``L ⊆ S`` iff ``p ∈ S`` and ``q ∈ S``, where ``p, q`` are
the end points of ``L``.
"""
function ⊆(L::LineSegment, S::LazySet, witness::Bool=false)
if !isconvextype(typeof(S))
error("an inclusion check for the given combination of set types is " *
"not available")
end
return _issubset_line_segment(L, S, witness)
end
# requires convexity of S
function _issubset_line_segment(L, S, witness)
p_in_S = L.p ∈ S
result = p_in_S && L.q ∈ S
if result
return _witness_result_empty(witness, true, L, S)
end
return witness ? (false, p_in_S ? L.q : L.p) : false
end
# disambiguation
function ⊆(L::LineSegment, H::AbstractHyperrectangle, witness::Bool=false)
return _issubset_line_segment(L, H, witness)
end
"""
⊆(x::Interval, y::Interval, [witness]::Bool=false)
Check whether an interval is contained in another interval.
### Input
- `x` -- inner interval
- `y` -- outer interval
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
`true` iff ``x ⊆ y``.
"""
function ⊆(x::Interval, y::Interval, witness::Bool=false)
if min(y) > min(x)
return witness ? (false, low(x)) : false
elseif max(x) > max(y)
return witness ? (false, high(x)) : false
end
return _witness_result_empty(witness, true, x, y)
end
"""
⊆(x::Interval, U::UnionSet, [witness]::Bool=false)
Check whether an interval is contained in the union of two convex sets.
### Input
- `x` -- inner interval
- `U` -- outer union of two convex sets
### Output
`true` iff `x ⊆ U`.
### Notes
This implementation assumes that `U` is a union of one-dimensional convex sets.
Since these are equivalent to intervals, we convert to `Interval`s.
### Algorithm
Let ``U = a ∪ b `` where ``a`` and ``b`` are intervals and assume that the lower
bound of ``a`` is to the left of ``b``.
If the lower bound of ``x`` is to the left of ``a``, we have a counterexample.
Otherwise we compute the set difference ``y = x \\ a`` and check whether
``y ⊆ b`` holds.
"""
function ⊆(x::Interval, U::UnionSet, witness::Bool=false)
@assert dim(U) == 1 "an interval is incompatible with a set of dimension " *
"$(dim(U))"
if !isconvextype(typeof(first(U))) || !isconvextype(typeof(second(U)))
error("an inclusion check for the given combination of set types is " *
"not available")
end
return _issubset_interval(x, convert(Interval, first(U)),
convert(Interval, second(U)), witness)
end
function ⊆(x::Interval, U::UnionSet{N,<:Interval,<:Interval},
witness::Bool=false) where {N}
return _issubset_interval(x, first(U), second(U), witness)
end
function _issubset_interval(x::Interval{N}, a::Interval, b::Interval,
witness) where {N}
if min(a) > min(b)
c = b
b = a
a = c
end
# a is on the left of b
if min(x) < min(a)
return witness ? (false, low(x)) : false
end
y = difference(x, a)
if y ⊆ b
return _witness_result_empty(witness, true, N)
elseif !witness
return false
end
# compute witness
w = min(b) > min(y) ? [(min(y) + min(b)) / 2] : high(y)
return (false, w)
end
function ⊆(x::Interval, U::UnionSetArray, witness::Bool=false)
@assert dim(U) == 1 "an interval is incompatible with a set of dimension " *
"$(dim(U))"
V = _get_interval_array_copy(U)
return _issubset_interval!(x, V, witness)
end
# disambiguation
function ⊆(x::Interval, U::UnionSetArray{N,<:AbstractHyperrectangle},
witness::Bool=false) where {N}
@assert dim(U) == 1 "an interval is incompatible with a set of dimension " *
"$(dim(U))"
V = _get_interval_array_copy(U)
return _issubset_interval!(x, V, witness)
end
function _get_interval_array_copy(U::UnionSetArray{N}) where {N}
out = Vector{LazySet{N}}(undef, length(array(U)))
for (i, Xi) in enumerate(array(U))
Yi = _to_unbounded_interval(Xi)
if Yi isa Universe
return Yi
end
out[i] = Yi
end
return out
end
_to_unbounded_interval(X::Interval) = X
_to_unbounded_interval(H::HalfSpace) = H
_to_unbounded_interval(U::Universe) = U
function _to_unbounded_interval(X::LazySet{N}) where {N}
if !isconvextype(typeof(X))
throw(ArgumentError("unions with non-convex sets are not supported"))
end
l, h = extrema(X, 1)
if isinf(l)
if isinf(h)
return [Universe{N}(1)]
else
return HalfSpace([one(N)], h)
end
elseif isinf(h)
return HalfSpace([-one(N)], -l)
else
return Interval(l, h)
end
end
function _get_interval_array_copy(U::UnionSetArray{N,<:AbstractHyperrectangle}) where {N}
return [convert(Interval, X) for X in U]
end
function _get_interval_array_copy(U::UnionSetArray{N,<:Interval}) where {N}
return copy(array(U))
end
function _issubset_interval!(x::Interval{N}, intervals, witness) where {N}
# sort intervals by lower bound
sort!(intervals; lt=(x, y) -> low(x, 1) <= low(y, 1))
# subtract intervals from x
for y in intervals
if low(y, 1) > low(x, 1)
# lowest point of x is not contained
# witness is the point in the middle
return witness ? (false, [(low(x, 1) + low(y, 1)) / 2]) : false
end
x = difference(x, y)
if isempty(x)
return _witness_result_empty(witness, true, N)
end
end
return witness ? (false, center(x)) : false
end
"""
⊆(X::LazySet{N}, U::UnionSetArray, witness::Bool=false;
filter_redundant_sets::Bool=true) where {N}
Check whether a set is contained in a union of a finite number of sets.
### Input
- `X` -- inner set
- `U` -- outer union of a finite number of sets
- `witness` -- (optional, default: `false`) compute a witness if activated
- `filter_redundant_sets` -- (optional, default: `true`) ignore sets in `U` that
do not intersect with `X`
### Output
`true` iff ``X ⊆ U``.
### Algorithm
This implementation is general and successively removes parts from `X` that are
covered by the sets in the union ``U`` using the `difference` function. For the
resulting subsets, this implementation relies on the methods `isdisjoint`, `⊆`,
and `intersection`.
As a preprocessing, this implementation checks if `X` is contained in any of the
sets in `U`.
The `filter_redundant_sets` option controls whether sets in `U` that do not
intersect with `X` should be ignored.
"""
function ⊆(X::LazySet, U::UnionSetArray, witness::Bool=false;
filter_redundant_sets::Bool=true)
return _issubset_unionsetarray(X, U, witness;
filter_redundant_sets=filter_redundant_sets)
end
function _issubset_unionsetarray(X, U, witness::Bool=false;
filter_redundant_sets::Bool=true)
# heuristics (necessary check): is X contained in any set in U?
for rhs in U
if X ⊆ rhs
return _witness_result_empty(witness, true, X, U)
end
end
if filter_redundant_sets
# filter out those sets in U that do not intersect with X
sets = Vector{eltype(array(U))}()
for rhs in U
if !isdisjoint(X, rhs)
push!(sets, rhs)
end
end
else
sets = array(U)
end
queue = _inclusion_in_union_container(X, U)
push!(queue, X)
while !isempty(queue)
Y = pop!(queue)
# first check if Y is fully contained to avoid splitting/recursion
# keep track of the first set that intersects with Y
idx = 0
contained = false
for (i, rhs) in enumerate(sets)
if Y ⊆ rhs
contained = true
break
elseif idx == 0 && !isdisjoint(Y, rhs)
# does not terminate if the intersection is flat
cap = intersection(Y, rhs)
if !isempty(cap) && !_inclusion_in_union_isflat(cap)
idx = i
end
end
end
if contained
continue
end
if idx == 0
return witness ? (false, an_element(Y)) : false
end
# split wrt. the i-th set
rhs = sets[idx]
append!(queue, array(difference(Y, rhs)))
end
return _witness_result_empty(witness, true, X, U)
end
# general container
function _inclusion_in_union_container(X::LazySet{N}, U::UnionSetArray) where {N}
return LazySet{N}[]
end
# hyperrectangle container
function _inclusion_in_union_container(H::AbstractHyperrectangle,
U::UnionSetArray{N,<:AbstractHyperrectangle}) where {N}
return AbstractHyperrectangle{N}[]
end
# generally ignore check for flat sets
function _inclusion_in_union_isflat(X)
return false
end
function _inclusion_in_union_isflat(H::AbstractHyperrectangle)
return isflat(H)
end
# disambiguations
for ST in [:LineSegment]
@eval function ⊆(X::($ST), U::UnionSetArray, witness::Bool=false)
return _issubset_unionsetarray(X, U, witness)
end
end
# disambiguation with additional kwarg
function ⊆(X::AbstractPolytope, U::UnionSetArray, witness::Bool=false; algorithm=nothing)
return _issubset_unionsetarray(X, U, witness)
end
"""
⊆(∅::EmptySet, X::LazySet, witness::Bool=false)
Check whether the empty set is contained in another set.
### Input
- `∅` -- inner empty set
- `X` -- outer set
- `witness` -- (optional, default: `false`) compute a witness if activated
(ignored, just kept for interface reasons)
### Output
`true`.
"""
function ⊆(∅::EmptySet, X::LazySet, witness::Bool=false)
return _issubset_emptyset(∅, X, witness)
end
function _issubset_emptyset(∅::EmptySet, X::LazySet, witness::Bool=false)
return _witness_result_empty(witness, true, ∅, X)
end
# disambiguations
for ST in [:AbstractPolyhedron, :AbstractHyperrectangle, :Complement, :UnionSet,
:UnionSetArray]
@eval ⊆(∅::EmptySet, X::($ST), witness::Bool=false) = _issubset_emptyset(∅, X, witness)
end
"""
⊆(X::LazySet, ∅::EmptySet, [witness]::Bool=false)
Check whether a set is contained in the empty set.
### Input
- `X` -- inner set
- `∅` -- outer empty set
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
`true` iff `X` is empty.
### Algorithm
We rely on `isempty(X)` for the emptiness check and on `an_element(X)` for
witness production.
"""
function ⊆(X::LazySet, ∅::EmptySet, witness::Bool=false)
return _issubset_in_emptyset(X, ∅, witness)
end
function _issubset_in_emptyset(X::LazySet, ∅::EmptySet, witness::Bool=false)
if isempty(X)
return _witness_result_empty(witness, true, X, ∅)
else
return witness ? (false, an_element(X)) : false
end
end
# disambiguations
for ST in [:AbstractPolytope, :UnionSet, :UnionSetArray]
@eval ⊆(X::($ST), ∅::EmptySet, witness::Bool=false) = _issubset_in_emptyset(X, ∅, witness)
end
# disambiguations for sets that are never empty
for ST in [:AbstractSingleton, :LineSegment]
@eval ⊆(X::($ST), ::EmptySet, witness::Bool=false) = witness ? (false, an_element(X)) : false
end
function ⊆(∅₁::EmptySet, ∅₂::EmptySet, witness::Bool=false)
return _witness_result_empty(witness, true, ∅₁, ∅₂)
end
"""
⊆(U::UnionSet, X::LazySet, [witness]::Bool=false)
Check whether a union of two convex sets is contained in another set.
### Input
- `U` -- inner union of two convex sets
- `X` -- outer set
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``\\text{U} ⊆ X``
* If `witness` option is activated:
* `(true, [])` iff ``\\text{U} ⊆ X``
* `(false, v)` iff ``\\text{U} \\not\\subseteq X`` and
``v ∈ \\text{U} ∖ X``
"""
function ⊆(U::UnionSet, X::LazySet, witness::Bool=false)
return _issubset_union_in_set(U, X, witness)
end
"""
⊆(U::UnionSetArray, X::LazySet, [witness]::Bool=false)
Check whether a union of a finite number of convex sets is contained in another
set.
### Input
- `U` -- inner union of a finite number of convex sets
- `X` -- outer set
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``\\text{U} ⊆ X``
* If `witness` option is activated:
* `(true, [])` iff ``\\text{U} ⊆ X``
* `(false, v)` iff ``\\text{U} \\not\\subseteq X`` and
``v ∈ \\text{U} ∖ X``
"""
function ⊆(U::UnionSetArray, X::LazySet, witness::Bool=false)
return _issubset_union_in_set(U, X, witness)
end
# check for each set in `sets` that they are included in X
function _issubset_union_in_set(cup::Union{UnionSet,UnionSetArray}, X::LazySet{N},
witness::Bool=false) where {N}
result = true
v = N[]
for Y in cup
if witness
result, v = ⊆(Y, X, witness)
else
result = ⊆(Y, X, witness)
end
if !result
break
end
end
return _witness_result(witness, result, v)
end
# disambiguations
for ST in [:AbstractHyperrectangle, :AbstractPolyhedron, :UnionSet,
:UnionSetArray]
@eval function ⊆(U::UnionSet, X::($ST), witness::Bool=false)
return _issubset_union_in_set(U, X, witness)
end
@eval function ⊆(U::UnionSetArray, X::($ST), witness::Bool=false)
return _issubset_union_in_set(U, X, witness)
end
end
"""
⊆(X::LazySet, U::Universe, [witness]::Bool=false)
Check whether a set is contained in a universe.
### Input
- `X` -- inner set
- `U` -- outer universe
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output