Skip to content

Commit

Permalink
random-zonotope generation
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Nov 3, 2018
1 parent 2117b4a commit 1f62cd9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/src/lib/representations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
49 changes: 48 additions & 1 deletion src/Zonotope.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Base:
import Base: rand,

export Zonotope,
order,
Expand Down Expand Up @@ -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 ---


Expand Down
3 changes: 3 additions & 0 deletions test/unit_Zonotope.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 1f62cd9

Please sign in to comment.