Skip to content
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
4 changes: 4 additions & 0 deletions src/Sets/HPolytope/init_LazySets.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
using .LazySets.EmptySetModule: EmptySet
using .LazySets.HPolygonModule: HPolygon
using .LazySets.IntervalModule: Interval
using .LazySets.SingletonModule: Singleton
using .LazySets.VPolygonModule: VPolygon
using .LazySets.VPolytopeModule: VPolytope
21 changes: 16 additions & 5 deletions src/Sets/HPolytope/rand.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,20 @@ function rand(::Type{HPolytope};
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int,Nothing}=nothing,
num_vertices::Int=-1)
require(@__MODULE__, :Polyhedra; fun_name="rand")
rng = reseed!(rng, seed)
vpolytope = rand(VPolytope; N=N, dim=dim, rng=rng, seed=seed,
num_vertices=num_vertices)
return convert(HPolytope, vpolytope)
require(@__MODULE__, :LazySets; fun_name="rand")

if num_vertices == 1
P = rand(Singleton; N=N, dim=dim, rng=rng, seed=seed)
elseif dim == 1
if num_vertices ∉ (-1, 2)
throw(ArgumentError("creating a 1D random polytope is only supported for 2 vertices"))
end
P = rand(Interval; N=N, dim=dim, rng=rng, seed=seed)
elseif dim == 2
P = rand(VPolygon; N=N, dim=dim, rng=rng, seed=seed, num_vertices=num_vertices)
else
rng = reseed!(rng, seed)
P = rand(VPolytope; N=N, dim=dim, rng=rng, seed=seed, num_vertices=num_vertices)
end
return convert(HPolytope, P)
end
23 changes: 18 additions & 5 deletions test/Sets/Polytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ global test_suite_polyhedra

for N in [Float64, Rational{Int}, Float32]
# random polytopes
if test_suite_polyhedra
rand(HPolytope)
else
@test_throws AssertionError rand(HPolytope)
end
@test_throws ArgumentError rand(HPolytope; N=N, dim=1, num_vertices=3)
@test_broken rand(HPolytope; N=N, dim=1, num_vertices=0)
rand(VPolytope)

# -----
Expand Down Expand Up @@ -347,6 +344,22 @@ end

# tests that only work with Float64 and Float32
for N in [Float64, Float32]
# rand
@test_broken rand(HPolytope; N=N, dim=2, num_vertices=0) # TODO fix
@test_broken rand(HPolytope; N=N, dim=3, num_vertices=0) # TODO fix
p = rand(HPolytope; N=N, num_vertices=1)
@test p isa HPolytope{N} && dim(p) == 2
p = rand(HPolytope; N=N, dim=1)
@test p isa HPolytope{N} && dim(p) == 1 # TODO fix
p = rand(HPolytope; N=N, dim=2)
@test p isa HPolytope{N} && dim(p) == 2
if test_suite_polyhedra
p = rand(HPolytope; N=N, dim=3)
@test p isa HPolytope{Float64} && dim(p) == 3
else
@test_throws AssertionError rand(HPolytope; N=N, dim=3)
end

# normalization
p1 = HPolytope([HalfSpace(N[1e5], N(3e5)), HalfSpace(N[-2e5], N(4e5))])
p2 = normalize(p1)
Expand Down
Loading