Skip to content

Commit

Permalink
random-polyhedron generation
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Nov 8, 2018
1 parent c59630b commit 4007634
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/src/lib/representations.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ The following methods are specific for polytopes.
The following methods are specific for `HPolyhedron`.

```@docs
rand(::Type{HPolyhedron})
vertices_list(::HPolyhedron{Real})
singleton_list(::HPolyhedron{N}) where {N<:Real}
```
Expand Down
43 changes: 43 additions & 0 deletions src/HPolyhedron.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using MathProgBase, GLPKMathProgInterface

import Base: isempty,
rand,
convert

export HPolyhedron,
Expand Down Expand Up @@ -199,6 +200,48 @@ function ∈(x::AbstractVector{N}, P::HPoly{N})::Bool where {N<:Real}
return true
end

"""
rand(::Type{HPolyhedron}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing
)::HPolyhedron{N}
Create a polyhedron.
### Input
- `HPolyhedron` -- type for dispatch
- `N` -- (optional, default: `Float64`) numeric type
- `dim` -- (optional, default: 2) dimension (is ignored)
- `rng` -- (optional, default: `GLOBAL_RNG`) random number generator
- `seed` -- (optional, default: `nothing`) seed for reseeding
### Output
A polyhedron.
### Algorithm
We first create a random polytope and then randomly remove some of the
constraints.
"""
function rand(::Type{HPolyhedron};
N::Type{<:Real}=Float64,
dim::Int=2,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing
)::HPolyhedron{N}
rng = reseed(rng, seed)
P = rand(HPolytope; N=N, dim=dim, rng=rng)
constraints_P = constraints_list(P)
constraints_Q = Vector{LinearConstraint{N}}()
for i in 1:length(constraints_P)
if rand(Bool)
push!(constraints_Q, constraints_P[i])
end
end
return HPolyhedron(constraints_Q)
end


# ===========================================
# HPolyhedron and HPolytope's shared methods
Expand Down
8 changes: 2 additions & 6 deletions test/unit_Polyhedron.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
global test_suite_polyhedra

for N in [Float64, Rational{Int}, Float32]
# -----
# H-rep
# -----
# random polyhedron
rand(HPolyhedron)

# constructor from matrix and vector
A = [N(1) N(2); N(-1) N(1)]
Expand Down Expand Up @@ -71,9 +70,6 @@ unconstrained_HPolyhedron = HPolyhedron()
# Polyhedra tests that only work with Float64
if test_suite_polyhedra
for N in [Float64]
# -----
# H-rep
# -----
# support function/vector
d = N[1, 0]
p_unbounded = HPolyhedron([LinearConstraint(N[-1, 0], N(0))])
Expand Down

0 comments on commit 4007634

Please sign in to comment.