/
overapproximate.jl
659 lines (525 loc) · 21.9 KB
/
overapproximate.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
using LazySets: block_to_dimension_indices,
substitute_blocks,
fast_interval_pow,
get_constrained_lowdimset
"""
overapproximate(X::S, ::Type{S}, args...) where {S<:LazySet}
Overapproximating a set of type `S` with type `S` is a no-op.
### Input
- `X` -- set
- `Type{S}` -- target set type
- `args` -- further arguments (ignored)
- `kwargs` -- further keyword arguments (ignored)
### Output
The input set.
"""
function overapproximate(X::S, ::Type{S}, args...; kwargs...) where {S<:LazySet}
return X
end
"""
overapproximate(S::LazySet, T::Type{<:LazySet}, [args]...; [kwargs]...)
Default overapproximation method that falls back to `convert`.
### Input
- `X` -- set
- `Type{S}` -- target set type
- `args` -- further arguments
- `kwargs` -- further keyword arguments
### Output
The result of `convert`, or a `MethodError` if no such method exists.
"""
function overapproximate(X::T1, T2::Type{<:LazySet}, args...; kwargs...) where {T1<:LazySet}
return convert(T2, X, args...; kwargs...)
end
"""
overapproximate(S::LazySet)
Alias for `overapproximate(S, Hyperrectangle)` resp. `box_approximation(S)`.
"""
overapproximate(S::LazySet) = box_approximation(S)
"""
overapproximate(S::LazySet, ::Type{<:Hyperrectangle})
Alias for `box_approximation(S)`.
"""
function overapproximate(S::LazySet, ::Type{<:Hyperrectangle})
return box_approximation(S)
end
"""
overapproximate(S::LazySet, ::Type{<:BallInf})
Alias for `ballinf_approximation(S)`.
"""
function overapproximate(S::LazySet, ::Type{<:BallInf})
return ballinf_approximation(S)
end
"""
overapproximate(S::LazySet{N},
::Type{<:HPolygon},
[ε]::Real=Inf) where {N}
Overapproximate a given 2D set using iterative refinement.
### Input
- `S` -- two-dimensional bounded set
- `HPolygon` -- target set type
- `ε` -- (optional, default: `Inf`) error tolerance
- `prune` -- (optional, default: `true`) flag for removing redundant
constraints in the end
### Output
A polygon in constraint representation.
### Notes
The result is always a convex overapproximation of the input set.
If no error tolerance ε is given, or is `Inf`, the result is a box-shaped
polygon. For convex input sets, the result is an ε-close polygonal
overapproximation with respect to the Hausdorff distance.
"""
function overapproximate(S::LazySet{N},
::Type{<:HPolygon},
ε::Real=Inf;
prune::Bool=true) where {N}
@assert dim(S) == 2 "epsilon-close overapproximation is only available " *
"for two-dimensional sets"
if ε == Inf
constraints = Vector{HalfSpace{N,Vector{N}}}(undef, 4)
constraints[1] = HalfSpace(DIR_EAST(N), ρ(DIR_EAST(N), S))
constraints[2] = HalfSpace(DIR_NORTH(N), ρ(DIR_NORTH(N), S))
constraints[3] = HalfSpace(DIR_WEST(N), ρ(DIR_WEST(N), S))
constraints[4] = HalfSpace(DIR_SOUTH(N), ρ(DIR_SOUTH(N), S))
return HPolygon(constraints; sort_constraints=false)
else
P = overapproximate_hausdorff(S, ε)
if prune
remove_redundant_constraints!(P)
end
return P
end
end
# error messages for dispatch on HPolytope
function overapproximate(X::S, ::Type{<:HPolytope}) where {S<:LazySet}
if dim(X) == 2
throw(ArgumentError("epsilon-close approximation is only available " *
"using polygons in constraint representation; try " *
"`overapproximate(X, HPolygon)`"))
else
throw(ArgumentError("epsilon-close approximation is only available " *
"for two-dimensional sets; try " *
"`overapproximate(X, HPolytope, dirs)` where `dirs` are " *
"template directions, e.g., `BoxDirections` or `OctDirections`"))
end
end
"""
overapproximate(S::LazySet, ε::Real)
Alias for `overapproximate(S, HPolygon, ε)`.
"""
function overapproximate(S::LazySet, ε::Real)
return overapproximate(S, HPolygon, ε)
end
# special case: overapproximation of empty set
overapproximate(∅::EmptySet, args...; kwargs...) = ∅
# disambiguation
for ST in LazySets.subtypes(LazySet, true)
if ST == HPolygon # must be defined separately below with extra argument
continue
end
@eval overapproximate(∅::EmptySet, ::Type{<:$ST}) = ∅
end
overapproximate(∅::EmptySet, ::Type{<:LazySet}, args...; kwargs...) = ∅
overapproximate(∅::EmptySet, ::Real) = ∅
overapproximate(∅::EmptySet, ::Type{<:HPolygon}, ::Real=Inf; kwargs...) = ∅
function overapproximate(∅::EmptySet, ::Type{<:Zonotope},
::Union{AbstractDirections,Type{<:AbstractDirections}};
kwargs...)
return ∅
end
"""
overapproximate(X::LazySet{N}, dirs::AbstractDirections;
[prune]::Bool=true) where {N}
Overapproximate a (possibly unbounded) set with template directions.
### Input
- `X` -- set
- `dirs` -- directions
- `prune` -- (optional, default: `true`) flag for removing redundant constraints
### Output
A polyhedron overapproximating the set `X` with the directions from `dirs`. The
overapproximation is computed using the support function.
The result is an `HPolytope` if it is bounded and otherwise an `HPolyhedron`.
"""
function overapproximate(X::LazySet, dirs::AbstractDirections; prune::Bool=true)
H = _overapproximate_directions(X, dirs, prune)
# if input is bounded and directions are bounding => output is bounded
# otherwise, check boundedness of the output
if (isbounded(X) && isbounding(dirs)) || _isbounded_stiemke(H)
return HPolytope(H; check_boundedness=false)
else
return HPolyhedron(H)
end
end
function _overapproximate_directions(X::LazySet{N},
dirs::AbstractDirections{N,VN},
prune::Bool) where {N,VN}
H = Vector{HalfSpace{N,VN}}()
sizehint!(H, length(dirs))
for d in dirs
sf = ρ(d, X)
if !isinf(sf)
push!(H, HalfSpace(d, sf))
end
end
if prune
remove_redundant_constraints!(H) || throw(ArgumentError("unable to " *
"remove redundant constraints"))
end
return H
end
# alias with HPolytope type as second argument
function overapproximate(X::LazySet, ::Type{<:HPolytope},
dirs::AbstractDirections; prune::Bool=true)
P = overapproximate(X, dirs; prune=prune)
P isa HPolytope || throw(ArgumentError("cannot overapproximate with an " *
"`HPolytope` because the set is unbounded; try using an `HPolyhedron`"))
return P
end
# alias with HPolyhedron type as second argument
function overapproximate(X::LazySet, ::Type{<:HPolyhedron},
dirs::AbstractDirections; prune::Bool=true)
H = _overapproximate_directions(X, dirs, prune)
return HPolyhedron(H)
end
# disambiguation
overapproximate(∅::EmptySet, ::Type{<:HPolytope}, dirs::AbstractDirections;
prune::Bool=true) = ∅
overapproximate(∅::EmptySet, ::Type{<:HPolyhedron}, dirs::AbstractDirections;
prune::Bool=true) = ∅
"""
overapproximate(X::LazySet{N}, dirs::Type{<:AbstractDirections}) where {N}
Overapproximate a set with template directions.
### Input
- `X` -- set
- `dirs` -- type of direction representation
### Output
A polyhedron overapproximating the set `X` with the directions from `dirs`.
The result is an `HPolytope` if it is bounded and otherwise an `HPolyhedron`.
"""
function overapproximate(X::LazySet{N},
dirs::Type{<:AbstractDirections}; kwargs...) where {N}
return overapproximate(X, dirs{N}(dim(X)); kwargs...)
end
# disambiguation
overapproximate(∅::EmptySet, dirs::Type{<:AbstractDirections}; kwargs...) = ∅
overapproximate(∅::EmptySet, dirs::AbstractDirections; prune::Bool=true) = ∅
function overapproximate_cap_helper(X::LazySet,
P::AbstractPolyhedron, # polyhedron
dirs::AbstractDirections;
kwargs...)
Hi = constraints_list(P)
m = length(Hi)
N = promote_type(eltype(X), eltype(P))
constraints = Vector{HalfSpace{N,Vector{N}}}() # TODO: use directions type, see #2031
sizehint!(constraints, length(dirs))
return_type = HPolytope
for di in dirs
ρ_X_Hi_min = ρ(di, X ∩ Hi[1], kwargs...)
for i in 2:m
ρ_X_Hi = ρ(di, X ∩ Hi[i], kwargs...)
if ρ_X_Hi < ρ_X_Hi_min
ρ_X_Hi_min = ρ_X_Hi
end
end
if isinf(ρ_X_Hi_min)
# unbounded in this direction => return a polyhedron later
return_type = HPolyhedron
else
push!(constraints, _normal_Vector(HalfSpace(di, ρ_X_Hi_min))) # TODO: remove vector
end
end
return return_type(constraints)
end
"""
overapproximate(cap::Intersection{N, <:LazySet, <:AbstractPolyhedron},
dirs::AbstractDirections;
kwargs...
) where {N}
Overapproximate the intersection between a set and a polyhedron given a set of
template directions.
### Input
- `cap` -- intersection of a set and a polyhedron
- `dirs` -- template directions
- `kwargs` -- additional arguments that are passed to the support function
algorithm
### Output
A polytope or polyhedron in H-representation such that the normal direction of
each half-space is given by an element of `dirs`.
### Algorithm
Let `di` be a direction drawn from the set of template directions `dirs`.
Let `X` be the set and let `P` be the polyhedron. We overapproximate the set
`X ∩ H` with a polytope or polyhedron in constraint representation using a given
set of template directions `dirs`.
The idea is to solve the univariate optimization problem `ρ(di, X ∩ Hi)` for
each half-space of the set `P` and then take the minimum.
This gives an overapproximation of the exact support function.
This algorithm is inspired from [1].
### Notes
This method relies on having available the `constraints_list` of the polyhedron
`P`.
This method may return a non-empty set even if the original set is empty.
[1] G. Frehse, R. Ray. *Flowpipe-Guard Intersection for Reachability
Computations with Support Functions*. ADHS 2012.
"""
function overapproximate(cap::Intersection{N, # TODO use better mechanism to detect polyhedral set
<:LazySet,
<:AbstractPolyhedron},
dirs::AbstractDirections;
kwargs...) where {N}
return overapproximate_cap_helper(first(cap), second(cap), dirs; kwargs...)
end
# symmetric method
function overapproximate(cap::Intersection{N,
<:AbstractPolyhedron,
<:LazySet},
dirs::AbstractDirections;
kwargs...) where {N}
return overapproximate_cap_helper(second(cap), first(cap), dirs; kwargs...)
end
# disambiguation
function overapproximate(cap::Intersection{N,
<:AbstractPolyhedron,
<:AbstractPolyhedron},
dirs::AbstractDirections;
kwargs...) where {N}
# important: the result may not be a polytope!
return overapproximate_cap_helper(first(cap), second(cap), dirs; kwargs...)
end
# disambiguation
function overapproximate(cap::Intersection{N,
<:AbstractPolytope,
<:AbstractPolyhedron},
dirs::AbstractDirections;
kwargs...) where {N}
return overapproximate_cap_helper(first(cap), second(cap), dirs; kwargs...)
end
# symmetric method
function overapproximate(cap::Intersection{N,
<:AbstractPolyhedron,
<:AbstractPolytope},
dirs::AbstractDirections;
kwargs...) where {N}
return overapproximate_cap_helper(second(cap), first(cap), dirs; kwargs...)
end
# disambiguation
function overapproximate(cap::Intersection{N,
<:AbstractPolytope,
<:AbstractPolytope},
dirs::AbstractDirections;
kwargs...) where {N}
return overapproximate_cap_helper(first(cap), second(cap), dirs; kwargs...)
end
"""
overapproximate(cap::Intersection{N, <:HalfSpace, <:AbstractPolytope},
dirs::AbstractDirections;
[kwargs]...
) where {N}
Overapproximate the intersection between a half-space and a polytope given a set
of template directions.
### Input
- `cap` -- intersection of a half-space and a polytope
- `dirs` -- template directions
- `kwargs` -- additional arguments that are passed to the support function
algorithm
### Output
A polytope in H-representation such that the normal direction of each half-space
is given by an element of `dirs`.
"""
function overapproximate(cap::Intersection{N,
<:HalfSpace,
<:AbstractPolytope},
dirs::AbstractDirections;
kwargs...) where {N}
H = HPolytope{N,Vector{N}}()
c = H.constraints
push!(c, _normal_Vector(first(cap)))
append!(c, _normal_Vector(second(cap)))
return overapproximate(H, dirs; kwargs...)
end
# symmetric method
function overapproximate(cap::Intersection{N,
<:AbstractPolytope,
<:HalfSpace},
dirs::AbstractDirections;
kwargs...) where {N}
return overapproximate(swap(cap), dirs; kwargs...)
end
function overapproximate(P::SimpleSparsePolynomialZonotope,
::Type{<:UnionSetArray{Zonotope}};
nsdiv=10, partition=nothing)
q = nparams(P)
dom = IA.IntervalBox(IA.interval(-1, 1), q)
cells = IA.mince(dom, isnothing(partition) ? nsdiv : partition)
return UnionSetArray([overapproximate(P, Zonotope, c) for c in cells])
end
function overapproximate(P::SparsePolynomialZonotope,
T::Type{<:UnionSetArray{Zonotope}};
nsdiv=10, partition=nothing)
SSPZ = convert(SimpleSparsePolynomialZonotope, P)
return overapproximate(SSPZ, T; nsdiv=nsdiv, partition=partition)
end
"""
overapproximate(Z::AbstractZonotope, ::Type{<:HParallelotope},
[indices]=1:dim(Z))
Overapproximate a zonotopic set with a parallelotope in constraint
representation.
### Input
- `Z` -- zonotopic set
- `HParallelotope` -- target set type
- `indices` -- (optional; default: `1:dim(Z)`) generator indices selected
when constructing the parallelotope
### Output
An overapproximation of the given zonotopic set using a parallelotope.
### Algorithm
The algorithm is based on Proposition 8 discussed in Section 5 of [1].
[1] Althoff, M., Stursberg, O., & Buss, M. (2010). *Computing reachable sets of
hybrid systems using a combination of zonotopes and polytopes*. Nonlinear
analysis: hybrid systems, 4(2), 233-249.
"""
function overapproximate(Z::AbstractZonotope, ::Type{<:HParallelotope},
indices=1:dim(Z))
Zred = _overapproximate_hparallelotope(Z, indices)
return convert(HParallelotope, Zred)
end
function _overapproximate_hparallelotope(Z::AbstractZonotope, indices=1:dim(Z))
length(indices) == dim(Z) || throw(ArgumentError("the number of " *
"generator indices is $(length(indices)), but it was expected to be " *
"$(dim(Z))"))
p, n = ngens(Z), dim(Z)
if p == n
return Z
elseif p < n
error("the zonotope order is $(order(Z)) but it should be at least 1")
end
G = genmat(Z)
Γ = G[:, indices]
□Γ⁻¹Z = box_approximation(linear_map(inv(Γ), Z))
return linear_map(Γ, □Γ⁻¹Z)
end
"""
overapproximate(X::Intersection{N, <:AbstractZonotope, <:Hyperplane},
dirs::AbstractDirections) where {N}
Overapproximate the intersection between a zonotopic set and a hyperplane with a
polyhedron or polytope using the given directions.
### Input
- `X` -- intersection between a zonotopic set and a hyperplane
- `dirs` -- type of direction representation
### Output
An overapproximation of the intersection between a zonotopic set and a
hyperplane. If the directions are bounding, the result is an `HPolytope`,
otherwise the result is an `HPolyhedron`.
### Algorithm
This function implements [Algorithm 8.1, 1].
[1] Colas Le Guernic. *Reachability Analysis of Hybrid Systems with Linear
continuous dynamics* (Doctoral dissertation). 2009.
"""
function overapproximate(X::Intersection{N,<:AbstractZonotope,<:Hyperplane},
dirs::AbstractDirections) where {N}
dim(X) == dim(dirs) || throw(ArgumentError("the dimension of the set " *
"$(dim(X)) does not match the dimension of the directions $(dim(dirs))"))
Z, G = first(X), second(X)
if isdisjoint(Z, G)
return EmptySet{N}(dim(Z))
end
n = G.a # normal vector to the hyperplane
γ = G.b # displacement of the hyperplane
Lᵧ = Line2D([one(N), zero(N)], γ) # line (x, y) : x = γ
constraints = Vector{HalfSpace{N,eltype(dirs)}}()
for l in dirs
Πₙₗ = vcat(n', l') # projection map
πZₙₗ = linear_map(Πₙₗ, Z)
ρₗ = LazySets._bound_intersect_2D(πZₙₗ, Lᵧ)
push!(constraints, HalfSpace(l, ρₗ))
end
T = isbounding(dirs) ? HPolytope : HPolyhedron
return T(constraints)
end
# symmetric method
function overapproximate(X::Intersection{N,<:Hyperplane,<:AbstractZonotope},
dirs::AbstractDirections) where {N}
return overapproximate(second(X) ∩ first(X), dirs)
end
# overload on direction type
function overapproximate(X::Intersection{N,<:AbstractZonotope,
<:Hyperplane}, dirs::Type{<:AbstractDirections}) where {N}
return overapproximate(X, dirs(dim(X)))
end
# symmetric method
function overapproximate(X::Intersection{N,<:Hyperplane,<:AbstractZonotope},
dirs::Type{<:AbstractDirections}) where {N}
return overapproximate(second(X) ∩ first(X), dirs(dim(X)))
end
"""
overapproximate(QM::QuadraticMap{N, <:SparsePolynomialZonotope},
::Type{<:SparsePolynomialZonotope}) where {N}
Overapproximate a quadratic map of a sparse polynomial zonotope with a sparse
polynomial zonotope.
### Input
- `QM` -- quadratic map of a sparse polynomial zonotope
- `SparsePolynomialZonotope` -- target type
### Output
A sparse polynomial zonotope overapproximating the quadratic map of a sparse
polynomial zonotope.
### Algorithm
This method implements Proposition 13 of [1].
[1] N. Kochdumper and M. Althoff. *Sparse Polynomial Zonotopes: A Novel Set
Representation for Reachability Analysis*. Transactions on Automatic Control
2021.
"""
function overapproximate(QM::QuadraticMap{N,<:SparsePolynomialZonotope},
::Type{<:SparsePolynomialZonotope}) where {N}
PZ = QM.X
Q = QM.Q
p = nparams(PZ)
q = ngens_indep(PZ)
c = center(PZ)
G = genmat_dep(PZ)
GI = genmat_indep(PZ)
Ĝ = hcat(G, GI)
Ê = cat(expmat(PZ), Matrix(1 * I, q, q); dims=(1, 2))
PZ1 = SimpleSparsePolynomialZonotope(c, Ĝ, Ê)
PZbar = quadratic_map(Q, PZ1)
c̄ = center(PZbar)
Ē = expmat(PZbar)
Ḡ = genmat(PZbar)
K = [!iszero(Ei[(p + 1):end]) for Ei in eachcol(Ē)]
H = .!K
PZK = SimpleSparsePolynomialZonotope(c̄, Ḡ[:, K], Ē[:, K])
Z = overapproximate(PZK, Zonotope)
cz = center(Z)
Gz = genmat(Z)
return SparsePolynomialZonotope(cz, Ḡ[:, H], Gz, Ē[1:p, H], indexvector(PZ))
end
# function to be loaded by Requires
function load_paving_overapproximation()
return quote
using .IntervalConstraintProgramming: Paving
"""
overapproximate(p::Paving{L, N}, dirs::AbstractDirections{N, VN})
where {L, N, VN}
Overapproximate a Paving-type set representation with a polyhedron in constraint
representation.
### Input
- `p` -- paving
- `dirs` -- template directions
### Output
An overapproximation of a paving using a polyhedron in constraint representation
(`HPolyhedron`) with constraints in direction `dirs`.
### Algorithm
This function takes the union of the elements at the boundary of `p`, first
converted into hyperrectangles, and then calculates the support function of the
set along each direction in dirs, to compute the `HPolyhedron` constraints.
This algorithm requires the IntervalConstraintProgramming package.
"""
function overapproximate(p::Paving{L,N}, dirs::AbstractDirections{N,VN}) where {L,N,VN}
# enclose outer approximation
Uouter = UnionSetArray(convert.(Hyperrectangle, p.boundary))
constraints = [HalfSpace(d, ρ(d, Uouter)) for d in dirs]
return HPolyhedron(constraints)
end
# alias with HPolyhedron type as second argument
function overapproximate(p::Paving{L,N}, ::Type{<:HPolyhedron},
dirs::AbstractDirections{N,VN}) where {L,N,VN}
return overapproximate(p, dirs)
end
end
end # quote / load_paving_overapproximation