/
Translation.jl
351 lines (250 loc) · 7.69 KB
/
Translation.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
import Base: isempty
export Translation,
an_element,
constraints_list
"""
Translation{N<:Real, VN<:AbstractVector{N}, S<:LazySet{N}} <: LazySet{N}
Type that represents a lazy translation.
The translation of set `X` along vector `v` is the map:
```math
x ↦ x + v,\\qquad x ∈ X
```
A translation is a special case of an affine map ``A x + b, x ∈ X`` where the
linear map ``A`` is the identity matrix and the translation vector ``b = v``.
### Fields
- `X` -- convex set
- `v` -- vector that defines the translation
### Example
```jldoctest translation
julia> X = BallInf([2.0, 2.0, 2.0], 1.0);
julia> v = [1.0, 0.0, 0.0]; # translation along dimension 1
julia> tr = Translation(X, v);
julia> typeof(tr)
Translation{Float64,Array{Float64,1},BallInf{Float64}}
julia> tr.X
BallInf{Float64}([2.0, 2.0, 2.0], 1.0)
julia> tr.v
3-element Array{Float64,1}:
1.0
0.0
0.0
```
The sum operator `+` is overloaded to create translations:
```jldoctest translation
julia> X + v == Translation(X, v)
true
```
And so does the Minkowski sum operator, `⊕`:
```jldoctest translation
julia> X ⊕ v == Translation(X, v)
true
```
The translation of a translation is performed immediately:
```jldoctest translation
julia> tr = (X+v)+v
Translation{Float64,Array{Float64,1},BallInf{Float64}}(BallInf{Float64}([2.0, 2.0, 2.0], 1.0), [2.0, 0.0, 0.0])
julia> tr.v
3-element Array{Float64,1}:
2.0
0.0
0.0
```
The dimension of a translation is obtained with the `dim` function:
```jldoctest translation
julia> dim(tr)
3
```
For the support vector (resp. support function) along vector `d`, use `σ` and
`ρ` respectively:
```jldoctest translation
julia> σ([1.0, 0.0, 0.0], tr)
3-element Array{Float64,1}:
5.0
3.0
3.0
julia> ρ([1.0, 0.0, 0.0], tr)
5.0
```
See the docstring of each of these functions for details.
The `an_element` function is useful to obtain an element of a translation:
```jldoctest translation
julia> e = an_element(tr)
3-element Array{Float64,1}:
4.0
2.0
2.0
```
The lazy linear map of a translation is again a translation, since the following
simplification rule applies: ``M * (X⊕v) = (M*X) ⊕ (M*v)``:
```jldoctest translation
julia> Q = Matrix(2.0I, 3, 3) * tr;
julia> Q isa Translation && Q.v == 2 * tr.v
true
```
Use the `isempty` method to query if the translation is empty; it falls back
to the `isempty` method of the wrapped set:
```jldoctest translation
julia> isempty(tr)
false
```
The list of constraints of the translation of a polyhedron (in general, a set
whose `constraints_list` is available) can be computed from a lazy translation:
```jldoctest translation
julia> constraints_list(tr)
6-element Array{HalfSpace{Float64},1}:
HalfSpace{Float64}([1.0, 0.0, 0.0], 5.0)
HalfSpace{Float64}([0.0, 1.0, 0.0], 3.0)
HalfSpace{Float64}([0.0, 0.0, 1.0], 3.0)
HalfSpace{Float64}([-1.0, -0.0, -0.0], -3.0)
HalfSpace{Float64}([-0.0, -1.0, -0.0], -1.0)
HalfSpace{Float64}([-0.0, -0.0, -1.0], -1.0)
```
"""
struct Translation{N<:Real, VN<:AbstractVector{N}, S<:LazySet{N}} <: LazySet{N}
X::S
v::VN
# default constructor with dimension check
function Translation(X::S, v::VN) where {N, VN<:AbstractVector{N}, S<:LazySet{N}}
@assert dim(X) == length(v) "cannot create a translation of a set of dimension $(dim(X)) " *
"along a vector of length $(length(v))"
return new{N, VN, S}(X, v)
end
end
@static if VERSION < v"0.7-"
@eval begin
# convenience constructor without type parameter
Translation(X::S, v::VN) where {N<:Real, VN<:AbstractVector{N}, S<:LazySet{N}} =
Translation{N, VN, S}(X, v)
end
end
# constructor from a Translation: perform the translation immediately
Translation(tr::Translation{N}, v::AbstractVector{N}) where {N<:Real} =
Translation(tr.X, tr.v + v)
"""
+(X::LazySet, v::AbstractVector)
Convenience constructor for a translation.
### Input
- `X` -- convex set
- `v` -- vector
### Output
The symbolic translation of ``X`` along vector ``v``.
"""
+(X::LazySet, v::AbstractVector) = Translation(X, v)
# translation from the left
+(v::AbstractVector, X::LazySet) = Translation(X, v)
"""
⊕(X::LazySet, v::AbstractVector)
Unicode alias constructor ⊕ (`oplus`) for the lazy translation operator.
"""
⊕(X::LazySet, v::AbstractVector) = Translation(X, v)
# translation from the left
⊕(v::AbstractVector, X::LazySet) = Translation(X, v)
# ============================
# LazySet interface functions
# ============================
"""
dim(tr::Translation)::Int
Return the dimension of a translation.
### Input
- `tr` -- translation
### Output
The dimension of a translation.
"""
function dim(tr::Translation)::Int
return length(tr.v)
end
"""
σ(d::AbstractVector{N}, tr::Translation{N}) where {N<:Real}
Return the support vector of a translation.
### Input
- `d` -- direction
- `tr` -- translation
### Output
The support vector in the given direction.
If the direction has norm zero, the result depends on the wrapped set.
"""
function σ(d::AbstractVector{N}, tr::Translation{N}) where {N<:Real}
return tr.v + σ(d, tr.X)
end
"""
ρ(d::AbstractVector{N}, tr::Translation{N}) where {N<:Real}
Return the support function of a translation.
### Input
- `d` -- direction
- `tr` -- translation
### Output
The support function in the given direction.
"""
function ρ(d::AbstractVector{N}, tr::Translation{N}) where {N<:Real}
return dot(d, tr.v) + ρ(d, tr.X)
end
"""
LinearMap(M::AbstractMatrix{N}, tr::Translation{N}) where {N<:Real}
Return the lazy linear map of a translation.
### Input
- `M` -- matrix
- `tr` -- translation
### Output
The translation defined by the linear map.
### Notes
This method defines the simplification rule: ``M * (X⊕v) = (M*X) ⊕ (M*v)``,
returning a new translation.
"""
function LinearMap(M::AbstractMatrix{N}, tr::Translation{N}) where {N<:Real}
return Translation(M * tr.X, M * tr.v)
end
"""
an_element(tr::Translation)
Return some element of a translation.
### Input
- `tr` -- translation
### Output
An element in the translation.
### Notes
This function first asks for `an_element` function of the wrapped set, then
translates this element according to the given translation vector.
"""
function an_element(tr::Translation)
return an_element(tr.X) + tr.v
end
"""
isempty(tr::Translation)::Bool
Return if a translation is empty or not.
### Input
- `tr` -- translation
### Output
`true` iff the wrapped set is empty.
"""
function isempty(tr::Translation)::Bool
return isempty(tr.X)
end
"""
constraints_list(tr::Translation{N}, ::Val{true}) where {N<:Real}
Return the list of constraints of the translation of a set.
### Input
- `tr` -- lazy translation of a polyhedron
### Output
The list of constraints of the translation.
### Notes
We assume that the set wrapped by the lazy translation `X` offers a method
`constraints_list(⋅)`.
### Algorithm
Let the translation be defined by the set of points `y` such that `y = x + v` for
all `x ∈ X`. Then, each defining halfspace `a⋅x ≤ b` is transformed to
`a⋅y ≤ b + a⋅v`.
"""
function constraints_list(tr::Translation{N}, ::Val{true}) where {N<:Real}
constraints_X = constraints_list(tr.X)
constraints_TX = similar(constraints_X)
@inbounds for (i, ci) in enumerate(constraints_X)
constraints_TX[i] = HalfSpace(ci.a, ci.b + dot(ci.a, tr.v))
end
return constraints_TX
end
function constraints_list(tr::Translation{N}) where {N<:Real}
has_constraints = applicable(constraints_list, tr.X)
return constraints_list(tr, Val(has_constraints))
end
function constraints_list(tr::Translation{N}, ::Val{false}) where {N<:Real}
throw(MethodError("this function requires that the `constraints_list` method is applicable"))
end