Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3203 - Make rand of HParallelotope return a nonempty set #3221

Merged
merged 2 commits into from
Feb 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/Sets/HParallelotope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,16 +344,42 @@ A random parallelotope.
### Notes

All numbers are normally distributed with mean 0 and standard deviation 1.

### Algorithm

The directions matrix and offset vector are created randomly. On average there
is a good chance that this resulting set is empty. We then modify the offset to
ensure non-emptiness.

There is a chance that the resulting set represents an unbounded set. This
implementation checks for that case and then samples a new set.
"""
function rand(::Type{HParallelotope};
N::Type{<:Real}=Float64,
dim::Int=2,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing)
rng = reseed(rng, seed)
D = randn(rng, N, dim, dim)
offset = randn(rng, N, 2 * dim)
return HParallelotope(D, offset)

while true
D = randn(rng, N, dim, dim)
offset = randn(rng, N, 2 * dim)

# make sure that the set is not empty:
# `offset[i] >= -offset[i-dim]` for all `i ∈ dim+1:2*dim`
@inbounds for i in dim+1:2*dim
offset[i] = -offset[i-dim] + abs(offset[i])
end

P = HParallelotope(D, offset)

# convert to polyhedron to check boundedness
Q = HPolyhedron(vcat(P.directions, -P.directions), P.offset)
if isbounded(Q)
return P
end
# set is unbounded; sample a new set in the next iteration
end
end

"""
Expand Down