Skip to content

Commit

Permalink
random-ball generation
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Nov 2, 2018
1 parent 7388c13 commit 38c3d99
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/src/lib/representations.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Ball2
σ(::AbstractVector{AbstractFloat}, ::Ball2{AbstractFloat})
∈(::AbstractVector{AbstractFloat}, ::Ball2{AbstractFloat})
center(::Ball2)
rand(::Type{Ball2})
```
Inherited from [`LazySet`](@ref):
* [`norm`](@ref norm(::LazySet, ::Real))
Expand All @@ -43,6 +44,7 @@ center(::BallInf)
radius(::BallInf, ::Real)
radius_hyperrectangle(::BallInf)
radius_hyperrectangle(::BallInf, ::Int)
rand(::Type{BallInf})
```
Inherited from [`LazySet`](@ref):
* [`diameter`](@ref diameter(::LazySet, ::Real))
Expand Down Expand Up @@ -72,6 +74,7 @@ Ball1
∈(::AbstractVector{Real}, ::Ball1{Real})
vertices_list(::Ball1)
center(::Ball1)
rand(::Type{Ball1})
constraints_list(::Ball1)
```
Inherited from [`LazySet`](@ref):
Expand All @@ -95,6 +98,7 @@ Ballp
σ(::AbstractVector{AbstractFloat}, ::Ballp{AbstractFloat})
∈(::AbstractVector{AbstractFloat}, ::Ballp{AbstractFloat})
center(::Ballp)
rand(::Type{Ballp})
```
Inherited from [`LazySet`](@ref):
* [`norm`](@ref norm(::LazySet, ::Real))
Expand Down
39 changes: 38 additions & 1 deletion src/Ball1.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Base.∈
import Base: rand,

export Ball1

Expand Down Expand Up @@ -183,6 +184,42 @@ function ∈(x::AbstractVector{N}, B::Ball1{N})::Bool where {N<:Real}
return sum <= B.radius
end

"""
rand(::Type{Ball1}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing
)::Ball1{N}
Create a random ball in the 1-norm.
### Input
- `Ball1` -- 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 ball in the 1-norm.
### Algorithm
All numbers are normally distributed with mean 0 and standard deviation 1.
Additionally, the radius is nonnegative.
"""
function rand(::Type{Ball1};
N::Type{<:Real}=Float64,
dim::Int=2,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing
)::Ball1{N}
rng = reseed(rng, seed)
center = randn(rng, N, dim)
radius = abs(randn(rng, N))
return Ball1(center, radius)
end

"""
constraints_list(P::Ball1{N})::Vector{LinearConstraint{N}} where {N<:Real}
Expand Down
39 changes: 38 additions & 1 deletion src/Ball2.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Base.∈
import Base: rand,

export Ball2

Expand Down Expand Up @@ -173,3 +174,39 @@ function ∈(x::AbstractVector{N}, B::Ball2{N})::Bool where {N<:AbstractFloat}
end
return sqrt(sum) <= B.radius
end

"""
rand(::Type{Ball2}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing
)::Ball2{N}
Create a random ball in the 2-norm.
### Input
- `Ball2` -- 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 ball in the 2-norm.
### Algorithm
All numbers are normally distributed with mean 0 and standard deviation 1.
Additionally, the radius is nonnegative.
"""
function rand(::Type{Ball2};
N::Type{<:Real}=Float64,
dim::Int=2,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing
)::Ball2{N}
rng = reseed(rng, seed)
center = randn(rng, N, dim)
radius = abs(randn(rng, N))
return Ball2(center, radius)
end
39 changes: 39 additions & 0 deletions src/BallInf.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Base.rand

export BallInf

"""
Expand Down Expand Up @@ -51,6 +53,7 @@ end
BallInf(center::Vector{N}, radius::N) where {N<:Real} =
BallInf{N}(center, radius)


# --- AbstractHyperrectangle interface functions ---


Expand Down Expand Up @@ -136,3 +139,39 @@ The radius is defined as the radius of the enclosing ball of the given
function radius(B::BallInf, p::Real=Inf)::Real
return (p == Inf) ? B.radius : norm(fill(B.radius, dim(B)), p)
end

"""
rand(::Type{BallInf}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing
)::BallInf{N}
Create a random ball in the infinity norm.
### Input
- `BallInf` -- 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 ball in the infinity norm.
### Algorithm
All numbers are normally distributed with mean 0 and standard deviation 1.
Additionally, the radius is nonnegative.
"""
function rand(::Type{BallInf};
N::Type{<:Real}=Float64,
dim::Int=2,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing
)::BallInf{N}
rng = reseed(rng, seed)
center = randn(rng, N, dim)
radius = abs(randn(rng, N))
return BallInf(center, radius)
end
43 changes: 42 additions & 1 deletion src/Ballp.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Base.∈
import Base: rand,

export Ballp

Expand Down Expand Up @@ -195,3 +196,43 @@ function ∈(x::AbstractVector{N}, B::Ballp{N})::Bool where {N<:AbstractFloat}
end
return sum^(one(N)/B.p) <= B.radius
end

"""
rand(::Type{Ballp}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing
)::Ballp{N}
Create a random ball in the p-norm.
### Input
- `Ballp` -- 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 ball in the p-norm.
### Algorithm
The center and radius are normally distributed with mean 0 and standard
deviation 1.
Additionally, the radius is nonnegative.
The p-norm is a normally distributed number ≥ 1 with mean 1 and standard
deviation 1.
"""
function rand(::Type{Ballp};
N::Type{<:Real}=Float64,
dim::Int=2,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing
)::Ballp{N}
rng = reseed(rng, seed)
p = one(N) + abs(randn(rng, N))
center = randn(rng, N, dim)
radius = abs(randn(rng, N))
return Ballp(p, center, radius)
end
3 changes: 3 additions & 0 deletions test/unit_Ball1.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
for N in [Float64, Rational{Int}, Float32]
# random ball
rand(Ball1)

# 1D Ball1
b = Ball1(N[0], N(1))
# dimension
Expand Down
3 changes: 3 additions & 0 deletions test/unit_Ball2.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
for N in [Float64, Float32]
# random ball
rand(Ball2)

# 1D Ball2
b = Ball2(N[0], N(1))
# Test Dimension
Expand Down
3 changes: 3 additions & 0 deletions test/unit_BallInf.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
for N in [Float64, Rational{Int}, Float32]
# random ball
rand(BallInf)

# 1D BallInf
b = BallInf(N[0], N(1))
# Test Dimension
Expand Down
3 changes: 3 additions & 0 deletions test/unit_Ballp.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
for N in [Float64, Float32]
# random ball
rand(Ballp)

# 1D Ball3
b = Ballp(N(3), N[0], N(1))
# dimension
Expand Down

0 comments on commit 38c3d99

Please sign in to comment.