-
Notifications
You must be signed in to change notification settings - Fork 32
/
Star.jl
453 lines (319 loc) · 10 KB
/
Star.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
export Star,
center,
basis,
predicate,
intersection!
"""
Star{N, VN<:AbstractVector{N}, MN<:AbstractMatrix{N}, PT<:AbstractPolyhedron{N}} <: AbstractPolyhedron{N}
Generalized star set with a polyhedral predicate, i.e.
```math
X = \\{x ∈ ℝ^n : x = x₀ + ∑_{i=1}^m α_i v_i,~~\\textrm{s.t. } P(α) = ⊤ \\},
```
where ``x₀ ∈ ℝ^n`` is the center, the ``m`` vectors ``v₁, …, vₘ`` form
the basis of the star set, and the combination factors
``α = (α₁, …, αₘ) ∈ ℝ^m`` are the predicate's decision variables,
i.e., ``P : α ∈ ℝ^m → \\{⊤, ⊥\\}`` where the polyhedral predicate
satisfies ``P(α) = ⊤`` if and only if ``A·α ≤ b`` for some fixed
``A ∈ ℝ^{p × m}`` and ``b ∈ ℝ^p``.
### Fields
- `c` -- vector that represents the center
- `V` -- matrix where each column corresponds to a basis vector
- `P` -- polyhedral set that represents the predicate
### Notes
The predicate function is implemented as a conjunction of linear constraints,
i.e., a subtype of `AbstractPolyhedron`. By a slight abuse of notation, the
predicate is also used to denote the subset of ``ℝ^n`` such that
``P(α) = ⊤`` holds.
The ``m`` basis vectors (each one ``n``-dimensional) are stored as the columns
of an ``n × m`` matrix.
We remark that a `Star` is mathematically equivalent to the affine map of the
polyhedral set `P`, with the transformation matrix and translation vector being
`V` and `c`, respectively.
### Examples
This example is drawn from Example 1 in [2]. Consider the two-dimensional plane
``ℝ^2``. Let
```jldoctest star_constructor
julia> V = [[1.0, 0.0], [0.0, 1.0]];
```
be the basis vectors and take
```jldoctest star_constructor
julia> c = [3.0, 3.0];
```
as the center of the star set. Let the predicate be the infinity-norm ball of
radius 1,
```jldoctest star_constructor
julia> P = BallInf(zeros(2), 1.0);
```
We construct the star set ``X = ⟨c, V, P⟩`` as follows:
```jldoctest star_constructor
julia> S = Star(c, V, P)
Star{Float64, Vector{Float64}, Matrix{Float64}, BallInf{Float64, Vector{Float64}}}([3.0, 3.0], [1.0 0.0; 0.0 1.0], BallInf{Float64, Vector{Float64}}([0.0, 0.0], 1.0))
```
We can use getter functions for each component field:
```jldoctest star_constructor
julia> center(S)
2-element Vector{Float64}:
3.0
3.0
julia> basis(S)
2×2 Matrix{Float64}:
1.0 0.0
0.0 1.0
julia> predicate(S)
BallInf{Float64, Vector{Float64}}([0.0, 0.0], 1.0)
```
In this case, the generalized star ``S`` above is equivalent to the rectangle
``T`` below.
```math
T = \\{(x, y) ∈ ℝ^2 : (2 ≤ x ≤ 4) ∧ (2 ≤ y ≤ 4) \\}
```
### References
Star sets as defined here were introduced in [1]; see also [2] for a preliminary
definition. For applications in reachability analysis of neural networks, see
[3].
- [1] Duggirala, P. S., and Mahesh V. *Parsimonious, simulation based verification of linear systems.*
International Conference on Computer Aided Verification. Springer, Cham, 2016.
- [2] Bak S, Duggirala PS. *Simulation-equivalent reachability of large linear systems with inputs.*
In International Conference on Computer Aided Verification
2017 Jul 24 (pp. 401-420). Springer, Cham.
- [3] Tran, H. D., Lopez, D. M., Musau, P., Yang, X., Nguyen, L. V., Xiang, W., & Johnson, T. T. (2019, October).
*Star-based reachability analysis of deep neural networks.*
In International Symposium on Formal Methods (pp. 670-686). Springer, Cham.
"""
struct Star{N,VN<:AbstractVector{N},MN<:AbstractMatrix{N},PT<:AbstractPolyhedron{N}} <:
AbstractPolyhedron{N}
c::VN # center
V::MN # basis
P::PT # predicate
# default constructor with size checks
function Star(c::VN, V::MN,
P::PT) where {N,VN<:AbstractVector{N},MN<:AbstractMatrix{N},
PT<:AbstractPolyhedron{N}}
@assert length(c) == size(V, 1) "a center of length $(length(c)) is " *
"incompatible with basis vectors of length $(size(V, 1))"
@assert dim(P) == size(V, 2) "the number of basis vectors " *
"$(size(V, 2)) is incompatible with the predicate's dimension " *
"$(dim(P))"
return new{N,VN,MN,PT}(c, V, P)
end
end
# constructor from list of generators
function Star(c::VN, vlist::AbstractVector{VN},
P::PT) where {N,VN<:AbstractVector{N},PT<:AbstractPolyhedron{N}}
V = to_matrix(vlist, length(c))
return Star(c, V, P)
end
# analogous AffineMap type
const STAR{N,VN<:AbstractVector{N},
MN<:AbstractMatrix{N},
PT<:AbstractPolyhedron{N}} = AffineMap{N,PT,N,MN,VN}
isoperationtype(::Type{<:Star}) = false
"""
center(X::Star)
Return the center of a star.
### Input
- `X` -- star
### Output
The center of the star.
"""
center(X::Star) = X.c
"""
basis(X::Star)
Return the basis vectors of a star.
### Input
- `X` -- star
### Output
A matrix where each column is a basis vector of the star.
"""
basis(X::Star) = X.V
"""
predicate(X::Star)
Return the predicate of a star.
### Input
- `X` -- star
### Output
A polyhedral set representing the predicate of the star.
"""
predicate(X::Star) = X.P
"""
dim(X::Star)
Return the dimension of a star.
### Input
- `X` -- star
### Output
The ambient dimension of a star.
"""
function dim(X::Star)
return length(X.c)
end
"""
σ(d::AbstractVector, X::Star)
Return a support vector of a star.
### Input
- `d` -- direction
- `X` -- star
### Output
A support vector in the given direction.
"""
function σ(d::AbstractVector, X::Star)
A = basis(X)
return A * σ(At_mul_B(A, d), predicate(X)) + center(X)
end
"""
ρ(d::AbstractVector, X::Star)
Evaluate the support function of a star.
### Input
- `d` -- direction
- `X` -- star
### Output
The support function in the given direction.
"""
function ρ(d::AbstractVector, X::Star)
return ρ(At_mul_B(basis(X), d), predicate(X)) + dot(d, center(X))
end
"""
an_element(X::Star)
Return some element of a star.
### Input
- `X` -- star
### Output
An element of the star.
### Algorithm
We apply the affine map to the result of `an_element` on the predicate.
"""
function an_element(X::Star)
return basis(X) * an_element(predicate(X)) + center(X)
end
"""
isempty(X::Star)
Check whether a star is empty.
### Input
- `X` -- star
### Output
`true` iff the predicate is empty.
"""
function isempty(X::Star)
return isempty(predicate(X))
end
"""
isbounded(X::Star; cond_tol::Number=DEFAULT_COND_TOL)
Check whether a star is bounded.
### Input
- `X` -- star
- `cond_tol` -- (optional) tolerance of matrix condition (used to check whether
the basis matrix is invertible)
### Output
`true` iff the star is bounded.
### Algorithm
See [`isbounded(::AbstractAffineMap)`](@ref).
"""
function isbounded(X::Star; cond_tol::Number=DEFAULT_COND_TOL)
am = convert(STAR, X)
return isbounded(am; cond_tol=cond_tol)
end
"""
∈(v::AbstractVector, X::Star)
Check whether a given point is contained in a star.
### Input
- `v` -- point/vector
- `X` -- star
### Output
`true` iff ``v ∈ X``.
### Algorithm
The implementation is identical to
[`∈(::AbstractVector, ::AbstractAffineMap)`](@ref).
"""
function ∈(x::AbstractVector, X::Star)
return basis(X) \ (x - center(X)) ∈ predicate(X)
end
"""
vertices_list(X::Star; apply_convex_hull::Bool=true)
Return the list of vertices of a star.
### Input
- `X` -- star
- `apply_convex_hull` -- (optional, default: `true`) if `true`, apply the convex
hull operation to the list of vertices of the star
### Output
A list of vertices.
### Algorithm
See [`vertices_list(::AbstractAffineMap)`](@ref).
"""
function vertices_list(X::Star; apply_convex_hull::Bool=true)
am = convert(STAR, X)
return vertices_list(am; apply_convex_hull=apply_convex_hull)
end
"""
constraints_list(X::Star)
Return the list of constraints of a star.
### Input
- `X` -- star
### Output
The list of constraints of the star.
### Algorithm
See [`constraints_list(::AbstractAffineMap)`](@ref).
"""
function constraints_list(X::Star)
am = convert(STAR, X)
return constraints_list(am)
end
"""
linear_map(M::AbstractMatrix, X::Star)
Return the linear map of a star.
### Input
- `M` -- matrix
- `X` -- star
### Output
The star obtained by applying `M` to `X`.
"""
function linear_map(M::AbstractMatrix, X::Star)
c′ = M * X.c
V′ = M * X.V
P′ = X.P
return Star(c′, V′, P′)
end
"""
affine_map(M::AbstractMatrix, X::Star, v::AbstractVector)
Return the affine map of a star.
### Input
- `M` -- matrix
- `X` -- star
- `v` -- vector
### Output
The star obtained by applying the affine map with matrix `M` and displacement
`v` to `X`.
"""
function affine_map(M::AbstractMatrix, X::Star, v::AbstractVector)
c′ = M * X.c + v
V′ = M * X.V
P′ = X.P
return Star(c′, V′, P′)
end
"""
rand(::Type{Star}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing)
Create a random star.
### Input
- `Star` -- type for dispatch
- `N` -- (optional, default: `Float64`) numeric type
- `dim` -- (optional, default: 2) dimension
- `rng` -- (optional, default: `GLOBAL_RNG`) random number generator
- `seed` -- (optional, default: `nothing`) seed for reseeding
- `P` -- (optional, default: a random `HPolytope`) predicate
### Output
A random star.
### Algorithm
By default we generate a random `HPolytope` of dimension `dim` as predicate.
Alternatively the predicate can be passed.
All numbers are normally distributed with mean 0 and standard deviation 1.
"""
function rand(::Type{Star};
N::Type{<:Real}=Float64,
dim::Int=2,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int,Nothing}=nothing,
P::AbstractPolyhedron=rand(HPolytope; N=N, dim=dim, rng=rng, seed=seed))
rng = reseed!(rng, seed)
c = randn(rng, N, dim)
V = randn(rng, N, dim, LazySets.dim(P))
return Star(c, V, P)
end