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

Vertices_list for HPolytope #189

Merged
merged 9 commits into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from 8 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 REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Plots
Expokit
MathProgBase
GLPKMathProgInterface
Requires
178 changes: 177 additions & 1 deletion src/HPolytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ using MathProgBase, GLPKMathProgInterface

export HPolytope,
addconstraint!,
constraints_list
constraints_list,
tosimplehrep

"""
HPolytope{N<:Real} <: AbstractPolytope{N}
Expand All @@ -25,9 +26,22 @@ struct HPolytope{N<:Real} <: AbstractPolytope{N}
end
# constructor for a HPolytope with no constraints
HPolytope{N}() where {N<:Real} = HPolytope{N}(Vector{N}(0))

# constructor for a HPolytope with no constraints of type Float64
HPolytope() = HPolytope{Float64}()

# constructor from a polygon in H-representation
HPolytope(P::HPolygon) = HPolytope(P.constraints_list)

# constructor for a HPolytope from a simple H-representation
function HPolytope(A::Matrix{N}, b::Vector{N}) where {N<:Real}
m = size(A, 1)
constraints = LinearConstraint{N}[]
@inbounds for i in 1:m
push!(constraints, LinearConstraint(A[i, :], b[i]))
end
return HPolytope(constraints)
end

# --- LazySet interface functions ---

Expand Down Expand Up @@ -164,3 +178,165 @@ function constraints_list(P::HPolytope{N}
)::Vector{LinearConstraint{N}} where {N<:Real}
return P.constraints
end

"""
tosimplehrep(P::HPolytope)

Return the simple H-representation ``Ax ≤ b`` of a polytope.

### Input

- `P` -- polytope

### Output

The tuple `(A, b)` where `A` is the matrix of normal directions and `b` are the offsets.
"""
function tosimplehrep(P::HPolytope{N}) where {N}
A = hcat([ci.a for ci in P.constraints]...)'
b = [ci.b for ci in P.constraints]
return (A, b)
end

@require Polyhedra begin

using CDDLib # default backend
import Polyhedra:polyhedron, SimpleHRepresentation, SimpleHRepresentation,
HRep, VRep,
removehredundancy!, removevredundancy!,
hreps, vreps,
intersect,
convexhull,
hcartesianproduct

export intersect, convex_hull, cartesian_product, vertices_list

# HPolytope from an HRep
function HPolytope(P::HRep{N, T}, backend=CDDLib.CDDLibrary()) where {N, T}
constraints = LinearConstraint{T}[]
for hi in hreps(P)
push!(constraints, LinearConstraint(hi.a, hi.β))
end
return HPolytope(constraints)
end

"""
polyhedron(P::HPolytope{N}, backend=CDDLib.CDDLibrary()) where {N}

### Input

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

### Output

An `HRep` polyhedron.
"""
function polyhedron(P::HPolytope{N}, backend=CDDLib.CDDLibrary()) where {N}
return polyhedron(SimpleHRepresentation(tosimplehrep(P)...), backend)
end

"""
intersect(P1::HPolytope{N}, P2::HPolytope{N},
backend=CDDLib.CDDLibrary())::HPolytope where {N<Real}

Compute the intersection of two polytopes in H-representation.

### Input

- `P1` -- polytope
- `P2` -- another polytope
- `backend` -- (optional, default: `CDDLib.CDDLibrary()`) the polyhedral
computations backend, see [Polyhedra's documentation](https://juliapolyhedra.github.io/Polyhedra.jl/latest/installation.html#Getting-Libraries-1)
for further information
- `prunefunc` -- (optional, default: `removehredundancy!`) function to post-process
the output of `intersect`

### Output

The `HPolytope` obtained by the intersection of `P1` and `P2`.
"""
function intersect(P1::HPolytope{N}, P2::HPolytope{N};
backend=CDDLib.CDDLibrary(),
prunefunc=removehredundancy!)::HPolytope{N} where {N<:Real}

P1 = polyhedron(P1, backend)
P2 = polyhedron(P2, backend)
Pint = intersect(P1, P2)
prunefunc(Pint)
return HPolytope(Pint)
end

"""
convex_hull(P1::HPolytope, P2::HPolytope; backend=CDDLib.CDDLibrary())

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

### Input

- `P1` -- polytope
- `P2` -- another polytope
- `backend` -- (optional, default: `CDDLib.CDDLibrary()`) 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 `HPolytope` obtained by the concrete convex hull of `P1` and `P2`.
"""
function convex_hull(P1::HPolytope, P2::HPolytope; backend=CDDLib.CDDLibrary())
Pch = convexhull(polyhedron(P1, backend), polyhedron(P2, backend))
return HPolytope(Pch)
end

"""
cartesian_product(P1::HPolytope, P2::HPolytope; backend=CDDLib.CDDLibrary())

Compute the Cartesian product of two polytopes in H-representaion.

### Input

- `P1` -- polytope
- `P2` -- another polytope
- `backend` -- (optional, default: `CDDLib.CDDLibrary()`) 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 `HPolytope` obtained by the concrete cartesian product of `P1` and `P2`.
"""
function cartesian_product(P1::HPolytope, P2::HPolytope; backend=CDDLib.CDDLibrary())
Pcp = hcartesianproduct(polyhedron(P1, backend), polyhedron(P2, backend))
return HPolytope(Pcp)
end

"""
vertices_list(P::HPolytope{N})::Vector{Vector{N}} where {N<:Real}
Copy link
Member

Choose a reason for hiding this comment

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

arguments missing (put in brackets)


Return the list of vertices of a polytope in constraint representation.

### Input

- `P` -- polytope in constraint representation
- `backend` -- (optional, default: `CDDLib.CDDLibrary()`) the polyhedral
computations backend, see Polyhedra's documentation
for further information
- `prunefunc` -- (optional, default: `removevredundancy!`) function to post-process
the output of `vreps`

### Output

List of vertices.
"""
function vertices_list(P::HPolytope{N};
backend=CDDLib.CDDLibrary(),
prunefunc=removevredundancy!)::Vector{Vector{N}} where {N<:Real}
P = polyhedron(P, backend)
prunefunc(P)
return [vi for vi in vreps(P)]
end

end
4 changes: 2 additions & 2 deletions src/LazySets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Main module for `LazySets.jl` -- a Julia package for calculus with convex sets.
"""
module LazySets

using RecipesBase, IterTools
using RecipesBase, IterTools, Requires

export LazySet,
ρ, support_function,
Expand Down Expand Up @@ -59,9 +59,9 @@ include("BallInf.jl")
include("Ball1.jl")
include("Ballp.jl")
include("Hyperrectangle.jl")
include("HPolytope.jl")
include("HPolygon.jl")
include("HPolygonOpt.jl")
include("HPolytope.jl")
include("VPolygon.jl")
include("Zonotope.jl")
include("Ellipsoid.jl")
Expand Down