From d2ddab15b8af941c4fe1c1fb37b81ff5888d252d Mon Sep 17 00:00:00 2001 From: schillic Date: Mon, 20 Jul 2020 12:13:52 +0200 Subject: [PATCH 1/2] specialized box approximation for vertex representation --- src/Approximations/overapproximate.jl | 18 ++++++++++++++++++ test/unit_box_approximation.jl | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/src/Approximations/overapproximate.jl b/src/Approximations/overapproximate.jl index 47648d20e5..cf0a8518d8 100644 --- a/src/Approximations/overapproximate.jl +++ b/src/Approximations/overapproximate.jl @@ -1513,6 +1513,24 @@ function overapproximate(am::AbstractAffineMap{N, <:AbstractHyperrectangle{N}}, return Hyperrectangle(center_MXv, radius_MX) end +function overapproximate(P::Union{VPolytope, VPolygon}, + ::Type{<:Hyperrectangle}) where {N<:Real} + n = dim(P) + vlist = vertices_list(P) + low = copy(vlist[1]) + high = copy(vlist[1]) + @inbounds for v in @view vlist[2:length(vlist)] + for i in 1:n + if v[i] > high[i] + high[i] = v[i] + elseif v[i] < low[i] + low[i] = v[i] + end + end + end + return Hyperrectangle(low=low, high=high) +end + """ overapproximate(X::LazySet{N}, ::Type{<:Zonotope}, dir::AbstractDirections{N}; diff --git a/test/unit_box_approximation.jl b/test/unit_box_approximation.jl index 03d7d06fed..11bbf4c043 100644 --- a/test/unit_box_approximation.jl +++ b/test/unit_box_approximation.jl @@ -29,6 +29,11 @@ for N in [Float64, Rational{Int}, Float32] @test h.center ≈ hexp.center @test h.radius ≈ hexp.radius + # Box approximation of a VPolytope + P = VPolytope([N[0, 0], N[2, 1], N[1, 2]]) + H = box_approximation(P) + @test H == Hyperrectangle(N[1, 1], N[1, 1]) + # empty set E = EmptySet{N}(2) @test box_approximation(E) == E From 34307c7b08e386b0c794455abaa17559fc7540ea Mon Sep 17 00:00:00 2001 From: schillic Date: Sun, 2 Aug 2020 10:02:47 +0200 Subject: [PATCH 2/2] faster implementation --- src/Approximations/overapproximate.jl | 29 ++++++++++++++++++--------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Approximations/overapproximate.jl b/src/Approximations/overapproximate.jl index cf0a8518d8..30e3bb4ed1 100644 --- a/src/Approximations/overapproximate.jl +++ b/src/Approximations/overapproximate.jl @@ -1514,21 +1514,30 @@ function overapproximate(am::AbstractAffineMap{N, <:AbstractHyperrectangle{N}}, end function overapproximate(P::Union{VPolytope, VPolygon}, - ::Type{<:Hyperrectangle}) where {N<:Real} + ::Type{<:Hyperrectangle}) n = dim(P) vlist = vertices_list(P) - low = copy(vlist[1]) - high = copy(vlist[1]) - @inbounds for v in @view vlist[2:length(vlist)] - for i in 1:n - if v[i] > high[i] - high[i] = v[i] - elseif v[i] < low[i] - low[i] = v[i] + @assert !isempty(vlist) "cannot overapproximate an empty polytope" + + @inbounds v1 = vlist[1] + center = similar(v1) + radius = similar(v1) + + @inbounds for i in 1:n + low_i = v1[i] + high_i = v1[i] + for v in vlist + if v[i] > high_i + high_i = v[i] + elseif v[i] < low_i + low_i = v[i] end end + radius_i = (high_i - low_i) / 2 + center[i] = low_i + radius_i + radius[i] = radius_i end - return Hyperrectangle(low=low, high=high) + return Hyperrectangle(center, radius) end """