/
TemplateReachSet.jl
95 lines (74 loc) · 3.61 KB
/
TemplateReachSet.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
# ================================================================
# Template reach set
# ================================================================
"""
TemplateReachSet{N, VN, TN<:AbstractDirections{N, VN}, SN<:AbstractVector{N}} <: AbstractLazyReachSet{N}
Reach set that stores the support function of a set at a give set of directions.
### Notes
The struct has the following parameters:
- `N` -- refers to the numerical type of the representation.
- `VN` -- refers to the vector type of the template
- `TN` -- refers to the template type
- `SN` -- vector type that holds the support function evaluations
Concrete subtypes of `AbstractDirections` are defined in the `LazySets` library.
This reach-set implicitly represents a set by a set of directions and support
functions. `set(R::TemplateReachSet)` returns a polyhedron in half-space
representation.
Apart from the getter functions inherited from the `AbstractReachSet` interface,
the following methods are available:
- `directions(R)` -- return the set of directions normal to the faces of this reach-set
- `support_functions(R)` -- return the vector of support function evaluations
- `support_functions(R, i)` -- return the `i`-th coordinate of the vector of support function evaluatons
"""
struct TemplateReachSet{N,VN,TN<:AbstractDirections{N,VN},SN<:AbstractVector{N}} <:
AbstractLazyReachSet{N}
dirs::TN
sf::SN
Δt::TimeInterval
end
# constructor from a given set (may overapproximate)
function TemplateReachSet(dirs::AbstractDirections, X::LazySet, Δt::TimeInterval)
sfunc = [ρ(d, X) for d in dirs]
return TemplateReachSet(dirs, sfunc, Δt)
end
# constructor from a given reach-set (may overapproximate)
function TemplateReachSet(dirs::AbstractDirections, R::AbstractLazyReachSet)
return TemplateReachSet(dirs, set(R), tspan(R))
end
# constructor with a time point
function TemplateReachSet(dirs::AbstractDirections{N}, X::LazySet, t::N) where {N}
return TemplateReachSet(dirs, X, interval(t))
end
# abstract reachset interface
function set(R::TemplateReachSet)
T = isbounding(R.dirs) ? HPolytope : HPolyhedron
return T([HalfSpace(di, R.sf[i]) for (i, di) in enumerate(R.dirs)])
end
# TODO requires adding boundedness property as a type parameter
setrep(::Type{<:TemplateReachSet{N,VN}}) where {N,VN} = HPolyhedron{N,VN}
setrep(::TemplateReachSet{N,VN}) where {N,VN} = HPolyhedron{N,VN}
tspan(R::TemplateReachSet) = R.Δt
tstart(R::TemplateReachSet) = inf(R.Δt)
tend(R::TemplateReachSet) = sup(R.Δt)
dim(R::TemplateReachSet) = dim(R.dirs)
vars(R::TemplateReachSet) = Tuple(Base.OneTo(dim(R)))
directions(R::TemplateReachSet) = R.dirs
support_functions(R::TemplateReachSet) = R.sf
support_functions(R::TemplateReachSet, i::Int) = R.sf[i]
# get the matrix of support function evaluations, assuming that the vector
# of support function evaluations for the reach-set is a view
function support_function_matrix(R::TemplateReachSet{N,VN,TN,SN}) where {N,VN,TN,SN<:SubArray}
return R.sf.parent
end
_collect(dirs::AbstractDirections) = collect(dirs)
_collect(dirs::CustomDirections) = dirs.directions
# convenience functions to get support directions of a given set
_getdirs(X::LazySet) = [c.a for c in constraints_list(X)]
LazySets.CustomDirections(X::LazySet) = CustomDirections(_getdirs(X), dim(X)) # TODO may use isboundedtype trait
# overapproximate a given reach-set with a template
function overapproximate(R::AbstractLazyReachSet, dirs::AbstractDirections)
return TemplateReachSet(dirs, R)
end
function overapproximate(R::AbstractLazyReachSet, dirs::Vector{VN}) where {VN}
return TemplateReachSet(CustomDirections(dirs), R)
end