-
Notifications
You must be signed in to change notification settings - Fork 32
/
LazySet.jl
1455 lines (1127 loc) · 38.8 KB
/
LazySet.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
export LazySet,
neutral,
absorbing,
tosimplehrep,
singleton_list,
chebyshev_center_radius,
○,
flatten
"""
LazySet{N}
Abstract type for the set types in LazySets.
### Notes
`LazySet` types should be parameterized with a type `N`, typically `N<:Real`,
for using different numeric types.
Every concrete `LazySet` must define the following method:
- `dim(X::LazySet)` -- the ambient dimension of `X`
While not strictly required, it is useful to define the following method:
- `σ(d::AbstractVector, X::LazySet)` -- the support vector of `X` in a given
direction `d`
The method
- `ρ(d::AbstractVector, X::LazySet)` -- the support function of `X` in a given
direction `d`
is optional because there is a fallback implementation relying on `σ`.
However, for potentially unbounded sets (which includes most lazy set types)
this fallback cannot be used and an explicit method must be implemented.
The subtypes of `LazySet` (including abstract interfaces):
```jldoctest; setup = :(using LazySets: subtypes)
julia> subtypes(LazySet, false)
17-element Vector{Any}:
AbstractAffineMap
AbstractPolynomialZonotope
Bloating
CachedMinkowskiSumArray
CartesianProduct
CartesianProductArray
Complement
ConvexSet
Intersection
IntersectionArray
MinkowskiSum
MinkowskiSumArray
Polygon
QuadraticMap
Rectification
UnionSet
UnionSetArray
```
If we only consider *concrete* subtypes, then:
```jldoctest; setup = :(using LazySets: subtypes)
julia> concrete_subtypes = subtypes(LazySet, true);
julia> length(concrete_subtypes)
54
julia> println.(concrete_subtypes);
AffineMap
Ball1
Ball2
BallInf
Ballp
Bloating
CachedMinkowskiSumArray
CartesianProduct
CartesianProductArray
Complement
ConvexHull
ConvexHullArray
DensePolynomialZonotope
Ellipsoid
EmptySet
ExponentialMap
ExponentialProjectionMap
HParallelotope
HPolygon
HPolygonOpt
HPolyhedron
HPolytope
HalfSpace
Hyperplane
Hyperrectangle
Intersection
IntersectionArray
Interval
InverseLinearMap
Line
Line2D
LineSegment
LinearMap
MinkowskiSum
MinkowskiSumArray
Polygon
QuadraticMap
Rectification
ResetMap
RotatedHyperrectangle
SimpleSparsePolynomialZonotope
Singleton
SparsePolynomialZonotope
Star
SymmetricIntervalHull
Tetrahedron
Translation
UnionSet
UnionSetArray
Universe
VPolygon
VPolytope
ZeroSet
Zonotope
```
"""
abstract type LazySet{N} end
"""
○(c, a)
Convenience constructor of `Ellipsoid`s or `Ball2`s depending on the type of `a`.
### Input
- `c` -- center
- `a` -- additional parameter (either a shape matrix for `Ellipsoid` or a radius
for `Ball2`)
### Output
A `Ellipsoid`s or `Ball2`s depending on the type of `a`.
### Notes
"`○`" can be typed by `\\bigcirc<tab>`.
"""
function ○(c, a) end
"""
### Algorithm
The default implementation returns `false`. All set types that can determine
convexity should override this behavior.
### Examples
A ball in the infinity norm is always convex:
```jldoctest convex_types
julia> isconvextype(BallInf)
true
```
The union (`UnionSet`) of two sets may in general be either convex or not. Since
convexity cannot be decided by just using type information, `isconvextype`
returns `false`.
```jldoctest convex_types
julia> isconvextype(UnionSet)
false
```
However, the type parameters of set operations allow to decide convexity in some
cases by falling back to the convexity information of the argument types.
Consider the lazy intersection. The intersection of two convex sets is always
convex:
```jldoctest convex_types
julia> isconvextype(Intersection{Float64, BallInf{Float64}, BallInf{Float64}})
true
```
"""
isconvextype(::Type{<:LazySet}) = false
# Polyhedra backend (fallback method)
function default_polyhedra_backend(P::LazySet{N}) where {N}
require(@__MODULE__, :Polyhedra; fun_name="default_polyhedra_backend")
if LazySets.dim(P) == 1
return default_polyhedra_backend_1d(N)
else
return default_polyhedra_backend_nd(N)
end
end
# Note: this method cannot be documented due to a bug in Julia
function low(X::LazySet, i::Int)
return _low(X, i)
end
function _low(X::LazySet{N}, i::Int) where {N}
n = dim(X)
d = SingleEntryVector(i, n, -one(N))
return -ρ(d, X)
end
"""
### Algorithm
The default implementation applies `low` in each dimension.
"""
function low(X::LazySet)
n = dim(X)
return [low(X, i) for i in 1:n]
end
# Note: this method cannot be documented due to a bug in Julia
function high(X::LazySet, i::Int)
return _high(X, i)
end
function _high(X::LazySet{N}, i::Int) where {N}
n = dim(X)
d = SingleEntryVector(i, n, one(N))
return ρ(d, X)
end
"""
### Algorithm
The default implementation applies `high` in each dimension.
"""
function high(X::LazySet)
n = dim(X)
return [high(X, i) for i in 1:n]
end
"""
### Algorithm
The default implementation computes the extrema via `low` and `high`.
"""
function extrema(X::LazySet, i::Int)
l = low(X, i)
h = high(X, i)
return (l, h)
end
"""
### Algorithm
The default implementation computes the extrema via `low` and `high`.
"""
function extrema(X::LazySet)
return _extrema_lowhigh(X)
end
function _extrema_lowhigh(X::LazySet)
l = low(X)
h = high(X)
return (l, h)
end
"""
plot_vlist(X::S, ε::Real) where {S<:LazySet}
Return a list of vertices used for plotting a two-dimensional set.
### Input
- `X` -- two-dimensional set
- `ε` -- precision parameter
### Output
A list of vertices of a polygon `P`.
For convex `X`, `P` usually satisfies that the Hausdorff distance to `X` is less
than `ε`.
"""
function plot_vlist(X::S, ε::Real) where {S<:LazySet}
@assert isconvextype(S) "can only plot convex sets"
P = overapproximate(X, ε)
return convex_hull(vertices_list(P))
end
"""
convex_hull(X::LazySet; kwargs...)
Compute the convex hull of a polytopic set.
### Input
- `X` -- polytopic set
### Output
The set `X` itself if its type indicates that it is convex, or a new set with
the list of the vertices describing the convex hull.
### Algorithm
For non-convex sets, this method relies on the `vertices_list` method.
"""
function convex_hull(X::LazySet; kwargs...)
if isconvextype(typeof(X))
return X
end
return _convex_hull_polytopes(X; kwargs...)
end
function _convex_hull_polytopes(X; kwargs...)
vlist = convex_hull(vertices_list(X); kwargs...)
return _convex_hull_set(vlist; n=dim(X))
end
"""
### Algorithm
The default implementation assumes that the first type parameter is `N`.
"""
eltype(::Type{<:LazySet{N}}) where {N} = N
"""
### Algorithm
The default implementation assumes that the first type parameter is `N`.
"""
eltype(::LazySet{N}) where {N} = N
"""
### Algorithm
The default implementation computes a support vector via `σ`.
"""
function ρ(d::AbstractVector, X::LazySet)
return dot(d, σ(d, X))
end
"""
### Notes
Note that some sets may still represent an unbounded set even though their type
actually does not (example: [`HPolytope`](@ref), because the construction with
non-bounding linear constraints is allowed).
### Algorithm
The default implementation returns `false`. All set types that can determine
boundedness should override this behavior.
"""
function isboundedtype(::Type{<:LazySet})
return false
end
"""
isbounded(X::LazySet; [algorithm]="support_function")
Check whether a set is bounded.
### Input
- `X` -- set
- `algorithm` -- (optional, default: `"support_function"`) algorithm choice,
possible options are `"support_function"` and `"stiemke"`
### Output
`true` iff the set is bounded.
### Algorithm
See the documentation of `_isbounded_unit_dimensions` or `_isbounded_stiemke`
for details.
"""
function isbounded(X::LazySet; algorithm="support_function")
if algorithm == "support_function"
return _isbounded_unit_dimensions(X)
elseif algorithm == "stiemke"
return _isbounded_stiemke(constraints_list(X))
else
throw(ArgumentError("unknown algorithm $algorithm"))
end
end
"""
_isbounded_unit_dimensions(X::LazySet)
Check whether a set is bounded in each unit dimension.
### Input
- `X` -- set
### Output
`true` iff the set is bounded in each unit dimension.
### Algorithm
This function asks for upper and lower bounds in each ambient dimension.
"""
function _isbounded_unit_dimensions(X::LazySet)
@inbounds for i in 1:dim(X)
if isinf(low(X, i)) || isinf(high(X, i))
return false
end
end
return true
end
"""
### Algorithm
The default implementation returns `false`. All set types that can determine the
polyhedral property should override this behavior.
"""
function is_polyhedral(::LazySet)
return false
end
"""
### Algorithm
The default implementation handles the case `p == Inf` using `extrema`.
Otherwise it checks whether `X` is polytopic, in which case it iterates over all
vertices.
"""
function norm(X::LazySet, p::Real=Inf)
if p == Inf
l, h = extrema(X)
return max(maximum(abs, l), maximum(abs, h))
elseif is_polyhedral(X) && isboundedtype(typeof(X))
return maximum(norm(v, p) for v in vertices_list(X))
else
error("the norm for this value of p=$p is not implemented")
end
end
"""
### Algorithm
The default implementation handles the case `p == Inf` using
`ballinf_approximation`.
"""
function radius(X::LazySet, p::Real=Inf)
if p == Inf
return radius(Approximations.ballinf_approximation(X), p)
else
error("the radius for this value of p=$p is not implemented")
end
end
"""
### Algorithm
The default implementation applies the function `radius` and doubles the result.
"""
function diameter(X::LazySet, p::Real=Inf)
return radius(X, p) * 2
end
"""
### Algorithm
The default implementation calls `translate!` on a copy of `X`.
"""
function translate(X::LazySet, v::AbstractVector)
Y = copy(X)
translate!(Y, v)
return Y
end
"""
### Algorithm
The default implementation applies the functions `linear_map` and `translate`.
"""
function affine_map(M, X::LazySet, v::AbstractVector; kwargs...)
return translate(linear_map(M, X; kwargs...), v)
end
"""
### Algorithm
The default implementation applies the functions `exp` and `linear_map`.
"""
function exponential_map(M::AbstractMatrix, X::LazySet)
return linear_map(exp(M), X)
end
"""
### Algorithm
The default implementation computes a support vector along direction
``[1, 0, …, 0]``. This may fail for unbounded sets.
"""
function an_element(X::LazySet)
return _an_element_lazySet(X)
end
function _an_element_lazySet(X::LazySet)
N = eltype(X)
e₁ = SingleEntryVector(1, dim(X), one(N))
return σ(e₁, X)
end
# hook into random API
function rand(rng::AbstractRNG, ::SamplerType{T}) where {T<:LazySet}
return rand(T; rng=rng)
end
# This function computes a `copy` of each field in `X`. See the documentation of
# `?copy` for further details.
function copy(X::T) where {T<:LazySet}
args = [copy(getfield(X, f)) for f in fieldnames(T)]
BT = basetype(X)
return BT(args...)
end
"""
tosimplehrep(S::LazySet)
Return the simple constraint representation ``Ax ≤ b`` of a polyhedral set from
its list of linear constraints.
### Input
- `S` -- polyhedral set
### Output
The tuple `(A, b)` where `A` is the matrix of normal directions and `b` is the
vector of offsets.
### Algorithm
This fallback implementation relies on `constraints_list(S)`.
"""
tosimplehrep(S::LazySet) = tosimplehrep(constraints_list(S))
"""
reflect(P::LazySet)
Concrete reflection of a set `P`, resulting in the reflected set `-P`.
### Input
- `P` -- polyhedral set
### Output
The set `-P`, which is either of type `HPolytope` if `P` is a polytope (i.e.,
bounded) or of type `HPolyhedron` otherwise.
### Algorithm
This function requires that the list of constraints of the set `P` is
available, i.e., that it can be written as
``P = \\{z ∈ ℝⁿ: ⋂ sᵢᵀz ≤ rᵢ, i = 1, ..., N\\}.``
This function can be used to implement the alternative definition of the
Minkowski difference
```math
A ⊖ B = \\{a − b \\mid a ∈ A, b ∈ B\\} = A ⊕ (-B)
```
by calling `minkowski_sum(A, reflect(B))`.
"""
function reflect(P::LazySet)
if !is_polyhedral(P)
error("this implementation requires a polyhedral set; try " *
"overapproximating with an `HPolyhedron` first")
end
F, g = tosimplehrep(P)
T = isbounded(P) ? HPolytope : HPolyhedron
return T(-F, g)
end
"""
### Algorithm
The default implementation determines `v ∈ interior(X)` with error tolerance
`ε` by checking whether a `Ballp` of norm `p` with center `v` and radius `ε` is
contained in `X`.
"""
function is_interior_point(v::AbstractVector{N}, X::LazySet{N}; p=N(Inf), ε=_rtol(N)) where {N}
return Ballp(p, v, ε) ⊆ X
end
"""
plot_recipe(X::LazySet, [ε])
Convert a compact convex set to a pair `(x, y)` of points for plotting.
### Input
- `X` -- compact convex set
- `ε` -- approximation-error bound
### Output
A pair `(x, y)` of points that can be plotted.
### Notes
We do not support three-dimensional or higher-dimensional sets at the moment.
### Algorithm
One-dimensional sets are converted to an `Interval`.
For two-dimensional sets, we first compute a polygonal overapproximation.
The second argument, `ε`, corresponds to the error in Hausdorff distance between
the overapproximating set and `X`.
On the other hand, if you only want to produce a fast box-overapproximation of
`X`, pass `ε=Inf`.
Finally, we use the plot recipe for the constructed set (interval or polygon).
"""
function plot_recipe(X::LazySet, ε)
@assert dim(X) <= 3 "cannot plot a $(dim(X))-dimensional $(typeof(X))"
@assert isboundedtype(typeof(X)) || isbounded(X) "cannot plot an " *
"unbounded $(typeof(X))"
@assert isconvextype(typeof(X)) "can only plot convex sets"
if dim(X) == 1
Y = convert(LazySets.Interval, X)
elseif dim(X) == 2
Y = overapproximate(X, ε)
else
return _plot_recipe_3d_polytope(X)
end
return plot_recipe(Y, ε)
end
function _plot_recipe_3d_polytope(P::LazySet, N=eltype(P))
require(@__MODULE__, :MiniQhull; fun_name="_plot_recipe_3d_polytope")
@assert is_polyhedral(P) && isboundedtype(typeof(P)) "3D plotting is " *
"only available for polytopes"
vlist, C = delaunay_vlist_connectivity(P; compute_triangles_3d=true)
m = length(vlist)
if m == 0
@warn "received a polyhedron with no vertices during plotting"
return plot_recipe(EmptySet{N}(2), zero(N))
end
x = Vector{N}(undef, m)
y = Vector{N}(undef, m)
z = Vector{N}(undef, m)
@inbounds for (i, vi) in enumerate(vlist)
x[i] = vi[1]
y[i] = vi[2]
z[i] = vi[3]
end
l = size(C, 2)
i = Vector{Int}(undef, l)
j = Vector{Int}(undef, l)
k = Vector{Int}(undef, l)
@inbounds for idx in 1:l
# normalization: -1 for zero indexing; convert to Int on 64-bit systems
i[idx] = Int(C[1, idx] - 1)
j[idx] = Int(C[2, idx] - 1)
k[idx] = Int(C[3, idx] - 1)
end
return x, y, z, i, j, k
end
"""
### Algorithm
The default implementation checks whether the set type of the input is an
operation type using [`isoperationtype(::Type{<:LazySet})`](@ref).
### Examples
```jldoctest
julia> B = BallInf([0.0, 0.0], 1.0);
julia> isoperation(B)
false
julia> isoperation(B ⊕ B)
true
```
"""
function isoperation(X::LazySet)
return isoperationtype(typeof(X))
end
# common error
function isoperation(::Type{<:LazySet})
throw(ArgumentError("`isoperation` cannot be applied to a set type; use " *
"`isoperationtype` instead"))
end
# common error
function isoperationtype(::LazySet)
throw(ArgumentError("`isoperationtype` cannot be applied to a set " *
"instance; use `isoperation` instead"))
end
"""
### Algorithm
The default implementation applies `area` for two-dimensional sets.
"""
function surface(X::LazySet)
if dim(X) == 2
return area(X)
else
throw(ArgumentError("the `surface` function is only implemented for two-dimensional" *
"sets, but the given set is $(dim(X))-dimensional"))
end
end
"""
area(X::LazySet)
Compute the area of a two-dimensional polytopic set.
### Input
- `X` -- two-dimensional polytopic set
### Output
A number representing the area of `X`.
### Notes
This algorithm is applicable to any polytopic set `X` whose list of vertices can
be computed via `vertices_list`.
### Algorithm
Let `m` be the number of vertices of `X`. We consider the following instances:
- `m = 0, 1, 2`: the output is zero.
- `m = 3`: the triangle case is solved using the Shoelace formula with 3 points.
- `m = 4`: the quadrilateral case is solved by the factored version of the
Shoelace formula with 4 points.
Otherwise, the general Shoelace formula is used; for details see the
[Wikipedia page](https://en.wikipedia.org/wiki/Shoelace_formula).
"""
function area(X::LazySet)
@assert dim(X) == 2 "this function only applies to two-dimensional sets, " *
"but the given set is $(dim(X))-dimensional"
@assert is_polyhedral(X) && isbounded(X) "this method requires a polytope"
vlist = vertices_list(X)
return _area_vlist(vlist)
end
# Notes:
# - dimension is expected to be 2D
# - implementation requires sorting of vertices
# - convex hull is applied in-place
function _area_vlist(vlist; apply_convex_hull::Bool=true)
if apply_convex_hull
convex_hull!(vlist)
end
m = length(vlist)
if m <= 2
N = eltype(eltype(vlist))
return zero(N)
end
if m == 3 # triangle
res = _area_triangle(vlist)
elseif m == 4 # quadrilateral
res = _area_quadrilateral(vlist)
else # general case
res = _area_polygon(vlist)
end
return res
end
function _area_triangle(v::Vector{VN}) where {N,VN<:AbstractVector{N}}
A = v[1]
B = v[2]
C = v[3]
res = A[1] * (B[2] - C[2]) + B[1] * (C[2] - A[2]) + C[1] * (A[2] - B[2])
return abs(res / 2)
end
function _area_quadrilateral(v::Vector{VN}) where {N,VN<:AbstractVector{N}}
A = v[1]
B = v[2]
C = v[3]
D = v[4]
res = A[1] * (B[2] - D[2]) + B[1] * (C[2] - A[2]) + C[1] * (D[2] - B[2]) +
D[1] * (A[2] - C[2])
return abs(res / 2)
end
function _area_polygon(v::Vector{VN}) where {N,VN<:AbstractVector{N}}
m = length(v)
@inbounds res = v[m][1] * v[1][2] - v[1][1] * v[m][2]
for i in 1:(m - 1)
@inbounds res += v[i][1] * v[i + 1][2] - v[i + 1][1] * v[i][2]
end
return abs(res / 2)
end
"""
singleton_list(P::LazySet)
Return the vertices of a polytopic set as a list of singletons.
### Input
- `P` -- polytopic set
### Output
A list of the vertices of `P` as `Singleton`s.
### Notes
This function relies on `vertices_list`, which raises an error if the set is
not polytopic (e.g., unbounded).
"""
function singleton_list(P::LazySet)
return [Singleton(x) for x in vertices_list(P)]
end
"""
### Algorithm
The default implementation returns `X`. All relevant lazy set types should
override this behavior, typically by recursively calling `concretize` on the
set arguments.
"""
function concretize(X::LazySet)
return X
end
"""
### Algorithm
The default implementation computes all constraints via `constraints_list`.
"""
function constraints(X::LazySet)
return _constraints_fallback(X)
end
function _constraints_fallback(X::LazySet)
return VectorIterator(constraints_list(X))
end
"""
### Algorithm
The default implementation computes all vertices via `vertices_list`.
"""
function vertices(X::LazySet)
return _vertices_fallback(X)
end
function _vertices_fallback(X::LazySet)
return VectorIterator(vertices_list(X))
end
function load_delaunay_MiniQhull()
return quote
import .MiniQhull: delaunay
export delaunay
"""
delaunay(X::LazySet)
Compute the Delaunay triangulation of the given polytopic set.
### Input
- `X` -- polytopic set
- `compute_triangles_3d` -- (optional; default: `false`) flag to compute the 2D
triangulation of a 3D set
### Output
A union of polytopes in vertex representation.
### Notes
This implementation requires the package
[MiniQhull.jl](https://github.com/gridap/MiniQhull.jl), which uses the library
[Qhull](http://www.qhull.org/).
The method works in arbitrary dimension and the requirement is that the list of
vertices of `X` can be obtained.
"""
function delaunay(X::LazySet; compute_triangles_3d::Bool=false)
vlist, connect_mat = delaunay_vlist_connectivity(X;
compute_triangles_3d=compute_triangles_3d)
nsimplices = size(connect_mat, 2)
if compute_triangles_3d
simplices = [VPolytope(vlist[connect_mat[1:3, j]]) for j in 1:nsimplices]
else
simplices = [VPolytope(vlist[connect_mat[:, j]]) for j in 1:nsimplices]
end
return UnionSetArray(simplices)
end
# compute the vertices and the connectivity matrix of the Delaunay triangulation
#
# if the flag `compute_triangles_3d` is set, the resulting matrix still has four
# rows, but the last row has no meaning
function delaunay_vlist_connectivity(X::LazySet;
compute_triangles_3d::Bool=false)
n = dim(X)
@assert !compute_triangles_3d || n == 3 "the `compute_triangles_3d` " *
"option requires 3D inputs"
vlist = vertices_list(X)
m = length(vlist)
coordinates = vcat(vlist...)
flags = compute_triangles_3d ? "qhull Qt" : nothing
connectivity_matrix = delaunay(n, m, coordinates, flags)
return vlist, connectivity_matrix
end
end
end # load_delaunay_MiniQhull
"""
### Algorithm
The default implementation assumes that `X` is polyhedral and returns a
`UnionSetArray` of `HalfSpace`s, i.e., the output is the union of the linear
constraints which are obtained by complementing each constraint of `X`. For any
pair of sets ``(X, Y)`` we have the identity ``(X ∩ Y)^C = X^C ∪ Y^C``. We can
apply this identity for each constraint that defines a polyhedral set.
"""
function complement(X::LazySet)
return UnionSetArray(constraints_list(Complement(X)))
end
"""
project(X::LazySet, block::AbstractVector{Int}, [::Nothing=nothing],
[n]::Int=dim(X); [kwargs...])
Project a set to a given block by using a concrete linear map.
### Input
- `X` -- set
- `block` -- block structure - a vector with the dimensions of interest
- `nothing` -- (default: `nothing`) needed for dispatch
- `n` -- (optional, default: `dim(X)`) ambient dimension of the set `X`
### Output
A set representing the projection of `X` to block `block`.
### Algorithm
We apply the function `linear_map`.
"""
@inline function project(X::LazySet, block::AbstractVector{Int},
::Nothing=nothing, n::Int=dim(X); kwargs...)
return _project_linear_map(X, block, n; kwargs...)
end
@inline function _project_linear_map(X::LazySet{N}, block::AbstractVector{Int},
n::Int=dim(X); kwargs...) where {N}
M = projection_matrix(block, n, N)
return linear_map(M, X)
end
"""
project(X::LazySet, block::AbstractVector{Int}, set_type::Type{T},
[n]::Int=dim(X); [kwargs...]) where {T<:LazySet}
Project a set to a given block and set type, possibly involving an
overapproximation.
### Input
- `X` -- set
- `block` -- block structure - a vector with the dimensions of interest
- `set_type` -- target set type
- `n` -- (optional, default: `dim(X)`) ambient dimension of the set `X`
### Output
A set of type `set_type` representing an overapproximation of the projection of
`X`.
### Algorithm