Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify code for UnionSet and UnionSetArray #834

Merged
merged 4 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/Continuous/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,16 @@ setrep(::InitialValueProblem{HS,ST}) where {HS,ST} = ST
LazySets.dim(::IA.Interval) = 1
LazySets.dim(::IA.IntervalBox{D,N}) where {D,N} = D

# lazy sets (or sets that behave like such -- TODO update once UnionSet <: LazySet)
_dim(X::Union{<:LazySet,<:IA.Interval,<:IA.IntervalBox,<:UnionSet,<:UnionSetArray}) = dim(X)
# lazy sets (or sets that behave as such)
_dim(X::Union{<:LazySet,<:IA.Interval,<:IA.IntervalBox}) = dim(X)

# singleton elements
_dim(X::Number) = 1
_dim(X::AbstractVector{N}) where {N<:Number} = length(X)

# vector of sets
function _dim(X::AbstractVector{UT}) where {UT<:Union{<:LazySet,
<:IA.Interval,<:IA.IntervalBox,<:UnionSet,
<:UnionSetArray}}
<:IA.Interval,<:IA.IntervalBox}}
n = _dim(first(X))
all(X -> _dim(X) == n, X) || throw(ArgumentError("dimension mismatch between " *
"the initial sets in this array; expected only sets of dimension $n"))
Expand Down
4 changes: 2 additions & 2 deletions src/Flowpipes/clustering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ end

function cluster(F, idx, method::LazyClustering{P,Val{true}}) where {P}
p = _partition(method, idx)
return convF = [convexify(view(F, cj)) for cj in p]
return [convexify(view(F, cj)) for cj in p]
end

function cluster(F, idx, method::LazyClustering{P,Val{false}}) where {P}
p = _partition(method, idx)
return convF = [view(F, cj) for cj in p]
return [view(F, cj) for cj in p]
end

# for Taylor model flowpipes we preprocess it with a zonotopic overapproximation
Expand Down
2 changes: 1 addition & 1 deletion src/Hybrid/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ end

#=
# TODO distribute initial set exceptions:
# IA.Interval, IntervalBox, UninSet, UnionSetArray
# IA.Interval, IntervalBox

# no-op if the ivp has a WaitingList

Expand Down
6 changes: 2 additions & 4 deletions src/ReachSets/AbstractLazyReachSet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ end
# Expose common LazySets operations
LazySets.constraints_list(R::AbstractLazyReachSet) = constraints_list(set(R))
LazySets.vertices_list(R::AbstractLazyReachSet) = vertices_list(set(R))
for ST in (LazySet, UnionSet, UnionSetArray)
@eval Base.:⊆(R::AbstractLazyReachSet, X::$(ST)) = ⊆(set(R), X)
@eval Base.:⊆(X::$(ST), R::AbstractLazyReachSet) = ⊆(X, set(R))
end
Base.:⊆(R::AbstractLazyReachSet, X::LazySet) = ⊆(set(R), X)
Base.:⊆(X::LazySet, R::AbstractLazyReachSet) = ⊆(X, set(R))
Base.:⊆(R::AbstractLazyReachSet, S::AbstractLazyReachSet) = ⊆(set(R), set(S))
LazySets.area(R::AbstractLazyReachSet) = area(set(R))
LazySets.volume(R::AbstractLazyReachSet) = volume(set(R))
Expand Down
3 changes: 1 addition & 2 deletions src/ReachSets/AbstractReachSet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ following methods:
abstract type AbstractReachSet{N} end

# convenience union for dispatch on structs that behave like a set
const SetOrReachSet = Union{LazySet,UnionSet,UnionSetArray,IA.Interval,IA.IntervalBox,
AbstractReachSet}
const SetOrReachSet = Union{LazySet,IA.Interval,IA.IntervalBox,AbstractReachSet}

"""
basetype(T::Type{<:AbstractReachSet})
Expand Down
30 changes: 1 addition & 29 deletions src/ReachSets/TaylorModelReachSet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,33 +85,6 @@ function _intersection(R::TaylorModelReachSet, Y::LazySet, ::BoxIntersection)
return isempty(out) ? EmptySet(dim(out)) : overapproximate(out, Hyperrectangle)
end

# TODO refactor? See LazySets#2158
function _intersection(R::TaylorModelReachSet, Y::UnionSetArray{N,HT},
::BoxIntersection) where {N,HT<:HalfSpace{N}}
X = set(overapproximate(R, Hyperrectangle))

# find first non-empty intersection
m = length(Y.array) # can't use array(Y) ?
i = 1
local W
@inbounds while i <= m
W = intersection(X, Y.array[i])
!isempty(W) && break
i += 1
end
if i == m + 1
return EmptySet(dim(Y))
end

# add all other non-empty intersections
out = [W]
@inbounds for j in (i + 1):m
W = intersection(X, Y.array[j])
!isempty(W) && push!(out, W)
end
return isempty(out) ? EmptySet(dim(out)) : overapproximate(UnionSetArray(out), Hyperrectangle)
end

# ======================
# Inclusion checks
# ======================
Expand Down Expand Up @@ -404,8 +377,7 @@ function overapproximate(R::TaylorModelReachSet{N}, ::Type{<:Zonotope},
end
Z = overapproximate.(fX̂, Zonotope)
Δt = tspan(R)
#return ReachSet(UnionSetArray(Z), Δt) # but UnionSetArray is not yet a lazyset
return ReachSet(ConvexHullArray(Z), Δt)
return ReachSet(UnionSetArray(Z), Δt)
end

# evaluate at a given time point and overapproximate the resulting set
Expand Down
Loading