/
MinkowskiSumArray.jl
226 lines (159 loc) · 5.14 KB
/
MinkowskiSumArray.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
export MinkowskiSumArray,
array
"""
MinkowskiSumArray{N, S<:LazySet{N}} <: LazySet{N}
Type that represents the Minkowski sum of a finite number of sets.
### Fields
- `array` -- array of sets
### Notes
This type assumes that the dimensions of all elements match.
The `ZeroSet` is the neutral element and the `EmptySet` is the absorbing element
for `MinkowskiSumArray`.
The Minkowski sum preserves convexity: if the set arguments are convex, then
their Minkowski sum is convex as well.
"""
struct MinkowskiSumArray{N,S<:LazySet{N}} <: LazySet{N}
array::Vector{S}
end
"""
+(X::LazySet, Xs::LazySet...)
+(Xs::Vector{<:LazySet})
Alias for the n-ary Minkowski sum.
"""
+(X::LazySet, Xs::LazySet...) = MinkowskiSumArray(vcat(X, Xs...))
+(X::LazySet) = X
+(Xs::Vector{<:LazySet}) = MinkowskiSumArray(Xs)
"""
⊕(X::LazySet, Xs::LazySet...)
⊕(Xs::Vector{<:LazySet})
Alias for the n-ary Minkowski sum.
### Notes
The function symbol can be typed via `\\oplus[TAB]`.
"""
⊕(X::LazySet, Xs::LazySet...) = +(X, Xs...)
⊕(Xs::Vector{<:LazySet}) = +(Xs)
isoperationtype(::Type{<:MinkowskiSumArray}) = true
isconvextype(::Type{MinkowskiSumArray{N,S}}) where {N,S} = isconvextype(S)
# constructor for an empty sum with optional size hint and numeric type
function MinkowskiSumArray(n::Int=0, N::Type=Float64)
arr = Vector{LazySet{N}}()
sizehint!(arr, n)
return MinkowskiSumArray(arr)
end
# ZeroSet is the neutral element for MinkowskiSumArray
@neutral(MinkowskiSumArray, ZeroSet)
# EmptySet and Universe are the absorbing elements for MinkowskiSumArray
@absorbing(MinkowskiSumArray, EmptySet)
# @absorbing(MinkowskiSumArray, Universe) # TODO problematic
# add functions connecting MinkowskiSum and MinkowskiSumArray
@declare_array_version(MinkowskiSum, MinkowskiSumArray)
"""
array(msa::MinkowskiSumArray)
Return the array of a Minkowski sum of a finite number of sets.
### Input
- `msa` -- Minkowski sum of a finite number of sets
### Output
The array of a Minkowski sum of a finite number of sets.
"""
function array(msa::MinkowskiSumArray)
return msa.array
end
"""
dim(msa::MinkowskiSumArray)
Return the dimension of a Minkowski sum of a finite number of sets.
### Input
- `msa` -- Minkowski sum of a finite number of sets
### Output
The ambient dimension of the Minkowski sum of a finite number of sets, or `0` if
there is no set in the array.
"""
function dim(msa::MinkowskiSumArray)
return length(msa.array) == 0 ? 0 : dim(msa.array[1])
end
"""
σ(d::AbstractVector, msa::MinkowskiSumArray)
Return a support vector of a Minkowski sum of a finite number of sets in a given
direction.
### Input
- `d` -- direction
- `msa` -- Minkowski sum of a finite number of sets
### Output
A support vector in the given direction.
If the direction has norm zero, the result depends on the summand sets.
"""
function σ(d::AbstractVector, msa::MinkowskiSumArray)
return _σ_msum_array(d, msa.array)
end
@inline function _σ_msum_array(d::AbstractVector{N},
array::AbstractVector{<:LazySet}) where {N}
return sum(σ(d, Xi) for Xi in array)
end
"""
ρ(d::AbstractVector, msa::MinkowskiSumArray)
Evaluate the support function of a Minkowski sum of a finite number of sets in a
given direction.
### Input
- `d` -- direction
- `msa` -- Minkowski sum of a finite number of sets
### Output
The evaluation of the support function in the given direction.
### Algorithm
The support function of the Minkowski sum of multiple sets evaluations to the
sum of the support-function evaluations of each set.
"""
function ρ(d::AbstractVector, msa::MinkowskiSumArray)
return sum(ρ(d, Xi) for Xi in msa.array)
end
"""
isbounded(msa::MinkowskiSumArray)
Check whether a Minkowski sum of a finite number of sets is bounded.
### Input
- `msa` -- Minkowski sum of a finite number of sets
### Output
`true` iff all wrapped sets are bounded.
"""
function isbounded(msa::MinkowskiSumArray)
return all(isbounded, msa.array)
end
function isboundedtype(::Type{<:MinkowskiSumArray{N,S}}) where {N,S}
return isboundedtype(S)
end
"""
isempty(msa::MinkowskiSumArray)
Check whether a Minkowski sum of a finite number of sets is empty.
### Input
- `msa` -- Minkowski sum of a finite number of sets
### Output
`true` iff any of the wrapped sets is empty.
"""
function isempty(msa::MinkowskiSumArray)
return any(isempty, array(msa))
end
"""
center(msa::MinkowskiSumArray)
Return the center of a Minkowski sum of a finite number of centrally-symmetric
sets.
### Input
- `msa` -- Minkowski sum of a finite number of centrally-symmetric sets
### Output
The center of the set.
"""
function center(msa::MinkowskiSumArray)
return sum(center(X) for X in msa)
end
function concretize(msa::MinkowskiSumArray)
a = array(msa)
@assert !isempty(a) "an empty Minkowski sum is not allowed"
X = msa
@inbounds for (i, Y) in enumerate(a)
if i == 1
X = concretize(Y)
else
X = minkowski_sum(X, concretize(Y))
end
end
return X
end
function translate(msa::MinkowskiSumArray, x::AbstractVector)
return MinkowskiSumArray([translate(X, x) for X in array(msa)])
end