From 14b94922af570bf3018863325a4769c800dfc8fb Mon Sep 17 00:00:00 2001 From: Luca Ferranti <49938764+lucaferranti@users.noreply.github.com> Date: Wed, 15 Jun 2022 20:52:29 +0300 Subject: [PATCH 1/7] linear combination and convex hull on sspz --- src/ConcreteOperations/convex_hull.jl | 35 ++++++++++++++++++++ src/ConcreteOperations/linear_combination.jl | 29 ++++++++++++++++ src/LazySets.jl | 1 + 3 files changed, 65 insertions(+) create mode 100644 src/ConcreteOperations/linear_combination.jl diff --git a/src/ConcreteOperations/convex_hull.jl b/src/ConcreteOperations/convex_hull.jl index 7fdadd49ee..65c1ca6520 100644 --- a/src/ConcreteOperations/convex_hull.jl +++ b/src/ConcreteOperations/convex_hull.jl @@ -595,3 +595,38 @@ end convex_hull(X::LazySet, ::EmptySet) = X convex_hull(::EmptySet, X::LazySet) = X convex_hull(∅::EmptySet, ::EmptySet) = ∅ + +""" + convex_hull(P1::SimpleSparsePolynomialZonotope, P2::SimpleSparsePolynomialZonotope) + +computes the convex hull of the simple spare polynomial zonotopes `P1` and `P2`. + +### Input + +- `P1` : simple sparse polynomial zonotopes +- `P2` : simple sparse polynomial zonotopes + +### Outuput + +Tightest convex simple sparse polynomial zonotope containing `P1` and `P2`. +""" +function convex_hull(P1::SimpleSparsePolynomialZonotope, P2::SimpleSparsePolynomialZonotope) + return linear_combination(linear_combination(P1, P1), linear_combination(P2, P2)) +end + +""" + convex_hull(P::SimpleSparsePolynomialZonotope) + +computes the convex hull of the simple spare polynomial zonotope `P`. + +### Input + +- `P` -- simple sparse polynomial zonotopes + +### Outuput + +Tightest convex simple sparse polynomial zonotope containing `P`. +""" +function convex_hull(P::SimpleSparsePolynomialZonotope) + return linear_combination(P, P) +end diff --git a/src/ConcreteOperations/linear_combination.jl b/src/ConcreteOperations/linear_combination.jl new file mode 100644 index 0000000000..4ae3784857 --- /dev/null +++ b/src/ConcreteOperations/linear_combination.jl @@ -0,0 +1,29 @@ +export linear_combination + +""" + linear_combination(P1::SimpleSparsePolynomialZonotope, P2::SimpleSparsePolynomialZonotope) + +compute the linear combination of simple sparse polynomial zonotopes `P1` and `P2`. + +### Input + +- `P1` -- simple sparse polynomial zonotope +- `P2` -- simple sparse polynomial zonotope + +### Output + +linear combination of `P1` and `P2`. +""" +function linear_combination(P1::SimpleSparsePolynomialZonotope, P2::SimpleSparsePolynomialZonotope) + c = 0.5 * (center(P1) + center(P2)) + G = 0.5 * hcat(center(P1) - center(P2), genmat(P1), genmat(P1), genmat(P2), -genmat(P2)) + E1, E2 = expmat(P1), expmat(P2) + et = promote_type(eltype(E1), eltype(E2)) + E = hcat(vcat(zeros(et, nparams(P1) + nparams(P2)), one(et)), + vcat(E1, zeros(et, nparams(P2) + 1, ngens(P1))), + vcat(E2, zeros(et, nparams(P2), ngens(P1)), ones(et, 1, ngens(P1))), + vcat(zeros(et, nparams(P1), ngens(P2)), E2, zeros(et, 1, ngens(P2))), + vcat(zeros(et, nparams(P1), ngens(P2)), E2, ones(et, 1, ngens(P2)))) + + return SimpleSparsePolynomialZonotope(c, G, E) +end diff --git a/src/LazySets.jl b/src/LazySets.jl index 16f6eacc78..fd4b9b03c7 100644 --- a/src/LazySets.jl +++ b/src/LazySets.jl @@ -159,6 +159,7 @@ include("ConcreteOperations/intersection.jl") include("ConcreteOperations/isdisjoint.jl") include("ConcreteOperations/issubset.jl") include("ConcreteOperations/isstrictsubset.jl") +include("ConcreteOperations/linear_combination.jl") include("ConcreteOperations/minkowski_difference.jl") include("ConcreteOperations/minkowski_sum.jl") include("Utils/samples.jl") From 9b93f96ca6b8cfdd66c52c76cae7fefbc810fea8 Mon Sep 17 00:00:00 2001 From: Luca Ferranti <49938764+lucaferranti@users.noreply.github.com> Date: Wed, 15 Jun 2022 21:57:58 +0300 Subject: [PATCH 2/7] Apply suggestions from code review Co-authored-by: Christian Schilling --- src/ConcreteOperations/convex_hull.jl | 8 ++++---- src/ConcreteOperations/linear_combination.jl | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ConcreteOperations/convex_hull.jl b/src/ConcreteOperations/convex_hull.jl index 65c1ca6520..d16d16c37d 100644 --- a/src/ConcreteOperations/convex_hull.jl +++ b/src/ConcreteOperations/convex_hull.jl @@ -599,14 +599,14 @@ convex_hull(∅::EmptySet, ::EmptySet) = ∅ """ convex_hull(P1::SimpleSparsePolynomialZonotope, P2::SimpleSparsePolynomialZonotope) -computes the convex hull of the simple spare polynomial zonotopes `P1` and `P2`. +Computes the convex hull of the simple sparse polynomial zonotopes `P1` and `P2`. ### Input - `P1` : simple sparse polynomial zonotopes - `P2` : simple sparse polynomial zonotopes -### Outuput +### Output Tightest convex simple sparse polynomial zonotope containing `P1` and `P2`. """ @@ -617,13 +617,13 @@ end """ convex_hull(P::SimpleSparsePolynomialZonotope) -computes the convex hull of the simple spare polynomial zonotope `P`. +Computes the convex hull of the simple sparse polynomial zonotope `P`. ### Input - `P` -- simple sparse polynomial zonotopes -### Outuput +### Output Tightest convex simple sparse polynomial zonotope containing `P`. """ diff --git a/src/ConcreteOperations/linear_combination.jl b/src/ConcreteOperations/linear_combination.jl index 4ae3784857..ded927659c 100644 --- a/src/ConcreteOperations/linear_combination.jl +++ b/src/ConcreteOperations/linear_combination.jl @@ -13,6 +13,10 @@ compute the linear combination of simple sparse polynomial zonotopes `P1` and `P ### Output linear combination of `P1` and `P2`. + +### Notes + +... """ function linear_combination(P1::SimpleSparsePolynomialZonotope, P2::SimpleSparsePolynomialZonotope) c = 0.5 * (center(P1) + center(P2)) From c03478997e2358cffc603a8808f02c9783adacfe Mon Sep 17 00:00:00 2001 From: Luca Ferranti <49938764+lucaferranti@users.noreply.github.com> Date: Thu, 16 Jun 2022 10:34:52 +0300 Subject: [PATCH 3/7] update docs --- docs/src/lib/sets/SimpleSparsePolynomialZonotope.md | 3 +++ src/ConcreteOperations/linear_combination.jl | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/src/lib/sets/SimpleSparsePolynomialZonotope.md b/docs/src/lib/sets/SimpleSparsePolynomialZonotope.md index bc979b8c71..dc83d22e2b 100644 --- a/docs/src/lib/sets/SimpleSparsePolynomialZonotope.md +++ b/docs/src/lib/sets/SimpleSparsePolynomialZonotope.md @@ -16,4 +16,7 @@ order(::SimpleSparsePolynomialZonotope) linear_map(::AbstractMatrix, ::SimpleSparsePolynomialZonotope) minkowski_sum(::SimpleSparsePolynomialZonotope, ::SimpleSparsePolynomialZonotope) cartesian_product(::SimpleSparsePolynomialZonotope, ::SimpleSparsePolynomialZonotope) +linear_combination(::SimpleSparsePolynomialZonotope, ::SimpleSparsePolynomialZonotope) +convex_hull(::SimpleSparsePolynomialZonotope, ::SimpleSparsePolynomialZonotope) +convex_hull(::SimpleSparsePolynomialZonotope) ``` diff --git a/src/ConcreteOperations/linear_combination.jl b/src/ConcreteOperations/linear_combination.jl index ded927659c..37b5b49810 100644 --- a/src/ConcreteOperations/linear_combination.jl +++ b/src/ConcreteOperations/linear_combination.jl @@ -16,7 +16,11 @@ linear combination of `P1` and `P2`. ### Notes -... +The linear combination of two sets ``P₁`` and ``P₂`` is defined as + +```math +\\{λp₁ + (1-λ)p₂ | p₁ ∈ P₁, p₂ ∈ P₂, λ ∈ [0, 1]\\} +``` """ function linear_combination(P1::SimpleSparsePolynomialZonotope, P2::SimpleSparsePolynomialZonotope) c = 0.5 * (center(P1) + center(P2)) From 68de2285bd271174b53680cd985a60beb97c5091 Mon Sep 17 00:00:00 2001 From: Luca Ferranti <49938764+lucaferranti@users.noreply.github.com> Date: Tue, 21 Jun 2022 10:41:25 +0300 Subject: [PATCH 4/7] incorporate review comments --- src/ConcreteOperations/linear_combination.jl | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ConcreteOperations/linear_combination.jl b/src/ConcreteOperations/linear_combination.jl index 37b5b49810..c7f997e20d 100644 --- a/src/ConcreteOperations/linear_combination.jl +++ b/src/ConcreteOperations/linear_combination.jl @@ -19,13 +19,17 @@ linear combination of `P1` and `P2`. The linear combination of two sets ``P₁`` and ``P₂`` is defined as ```math -\\{λp₁ + (1-λ)p₂ | p₁ ∈ P₁, p₂ ∈ P₂, λ ∈ [0, 1]\\} +\\{1/2(1+λ)p₁ + 1/2(1-λ)p₂ | p₁ ∈ P₁, p₂ ∈ P₂, λ ∈ [-1, 1]\\} ``` """ function linear_combination(P1::SimpleSparsePolynomialZonotope, P2::SimpleSparsePolynomialZonotope) - c = 0.5 * (center(P1) + center(P2)) - G = 0.5 * hcat(center(P1) - center(P2), genmat(P1), genmat(P1), genmat(P2), -genmat(P2)) + c1, c2 = center(P1), center(P2) + G1, G2 = genmat(P1), genmat(P2) E1, E2 = expmat(P1), expmat(P2) + + c = 0.5 * (c1 + c2) + G = 0.5 * hcat(c1 - c2, G1, G1, G2, -G2) + et = promote_type(eltype(E1), eltype(E2)) E = hcat(vcat(zeros(et, nparams(P1) + nparams(P2)), one(et)), vcat(E1, zeros(et, nparams(P2) + 1, ngens(P1))), From ecf39798e7d6c1f6b6d73a209a1e118be64705d8 Mon Sep 17 00:00:00 2001 From: Luca Ferranti <49938764+lucaferranti@users.noreply.github.com> Date: Tue, 21 Jun 2022 10:51:05 +0300 Subject: [PATCH 5/7] add tests --- test/Sets/SimpleSparsePolynomialZonotope.jl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/Sets/SimpleSparsePolynomialZonotope.jl b/test/Sets/SimpleSparsePolynomialZonotope.jl index a8993773da..73e1b863bc 100644 --- a/test/Sets/SimpleSparsePolynomialZonotope.jl +++ b/test/Sets/SimpleSparsePolynomialZonotope.jl @@ -32,4 +32,25 @@ for N in [Float64, Float32, Rational{Int}] @test genmat(CPS) == [1 2 0 0;2 2 0 0;0 0 1 2;0 0 2 2.] @test expmat(CPS) == [1 4 0 0;1 2 0 0;0 0 1 4;0 0 1 2] + _c = N[2.0, 0.0] + _g = N[ 0.0 0.5 1.0 0.5 1.0 0.5 1.0 -0.5 -1.0 + 0.0 1.0 1.0 1.0 1.0 1.0 1.0 -1.0 -1.0] + + _e = [0 1 4 1 4 0 0 0 0 + 0 1 2 1 2 0 0 0 0 + 0 0 0 0 0 1 4 1 4 + 0 0 0 0 0 1 2 1 2 + 1 0 0 1 1 0 0 1 1] + + LCS = linear_combination(S, S) + + @test center(LCS) == _c + @test genmat(LCS) == _g + @test expmat(LCS) == _e + + CH1 = convex_hull(S) + + @test center(CH1) == _c + @test genmat(CH1) == _g + @test expmat(CH1) == _e end From 2f22f79d3c14355d30c8ce1124fd939600ab1eca Mon Sep 17 00:00:00 2001 From: Marcelo Forets Date: Wed, 22 Jun 2022 15:33:31 -0300 Subject: [PATCH 6/7] Update src/ConcreteOperations/linear_combination.jl --- src/ConcreteOperations/linear_combination.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ConcreteOperations/linear_combination.jl b/src/ConcreteOperations/linear_combination.jl index c7f997e20d..81718ff4e2 100644 --- a/src/ConcreteOperations/linear_combination.jl +++ b/src/ConcreteOperations/linear_combination.jl @@ -30,12 +30,12 @@ function linear_combination(P1::SimpleSparsePolynomialZonotope, P2::SimpleSparse c = 0.5 * (c1 + c2) G = 0.5 * hcat(c1 - c2, G1, G1, G2, -G2) - et = promote_type(eltype(E1), eltype(E2)) - E = hcat(vcat(zeros(et, nparams(P1) + nparams(P2)), one(et)), - vcat(E1, zeros(et, nparams(P2) + 1, ngens(P1))), - vcat(E2, zeros(et, nparams(P2), ngens(P1)), ones(et, 1, ngens(P1))), - vcat(zeros(et, nparams(P1), ngens(P2)), E2, zeros(et, 1, ngens(P2))), - vcat(zeros(et, nparams(P1), ngens(P2)), E2, ones(et, 1, ngens(P2)))) + N = promote_type(eltype(E1), eltype(E2)) + E = hcat(vcat(zeros(N, nparams(P1) + nparams(P2)), one(N)), + vcat(E1, zeros(N, nparams(P2) + 1, ngens(P1))), + vcat(E2, zeros(N, nparams(P2), ngens(P1)), ones(N, 1, ngens(P1))), + vcat(zeros(N, nparams(P1), ngens(P2)), E2, zeros(N 1, ngens(P2))), + vcat(zeros(N, nparams(P1), ngens(P2)), E2, ones(N, 1, ngens(P2)))) return SimpleSparsePolynomialZonotope(c, G, E) end From f62e1bd481ac48430c641f347c796908ccc4a9c6 Mon Sep 17 00:00:00 2001 From: Marcelo Forets Date: Wed, 22 Jun 2022 15:48:18 -0300 Subject: [PATCH 7/7] Update src/ConcreteOperations/linear_combination.jl --- src/ConcreteOperations/linear_combination.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ConcreteOperations/linear_combination.jl b/src/ConcreteOperations/linear_combination.jl index 81718ff4e2..a517e4fff5 100644 --- a/src/ConcreteOperations/linear_combination.jl +++ b/src/ConcreteOperations/linear_combination.jl @@ -34,7 +34,7 @@ function linear_combination(P1::SimpleSparsePolynomialZonotope, P2::SimpleSparse E = hcat(vcat(zeros(N, nparams(P1) + nparams(P2)), one(N)), vcat(E1, zeros(N, nparams(P2) + 1, ngens(P1))), vcat(E2, zeros(N, nparams(P2), ngens(P1)), ones(N, 1, ngens(P1))), - vcat(zeros(N, nparams(P1), ngens(P2)), E2, zeros(N 1, ngens(P2))), + vcat(zeros(N, nparams(P1), ngens(P2)), E2, zeros(N, 1, ngens(P2))), vcat(zeros(N, nparams(P1), ngens(P2)), E2, ones(N, 1, ngens(P2)))) return SimpleSparsePolynomialZonotope(c, G, E)