From 8e12af529e0e8693760f4c92df012bf56efa1a8e Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 5 Dec 2018 18:25:55 +1100 Subject: [PATCH 1/5] Added lazy projection and exact decompose --- src/Approximations/decompositions.jl | 64 +++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/src/Approximations/decompositions.jl b/src/Approximations/decompositions.jl index b8a9bb4470..0a7b17e121 100644 --- a/src/Approximations/decompositions.jl +++ b/src/Approximations/decompositions.jl @@ -228,7 +228,7 @@ function decompose(S::LazySet{N}; if directions != nothing # template directions - # potentially defined option set_type is *ignored* + # potentially defined option set_type is *ignored* block_start = 1 @inbounds for bi in blocks push!(result, project(S, block_start:(block_start + bi - 1), directions(bi), n)) @@ -268,6 +268,42 @@ function decompose(S::LazySet{N}; return CartesianProductArray(result) end + +""" + decompose_explicit(S::LazySet{N}; + blocks::AbstractVector{Int}=default_block_structure(S, set_type) + )::CartesianProductArray where {N<:Real} + +Decompose a high-dimensional set into a Cartesian product of overapproximations +of the projections over the specified subspaces. + +### Input + +- `S` -- set +- `blocks` -- (optional, default: [2, …, 2] or [1, …, 1]) + block structure - a vector with the size of each block + +### Output + +A `CartesianProductArray` containing lazy low-dimensional exact +projections. +""" + +function decompose_explicit(S::LazySet{N}; + blocks::AbstractVector{Int}=default_block_structure(S, Interval) + )::CartesianProductArray where {N<:Real} + n = dim(S) + result = Vector{LazySet{N}}() + + block_start = 1 + @inbounds for bi in blocks + push!(result, project(S, block_start:(block_start + bi - 1), n)) + block_start += bi + end + + return CartesianProductArray(result) +end + """ project(S::LazySet{N}, block::AbstractVector{Int}, @@ -442,3 +478,29 @@ The template direction approximation of the projection of `S`. M = sparse(1:length(block), block, ones(N, length(block)), length(block), n) return overapproximate(M * S, directions) end + +""" + project(S::LazySet{N}, + block::AbstractVector{Int}, + n::Int + )::HPolytope where {N<:Real} + +Project a high-dimensional set to a low-dimensional set. + +### Input + +- `S` -- set +- `block` -- block structure - a vector with the dimensions of interest +- `n` -- (optional, default: `dim(S)`) ambient dimension of the set `S` + +### Output + +The lazy linear map of the projection of `S`. +""" +@inline function project(S::LazySet{N}, + block::AbstractVector{Int}, + n::Int=dim(S) + )::LinearMap where {N<:Real} + M = sparse(1:length(block), block, ones(N, length(block)), length(block), n) + return M * S +end From 28e57ba2f5f587651bafb6d41446d4ff87378a8d Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 6 Dec 2018 13:45:05 +1100 Subject: [PATCH 2/5] adding decomposition of exact projections --- src/Approximations/decompositions.jl | 47 ++++++---------------------- 1 file changed, 9 insertions(+), 38 deletions(-) diff --git a/src/Approximations/decompositions.jl b/src/Approximations/decompositions.jl index 0a7b17e121..33cacabbd9 100644 --- a/src/Approximations/decompositions.jl +++ b/src/Approximations/decompositions.jl @@ -217,7 +217,7 @@ julia> [typeof(ai) for ai in array(decompose(S, block_types=bt, ε=0.01))] ``` """ function decompose(S::LazySet{N}; - set_type::Type{<:Union{HPolygon, Hyperrectangle, Interval}}=Hyperrectangle, + set_type::Type{<:Union{HPolygon, Hyperrectangle, Interval, LinearMap}}=Hyperrectangle, ε::Real=Inf, blocks::AbstractVector{Int}=default_block_structure(S, set_type), block_types=Dict{Type{<:LazySet}, AbstractVector{<:AbstractVector{Int}}}(), @@ -226,7 +226,13 @@ function decompose(S::LazySet{N}; n = dim(S) result = Vector{LazySet{N}}() - if directions != nothing + if set_type == LinearMap + block_start = 1 + @inbounds for bi in blocks + push!(result, project(S, block_start:(block_start + bi - 1), n)) + block_start += bi + end + elseif directions != nothing # template directions # potentially defined option set_type is *ignored* block_start = 1 @@ -269,41 +275,6 @@ function decompose(S::LazySet{N}; end -""" - decompose_explicit(S::LazySet{N}; - blocks::AbstractVector{Int}=default_block_structure(S, set_type) - )::CartesianProductArray where {N<:Real} - -Decompose a high-dimensional set into a Cartesian product of overapproximations -of the projections over the specified subspaces. - -### Input - -- `S` -- set -- `blocks` -- (optional, default: [2, …, 2] or [1, …, 1]) - block structure - a vector with the size of each block - -### Output - -A `CartesianProductArray` containing lazy low-dimensional exact -projections. -""" - -function decompose_explicit(S::LazySet{N}; - blocks::AbstractVector{Int}=default_block_structure(S, Interval) - )::CartesianProductArray where {N<:Real} - n = dim(S) - result = Vector{LazySet{N}}() - - block_start = 1 - @inbounds for bi in blocks - push!(result, project(S, block_start:(block_start + bi - 1), n)) - block_start += bi - end - - return CartesianProductArray(result) -end - """ project(S::LazySet{N}, block::AbstractVector{Int}, @@ -483,7 +454,7 @@ end project(S::LazySet{N}, block::AbstractVector{Int}, n::Int - )::HPolytope where {N<:Real} + )::LinearMap where {N<:Real} Project a high-dimensional set to a low-dimensional set. From 01af155c54a45c3a9fc5e060eb7b634de0fc97d8 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 7 Dec 2018 14:00:35 +1100 Subject: [PATCH 3/5] refactoring according to @schillic comments --- src/Approximations/decompositions.jl | 15 +++++---------- test/unit_decompose.jl | 3 +++ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Approximations/decompositions.jl b/src/Approximations/decompositions.jl index 33cacabbd9..67331d142b 100644 --- a/src/Approximations/decompositions.jl +++ b/src/Approximations/decompositions.jl @@ -226,13 +226,7 @@ function decompose(S::LazySet{N}; n = dim(S) result = Vector{LazySet{N}}() - if set_type == LinearMap - block_start = 1 - @inbounds for bi in blocks - push!(result, project(S, block_start:(block_start + bi - 1), n)) - block_start += bi - end - elseif directions != nothing + if directions != nothing # template directions # potentially defined option set_type is *ignored* block_start = 1 @@ -469,9 +463,10 @@ Project a high-dimensional set to a low-dimensional set. The lazy linear map of the projection of `S`. """ @inline function project(S::LazySet{N}, - block::AbstractVector{Int}, - n::Int=dim(S) - )::LinearMap where {N<:Real} + block::AbstractVector{Int}, + set_type::Type{<:LinearMap}, + n::Int=dim(S), + ε::Real=Inf)::LinearMap where {N<:Real} M = sparse(1:length(block), block, ones(N, length(block)), length(block), n) return M * S end diff --git a/test/unit_decompose.jl b/test/unit_decompose.jl index 4444b17c32..25d47eeaf0 100644 --- a/test/unit_decompose.jl +++ b/test/unit_decompose.jl @@ -21,6 +21,9 @@ for N in [Float64, Rational{Int}, Float32] d = decompose(b, set_type=Hyperrectangle) @test d.array[1] isa Hyperrectangle && test_directions(d.array[1]) + d = decompose(b, set_type=LinearMap) + @test d.array[1] isa LinearMap && test_directions(d.array[1]) + d = decompose(b, set_type=Interval, blocks=ones(Int, 6)) @test d.array[1] isa Interval && σ(N[1], d.array[1])[1] == one(N) && σ(N[-1], d.array[1])[1] == -one(N) From 5331bc5c4f90418c0805963cac8e1e618e76157e Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 10 Dec 2018 15:13:29 +1100 Subject: [PATCH 4/5] fixed doc for new projection --- src/Approximations/decompositions.jl | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Approximations/decompositions.jl b/src/Approximations/decompositions.jl index 67331d142b..83286c0ab6 100644 --- a/src/Approximations/decompositions.jl +++ b/src/Approximations/decompositions.jl @@ -445,22 +445,25 @@ The template direction approximation of the projection of `S`. end """ - project(S::LazySet{N}, - block::AbstractVector{Int}, - n::Int - )::LinearMap where {N<:Real} + function project(S::LazySet{N}, + block::AbstractVector{Int}, + set_type::Type{<:LinearMap}, + n::Int=dim(S), + ε::Real=Inf)::LinearMap -Project a high-dimensional set to a low-dimensional set. +Project a high-dimensional set to a low-dimensional set by a lazy linear map. ### Input - `S` -- set - `block` -- block structure - a vector with the dimensions of interest +- `set_type` -- `Hyperrectangle` - used for dispatch - `n` -- (optional, default: `dim(S)`) ambient dimension of the set `S` +- `ε` -- (optional, default: `Inf`) - used for dispatch, ignored ### Output -The lazy linear map of the projection of `S`. +A lazy `LinearMap` representing a projection of the high-dimensional set to a low-dimensional. """ @inline function project(S::LazySet{N}, block::AbstractVector{Int}, From a39919d917b894580432e5a539cc1be3644a4446 Mon Sep 17 00:00:00 2001 From: schillic Date: Mon, 10 Dec 2018 22:51:55 +0100 Subject: [PATCH 5/5] use new method at other places --- src/Approximations/decompositions.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Approximations/decompositions.jl b/src/Approximations/decompositions.jl index 83286c0ab6..8d40b6e6db 100644 --- a/src/Approximations/decompositions.jl +++ b/src/Approximations/decompositions.jl @@ -304,8 +304,8 @@ A set of type `set_type` representing an overapproximation of the projection of n::Int=dim(S), ε::Real=Inf )::LazySet{N} where {N<:Real} - M = sparse(1:length(block), block, ones(N, length(block)), length(block), n) - return overapproximate(M * S, set_type) + lm = project(S, block, LinearMap, n) + return overapproximate(lm, set_type) end """ @@ -440,8 +440,8 @@ The template direction approximation of the projection of `S`. directions::AbstractDirections{N}, n::Int=dim(S) )::HPolytope where {N<:Real} - M = sparse(1:length(block), block, ones(N, length(block)), length(block), n) - return overapproximate(M * S, directions) + lm = project(S, block, LinearMap, n) + return overapproximate(lm, directions) end """