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

Non-uniform split of hyperrectangles #3505

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ IntervalArithmetic = "0.15 - 0.21, =0.21.2" # v0.22 removed IntervalBox
JuMP = "0.21 - 0.23, 1"
LinearAlgebra = "<0.0.1, 1.6"
Random = "<0.0.1, 1.6"
ReachabilityBase = "0.2.1"
ReachabilityBase = "0.2.5"
RecipesBase = "0.6 - 0.8, 1"
Reexport = "0.2, 1"
Requires = "0.5, 1"
Expand Down
32 changes: 32 additions & 0 deletions src/Interfaces/AbstractHyperrectangle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,38 @@ function split(H::AbstractHyperrectangle{N},
return result
end

# non-uniform partition
function split(H::AbstractHyperrectangle{N},
bounds::AbstractVector{<:AbstractVector{<:Real}}) where {N}
@assert length(bounds) == dim(H) "the bounds ($(length(bounds))) must be " *
"specified in each dimension ($(dim(H)))"
lo = low(H)
hi = high(H)
n = length(lo)

bs = [length(b) + 1 for b in bounds]
total_number = prod(bs)
result = Vector{Hyperrectangle{N,Vector{N},Vector{N}}}(undef, total_number)
for (j, vj) in enumerate(CartesianIterator(bs, false))
l = Vector{N}(undef, n)
h = Vector{N}(undef, n)
for (i, ei) in enumerate(vj)
if ei == 1
l[i] = lo[i]
else
l[i] = bounds[i][ei-1]
end
if ei == bs[i]
h[i] = hi[i]
else
h[i] = bounds[i][ei]
end
end
result[j] = Hyperrectangle(low=l, high=h)
end
return result
end

"""
rectify(H::AbstractHyperrectangle)

Expand Down
27 changes: 27 additions & 0 deletions test/Sets/Hyperrectangle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,33 @@ for N in [Float64, Rational{Int}, Float32]
H = Hyperrectangle(SA[N(0), N(0)], SA[N(1), N(2)])
S = split(H, [2, 2])
@test S isa Vector{typeof(H)}
# split with non-uniform partition
H = Hyperrectangle(low=N[1, 2], high=N[5, 6])
S = split(H, [N[3], N[4]])
@test ispermutation(S, [Hyperrectangle(low=N[1, 2], high=N[3, 4]),
Hyperrectangle(low=N[3, 2], high=N[5, 4]),
Hyperrectangle(low=N[1, 4], high=N[3, 6]),
Hyperrectangle(low=N[3, 4], high=N[5, 6])])
S = split(H, [N[2, 3], N[4, 5]])
@test ispermutation(S, [Hyperrectangle(low=N[1, 2], high=N[2, 4]),
Hyperrectangle(low=N[2, 2], high=N[3, 4]),
Hyperrectangle(low=N[3, 2], high=N[5, 4]),
Hyperrectangle(low=N[1, 4], high=N[2, 5]),
Hyperrectangle(low=N[2, 4], high=N[3, 5]),
Hyperrectangle(low=N[3, 4], high=N[5, 5]),
Hyperrectangle(low=N[1, 5], high=N[2, 6]),
Hyperrectangle(low=N[2, 5], high=N[3, 6]),
Hyperrectangle(low=N[3, 5], high=N[5, 6])])
H = Hyperrectangle(low=N[1, 2, 3], high=N[5, 6, 7])
S = split(H, [N[3], N[4], N[5]])
@test ispermutation(S, [Hyperrectangle(low=N[1, 2, 3], high=N[3, 4, 5]),
Hyperrectangle(low=N[1, 2, 5], high=N[3, 4, 7]),
Hyperrectangle(low=N[1, 4, 3], high=N[3, 6, 5]),
Hyperrectangle(low=N[1, 4, 5], high=N[3, 6, 7]),
Hyperrectangle(low=N[3, 2, 3], high=N[5, 4, 5]),
Hyperrectangle(low=N[3, 2, 5], high=N[5, 4, 7]),
Hyperrectangle(low=N[3, 4, 3], high=N[5, 6, 5]),
Hyperrectangle(low=N[3, 4, 5], high=N[5, 6, 7])])

# subset
H1 = Hyperrectangle(N[1, 3], N[0.5, 0.5])
Expand Down
Loading