/
Ball1.jl
276 lines (198 loc) · 6.42 KB
/
Ball1.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
import Base: rand,
∈
export Ball1
"""
Ball1{N<:Real} <: AbstractCentrallySymmetricPolytope{N}
Type that represents a ball in the 1-norm (also known as the Manhattan norm).
The ball is also known as a
[cross-polytope](https://en.wikipedia.org/wiki/Cross-polytope).
It is defined as the set
```math
\\mathcal{B}_1^n(c, r) = \\{ x ∈ \\mathbb{R}^n : ∑_{i=1}^n |c_i - x_i| ≤ r \\},
```
where ``c ∈ \\mathbb{R}^n`` is its center and ``r ∈ \\mathbb{R}_+`` its radius.
### Fields
- `center` -- center of the ball as a real vector
- `radius` -- radius of the ball as a scalar (``≥ 0``)
### Examples
Unit ball in the 1-norm in the plane:
```jldoctest ball1_constructor
julia> B = Ball1(zeros(2), 1.)
Ball1{Float64}([0.0, 0.0], 1.0)
julia> dim(B)
2
```
We evaluate the support vector in the East direction:
```jldoctest ball1_constructor
julia> σ([0.,1], B)
2-element Array{Float64,1}:
0.0
1.0
```
"""
struct Ball1{N<:Real} <: AbstractCentrallySymmetricPolytope{N}
center::Vector{N}
radius::N
# default constructor with domain constraint for radius
function Ball1{N}(center::Vector{N}, radius::N) where {N<:Real}
@assert radius >= zero(N) "radius must not be negative"
return new{N}(center, radius)
end
end
# convenience constructor without type parameter
Ball1(center::Vector{N}, radius::N) where {N<:Real} = Ball1{N}(center, radius)
# --- AbstractCentrallySymmetric interface functions ---
"""
center(B::Ball1{N})::Vector{N} where {N<:Real}
Return the center of a ball in the 1-norm.
### Input
- `B` -- ball in the 1-norm
### Output
The center of the ball in the 1-norm.
"""
function center(B::Ball1{N})::Vector{N} where {N<:Real}
return B.center
end
# --- AbstractPolytope interface functions ---
"""
vertices_list(B::Ball1{N})::Vector{Vector{N}} where {N<:Real}
Return the list of vertices of a ball in the 1-norm.
### Input
- `B` -- ball in the 1-norm
### Output
A list containing the vertices of the ball in the 1-norm.
"""
function vertices_list(B::Ball1{N})::Vector{Vector{N}} where {N<:Real}
# fast evaluation if B has radius 0
if iszero(B.radius)
return [B.center]
end
vertices = Vector{Vector{N}}()
sizehint!(vertices, 2 * dim(B))
v = copy(B.center)
for i in 1:dim(B)
v[i] += B.radius
push!(vertices, copy(v))
v[i] = B.center[i] - B.radius
push!(vertices, copy(v))
v[i] = B.center[i]
end
return vertices
end
# --- LazySet interface functions ---
"""
σ(d::AbstractVector{N}, B::Ball1{N}) where {N<:Real}
Return the support vector of a ball in the 1-norm in a given direction.
### Input
- `d` -- direction
- `B` -- ball in the 1-norm
### Output
Support vector in the given direction.
"""
function σ(d::AbstractVector{N}, B::Ball1{N}) where {N<:Real}
res = copy(B.center)
imax = argmax(abs.(d))
res[imax] += sign(d[imax]) * B.radius
return res
end
"""
∈(x::AbstractVector{N}, B::Ball1{N})::Bool where {N<:Real}
Check whether a given point is contained in a ball in the 1-norm.
### Input
- `x` -- point/vector
- `B` -- ball in the 1-norm
### Output
`true` iff ``x ∈ B``.
### Notes
This implementation is worst-case optimized, i.e., it is optimistic and first
computes (see below) the whole sum before comparing to the radius.
In applications where the point is typically far away from the ball, a fail-fast
implementation with interleaved comparisons could be more efficient.
### Algorithm
Let ``B`` be an ``n``-dimensional ball in the 1-norm with radius ``r`` and let
``c_i`` and ``x_i`` be the ball's center and the vector ``x`` in dimension
``i``, respectively.
Then ``x ∈ B`` iff ``∑_{i=1}^n |c_i - x_i| ≤ r``.
### Examples
```jldoctest
julia> B = Ball1([1., 1.], 1.);
julia> ∈([.5, -.5], B)
false
julia> ∈([.5, 1.5], B)
true
```
"""
function ∈(x::AbstractVector{N}, B::Ball1{N})::Bool where {N<:Real}
@assert length(x) == dim(B)
sum = zero(N)
for i in eachindex(x)
sum += abs(B.center[i] - x[i])
end
return sum <= B.radius
end
"""
rand(::Type{Ball1}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing
)::Ball1{N}
Create a random ball in the 1-norm.
### Input
- `Ball1` -- 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
### Output
A random ball in the 1-norm.
### Algorithm
All numbers are normally distributed with mean 0 and standard deviation 1.
Additionally, the radius is nonnegative.
"""
function rand(::Type{Ball1};
N::Type{<:Real}=Float64,
dim::Int=2,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing
)::Ball1{N}
rng = reseed(rng, seed)
center = randn(rng, N, dim)
radius = abs(randn(rng, N))
return Ball1(center, radius)
end
"""
constraints_list(P::Ball1{N})::Vector{LinearConstraint{N}} where {N<:Real}
Return the list of constraints defining a ball in the 1-norm.
### Input
- `B` -- ball in the 1-norm
### Output
The list of constraints of the ball.
### Algorithm
The constraints can be defined as ``d_i^T (x-c) ≤ r`` for all ``d_i``, where
``d_i`` is a vector with elements ``1`` or ``-1`` in ``n`` dimensions. To span
all possible ``d_i``, the function `Iterators.product` is used.
"""
function constraints_list(B::Ball1{N})::Vector{LinearConstraint{N}} where {N<:Real}
n = LazySets.dim(B)
c, r = B.center, B.radius
clist = Vector{LinearConstraint{N}}(undef, 2^n)
for (i, di) in enumerate(Iterators.product([[one(N), -one(N)] for i = 1:n]...))
d = collect(di) # tuple -> vector
clist[i] = LinearConstraint(d, dot(d, c) + r)
end
return clist
end
"""
translate(B::Ball1{N}, v::AbstractVector{N}) where {N<:Real}
Translate (i.e., shift) a ball in the 1-norm by a given vector.
### Input
- `B` -- ball in the 1-norm
- `v` -- translation vector
### Output
A translated ball in the 1-norm.
### Algorithm
We add the vector to the center of the ball.
"""
function translate(B::Ball1{N}, v::AbstractVector{N}) where {N<:Real}
@assert length(v) == dim(B) "cannot translate a $(dim(B))-dimensional " *
"set by a $(length(v))-dimensional vector"
return Ball1(center(B) + v, B.radius)
end