From 1f62cd97925da1efa9e2e932f24ecb3a59273c9e Mon Sep 17 00:00:00 2001 From: schillic Date: Sat, 3 Nov 2018 10:04:13 +0100 Subject: [PATCH] random-zonotope generation --- docs/src/lib/representations.md | 1 + src/Zonotope.jl | 49 ++++++++++++++++++++++++++++++++- test/unit_Zonotope.jl | 3 ++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/docs/src/lib/representations.md b/docs/src/lib/representations.md index dee2cd8da4..fca2c53517 100644 --- a/docs/src/lib/representations.md +++ b/docs/src/lib/representations.md @@ -514,6 +514,7 @@ Inherited from [`AbstractSingleton`](@ref): Zonotope σ(::AbstractVector{Real}, ::Zonotope{Real}) ∈(::AbstractVector{Real}, ::Zonotope{Real}) +rand(::Type{Zonotope}) vertices_list(::Zonotope) center(::Zonotope) order(::Zonotope) diff --git a/src/Zonotope.jl b/src/Zonotope.jl index 9d3422e458..b5c2b73de4 100644 --- a/src/Zonotope.jl +++ b/src/Zonotope.jl @@ -1,4 +1,5 @@ -import Base:∈ +import Base: rand, + ∈ export Zonotope, order, @@ -238,6 +239,52 @@ function ∈(x::AbstractVector{N}, Z::Zonotope{N}; return (lp.status == :Optimal) # Infeasible or Unbounded => false end +""" + rand(::Type{Zonotope}; [N]::Type{<:Real}=Float64, [dim]::Int=2, + [rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing + )::Zonotope{N} + +Create a random zonotope. + +### Input + +- `Zonotope` -- type for dispatch +- `N` -- (optional, default: `Float64`) numeric type +- `dim` -- (optional, default: 2) dimension +- `rng` -- (optional, default: `GLOBAL_RNG`) random number generator +- `seed` -- (optional, default: `nothing`) seed for reseeding +- `num_generators` -- (optional, default: `-1`) number of generators of the + zonotope (see comment below) + +### Output + +A random zonotope. + +### Algorithm + +All numbers are normally distributed with mean 0 and standard deviation 1. + +The number of generators can be controlled with the argument `num_generators`. +For a negative value we choose a random number in the range `dim:2*dim` (except +if `dim == 1`, in which case we only create a single generator). +""" +function rand(::Type{Zonotope}; + N::Type{<:Real}=Float64, + dim::Int=2, + rng::AbstractRNG=GLOBAL_RNG, + seed::Union{Int, Nothing}=nothing, + num_generators::Int=-1 + )::Zonotope{N} + rng = reseed(rng, seed) + center = randn(rng, N, dim) + if num_generators < 0 + num_generators = (dim == 1) ? 1 : rand(dim:2*dim) + end + generators = randn(rng, N, dim, num_generators) + return Zonotope(center, generators) +end + + # --- Zonotope functions --- diff --git a/test/unit_Zonotope.jl b/test/unit_Zonotope.jl index 644741d616..1f61c0651a 100644 --- a/test/unit_Zonotope.jl +++ b/test/unit_Zonotope.jl @@ -1,4 +1,7 @@ for N in [Float64, Rational{Int}, Float32] + # random zonotope + rand(Zonotope) + # 1D Zonotope z = Zonotope(N[0], Matrix{N}(I, 1, 1)) # Test Dimension