Skip to content

Commit

Permalink
Merge pull request #3677 from JuliaReach/schillic/rand_hpolytope
Browse files Browse the repository at this point in the history
`HPolytope`: simplify `rand` in 1D/2D or for 1 vertex
  • Loading branch information
schillic authored Dec 16, 2024
2 parents 7283e28 + e40dbfd commit 5ad9432
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
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

0 comments on commit 5ad9432

Please sign in to comment.