From 87ef0a6c3d93d1415163078a04076ee6d3ebc990 Mon Sep 17 00:00:00 2001 From: lucaferranti Date: Sat, 6 Aug 2022 20:02:58 +0300 Subject: [PATCH 1/2] add merge ID --- docs/src/lib/sets/SparsePolynomialZonotope.md | 1 + src/Sets/SparsePolynomialZonotope.jl | 39 ++++++++++++++++++- test/Sets/SparsePolynomialZonotope.jl | 17 ++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/docs/src/lib/sets/SparsePolynomialZonotope.md b/docs/src/lib/sets/SparsePolynomialZonotope.md index 4dd9360364..050f33dca5 100644 --- a/docs/src/lib/sets/SparsePolynomialZonotope.md +++ b/docs/src/lib/sets/SparsePolynomialZonotope.md @@ -13,6 +13,7 @@ independent_genmat(::SparsePolynomialZonotope) expmat(::SparsePolynomialZonotope) indexvector(P::SparsePolynomialZonotope) uniqueID(::Int) +mergeID(::SparsePolynomialZonotope, ::SparsePolynomialZonotope) dim(::SparsePolynomialZonotope) ndependentgens(::SparsePolynomialZonotope) nindependentgens(::SparsePolynomialZonotope) diff --git a/src/Sets/SparsePolynomialZonotope.jl b/src/Sets/SparsePolynomialZonotope.jl index f2cc9941e1..eaa15e5634 100644 --- a/src/Sets/SparsePolynomialZonotope.jl +++ b/src/Sets/SparsePolynomialZonotope.jl @@ -1,5 +1,5 @@ export SparsePolynomialZonotope, expmat, nparams, ndependentgens, nindependentgens, - dependent_genmat, independent_genmat, indexvector, exact_sum, ⊞, + dependent_genmat, independent_genmat, indexvector, mergeID, exact_sum, ⊞, linear_map, quadratic_map, remove_redundant_generators """ @@ -207,6 +207,43 @@ indexvector(P::SPZ) = P.idx Returns a collection of n unique identifiers (intergers 1, …, n). """ uniqueID(n::Int) = 1:n + +""" + mergeID(P1::SparsePolynomialZonotope, P2::SparsePolynomialZonotope) + +Returns two polynomial zonotopes equivalent to the correspondent input but with the same +index vector. + +### Input + +- `P1` -- sparse polynomial zonotope +- `P2` -- sprase polynomial zonotope + +### Output + +two polynomial zonotopes with the same index vector +""" +function mergeID(P1::SparsePolynomialZonotope, P2::SparsePolynomialZonotope) + + idx1 = indexvector(P1) + idx2 = indexvector(P2) + idx1 == idx2 && return (P1, P2) + + H = setdiff(idx2, idx1) + idx_new = vcat(idx1, H) + @show idx_new + E1 = vcat(expmat(P1), zeros(Int, length(H), ndependentgens(P1))) + E2 = zeros(Int, length(idx1) + length(H), ndependentgens(P2)) + E2_old = expmat(P2) + @inbounds for (i, idx) in enumerate(idx_new) + j = findfirst(isequal(idx), idx2) + !isnothing(j) && (E2[i, :] .= E2_old[j, :]) + end + P1_new = SparsePolynomialZonotope(center(P1), dependent_genmat(P1), independent_genmat(P1), E1, idx_new) + P2_new = SparsePolynomialZonotope(center(P2), dependent_genmat(P2), independent_genmat(P2), E2, idx_new) + return (P1_new, P2_new) +end + """ linear_map(M::AbstractMatrix, P::SparsePolynomialZonotope) diff --git a/test/Sets/SparsePolynomialZonotope.jl b/test/Sets/SparsePolynomialZonotope.jl index 649f73b633..66fdbd0535 100644 --- a/test/Sets/SparsePolynomialZonotope.jl +++ b/test/Sets/SparsePolynomialZonotope.jl @@ -53,3 +53,20 @@ for Z in [rand(Zonotope), rand(Hyperrectangle)] @test isempty(independent_genmat(ZS)) @test expmat(ZS) == I end + +E1 = [1 2 3; 4 5 6;7 8 9] +E2 = [1 2 0;1 1 1] +PZ1 = SparsePolynomialZonotope(rand(2), rand(2, 3), rand(2, 2), E1, 1:3) +PZ2 = SparsePolynomialZonotope(rand(2), rand(2, 3), rand(2, 2), E2, [2, 4]) + +mPZ1, mPZ2 = mergeID(PZ1, PZ2) +@test indexvector(mPZ1) == [1, 2, 3, 4] +@test indexvector(mPZ2) == [1, 2, 3, 4] + +for f in (:center, :dependent_genmat, :independent_genmat) + @eval @test $f(mPZ1) == $f(PZ1) + @eval @test $f(mPZ2) == $f(PZ2) +end + +@test expmat(mPZ1) == [1 2 3;4 5 6;7 8 9;0 0 0] +@test expmat(mPZ2) == [0 0 0;1 2 0;0 0 0;1 1 1] From 1d4ae3b38d1107e83dc918b112db6550fd802a59 Mon Sep 17 00:00:00 2001 From: Luca Ferranti <49938764+lucaferranti@users.noreply.github.com> Date: Mon, 8 Aug 2022 10:06:43 +0300 Subject: [PATCH 2/2] Update src/Sets/SparsePolynomialZonotope.jl --- src/Sets/SparsePolynomialZonotope.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Sets/SparsePolynomialZonotope.jl b/src/Sets/SparsePolynomialZonotope.jl index eaa15e5634..cf7aeb7070 100644 --- a/src/Sets/SparsePolynomialZonotope.jl +++ b/src/Sets/SparsePolynomialZonotope.jl @@ -231,7 +231,6 @@ function mergeID(P1::SparsePolynomialZonotope, P2::SparsePolynomialZonotope) H = setdiff(idx2, idx1) idx_new = vcat(idx1, H) - @show idx_new E1 = vcat(expmat(P1), zeros(Int, length(H), ndependentgens(P1))) E2 = zeros(Int, length(idx1) + length(H), ndependentgens(P2)) E2_old = expmat(P2)