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

Add constrained_dimensions method for HPolyhedron #933

Merged
merged 2 commits into from
Nov 30, 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
2 changes: 1 addition & 1 deletion docs/src/lib/representations.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,6 @@ vertices_list(::HPolytope{N}) where {N<:Real}

Inherited from [`AbstractPolytope`](@ref):
* [`singleton_list`](@ref singleton_list(::AbstractPolytope{N}) where {N<:Real})
The following methods are specific for polytopes.

#### Polyhedra

Expand All @@ -452,6 +451,7 @@ The following methods are specific for `HPolyhedron`.
rand(::Type{HPolyhedron})
vertices_list(::HPolyhedron{N}) where {N<:Real}
singleton_list(::HPolyhedron{N}) where {N<:Real}
constrained_dimensions(::HPolyhedron{N}) where {N<:Real}
```

### Vertex representation
Expand Down
32 changes: 31 additions & 1 deletion src/HPolyhedron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export HPolyhedron,
singleton_list,
isempty,
remove_redundant_constraints,
remove_redundant_constraints!
remove_redundant_constraints!,
constrained_dimensions

"""
HPolyhedron{N<:Real} <: LazySet{N}
Expand Down Expand Up @@ -247,6 +248,35 @@ function rand(::Type{HPolyhedron};
return HPolyhedron(constraints_Q)
end

"""
constrained_dimensions(P::HPolyhedron{N})::Vector{Int} where {N<:Real}

Return the indices in which a polyhedron in constraint representation is
constrained.

### Input

- `P` -- polyhedron in constraint representation

### Output

A vector of ascending indices `i` such that the polyhedron is constrained in
dimension `i`.

### Examples

A 2D polyhedron with constraint ``x1 ≥ 0`` is constrained in dimension 1 only.
"""
function constrained_dimensions(P::HPolyhedron{N})::Vector{Int} where {N<:Real}
zero_indices = zeros(Int, dim(P))
for constraint in P.constraints
for i in constrained_dimensions(constraint)
zero_indices[i] = i
end
end
return filter(x -> x != 0, zero_indices)
end


# ===========================================
# HPolyhedron and HPolytope's shared methods
Expand Down
2 changes: 1 addition & 1 deletion src/HalfSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ Return the indices in which a half-space is constrained.
A vector of ascending indices `i` such that the half-space is constrained in
dimension `i`.

### Notes
### Examples

A 2D half-space with constraint ``x1 ≥ 0`` is constrained in dimension 1 only.
"""
Expand Down
2 changes: 1 addition & 1 deletion src/Hyperplane.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ Return the indices in which a hyperplane is constrained.
A vector of ascending indices `i` such that the hyperplane is constrained in
dimension `i`.

### Notes
### Examples

A 2D hyperplane with constraint ``x1 = 0`` is constrained in dimension 1 only.
"""
Expand Down
2 changes: 1 addition & 1 deletion src/Line.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ Return the indices in which a line is constrained.
A vector of ascending indices `i` such that the line is constrained in dimension
`i`.

### Notes
### Examples

A line with constraint ``x1 = 0`` is constrained in dimension 1 only.
"""
Expand Down
5 changes: 5 additions & 0 deletions test/unit_Polyhedron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ for N in [Float64, Rational{Int}, Float32]
@test ∈(N[5 / 4, 7 / 4], p)
@test !∈(N[4, 1], p)

# constrained dimensions
@test constrained_dimensions(p) == [1, 2]
@test constrained_dimensions(
HPolyhedron{N}([LinearConstraint(N[1, 0], N(1))])) == [1]

if test_suite_polyhedra
# conversion to and from Polyhedra's VRep data structure
cl = constraints_list(HPolyhedron(polyhedron(p)))
Expand Down