Skip to content

Commit

Permalink
random generation for several set types
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Nov 3, 2018
1 parent ddb8285 commit ecf4208
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 4 deletions.
5 changes: 5 additions & 0 deletions docs/src/lib/representations.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ dim(::EmptySet)
σ(::AbstractVector{Real}, ::EmptySet{Real})
∈(::AbstractVector{Real}, ::EmptySet{Real})
an_element(::EmptySet)
rand(::Type{EmptySet})
isempty(::EmptySet)
norm(::EmptySet, ::Real)
radius(::EmptySet, ::Real)
Expand Down Expand Up @@ -230,6 +231,7 @@ high(::Interval)
+(::Interval, ::Interval)
-(::Interval, ::Interval)
*(::Interval, ::Interval)
rand(::Type{Interval})
```
Inherited from [`LazySet`](@ref):
* [`diameter`](@ref diameter(::LazySet, ::Real))
Expand Down Expand Up @@ -268,6 +270,7 @@ LineSegment
dim(::LineSegment)
σ(::AbstractVector{Real}, ::LineSegment{Real})
∈(::AbstractVector{Real}, ::LineSegment{Real})
rand(::Type{LineSegment})
LazySets.halfspace_left(::LineSegment)
LazySets.halfspace_right(::LineSegment)
LazySets.constraints_list(::LineSegment)
Expand Down Expand Up @@ -438,6 +441,7 @@ Inherited from [`AbstractPolytope`](@ref):

```@docs
Singleton
rand(::Type{Singleton})
element(::Singleton)
element(::Singleton, ::Int)
```
Expand Down Expand Up @@ -474,6 +478,7 @@ ZeroSet
dim(::ZeroSet)
σ(::AbstractVector{N}, ::ZeroSet{N}) where {N<:Real}
∈(::AbstractVector{N}, ::ZeroSet{N}) where {N<:Real}
rand(::Type{ZeroSet})
element(::ZeroSet)
element(::ZeroSet, ::Int)
linear_map(::AbstractMatrix, ::ZeroSet{N}) where {N<:Real}
Expand Down
37 changes: 36 additions & 1 deletion src/EmptySet.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Base: , isempty
import Base: rand,
,
isempty

export EmptySet, ∅,
an_element
Expand All @@ -20,6 +22,10 @@ An `EmptySet` instance of type `Float64`.
"""
const= EmptySet{Float64}()


# --- LazySet interface functions ---


"""
dim(∅::EmptySet)
Expand Down Expand Up @@ -96,6 +102,35 @@ function an_element(∅::EmptySet)
error("an empty set does not have any element")
end

"""
rand(::Type{EmptySet}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing
)::EmptySet{N}
Create an empty set (note that there is nothing to randomize).
### Input
- `EmptySet` -- type for dispatch
- `N` -- (optional, default: `Float64`) numeric type
- `dim` -- (optional, default: 0) dimension (is ignored)
- `rng` -- (optional, default: `GLOBAL_RNG`) random number generator
- `seed` -- (optional, default: `nothing`) seed for reseeding
### Output
The (only) empty set of the given numeric type.
"""
function rand(::Type{EmptySet};
N::Type{<:Real}=Float64,
dim::Int=0,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing
)::EmptySet{N}
rng = reseed(rng, seed)
return EmptySet{N}()
end

"""
isempty(∅::EmptySet)::Bool
Expand Down
38 changes: 37 additions & 1 deletion src/Interval.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import IntervalArithmetic
import IntervalArithmetic: AbstractInterval
import Base:+, -, *, ,
import Base: +, -, *, , , rand

export Interval,
dim, σ, center,
Expand Down Expand Up @@ -288,6 +288,42 @@ function an_element(x::Interval{N})::Vector{N} where {N<:Real}
return [low(x)]
end

"""
rand(::Type{Interval}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing
)::Interval{N}
Create a random interval.
### Input
- `Interval` -- type for dispatch
- `N` -- (optional, default: `Float64`) numeric type
- `dim` -- (optional, default: 1) dimension
- `rng` -- (optional, default: `GLOBAL_RNG`) random number generator
- `seed` -- (optional, default: `nothing`) seed for reseeding
### Output
A random interval.
### Algorithm
All numbers are normally distributed with mean 0 and standard deviation 1.
"""
function rand(::Type{Interval};
N::Type{<:Real}=Float64,
dim::Int=1,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing
)::Interval{N}
@assert dim == 1 "an Interval must have dimension 1"
rng = reseed(rng, seed)
x = randn(rng, N)
y = randn(rng, N)
return x < y ? Interval(x, y) : Interval(y, x)
end

"""
vertices_list(x::Interval)
Expand Down
43 changes: 42 additions & 1 deletion src/LineSegment.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Base.∈
import Base: rand,

export LineSegment,
halfspace_left, halfspace_right,
Expand Down Expand Up @@ -212,6 +213,46 @@ function vertices_list(L::LineSegment{N}
end


# --- LazySet interface functions ---


"""
rand(::Type{LineSegment}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing
)::LineSegment{N}
Create a random line segment.
### Input
- `LineSegment` -- 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
### Output
A random line segment.
### Algorithm
All numbers are normally distributed with mean 0 and standard deviation 1.
"""
function rand(::Type{LineSegment};
N::Type{<:Real}=Float64,
dim::Int=2,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing
)::LineSegment{N}
@assert dim == 2 "a LineSegment must have dimension 2"
rng = reseed(rng, seed)
p = randn(rng, N, dim)
q = randn(rng, N, dim)
return LineSegment(p, q)
end


# --- LineSegment functions ---


Expand Down
41 changes: 41 additions & 0 deletions src/Singleton.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Base.rand

export Singleton

"""
Expand Down Expand Up @@ -51,3 +53,42 @@ The i-th entry of the element of the singleton.
function element(S::Singleton{N}, i::Int)::N where {N<:Real}
return S.element[i]
end


# --- LazySet interface functions ---


"""
rand(::Type{Singleton}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing
)::Singleton{N}
Create a random singleton.
### Input
- `Singleton` -- 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
### Output
A random singleton.
### Algorithm
The element is a normally distributed vector with entries of mean 0 and standard
deviation 1.
"""
function rand(::Type{Singleton};
N::Type{<:Real}=Float64,
dim::Int=2,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing
)::Singleton{N}
rng = reseed(rng, seed)
element = randn(rng, N, dim)
return Singleton(element)
end
32 changes: 31 additions & 1 deletion src/ZeroSet.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Base.∈
import Base: rand,

export ZeroSet,
linear_map
Expand Down Expand Up @@ -130,6 +131,35 @@ function ∈(x::AbstractVector{N}, Z::ZeroSet{N})::Bool where {N<:Real}
return all(i -> x[i] == zero_N, eachindex(x))
end

"""
rand(::Type{ZeroSet}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing
)::ZeroSet{N}
Create a zero set (note that there is nothing to randomize).
### Input
- `ZeroSet` -- 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
### Output
The (only) zero set of the given numeric type and dimension.
"""
function rand(::Type{ZeroSet};
N::Type{<:Real}=Float64,
dim::Int=2,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing
)::ZeroSet{N}
rng = reseed(rng, seed)
return ZeroSet{N}(dim)
end

"""
linear_map(M::AbstractMatrix, Z::ZeroSet{N}) where {N<:Real}
Expand Down
3 changes: 3 additions & 0 deletions test/unit_EmptySet.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
for N in [Float64, Rational{Int}, Float32]
# random empty set
rand(EmptySet)

E = EmptySet{N}()
B = BallInf(ones(N, 2), N(1))

Expand Down
3 changes: 3 additions & 0 deletions test/unit_Interval.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import IntervalArithmetic

for N in [Float64, Float32, Rational{Int}]
# random interval
rand(Interval)

# constructor from IntervalArithmetic.Interval
x = Interval(IntervalArithmetic.Interval(N(0), N(1)))

Expand Down
3 changes: 3 additions & 0 deletions test/unit_LineSegment.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
for N in [Float64, Rational{Int}, Float32]
# random line segment
rand(LineSegment)

# construction
p, q = N[1, 1], N[2, 2]
l = LineSegment(p, q)
Expand Down
3 changes: 3 additions & 0 deletions test/unit_Singleton.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
for N in [Float64, Rational{Int}, Float32]
# random singleton
rand(Singleton)

# 1D singleton
s = Singleton(N[1])
d = N[1]
Expand Down
3 changes: 3 additions & 0 deletions test/unit_ZeroSet.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
for N in [Float64, Rational{Int}, Float32]
# random zero set
rand(ZeroSet)

Z = ZeroSet{N}(2)
B = BallInf(ones(N, 2), N(1))

Expand Down

0 comments on commit ecf4208

Please sign in to comment.