/
isdisjoint.jl
1296 lines (1050 loc) · 40.9 KB
/
isdisjoint.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: isdisjoint
export isdisjoint, is_intersection_empty
"""
isdisjoint(X::LazySet, Y::LazySet, [witness]::Bool=false)
Check whether two sets do not intersect, and otherwise optionally compute a
witness.
### 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 ∩ Y``
### Algorithm
This is a fallback implementation that computes the concrete intersection,
`intersection`, of the given sets.
A witness is constructed using the `an_element` implementation of the result.
"""
function isdisjoint(X::LazySet, Y::LazySet, witness::Bool=false)
if _isdisjoint_convex_sufficient(X, Y)
return _witness_result_empty(witness, true, X, Y)
end
return _isdisjoint_general(X, Y, witness)
end
function _isdisjoint_general(X::LazySet, Y::LazySet, witness::Bool=false)
cap = intersection(X, Y)
empty_intersection = isempty(cap)
if empty_intersection
return _witness_result_empty(witness, true, X, Y)
end
return witness ? (false, an_element(cap)) : false
end
# alias
const is_intersection_empty = isdisjoint
# quick sufficient check that tries to find a separating hyperplane
# the result `true` is also sufficient for non-convex sets
function _isdisjoint_convex_sufficient(X::LazySet, Y::LazySet)
x = an_element(X)
y = an_element(Y)
d = x - y
return ρ(d, Y) < -ρ(-d, X)
end
# conversion for IA types
isdisjoint(X::LazySet, Y::IA.Interval, witness::Bool=false) = isdisjoint(X, Interval(Y), witness)
isdisjoint(X::IA.Interval, Y::LazySet, witness::Bool=false) = isdisjoint(Interval(X), Y, witness)
function isdisjoint(X::LazySet, Y::IA.IntervalBox, witness::Bool=false)
return isdisjoint(X, convert(Hyperrectangle, Y), witness)
end
function isdisjoint(X::IA.IntervalBox, Y::LazySet, witness::Bool=false)
return isdisjoint(convert(Hyperrectangle, X), Y, witness)
end
"""
isdisjoint(H1::AbstractHyperrectangle, H2::AbstractHyperrectangle,
[witness]::Bool=false)
Check whether two hyperrectangular sets do not intersect, and otherwise
optionally compute a witness.
### Input
- `H1` -- hyperrectangular set
- `H2` -- 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_2 - c_1| ≤ r_1 + r_2``, where ``≤`` is taken
component-wise.
A witness is computed by starting in one center and moving toward the other
center for as long as the minimum of the radius and the center distance.
In other words, the witness is the point in `H1` that is closest to the center
of `H2`.
"""
function isdisjoint(H1::AbstractHyperrectangle, H2::AbstractHyperrectangle,
witness::Bool=false)
empty_intersection = false
center_diff = center(H2) - center(H1)
@inbounds for i in eachindex(center_diff)
if abs(center_diff[i]) >
radius_hyperrectangle(H1, i) + radius_hyperrectangle(H2, i)
empty_intersection = true
break
end
end
if empty_intersection
return _witness_result_empty(witness, true, H1, H2)
elseif !witness
return false
end
# compute a witness 'v' in the intersection
v = copy(center(H1))
c2 = center(H2)
@inbounds for i in eachindex(center_diff)
if v[i] <= c2[i]
# second center is right of first center
v[i] += min(radius_hyperrectangle(H1, i), center_diff[i])
else
# second center is left of first center
v[i] -= min(radius_hyperrectangle(H1, i), -center_diff[i])
end
end
return (false, v)
end
"""
isdisjoint(I1::Interval, I2::Interval, [witness]::Bool=false)
Check whether two intervals do not intersect, and otherwise optionally compute a
witness.
### Input
- `I1` -- interval
- `I2` -- interval
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``I1 ∩ I2 = ∅``
* If `witness` option is activated:
* `(true, [])` iff ``I1 ∩ I2 = ∅``
* `(false, v)` iff ``I1 ∩ I2 ≠ ∅`` and ``v ∈ I1 ∩ I2``
### Algorithm
``I1 ∩ I2 ≠ ∅`` iff there is a gap between the left-most point of the second
interval and the right-most point of the first interval, or vice-versa.
A witness is computed by taking the maximum over the left-most points of each
interval, which is guaranteed to belong to the intersection.
"""
function isdisjoint(I1::Interval, I2::Interval, witness::Bool=false)
if witness
return _isdisjoint(I1, I2, Val(true))
else
return _isdisjoint(I1, I2, Val(false))
end
end
function _isdisjoint(I1::Interval, I2::Interval, witness::Val{false})
return !_leq(min(I2), max(I1)) || !_leq(min(I1), max(I2))
end
function _isdisjoint(I1::Interval, I2::Interval, witness::Val{true})
check = _isdisjoint(I1, I2, Val(false))
if check
N = promote_type(eltype(I1), eltype(I2))
return (true, N[])
else
return (false, [max(min(I1), min(I2))])
end
end
# common code for singletons
function _isdisjoint_singleton(S::AbstractSingleton, X::LazySet,
witness::Bool=false)
s = element(S)
empty_intersection = s ∉ X
return _witness_result_empty(witness, empty_intersection, S, X, s)
end
"""
isdisjoint(X::LazySet, S::AbstractSingleton, [witness]::Bool=false)
Check whether a set and a set with a single value do not intersect, and
otherwise optionally compute a witness.
### Input
- `X` -- set
- `S` -- set with a single value
- `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` = `element(S)` ``∈ S ∩ X``
### Algorithm
``S ∩ X = ∅`` iff `element(S)` ``∉ X``.
"""
@commutative function isdisjoint(X::LazySet, S::AbstractSingleton,
witness::Bool=false)
return _isdisjoint_singleton(S, X, witness)
end
# disambiguations
for ST in [:AbstractPolyhedron, :AbstractZonotope, :AbstractHyperrectangle,
:Hyperplane, :Line2D, :HalfSpace, :CartesianProductArray, :UnionSet,
:UnionSetArray]
@eval @commutative function isdisjoint(X::($ST), S::AbstractSingleton,
witness::Bool=false)
return _isdisjoint_singleton(S, X, witness)
end
end
"""
isdisjoint(S1::AbstractSingleton, S2::AbstractSingleton,
[witness]::Bool=false)
Check whether two sets with a single value do not intersect, and otherwise
optionally compute a witness.
### Input
- `S1` -- set with a single value
- `S2` -- set with a single value
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``S1 ∩ S2 = ∅``
* If `witness` option is activated:
* `(true, [])` iff ``S1 ∩ S2 = ∅``
* `(false, v)` iff ``S1 ∩ S2 ≠ ∅`` and `v` = `element(S1)` ``∈ S1 ∩ S2``
### Algorithm
``S1 ∩ S2 = ∅`` iff ``S1 ≠ S2``.
"""
function isdisjoint(S1::AbstractSingleton, S2::AbstractSingleton,
witness::Bool=false)
s1 = element(S1)
empty_intersection = !isapprox(s1, element(S2))
return _witness_result_empty(witness, empty_intersection, S1, S2, s1)
end
"""
isdisjoint(B1::Ball2, B2::Ball2, [witness]::Bool=false)
Check whether two balls in the 2-norm do not intersect, and otherwise optionally
compute a witness.
### Input
- `B1` -- ball in the 2-norm
- `B2` -- 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_2 - c_1 ‖_2 > r_1 + r_2``.
A witness is computed depending on the smaller/bigger ball (to break ties,
choose `B1` for the smaller ball) as follows.
- If the smaller ball's center is contained in the bigger ball, we return it.
- Otherwise start in the smaller ball's center and move toward the other center
until hitting the smaller ball's border.
In other words, the witness is the point in the smaller ball that is closest
to the center of the bigger ball.
"""
function isdisjoint(B1::Ball2, B2::Ball2, witness::Bool=false)
center_diff_normed = norm(center(B2) - center(B1), 2)
empty_intersection = center_diff_normed > B1.radius + B2.radius
if empty_intersection
return _witness_result_empty(witness, true, B1, B2)
elseif !witness
return false
end
# compute a witness 'v' in the intersection
if B1.radius <= B2.radius
smaller = B1
bigger = B2
else
smaller = B2
bigger = B1
end
if center_diff_normed <= bigger.radius
# smaller ball's center is contained in bigger ball
v = smaller.center
else
# scale center difference with smaller ball's radius
direction = (bigger.center - smaller.center)
v = smaller.center + direction / center_diff_normed * smaller.radius
end
return (false, v)
end
"""
isdisjoint(Z::AbstractZonotope, H::Hyperplane, [witness]::Bool=false)
Check whether a zonotopic set and a hyperplane do not intersect, and otherwise
optionally compute a witness.
### Input
- `Z` -- zonotopic set
- `H` -- hyperplane
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``Z ∩ H = ∅``
* If `witness` option is activated:
* `(true, [])` iff ``Z ∩ H = ∅``
* `(false, v)` iff ``Z ∩ H ≠ ∅`` and ``v ∈ Z ∩ H``
### Algorithm
``Z ∩ H = ∅`` iff ``(b - a⋅c) ∉ \\left[ ± ∑_{i=1}^p |a⋅g_i| \\right]``,
where ``a``, ``b`` are the hyperplane coefficients, ``c`` is the zonotope's
center, and ``g_i`` are the zonotope's generators.
For witness production we fall back to a less efficient implementation for
general sets as the first argument.
"""
@commutative function isdisjoint(Z::AbstractZonotope, H::Hyperplane,
witness::Bool=false)
return _isdisjoint_zonotope_hyperplane(Z, H, witness)
end
@commutative function isdisjoint(Z::AbstractZonotope, H::Line2D,
witness::Bool=false)
return _isdisjoint_zonotope_hyperplane(Z, H, witness)
end
function _isdisjoint_zonotope_hyperplane(Z::AbstractZonotope,
H::Union{Hyperplane,Line2D},
witness::Bool=false)
if witness
return _isdisjoint(Z, H, Val(true))
else
return _isdisjoint(Z, H, Val(false))
end
end
function _isdisjoint(Z::AbstractZonotope, H::Union{Hyperplane,Line2D},
::Val{false})
c, G = center(Z), genmat(Z)
v = H.b - dot(H.a, c)
p = size(G, 2)
p == 0 && return !isapproxzero(v)
asum = abs_sum(H.a, G)
return !_geq(v, -asum) || !_leq(v, asum)
end
function _isdisjoint(Z::AbstractZonotope, H::Union{Hyperplane,Line2D},
::Val{true})
return _isdisjoint_hyperplane(H, Z, true)
end
"""
isdisjoint(Z1::AbstractZonotope, Z2::AbstractZonotope,
[witness]::Bool=false; [solver]=nothing)
Check whether two zonotopic sets do not intersect, and otherwise optionally
compute a witness.
### Input
- `Z1` -- zonotopic set
- `Z2` -- zonotopic set
- `witness` -- (optional, default: `false`) compute a witness if activated
- `solver` -- (optional, default: `nothing`) the backend used to solve the
linear program
### Output
* If `witness` option is deactivated: `true` iff ``Z1 ∩ Z2 = ∅``
* If `witness` option is activated:
* `(true, [])` iff ``Z1 ∩ Z2 = ∅``
* `(false, v)` iff ``Z1 ∩ Z2 ≠ ∅`` and ``v ∈ Z1 ∩ Z2``
### Algorithm
The algorithm is taken from [1].
``Z1 ∩ Z2 = ∅`` iff ``c_1 - c_2 ∉ Z(0, (g_1, g_2))`` where ``c_i`` and ``g_i``
are the center and generators of zonotope `Zi` and ``Z(c, g)`` represents the
zonotope with center ``c`` and generators ``g``.
[1] L. J. Guibas, A. T. Nguyen, L. Zhang: *Zonotopes as bounding volumes*. SODA
2003.
"""
function isdisjoint(Z1::AbstractZonotope, Z2::AbstractZonotope,
witness::Bool=false; solver=nothing)
if _isdisjoint_convex_sufficient(Z1, Z2)
return _witness_result_empty(witness, true, Z1, Z2)
end
n = dim(Z1)
@assert n == dim(Z2) "the zonotopes need to have the same dimensions"
N = promote_type(eltype(Z1), eltype(Z2))
Z = Zonotope(zeros(N, n), hcat(genmat(Z1), genmat(Z2)))
result = !∈(center(Z1) - center(Z2), Z; solver=solver)
if result
return _witness_result_empty(witness, true, N)
elseif witness
error("witness production is not supported yet")
else
return false
end
end
"""
isdisjoint(L1::LineSegment, L2::LineSegment, [witness]::Bool=false)
Check whether two line segments do not intersect, and otherwise optionally
compute a witness.
### Input
- `L1` -- line segment
- `L2` -- line segment
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``L1 ∩ L2 = ∅``
* If `witness` option is activated:
* `(true, [])` iff ``L1 ∩ L2 = ∅``
* `(false, v)` iff ``L1 ∩ L2 ≠ ∅`` and ``v ∈ L1 ∩ L2``
### Algorithm
The algorithm is inspired from [here](https://stackoverflow.com/a/565282), which
again is the special 2D case of a 3D algorithm from [1].
We first check if the two line segments are parallel, and if so, if they are
collinear. In the latter case, we check membership of any of the end points in
the other line segment. Otherwise the lines are not parallel, so we can solve an
equation of the intersection point, if it exists.
[1] Ronald Goldman. *Intersection of two lines in three-space*. Graphics Gems
1990.
"""
function isdisjoint(L1::LineSegment, L2::LineSegment, witness::Bool=false)
r = L1.q - L1.p
if all(isapproxzero, r)
# first line segment is a point
empty_intersection = L1.q ∉ L2
return _witness_result_empty(witness, empty_intersection, L1, L2, L1.q)
end
s = L2.q - L2.p
if all(isapproxzero, s)
# second line segment is a point
empty_intersection = L2.q ∉ L1
return _witness_result_empty(witness, empty_intersection, L1, L2, L2.q)
end
p1p2 = L2.p - L1.p
u_numerator = right_turn(p1p2, r)
u_denominator = right_turn(r, s)
if u_denominator == 0
# line segments are parallel
if u_numerator == 0
# line segments are collinear
if L1.p ∈ L2
empty_intersection = false
if witness
v = L1.p
end
elseif L1.q ∈ L2
empty_intersection = false
if witness
v = L1.q
end
else
empty_intersection = true
end
else
# line segments are parallel and not collinear
empty_intersection = true
end
else
# line segments are not parallel
u = u_numerator / u_denominator
if u < 0 || u > 1
empty_intersection = true
else
t = right_turn(p1p2, s) / u_denominator
empty_intersection = t < 0 || t > 1
if witness
v = L1.p + t * r
end
end
end
if witness && !empty_intersection
return (false, v)
end
return _witness_result_empty(witness, empty_intersection, L1, L2)
end
function _isdisjoint_hyperplane(hp::Union{Hyperplane,Line2D}, X::LazySet,
witness::Bool=false)
if !isconvextype(typeof(X))
error("this implementation requires a convex set")
end
normal_hp = hp.a
sv_left = σ(-normal_hp, X)
if -dot(sv_left, -normal_hp) <= hp.b
sv_right = σ(normal_hp, X)
empty_intersection = (hp.b > dot(sv_right, normal_hp))
else
empty_intersection = true
end
if !witness || empty_intersection
return _witness_result_empty(witness, empty_intersection, hp, X)
end
# compute witness
point_hp = an_element(hp)
point_line = sv_left
dir_line = sv_right - sv_left
d = dot((point_hp - point_line), normal_hp) /
dot(dir_line, normal_hp)
v = d * dir_line + point_line
return (false, v)
end
"""
isdisjoint(X::LazySet, hp::Hyperplane, [witness]::Bool=false)
Check whether a convex set an a hyperplane do not intersect, and otherwise
optionally compute a witness.
### Input
- `X` -- convex set
- `hp` -- hyperplane
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``X ∩ hp = ∅``
* If `witness` option is activated:
* `(true, [])` iff ``X ∩ hp = ∅``
* `(false, v)` iff ``X ∩ hp ≠ ∅`` and ``v ∈ X ∩ hp``
### Algorithm
A convex set intersects with a hyperplane iff the support function in the
negative resp. positive direction of the hyperplane's normal vector ``a`` is to
the left resp. right of the hyperplane's constraint ``b``:
```math
-ρ(-a, X) ≤ b ≤ ρ(a, X)
```
For witness generation, we compute a line connecting the support vectors to the
left and right, and then take the intersection of the line with the hyperplane.
We follow
[this algorithm](https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection#Algebraic_form)
for the line-hyperplane intersection.
"""
@commutative function isdisjoint(X::LazySet, hp::Hyperplane,
witness::Bool=false)
return _isdisjoint_hyperplane(hp, X, witness)
end
@commutative function isdisjoint(X::LazySet, L::Line2D, witness::Bool=false)
return _isdisjoint_hyperplane(L, X, witness)
end
#disambiguations
for ST in [:AbstractPolyhedron]
@eval @commutative function isdisjoint(X::($ST), H::Hyperplane,
witness::Bool=false)
return _isdisjoint_hyperplane(H, X, witness)
end
@eval @commutative function isdisjoint(X::($ST), L::Line2D, witness::Bool=false)
return _isdisjoint_hyperplane(L, X, witness)
end
end
function isdisjoint(hp1::Hyperplane, hp2::Hyperplane, witness::Bool=false)
return _isdisjoint_hyperplane_hyperplane(hp1, hp2, witness)
end
@commutative function isdisjoint(hp::Hyperplane, L::Line2D, witness::Bool=false)
return _isdisjoint_hyperplane_hyperplane(hp, L, witness)
end
function _isdisjoint_hyperplane_hyperplane(hp1::Union{Hyperplane,Line2D},
hp2::Union{Hyperplane,Line2D},
witness::Bool=false)
if isequivalent(hp1, hp2)
res = false
if witness
w = an_element(hp1)
end
else
cap = intersection(hp1, hp2)
res = cap isa EmptySet
if !res && witness
w = an_element(cap)
end
end
if res
return _witness_result_empty(witness, true, hp1, hp2)
end
return witness ? (false, w) : false
end
function _isdisjoint_halfspace(hs::HalfSpace, X::LazySet, witness::Bool=false)
if !witness
return !_leq(-ρ(-hs.a, X), hs.b)
end
# for witness production, we compute the support vector instead
svec = σ(-hs.a, X)
empty_intersection = svec ∉ hs
return _witness_result_empty(witness, empty_intersection, hs, X, svec)
end
"""
isdisjoint(X::LazySet, hs::HalfSpace, [witness]::Bool=false)
Check whether a set an a half-space do not intersect, and otherwise optionally
compute a witness.
### Input
- `X` -- set
- `hs` -- half-space
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``X ∩ hs = ∅``
* If `witness` option is activated:
* `(true, [])` iff ``X ∩ hs = ∅``
* `(false, v)` iff ``X ∩ hs ≠ ∅`` and ``v ∈ X ∩ hs``
### Algorithm
A set intersects with a half-space iff the support function in the negative
direction of the half-space's normal vector ``a`` is less than the constraint
``b`` of the half-space: ``-ρ(-a, X) ≤ b``.
For compact set `X`, we equivalently have that the support vector in the
negative direction ``-a`` is contained in the half-space: ``σ(-a) ∈ hs``.
The support vector is thus also a witness if the sets are not disjoint.
"""
@commutative function isdisjoint(X::LazySet, hs::HalfSpace, witness::Bool=false)
return _isdisjoint_halfspace(hs, X, witness)
end
"""
isdisjoint(H1::HalfSpace, H2::HalfSpace, [witness]::Bool=false)
Check whether two half-spaces do not intersect, and otherwise optionally compute
a witness.
### Input
- `H1` -- half-space
- `H2` -- half-space
- `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
Two half-spaces do not intersect if and only if their normal vectors point in
the opposite direction and there is a gap between the two defining hyperplanes.
The latter can be checked as follows:
Let ``H1 : a_1⋅x = b_1`` and ``H2 : a_2⋅x = b_2``.
Then we already know that ``a_2 = -k⋅a_1`` for some positive scaling factor
``k``.
Let ``x_1`` be a point on the defining hyperplane of ``H1``.
We construct a line segment from ``x_1`` to the point ``x_2`` on the defining
hyperplane of ``hs_2`` by shooting a ray from ``x_1`` with direction ``a_1``.
Thus we look for a factor ``s`` such that ``(x_1 + s⋅a_1)⋅a_2 = b_2``.
This gives us ``s = (b_2 - x_1⋅a_2) / (-k a_1⋅a_1)``.
The gap exists if and only if ``s`` is positive.
If the normal vectors do not point in opposite directions, then the defining
hyperplanes intersect and we can produce a witness as follows.
All points ``x`` in this intersection satisfy ``a_1⋅x = b_1`` and
``a_2⋅x = b_2``. Thus we have ``(a_1 + a_2)⋅x = b_1+b_2``.
We now find a dimension where ``a_1 + a_2`` is non-zero, say, ``i``.
Then the result is a vector with one non-zero entry in dimension ``i``, defined
as ``[0, …, 0, (b_1 + b_2)/(a_1[i] + a_2[i]), 0, …, 0]``.
Such a dimension ``i`` always exists.
"""
function isdisjoint(H1::HalfSpace, H2::HalfSpace, witness::Bool=false)
a1 = H1.a
a2 = H2.a
N = promote_type(eltype(H1), eltype(H2))
issamedir, k = samedir(a1, -a2)
if issamedir
x1 = an_element(Hyperplane(a1, H1.b))
b2 = H2.b
s = (b2 - dot(x1, a2)) / (-k * dot(a1, a1))
empty_intersection = s > 0
# if `!empty_intersection`, x1 is a witness because both defining
# hyperplanes are contained in each half-space
return _witness_result_empty(witness, empty_intersection, H1, H2, x1)
elseif !witness
return false
end
# compute witness
v = zeros(N, length(a1))
for i in eachindex(a1)
a_sum_i = a1[i] + a2[i]
if a_sum[i] != 0
v[i] = (H1.b + H2.b) / a_sum_i
break
end
end
return (false, v)
end
# disambiguations
for ST in [:AbstractPolyhedron, :AbstractZonotope, :Hyperplane, :Line2D,
:CartesianProductArray]
@eval @commutative function isdisjoint(X::($ST), H::HalfSpace, witness::Bool=false)
return _isdisjoint_halfspace(H, X, witness)
end
end
"""
isdisjoint(P::AbstractPolyhedron, X::LazySet, [witness]::Bool=false;
[solver]=nothing, [algorithm]="exact")
Check whether a polyhedral set and another set do not intersect, and otherwise
optionally compute a witness.
### Input
- `P` -- polyhedral set
- `X` -- set (see the Notes section below)
- `witness` -- (optional, default: `false`) compute a witness if activated
- `solver` -- (optional, default: `nothing`) the backend used to solve the
linear program
- `algorithm` -- (optional, default: `"exact"`) algorithm keyword, one of:
* `"exact" (exact, uses a feasibility LP)
* `"sufficient" (sufficient, uses half-space checks)
### Output
* If `witness` option is deactivated: `true` iff ``P ∩ X = ∅``
* If `witness` option is activated:
* `(true, [])` iff ``P ∩ X = ∅``
* `(false, v)` iff ``P ∩ X ≠ ∅`` and ``v ∈ P ∩ X``
### Notes
For `algorithm == "exact"`, we assume that `constraints_list(X)` is defined.
For `algorithm == "sufficient"`, witness production is not supported.
For `solver == nothing`, we fall back to `default_lp_solver(N)`.
### Algorithm
For `algorithm == "exact"`, see [`isempty(P::HPoly, ::Bool)`](@ref).
For `algorithm == "sufficient"`, we rely on the intersection check between the
set `X` and each constraint in `P`.
This requires one support-function evaluation of `X` for each constraint of `P`.
With this algorithm, the method may return `false` even in the case where the
intersection is empty. On the other hand, if the algorithm returns `true`, then
it is guaranteed that the intersection is empty.
"""
@commutative function isdisjoint(P::AbstractPolyhedron, X::LazySet,
witness::Bool=false; solver=nothing,
algorithm="exact")
return _isdisjoint_polyhedron(P, X, witness; solver=solver,
algorithm=algorithm)
end
function _isdisjoint_polyhedron(P::AbstractPolyhedron, X::LazySet,
witness::Bool=false; solver=nothing,
algorithm="exact")
N = promote_type(eltype(P), eltype(X))
if algorithm == "sufficient"
# sufficient check for empty intersection using half-space checks
for Hi in constraints_list(P)
if isdisjoint(X, Hi)
return _witness_result_empty(witness, true, N)
end
end
if witness
error("witness production is not supported yet")
end
return false
elseif algorithm == "exact"
# exact check for empty intersection using a feasibility LP
if !is_polyhedral(X)
error("this algorithm requires a polyhedral input")
end
clist_P = _normal_Vector(P) # TODO
clist_X = _normal_Vector(X) # TODO
if isnothing(solver)
solver = default_lp_solver(N)
end
return _isempty_polyhedron_lp([clist_P; clist_X], witness; solver=solver)
else
error("algorithm $algorithm unknown")
end
end
# disambiguation
function isdisjoint(X::AbstractPolyhedron, P::AbstractPolyhedron, witness::Bool=false;
solver=nothing, algorithm="exact")
return _isdisjoint_polyhedron(P, X, witness)
end
"""
isdisjoint(U::UnionSet, X::LazySet, [witness]::Bool=false)
Check whether a union of two sets and another set do not intersect, and
otherwise optionally compute a witness.
### Input
- `U` -- union of two sets
- `X` -- set
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
`true` iff ``\\text{U} ∩ X = ∅``.
"""
@commutative function isdisjoint(U::UnionSet, X::LazySet, witness::Bool=false)
return _isdisjoint_union(U, X, witness)
end
"""
isdisjoint(U::UnionSetArray, X::LazySet, [witness]::Bool=false)
Check whether a union of a finite number of sets and another set do not
intersect, and otherwise optionally compute a witness.
### Input
- `U` -- union of a finite number of sets
- `X` -- set
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
`true` iff ``\\text{U} ∩ X = ∅``.
"""
@commutative function isdisjoint(U::UnionSetArray, X::LazySet,
witness::Bool=false)
return _isdisjoint_union(U, X, witness)
end
function _isdisjoint_union(cup::Union{UnionSet,UnionSetArray}, X::LazySet{N},
witness::Bool=false) where {N}
for Y in cup
if witness
result, w = isdisjoint(Y, X, witness)
else
result = isdisjoint(Y, X, witness)
end
if !result
return witness ? (false, w) : false
end
end
return _witness_result_empty(witness, true, N)
end
# disambiguations
for ST in [:AbstractPolyhedron, :Hyperplane, :Line2D, :HalfSpace]
@eval @commutative function isdisjoint(U::UnionSet, X::($ST), witness::Bool=false)
return _isdisjoint_union(U, X, witness)
end
@eval @commutative function isdisjoint(U::UnionSetArray, X::($ST),
witness::Bool=false)
return _isdisjoint_union(U, X, witness)
end
end
@commutative function isdisjoint(U1::UnionSet, U2::UnionSetArray,
witness::Bool=false)
return _isdisjoint_union(U1, U2, witness)
end
function isdisjoint(U1::UnionSet, U2::UnionSet, witness::Bool=false)
return _isdisjoint_union(U1, U2, witness)
end
function isdisjoint(U1::UnionSetArray, U2::UnionSetArray, witness::Bool=false)
return _isdisjoint_union(U1, U2, witness)
end
"""
isdisjoint(U::Universe, X::LazySet, [witness]::Bool=false)
Check whether a universe and another set do not intersect, and otherwise
optionally compute a witness.
### Input
- `U` -- universe
- `X` -- set
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
`true` iff ``X ≠ ∅``.
"""
@commutative function isdisjoint(U::Universe, X::LazySet, witness::Bool=false)
return _isdisjoint_universe(U, X, witness)
end
function _isdisjoint_universe(U::Universe, X::LazySet, witness)
@assert dim(X) == dim(U) "the dimensions of the given sets should match, " *
"but they are $(dim(X)) and $(dim(U)), respectively"
result = isempty(X)
if result
return _witness_result_empty(witness, true, U, X)
else
return witness ? (false, an_element(X)) : false
end
end
# disambiguations
for ST in [:AbstractPolyhedron, :AbstractZonotope, :AbstractSingleton,
:HalfSpace, :Hyperplane, :Line2D, :CartesianProductArray, :UnionSet,
:UnionSetArray, :Complement]
@eval @commutative function isdisjoint(U::Universe, X::($ST), witness::Bool=false)
return _isdisjoint_universe(U, X, witness)
end
end
function isdisjoint(U::Universe, ::Universe, witness::Bool=false)
return witness ? (false, an_element(U)) : false
end
"""
isdisjoint(C::Complement, X::LazySet, [witness]::Bool=false)
Check whether the complement of a set and another set do not intersect, and
otherwise optionally compute a witness.
### Input
- `C` -- complement of a set
- `X` -- set
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` option is deactivated: `true` iff ``X ∩ C = ∅``
* If `witness` option is activated:
* `(true, [])` iff ``X ∩ C = ∅``
* `(false, v)` iff ``X ∩ C ≠ ∅`` and ``v ∈ X ∩ C``
### Algorithm
We fall back to `X ⊆ C.X`, which can be justified as follows:
```math
X ∩ Y^C = ∅ ⟺ X ⊆ Y
```
"""