From e40dbfd84162886da86e0a208b92d71d93894f0b Mon Sep 17 00:00:00 2001 From: schillic Date: Sun, 15 Dec 2024 22:42:23 +0100 Subject: [PATCH] simplify 'rand' of HPolytope in 1D/2D or 1 vertex --- src/Sets/HPolytope/init_LazySets.jl | 4 ++++ src/Sets/HPolytope/rand.jl | 21 ++++++++++++++++----- test/Sets/Polytope.jl | 23 ++++++++++++++++++----- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/Sets/HPolytope/init_LazySets.jl b/src/Sets/HPolytope/init_LazySets.jl index adca20c9ce..b4395cd8cc 100644 --- a/src/Sets/HPolytope/init_LazySets.jl +++ b/src/Sets/HPolytope/init_LazySets.jl @@ -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 diff --git a/src/Sets/HPolytope/rand.jl b/src/Sets/HPolytope/rand.jl index d93ca2a4b2..d4f99d0009 100644 --- a/src/Sets/HPolytope/rand.jl +++ b/src/Sets/HPolytope/rand.jl @@ -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 diff --git a/test/Sets/Polytope.jl b/test/Sets/Polytope.jl index 646de47125..1dfc42fed0 100644 --- a/test/Sets/Polytope.jl +++ b/test/Sets/Polytope.jl @@ -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) # ----- @@ -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)