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

Some cleanup and simplification to polytopes code #287

Merged
merged 1 commit into from
Feb 15, 2021
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
31 changes: 14 additions & 17 deletions src/Polytopes/LinearProgram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@
The linear program on the feasible set P (a Polyhedron) with
respect to the function x ↦ dot(c,x)+k where k is optional (default 0).

""" struct LinearProgram
"""
struct LinearProgram
feasible_region::Polyhedron
polymake_lp::Polymake.BigObjectAllocated
convention::Symbol
function LinearProgram(Q::Polyhedron, objective::AbstractVector, k; convention = :max)
function LinearProgram(Q::Polyhedron, objective::AbstractVector, k = 0; convention = :max)
P=Polyhedron(Polymake.polytope.Polytope(pm_polytope(Q)))
ambDim = ambient_dim(P)
size(objective, 1) == ambDim || error("objective has wrong dimension.")
lp = Polymake.polytope.LinearProgram(LINEAR_OBJECTIVE=homogenize(objective, k))
pm_polytope(P).LP = lp
new(P, lp, convention)
end
function LinearProgram(Q::Polyhedron, objective::AbstractVector; convention = :max)
LinearProgram(Q,objective,0; convention=convention)
end
end


Expand Down Expand Up @@ -61,15 +59,14 @@ The allowed values for `as` are


"""
function objective_function(LP::LinearProgram; as = :pair)
function objective_function(LP::LinearProgram; as::Symbol = :pair)
if as == :pair
return(dehomogenize(LP.polymake_lp.LINEAR_OBJECTIVE),LP.polymake_lp.LINEAR_OBJECTIVE[1])
return dehomogenize(LP.polymake_lp.LINEAR_OBJECTIVE),LP.polymake_lp.LINEAR_OBJECTIVE[1]
elseif as == :function
(c,k) = objective_function(LP, as = :pair)
function f(x)
eval=sum(x.*c)+k
end
return(f)
return x -> sum(x.*c)+k
else
throw(ArgumentError("Unsupported `as` argument :" * string(as)))
end
end

Expand All @@ -86,18 +83,18 @@ feasible_region(lp::LinearProgram) = lp.feasible_region
function minimal_vertex(lp::LinearProgram)
mv = lp.polymake_lp.MINIMAL_VERTEX
if mv != nothing
return(dehomogenize(mv))
return dehomogenize(mv)
else
return(nothing)
return nothing
end
end

function maximal_vertex(lp::LinearProgram)
mv = lp.polymake_lp.MAXIMAL_VERTEX
if mv != nothing
return(dehomogenize(mv))
return dehomogenize(mv)
else
return(nothing)
return nothing
end
end

Expand All @@ -114,9 +111,9 @@ Gives a pair `(m,v)` where the optimal value `m` of the objective
"""
function solve_lp(lp::LinearProgram)
if lp.convention == :max
return(maximal_value(lp),maximal_vertex(lp))
return maximal_value(lp),maximal_vertex(lp)
elseif lp.convention == :min
return(minimal_value(lp),minimal_vertex(lp))
return minimal_value(lp),minimal_vertex(lp)
end
end

Expand Down
16 changes: 8 additions & 8 deletions src/Polytopes/Polyhedron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ Base.length(iter::VertexPointIterator) = n_vertices(iter.p)

Returns the vertices of a polyhedron.
"""
function vertices(P::Polyhedron; as = :points)
function vertices(P::Polyhedron; as::Symbol = :points)
if as == :points
VertexPointIterator(P)
elseif as == :point_matrix
return(decompose_vdata(pm_polytope(P).VERTICES).vertices)
return decompose_vdata(pm_polytope(P).VERTICES).vertices
else
throw(ArgumentError("Unsupported `as` argument :" * string(as)))
end
Expand Down Expand Up @@ -124,11 +124,11 @@ n_vertices(P::Polyhedron) = pm_polytope(P).N_VERTICES - n_rays(P)

Returns minimal set of generators of the cone of unbounded directions of a polyhedron.
"""
function rays(P::Polyhedron; as = :points)
function rays(P::Polyhedron; as::Symbol = :points)
if as == :points
PolyhedronRayIterator(P)
elseif as == :point_matrix
return(decompose_vdata(pm_polytope(P).VERTICES).rays)
return decompose_vdata(pm_polytope(P).VERTICES).rays
else
throw(ArgumentError("Unsupported `as` argument :" * string(as)))
end
Expand Down Expand Up @@ -183,13 +183,13 @@ The allowed values for `as` are
* `polyhedra`: Returns for each facet its realization as a polyhedron
* `halfspace_matrix_pair`: Returns `(A,b)` such `P={x | Ax ≦ b }`
"""
function facets(P::Polyhedron; as = :halfspaces)
function facets(P::Polyhedron; as::Symbol = :halfspaces)
if as == :halfspaces
PolyhedronFacetHalfspaceIterator(P)
elseif as == :polyhedra
PolyhedronFacetPolyhedronIterator(P)
elseif as == :halfspace_matrix_pair
return(decompose_hdata(pm_polytope(P).FACETS))
return decompose_hdata(pm_polytope(P).FACETS)
else
throw(ArgumentError("Unsupported `as` argument :" * string(as)))
end
Expand Down Expand Up @@ -318,7 +318,7 @@ Produces a function `h(ω) = max{dot(x,ω) | x ∈ P}`. max may be changed
function support_function(P::Polyhedron; convention = :max)
function h(ω::AbstractVector)
lp=LinearProgram(P,ω; convention = convention)
return(solve_lp(lp)[1])
return solve_lp(lp)[1]
end
return(h)
return h
end
4 changes: 2 additions & 2 deletions src/Polytopes/helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ stack(A::AbstractVector, B::AbstractVector) = isempty(A) ? B : [A'; B']
#=
function stack(A::Array{Polymake.VectorAllocated{Polymake.Rational},1})
if length(A)==2
return(stack(A[1],A[2]))
return stack(A[1],A[2])
end
M=stack(A[1],A[2])
for i in 3:length(A)
M=stack(M,A[i])
end
return(M)
return M
end
=#

Expand Down