Skip to content

Commit

Permalink
random generation for unbounded sets
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Nov 3, 2018
1 parent 415109f commit ed91e3c
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/src/lib/representations.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ dim(::HalfSpace)
σ(::AbstractVector{Real}, ::HalfSpace{Real})
∈(::AbstractVector{Real}, ::HalfSpace{Real})
an_element(::HalfSpace{N}) where {N<:Real}
rand(::Type{HalfSpace})
isempty(::HalfSpace)
constraints_list(::HalfSpace{N}) where {N<:Real}
constrained_dimensions(::HalfSpace{N}) where {N<:Real}
Expand All @@ -177,6 +178,7 @@ dim(::Hyperplane)
σ(::AbstractVector{Real}, ::Hyperplane{Real})
∈(::AbstractVector{Real}, ::Hyperplane{Real})
an_element(::Hyperplane{N}) where {N<:Real}
rand(::Type{Hyperplane})
isempty(::Hyperplane)
constrained_dimensions(::Hyperplane{N}) where {N<:Real}
```
Expand Down Expand Up @@ -255,6 +257,7 @@ dim(::Line)
σ(::AbstractVector{Real}, ::Line{Real})
∈(::AbstractVector{Real}, ::Line{Real})
an_element(::Line{N}) where {N<:Real}
rand(::Type{Line})
isempty(::Line)
constrained_dimensions(::Line{N}) where {N<:Real}
```
Expand Down
43 changes: 42 additions & 1 deletion src/HalfSpace.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Base: , isempty
import Base: rand,
,
isempty

export HalfSpace, LinearConstraint,
an_element,
Expand Down Expand Up @@ -119,6 +121,45 @@ function ∈(x::AbstractVector{N}, hs::HalfSpace{N})::Bool where {N<:Real}
return dot(x, hs.a) <= hs.b
end

"""
rand(::Type{HalfSpace}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing
)::HalfSpace{N}
Create a random half-space.
### Input
- `HalfSpace` -- 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 half-space.
### Algorithm
All numbers are normally distributed with mean 0 and standard deviation 1.
Additionally, the constraint `a` is nonzero.
"""
function rand(::Type{HalfSpace};
N::Type{<:Real}=Float64,
dim::Int=2,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing
)::HalfSpace{N}
rng = reseed(rng, seed)
a = randn(rng, N, dim)
while iszero(a)
a = randn(rng, N, dim)
end
b = randn(rng, N)
return HalfSpace(a, b)
end

"""
isempty(hs::HalfSpace)::Bool
Expand Down
43 changes: 42 additions & 1 deletion src/Hyperplane.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Base: , isempty
import Base: rand,
,
isempty

export Hyperplane,
an_element
Expand Down Expand Up @@ -110,6 +112,45 @@ function ∈(x::AbstractVector{N}, hp::Hyperplane{N})::Bool where {N<:Real}
return dot(x, hp.a) == hp.b
end

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

"""
isempty(hp::Hyperplane)::Bool
Expand Down
44 changes: 43 additions & 1 deletion src/Line.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Base: , isempty
import Base: rand,
,
isempty

export Line,
an_element
Expand Down Expand Up @@ -175,6 +177,46 @@ function ∈(x::AbstractVector{N}, L::Line{N})::Bool where {N<:Real}
return dot(L.a, x) == L.b
end

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

"""
isempty(L::Line)::Bool
Expand Down
3 changes: 3 additions & 0 deletions test/unit_HalfSpace.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
for N in [Float64, Rational{Int}, Float32]
# random half-space
rand(HalfSpace)

# normal constructor
normal = ones(N, 3)
hs = HalfSpace(normal, N(5))
Expand Down
3 changes: 3 additions & 0 deletions test/unit_Hyperplane.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
for N in [Float64, Rational{Int}, Float32]
# random hyperplane
rand(Hyperplane)

# normal constructor
a = ones(N, 3)
hp = Hyperplane(a, N(5))
Expand Down
3 changes: 3 additions & 0 deletions test/unit_Line.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
for N in [Float64, Rational{Int}, Float32]
# random line
rand(Line)

# construction
l1 = Line(N[0, 1], N(1))
l2 = Line(N[1, 0], N(1))
Expand Down

0 comments on commit ed91e3c

Please sign in to comment.