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

#1769 - Faster box approximation of VPolytope #2252

Merged
merged 2 commits into from
Oct 27, 2020
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
27 changes: 27 additions & 0 deletions src/Approximations/overapproximate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,33 @@ function overapproximate(am::AbstractAffineMap{N, <:AbstractHyperrectangle{N}},
return Hyperrectangle(center_MXv, radius_MX)
end

function overapproximate(P::Union{VPolytope, VPolygon},
::Type{<:Hyperrectangle})
n = dim(P)
vlist = vertices_list(P)
@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(center, radius)
end

"""
overapproximate(X::LazySet{N}, ::Type{<:Zonotope},
dir::AbstractDirections{N};
Expand Down
5 changes: 5 additions & 0 deletions test/unit_box_approximation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down