From ff8711492928ca8b0f3700dd922b7f472e8a5875 Mon Sep 17 00:00:00 2001 From: schillic Date: Sun, 16 Feb 2020 22:32:51 +0100 Subject: [PATCH 1/2] faster Interval conversion --- src/convert.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/convert.jl b/src/convert.jl index fb6e2f124e..c3504c5db0 100644 --- a/src/convert.jl +++ b/src/convert.jl @@ -491,8 +491,8 @@ Interval{Float64,IntervalArithmetic.Interval{Float64}}([0, 1]) ``` """ function convert(::Type{Interval}, H::AbstractHyperrectangle) - @assert dim(H) == 1 "can only convert a one-dimensional $(typeof(H)) to `Interval`" - return Interval([low(H); high(H)]) + @assert dim(H) == 1 "cannot convert a $(dim(H))-dimensional $(typeof(H)) to `Interval`" + return Interval(low(H)[1], high(H)[1]) end """ @@ -510,7 +510,7 @@ Converts a convex set to an interval. An interval. """ function convert(::Type{Interval}, S::LazySet{N}) where {N<:Real} - @assert dim(S) == 1 "can only convert a one-dimensional $(typeof(S)) to `Interval`" + @assert dim(S) == 1 "cannot convert a $(dim(H))-dimensional $(typeof(S)) to `Interval`" return Interval(-ρ(N[-1], S), ρ(N[1], S)) end From 30c8832b580062e11877d9818410421f18dccd99 Mon Sep 17 00:00:00 2001 From: schillic Date: Sun, 16 Feb 2020 22:33:41 +0100 Subject: [PATCH 2/2] exact interval overapproximation for convex sets --- src/Approximations/overapproximate.jl | 72 +++++++++++++++++++++++++-- test/unit_overapproximate.jl | 10 +++- 2 files changed, 75 insertions(+), 7 deletions(-) diff --git a/src/Approximations/overapproximate.jl b/src/Approximations/overapproximate.jl index 399497ff3d..5c65b26f99 100644 --- a/src/Approximations/overapproximate.jl +++ b/src/Approximations/overapproximate.jl @@ -588,7 +588,7 @@ end """ overapproximate(S::LazySet{N}, ::Type{<:Interval}) where {N<:Real} -Return the overapproximation of a real unidimensional set with an interval. +Return the overapproximation of a unidimensional set with an interval. ### Input @@ -601,12 +601,74 @@ An interval. ### Algorithm -The method relies on the exact conversion to `Interval`. Two support -function evaluations are needed in general. +We use two support-function evaluations. """ function overapproximate(S::LazySet{N}, ::Type{<:Interval}) where {N<:Real} - @assert dim(S) == 1 "cannot overapproximate a $(dim(S))-dimensional set with an `Interval`" - return convert(Interval, S) + @assert dim(S) == 1 "cannot overapproximate a $(dim(S))-dimensional set " * + "with an `Interval`" + return Interval(-ρ(N[-1], S), ρ(N[1], S)) +end + +""" + overapproximate(cap::Intersection{N}, ::Type{<:Interval}) where {N<:Real} + +Return the overapproximation of a unidimensional intersection with an interval. + +### Input + +- `cap` -- one-dimensional lazy intersection +- `Interval` -- type for dispatch + +### Output + +An interval. + +### Algorithm + +The algorithm recursively overapproximates the two intersected sets with +intervals and then intersects these. +""" +function overapproximate(cap::Intersection{N}, ::Type{<:Interval}) where {N<:Real} + @assert dim(cap) == 1 "cannot overapproximate a $(dim(cap))-dimensional " * + "intersection with an `Interval`" + X = overapproximate(cap.X, Interval) + Y = overapproximate(cap.Y, Interval) + return intersection(X, Y) +end + +""" + overapproximate(cap::IntersectionArray{N}, ::Type{<:Interval}) where {N<:Real} + +Return the overapproximation of a unidimensional intersection with an interval. + +### Input + +- `cap` -- one-dimensional lazy intersection +- `Interval` -- type for dispatch + +### Output + +An interval. + +### Algorithm + +The algorithm recursively overapproximates the two intersected sets with +intervals and then intersects these. +""" +function overapproximate(cap::IntersectionArray{N}, + ::Type{<:Interval}) where {N<:Real} + @assert dim(cap) == 1 "cannot overapproximate a $(dim(cap))-dimensional " * + "intersection with an `Interval`" + a = array(cap) + X = overapproximate(a[1], Interval) + @inbounds for i in 2:length(a) + Y = a[i] + X = intersection(X, Y) + if isempty(X) + return X + end + end + return X end function overapproximate_cap_helper(X::LazySet{N}, # convex set diff --git a/test/unit_overapproximate.jl b/test/unit_overapproximate.jl index bf47702b71..2cab72ba71 100644 --- a/test/unit_overapproximate.jl +++ b/test/unit_overapproximate.jl @@ -36,9 +36,15 @@ for N in [Float64, Rational{Int}, Float32] # Interval approximation b = Ball1(N[0], N(1)) - p = overapproximate(b, LazySets.Interval) + p1 = overapproximate(b, LazySets.Interval) + cap = Intersection(b, BallInf(N[1], N(1))) + p2 = overapproximate(cap, LazySets.Interval) + caparray = IntersectionArray([b, BallInf(N[1], N(1)), BallInf(N[1//2], N(1//4))]) + p3 = overapproximate(caparray, LazySets.Interval) for d in [N[1], N[-1]] - @test σ(d, p)[1] ≈ σ(d, b)[1] + for (o, p) in [(b, cap, caparray), (p1, p2, p3)] + @test σ(d, o)[1] ≈ d[1] + end end # approximation with an axis-aligned hyperrectangle