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

#943 - Update convexhull #944

Merged
merged 6 commits into from
Dec 18, 2018
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
6 changes: 5 additions & 1 deletion src/HPolyhedron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -499,15 +499,19 @@ The `HPolyhedron` (resp. `HPolytope`) obtained by the concrete convex hull of

### Notes

For performance reasons, it is suggested to use the `CDDLib.Library()` backend
for the `convex_hull`.

For further information on the supported backends see
[Polyhedra's documentation](https://juliapolyhedra.github.io/Polyhedra.jl/).
"""
function convex_hull(P1::HPoly{N},
P2::HPoly{N};
backend=default_polyhedra_backend(P1, N)) where {N}
@assert isdefined(@__MODULE__, :Polyhedra) "the function `convex_hull` needs " *
"the package 'Polyhedra' to be loaded"
"the package 'Polyhedra' to be loaded"
Pch = convexhull(polyhedron(P1; backend=backend), polyhedron(P2; backend=backend))
removehredundancy!(Pch)
return convert(typeof(P1), Pch)
end

Expand Down
66 changes: 37 additions & 29 deletions src/VPolytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ using MathProgBase, GLPKMathProgInterface
import Base.rand

export VPolytope,
vertices_list
vertices_list,
convex_hull

"""
VPolytope{N<:Real} <: AbstractPolytope{N}
Expand Down Expand Up @@ -187,16 +188,48 @@ function constraints_list(P::VPolytope{N})::Vector{LinearConstraint{N}} where {N
return constraints_list(tohrep(P))
end

"""
convex_hull(P1::VPolytope{N}, P2::VPolytope{N};
[backend]=default_polyhedra_backend(P1, N)) where {N}

Compute the convex hull of the set union of two polytopes in V-representation.

### Input

- `P1` -- polytope
- `P2` -- another polytope
- `backend` -- (optional, default: `default_polyhedra_backend(P1, N)`) the polyhedral
computations backend, see [Polyhedra's documentation](https://juliapolyhedra.github.io/Polyhedra.jl/latest/installation.html#Getting-Libraries-1)
for further information

### Output

# --- functions that use Polyhedra.jl ---
The `VPolytope` obtained by the concrete convex hull of `P1` and `P2`.

### Notes

For performance reasons, it is suggested to use the `CDDLib.Library()` backend
for the `convex_hull`.
"""
function convex_hull(P1::VPolytope{N}, P2::VPolytope{N};
backend=default_polyhedra_backend(P1, N)) where {N}
@assert isdefined(@__MODULE__, :Polyhedra) "the function `convex_hull` needs " *
"the package 'Polyhedra' to be loaded"
Pch = convexhull(polyhedron(P1; backend=backend),
polyhedron(P2; backend=backend))
removevredundancy!(Pch)
return VPolytope(Pch)
end

# ==========================================
# Lower level methods that use Polyhedra.jl
# ==========================================

function load_polyhedra_vpolytope() # function to be loaded by Requires
return quote
# see the interface file AbstractPolytope.jl for the imports

export convex_hull,
cartesian_product,
export cartesian_product,
vertices_list,
tohrep,
tovrep
Expand Down Expand Up @@ -247,31 +280,6 @@ function polyhedron(P::VPolytope{N};
return polyhedron(Polyhedra.vrep(V), backend)
end

"""
convex_hull(P1::VPolytope{N}, P2::VPolytope{N};
[backend]=default_polyhedra_backend(P1, N)) where {N}

Compute the convex hull of the set union of two polytopes in V-representation.

### Input

- `P1` -- polytope
- `P2` -- another polytope
- `backend` -- (optional, default: `default_polyhedra_backend(P1, N)`) the polyhedral
computations backend, see [Polyhedra's documentation](https://juliapolyhedra.github.io/Polyhedra.jl/latest/installation.html#Getting-Libraries-1)
for further information

### Output

The `VPolytope` obtained by the concrete convex hull of `P1` and `P2`.
"""
function convex_hull(P1::VPolytope{N}, P2::VPolytope{N};
backend=default_polyhedra_backend(P1, N)) where {N}
Pch = convexhull(polyhedron(P1; backend=backend),
polyhedron(P2; backend=backend))
return VPolytope(Pch)
end

"""
cartesian_product(P1::VPolytope{N}, P2::VPolytope{N};
[backend]=default_polyhedra_backend(P1, N)) where {N}
Expand Down
6 changes: 1 addition & 5 deletions test/unit_Polytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,7 @@ if test_suite_polyhedra
p2 = VPolytope([v3, v4])
ch = convex_hull(p1, p2)
vl = vertices_list(ch)
@test v1 ∈ vl && v2 ∈ vl && v3 ∈ vl && v4 ∈ vl
# Note: The redundant vertex v5 is not removed (see #561).
# This test can be removed (and the length above should be corrected)
# when that issue is resolved.
@test length(vl) == 5 && v5 ∈ vl
@test ispermutation(vl, [v1, v2, v3, v4])

# Cartesian product
p1 = VPolytope([N[0, 0], N[1, 1]])
Expand Down