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

#619 Is intersection empty for polytopes #624

Merged
merged 9 commits into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions docs/src/lib/binary_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ is_intersection_empty(::Zonotope{N}, ::Hyperplane{N}, ::Bool=false) where {N<:Re
is_intersection_empty(::Hyperplane{N}, ::Zonotope{N}, ::Bool=false) where {N<:Real}
is_intersection_empty(::Ball2{N}, ::Ball2{N}, ::Bool=false) where {N<:AbstractFloat}
is_intersection_empty(::LineSegment{N}, ::LineSegment{N}, ::Bool=false) where {N<:Real}
is_intersection_empty(::AbstractPolytope{N}, ::AbstractPolytope{N}, ::Bool=false) where {N<:Real}
```

## Intersection of two sets
Expand Down
1 change: 1 addition & 0 deletions docs/src/lib/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ This interface defines the following functions:
```@docs
singleton_list(::AbstractPolytope)
linear_map(::AbstractMatrix, ::AbstractPolytope)
isempty(::AbstractPolytope)
```

### Polygon
Expand Down
27 changes: 26 additions & 1 deletion src/AbstractPolytope.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Base.isempty

export AbstractPolytope,
vertices_list,
singleton_list,
linear_map
linear_map,
isempty

"""
AbstractPolytope{N<:Real} <: LazySet{N}
Expand Down Expand Up @@ -81,3 +84,25 @@ function linear_map(M::AbstractMatrix, P::AbstractPolytope{N}) where {N<:Real}
end
return T(new_vlist)
end

"""
isempty(P::AbstractPolytope{N})::Bool where {N<:Real}

Determine whether a polytope is empty.

### Input

- `P` -- abstract polytope

### Output

`true` if the given polytope contains no vertices, and `false` otherwise.

### Algorithm

This algorithm checks whether the `vertices_list` of the given polytope is empty
or not.
"""
function isempty(P::AbstractPolytope{N})::Bool where {N<:Real}
return isempty(vertices_list(P))
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this is the correct interpretation. To me, an HPolytope/HPolygon with no constraints is either not defined or universal (because each new constraint removes parts of the set).

Also, add a new line at the end of the file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the HPolytope with no constraints is particular case that could be treated separately as being "universal", the whole R^n.

For VPolytope, i think it is reasonable to say that if it has no vertices then it only contains the empty set, V = convex_hull{{}}.

I was concerned with the case where the set is defined by a set of constraints with empty intersection. For instance consider the pair of constraints x <= -1 && x >= 1:

julia> P = HPolytope([LazySets.HalfSpace([1.0, 0.0], -1.0), LazySets.HalfSpace([-1.0, 0.0], -1.0)])
LazySets.HPolytope{Float64}(LazySets.HalfSpace{Float64}[LazySets.HalfSpace{Float64}([1.0, 0.0], -1.0), LazySets.HalfSpace{Float64}([-1.0, 0.0], -1.0)])

julia> isempty(P)
true

julia> vertices_list(P)
0-element Array{Array{Float64,1},1}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than finding workarounds, we could accept that our algorithms are generally not correct for invalid polytope inputs, like unbounded or contradicting constraints 😉

For VPolytope, i think it is reasonable to say that if it has no vertices then it only contains the empty set, V = convex_hull{{}}.

Sure.

If HPolytope and HPolygon are problematic, should we replace AbstractPolytope by Union{VPolygon, VPolytope} then? Our other polytopic sets can never be empty by definition anyway.

8 changes: 7 additions & 1 deletion src/HPolytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,13 @@ import Polyhedra:polyhedron, SimpleHRepresentation, SimpleHRepresentation,
hcartesianproduct,
points

export intersection, convex_hull, cartesian_product, vertices_list, tovrep, tohrep
export intersection,
convex_hull,
cartesian_product,
vertices_list,
tovrep,
tohrep,
is_intersection_empty

# HPolytope from an HRep
function HPolytope(P::HRep{N, T}, backend=CDDLib.CDDLibrary()) where {N, T}
Expand Down
45 changes: 45 additions & 0 deletions src/is_intersection_empty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,48 @@ function is_intersection_empty(ls1::LineSegment{N},
return empty_intersection
end
end

# --- AbstractPolytope ---

"""
is_intersection_empty(P::AbstractPolytope{N},
Q::AbstractPolytope{N},
witness::Bool=false
)::Union{Bool, Tuple{Bool, Vector{N}}} where {N<:Real}

Check whether a pair of polytopes do not intersect, and otherwise optionally
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"a pair of" → "two"

compute a witness.

### Input

- `P` -- polytope
- `Q` -- another polytope
- `witness` -- (optional, default: `false`) compute a witness if activated

### Output

* If `witness` option is deactivated: `true` iff ``P ∩ Q = ∅``
* If `witness` option is activated:
* `(true, [])` iff ``P ∩ Q = ∅``
* `(false, v)` iff ``P ∩ Q ≠ ∅`` and ``v ∈ P ∩ Q``

### Algorithm

This is a fallback implementation of the `AbstractPolytope` interface that computes
the concrete intersection, `intersection`, of the given pair of polytopes. If a
witness is required, the first vertex of the resulting intersection polytope is
returned.
"""
function is_intersection_empty(P::AbstractPolytope{N},
Q::AbstractPolytope{N},
witness::Bool=false
)::Union{Bool, Tuple{Bool, Vector{N}}} where {N<:Real}
X = intersection(P, Q)
isempty_flag = isempty(X)
if witness
witness_vertex = isempty_flag ? N[] : vertices_list(X)[1]
return (isempty_flag, witness_vertex)
else
return isempty_flag
end
end
11 changes: 9 additions & 2 deletions test/unit_Polytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,15 @@ for N in [Float64, Rational{Int}, Float32]
end

# default Float64 constructors
@test HPolytope() isa HPolytope{Float64}
@test VPolytope() isa VPolytope{Float64}
empty_H_polytope = HPolytope()
empty_V_polytope = VPolytope()
@test empty_H_polytope isa HPolytope{Float64}
@test empty_V_polytope isa VPolytope{Float64}

# check isempty function of the AbstractPolytope interface
@test isempty(empty_H_polytope)
@test isempty(empty_V_polytope)
is_intersection_empty(empty_H_polytope, empty_V_polytope)

# Polyhedra tests that only work with Float64
if test_suite_polyhedra
Expand Down