-
Notifications
You must be signed in to change notification settings - Fork 32
/
UnionSetArray.jl
289 lines (207 loc) · 6.98 KB
/
UnionSetArray.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
export UnionSetArray,
array
"""
UnionSetArray{N, S<:LazySet{N}} <: LazySet{N}
Type that represents the set union of a finite number of sets.
### Fields
- `array` -- array of sets
### Notes
The union of convex sets is typically not convex.
"""
struct UnionSetArray{N,S<:LazySet{N}} <: LazySet{N}
array::Vector{S}
end
isoperationtype(::Type{<:UnionSetArray}) = true
isconvextype(::Type{<:UnionSetArray}) = false
# constructor for an empty union with optional size hint and numeric type
function UnionSetArray(n::Int=0, N::Type=Float64)
arr = Vector{LazySet{N}}()
sizehint!(arr, n)
return UnionSetArray(arr)
end
# EmptySet is the neutral element for UnionSetArray
@neutral(UnionSetArray, EmptySet)
# Universe is the absorbing element for UnionSetArray
@absorbing(UnionSetArray, Universe)
# add functions connecting UnionSet and UnionSetArray
@declare_array_version(UnionSet, UnionSetArray)
concretize(cup::UnionSetArray) = UnionSetArray([concretize(X) for X in array(cup)])
"""
dim(cup::UnionSetArray)
Return the dimension of the union of a finite number of sets.
### Input
- `cup` -- union of a finite number of sets
### Output
The ambient dimension of the union of a finite number of sets, or `0` if there
is no set in the array.
"""
function dim(cup::UnionSetArray)
return length(cup.array) == 0 ? 0 : dim(cup.array[1])
end
"""
array(cup::UnionSetArray)
Return the array of the union of a finite number of sets.
### Input
- `cup` -- union of a finite number of sets
### Output
The array of the union.
"""
function array(cup::UnionSetArray)
return cup.array
end
"""
σ(d::AbstractVector, cup::UnionSetArray; [algorithm]="support_vector")
Return a support vector of the union of a finite number of sets in a given
direction.
### Input
- `d` -- direction
- `cup` -- union of a finite number of sets
- `algorithm` -- (optional, default: "support_vector"): the algorithm to compute
the support vector; if "support_vector", use the support
vector of each argument; if "support_function", use the support
function of each argument and evaluate the support vector of
only one of them
### Output
A support vector in the given direction.
### Algorithm
The support vector of the union of a finite number of sets ``X₁, X₂, ...`` can
be obtained as the vector that maximizes the support function, i.e., it is
sufficient to find the ``\\argmax([ρ(d, X₂), ρ(d, X₂), ...])`` and evaluate its
support vector.
The default implementation, with option `algorithm="support_vector"`, computes
the support vector of all ``X₁, X₂, ...`` and then compares the support function
using the dot product.
If the support function can be computed more efficiently, the alternative
implementation `algorithm="support_function"` can be used, which evaluates the
support function of each set directly and then calls only the support vector of
one of the ``Xᵢ``.
"""
function σ(d::AbstractVector, cup::UnionSetArray; algorithm="support_vector")
arr = array(cup)
if algorithm == "support_vector"
return _σ_union(d, arr)
elseif algorithm == "support_function"
m = argmax(i -> ρ(d, @inbounds arr[i]), eachindex(arr))
return σ(d, arr[m])
else
error("algorithm $algorithm for the support vector of a " *
"`UnionSetArray` is unknown")
end
end
function _σ_union(d::AbstractVector, sets)
σmax = d
N = eltype(d)
ρmax = N(-Inf)
for Xi in sets
σX = σ(d, Xi)
ρX = dot(d, σX)
if ρX > ρmax
ρmax = ρX
σmax = σX
end
end
return σmax
end
"""
ρ(d::AbstractVector, cup::UnionSetArray)
Evaluate the support function of the union of a finite number of sets in a given
direction.
### Input
- `d` -- direction
- `cup` -- union of a finite number of sets
### Output
The evaluation of the support function in the given direction.
### Algorithm
The support function of the union of a finite number of sets ``X₁, X₂, ...``
can be obtained as the maximum of ``ρ(d, X₂), ρ(d, X₂), ...``.
"""
function ρ(d::AbstractVector, cup::UnionSetArray)
return maximum(Xi -> ρ(d, Xi), array(cup))
end
"""
an_element(cup::UnionSetArray)
Return some element of the union of a finite number of sets.
### Input
- `cup` -- union of a finite number of sets
### Output
An element in the union of a finite number of sets.
### Algorithm
We use `an_element` on the first non-empty wrapped set.
"""
function an_element(cup::UnionSetArray)
for Xi in cup
if !isempty(Xi)
return an_element(Xi)
end
end
return error("an empty set does not have any element")
end
"""
∈(x::AbstractVector, cup::UnionSetArray)
Check whether a given point is contained in the union of a finite number of
sets.
### Input
- `x` -- point/vector
- `cup` -- union of a finite number of sets
### Output
`true` iff ``x ∈ cup``.
"""
function ∈(x::AbstractVector, cup::UnionSetArray)
return any(X -> x ∈ X, array(cup))
end
"""
isempty(cup::UnionSetArray)
Check whether the union of a finite number of sets is empty.
### Input
- `cup` -- union of a finite number of sets
### Output
`true` iff the union is empty.
"""
function isempty(cup::UnionSetArray)
return all(isempty, array(cup))
end
"""
isbounded(cup::UnionSetArray)
Check whether the union of a finite number of sets is bounded.
### Input
- `cup` -- union of a finite number of sets
### Output
`true` iff the union is bounded.
"""
function isbounded(cup::UnionSetArray)
return all(isbounded, array(cup))
end
function isboundedtype(::Type{<:UnionSetArray{N,S}}) where {N,S}
return isboundedtype(S)
end
"""
vertices_list(cup::UnionSetArray; [apply_convex_hull]::Bool=false,
[backend]=nothing)
Return a list of vertices of the union of a finite number of sets.
### Input
- `cup` -- union of a finite number of sets
- `apply_convex_hull` -- (optional, default: `false`) if `true`, post-process
the vertices using a convex-hull algorithm
- `backend` -- (optional, default: `nothing`) backend for computing
the convex hull (see argument `apply_convex_hull`)
### Output
A list of vertices, possibly reduced to the list of vertices of the convex hull.
"""
function vertices_list(cup::UnionSetArray;
apply_convex_hull::Bool=false,
backend=nothing)
vlist = vcat([vertices_list(Xi) for Xi in cup]...)
if apply_convex_hull
convex_hull!(vlist; backend=backend)
end
return vlist
end
function linear_map(M::AbstractMatrix, cup::UnionSetArray)
return UnionSetArray([linear_map(M, X) for X in cup])
end
function project(cup::UnionSetArray, block::AbstractVector{Int}; kwargs...)
return UnionSetArray([project(X, block; kwargs...) for X in cup])
end
function translate(cup::UnionSetArray, v::AbstractVector)
return UnionSetArray([translate(X, v) for X in cup])
end