From 9f4f0bdc029f680ba70c4c3c76faf578d9ab96eb Mon Sep 17 00:00:00 2001 From: mforets Date: Mon, 10 Sep 2018 14:20:22 -0300 Subject: [PATCH 01/10] add convert function --- src/convert.jl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/convert.jl b/src/convert.jl index c67b8d35ce..a08fb76ae8 100644 --- a/src/convert.jl +++ b/src/convert.jl @@ -173,3 +173,21 @@ Hyperrectangle{Float64}([0.5], [0.5]) function convert(::Type{Hyperrectangle}, x::Interval{N, IN}) where {N, IN <: AbstractInterval{N}} return Hyperrectangle(low=[low(x)], high=[high(x)]) end + +""" + convert(::Type{HPolytope}, H::AbstractHyperrectangle{N}) where {N} + +Converts a hyperrectangular set to a polytope in constraint representation. + +### Input + +- `HPolytope` -- type used for dispatch +- `H` -- hyperrectangular set + +### Output + +A polytope in constraint representation. +""" +function convert(::Type{HPolytope}, H::AbstractHyperrectangle{N}) where {N} + return HPolytope{N}(constraints_list(H)) +end From 46a0c251f538fc9912aa65c8f27440a1a43e0058 Mon Sep 17 00:00:00 2001 From: mforets Date: Mon, 10 Sep 2018 14:25:25 -0300 Subject: [PATCH 02/10] add constraints_list function --- src/AbstractHyperrectangle.jl | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/AbstractHyperrectangle.jl b/src/AbstractHyperrectangle.jl index c63156e787..0be6c7ce36 100644 --- a/src/AbstractHyperrectangle.jl +++ b/src/AbstractHyperrectangle.jl @@ -1,7 +1,8 @@ import Base.∈ export AbstractHyperrectangle, - radius_hyperrectangle + radius_hyperrectangle, + constraints_list """ AbstractHyperrectangle{N<:Real} <: AbstractCentrallySymmetricPolytope{N} @@ -59,6 +60,30 @@ function vertices_list(H::AbstractHyperrectangle{N} for si in Iterators.product([[1, -1] for i = 1:dim(H)]...)][:] end +""" + constraints_list(H::AbstractHyperrectangle{N})::Vector{Vector{N}} where {N<:Real} + +Return the list of constraints of an axis-aligned hyperrectangular set. + +### Input + +- `H` -- hyperrectangular set + +### Output + +A list of linear constraints. +""" +function constraints_list(H::AbstractHyperrectangle{N})::Vector{LinearConstraint{N}} where {N<:Real} + n = dim(H) + constraints = Vector{LinearConstraint{N}}(2*n) + A, b, c = eye(n), high(H), -low(H) + + for i in 1:n + constraints[i] = HalfSpace(A[i, :], b[i]) + constraints[i+n] = HalfSpace(-A[i, :], c[i]) + end + return constraints +end # --- LazySet interface functions --- From 25d629a582de59afd30140aaeff3d4425eb8cabf Mon Sep 17 00:00:00 2001 From: mforets Date: Tue, 11 Sep 2018 00:56:37 -0300 Subject: [PATCH 03/10] use unit vector --- src/AbstractHyperrectangle.jl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/AbstractHyperrectangle.jl b/src/AbstractHyperrectangle.jl index 0be6c7ce36..7c3be18fb9 100644 --- a/src/AbstractHyperrectangle.jl +++ b/src/AbstractHyperrectangle.jl @@ -76,11 +76,12 @@ A list of linear constraints. function constraints_list(H::AbstractHyperrectangle{N})::Vector{LinearConstraint{N}} where {N<:Real} n = dim(H) constraints = Vector{LinearConstraint{N}}(2*n) - A, b, c = eye(n), high(H), -low(H) - + b, c = high(H), -low(H) + for i in 1:n - constraints[i] = HalfSpace(A[i, :], b[i]) - constraints[i+n] = HalfSpace(-A[i, :], c[i]) + ei = LazySets.Approximations.UnitVector(i, n, 1.0) + constraints[i] = HalfSpace(ei, b[i]) + constraints[i+n] = HalfSpace(-ei, c[i]) end return constraints end From aa65859188329ce6b37076ab8d269742907e64ca Mon Sep 17 00:00:00 2001 From: mforets Date: Tue, 11 Sep 2018 00:59:36 -0300 Subject: [PATCH 04/10] update docs --- docs/src/lib/conversion.md | 1 + docs/src/lib/interfaces.md | 1 + 2 files changed, 2 insertions(+) diff --git a/docs/src/lib/conversion.md b/docs/src/lib/conversion.md index dc9ee78a81..42fd02c628 100644 --- a/docs/src/lib/conversion.md +++ b/docs/src/lib/conversion.md @@ -21,4 +21,5 @@ convert(::Type{HPolytope}, ::HPolygon) convert(::Type{HPolygon}, ::HPolytope) convert(::Type{Zonotope}, ::AbstractHyperrectangle) convert(::Type{Hyperrectangle}, ::Interval) +convert(::Type{HPolytope}, ::AbstractHyperrectangle) ``` diff --git a/docs/src/lib/interfaces.md b/docs/src/lib/interfaces.md index aafb05b48b..84a6a15ea5 100644 --- a/docs/src/lib/interfaces.md +++ b/docs/src/lib/interfaces.md @@ -162,6 +162,7 @@ radius(::AbstractHyperrectangle, ::Real) σ(::AbstractVector{Real}, ::AbstractHyperrectangle{Real}) ∈(::AbstractVector{Real}, ::AbstractHyperrectangle{Real}) vertices_list(::AbstractHyperrectangle{Real}) +constraints_list(::AbstractHyperrectangle{Real}) ``` #### Singleton From 9e46ceaaf2f892161885c312fc90fc9f690a4ad8 Mon Sep 17 00:00:00 2001 From: mforets Date: Tue, 11 Sep 2018 01:02:59 -0300 Subject: [PATCH 05/10] add convert to convert.jl --- docs/src/lib/conversion.md | 1 + src/convert.jl | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/docs/src/lib/conversion.md b/docs/src/lib/conversion.md index 42fd02c628..5303dd57f4 100644 --- a/docs/src/lib/conversion.md +++ b/docs/src/lib/conversion.md @@ -22,4 +22,5 @@ convert(::Type{HPolygon}, ::HPolytope) convert(::Type{Zonotope}, ::AbstractHyperrectangle) convert(::Type{Hyperrectangle}, ::Interval) convert(::Type{HPolytope}, ::AbstractHyperrectangle) +convert(::Type{HPolygon}, ::AbstractHyperrectangle) ``` diff --git a/src/convert.jl b/src/convert.jl index a08fb76ae8..50641f4b1d 100644 --- a/src/convert.jl +++ b/src/convert.jl @@ -191,3 +191,21 @@ A polytope in constraint representation. function convert(::Type{HPolytope}, H::AbstractHyperrectangle{N}) where {N} return HPolytope{N}(constraints_list(H)) end + +""" + convert(::Type{HPolytope}, H::AbstractHyperrectangle{N}) where {N} + +Converts a hyperrectangular set to a polytope in constraint representation. + +### Input + +- `HPolytope` -- type used for dispatch +- `H` -- hyperrectangular set + +### Output + +A polytope in constraint representation. +""" +function convert(::Type{HPOLYGON}, H::AbstractHyperrectangle{N}) where {N, HPOLYGON<:AbstractHPolygon} + return HPOLYGON{N}(constraints_list(H)) +end From 74db6f482f6b3a53b8892ad1b75a3f0a53f91821 Mon Sep 17 00:00:00 2001 From: mforets Date: Tue, 11 Sep 2018 10:48:23 -0300 Subject: [PATCH 06/10] add hyperrectangle to hpolytope conversion --- src/convert.jl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/convert.jl b/src/convert.jl index 50641f4b1d..dd079133a8 100644 --- a/src/convert.jl +++ b/src/convert.jl @@ -193,19 +193,20 @@ function convert(::Type{HPolytope}, H::AbstractHyperrectangle{N}) where {N} end """ - convert(::Type{HPolytope}, H::AbstractHyperrectangle{N}) where {N} + convert(::Type{HPOLYGON}, H::AbstractHyperrectangle{N}) where {N, HPOLYGON<:AbstractHPolygon} -Converts a hyperrectangular set to a polytope in constraint representation. +Converts a hyperrectangular set to a polygon in constraint representation. ### Input -- `HPolytope` -- type used for dispatch +- `HPOLYGON` -- type used for dispatch - `H` -- hyperrectangular set ### Output A polytope in constraint representation. """ -function convert(::Type{HPOLYGON}, H::AbstractHyperrectangle{N}) where {N, HPOLYGON<:AbstractHPolygon} +function convert(X::Type{HPOLYGON}, H::AbstractHyperrectangle{N}) where {N, HPOLYGON<:AbstractHPolygon} + @assert dim(H) == 2 "cannot convert a $(dim(H)) dimensional hyperrectangle into a two-dimensional polygon" return HPOLYGON{N}(constraints_list(H)) end From 4b14d20fbbdb08a7bbaadffdaf8bfeef28254404 Mon Sep 17 00:00:00 2001 From: mforets Date: Tue, 11 Sep 2018 10:49:24 -0300 Subject: [PATCH 07/10] add conversion for polygon --- src/convert.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/convert.jl b/src/convert.jl index dd079133a8..d316b32daf 100644 --- a/src/convert.jl +++ b/src/convert.jl @@ -207,6 +207,6 @@ Converts a hyperrectangular set to a polygon in constraint representation. A polytope in constraint representation. """ function convert(X::Type{HPOLYGON}, H::AbstractHyperrectangle{N}) where {N, HPOLYGON<:AbstractHPolygon} - @assert dim(H) == 2 "cannot convert a $(dim(H)) dimensional hyperrectangle into a two-dimensional polygon" + @assert dim(H) == 2 "cannot convert a $(dim(H))-dimensional hyperrectangle into a two-dimensional polygon" return HPOLYGON{N}(constraints_list(H)) end From ca66107cdfbc2ac4e0b572601e6b005a6f99f5bb Mon Sep 17 00:00:00 2001 From: mforets Date: Tue, 11 Sep 2018 11:14:52 -0300 Subject: [PATCH 08/10] add unit tests --- src/AbstractHyperrectangle.jl | 4 ++-- src/convert.jl | 2 +- test/unit_Hyperrectangle.jl | 6 ++++++ test/unit_Polytope.jl | 7 +++++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/AbstractHyperrectangle.jl b/src/AbstractHyperrectangle.jl index 7c3be18fb9..843aa90712 100644 --- a/src/AbstractHyperrectangle.jl +++ b/src/AbstractHyperrectangle.jl @@ -77,9 +77,9 @@ function constraints_list(H::AbstractHyperrectangle{N})::Vector{LinearConstraint n = dim(H) constraints = Vector{LinearConstraint{N}}(2*n) b, c = high(H), -low(H) - + one_N = one(N) for i in 1:n - ei = LazySets.Approximations.UnitVector(i, n, 1.0) + ei = LazySets.Approximations.UnitVector(i, n, one_N) constraints[i] = HalfSpace(ei, b[i]) constraints[i+n] = HalfSpace(-ei, c[i]) end diff --git a/src/convert.jl b/src/convert.jl index d316b32daf..4eebd37c76 100644 --- a/src/convert.jl +++ b/src/convert.jl @@ -204,7 +204,7 @@ Converts a hyperrectangular set to a polygon in constraint representation. ### Output -A polytope in constraint representation. +A polygon in constraint representation. """ function convert(X::Type{HPOLYGON}, H::AbstractHyperrectangle{N}) where {N, HPOLYGON<:AbstractHPolygon} @assert dim(H) == 2 "cannot convert a $(dim(H))-dimensional hyperrectangle into a two-dimensional polygon" diff --git a/test/unit_Hyperrectangle.jl b/test/unit_Hyperrectangle.jl index 82423db9f8..492630cee7 100644 --- a/test/unit_Hyperrectangle.jl +++ b/test/unit_Hyperrectangle.jl @@ -140,4 +140,10 @@ for N in [Float64, Rational{Int}, Float32] H = Hyperrectangle(fill(N(1.), 100), fill(N(0.), 100)) vl = vertices_list(H) @test length(vl) == 1 && vl[1] == H.center + + # transform a hyperrectangle into a polygon + H1pol = convert(HPolygon, H1) + vlist = vertices_list(H1pol) + @test length(vlist) == 4 + @test all([vi ∈ vlist for vi in [N[3, 3], N[3, -1], N[-1, -1], N[-1, 3]]]) end diff --git a/test/unit_Polytope.jl b/test/unit_Polytope.jl index de6fc5e543..8aaccef81e 100644 --- a/test/unit_Polytope.jl +++ b/test/unit_Polytope.jl @@ -56,6 +56,13 @@ for N in [Float64, Rational{Int}, Float32] @test length(p.constraints) == length(cl) end + # convert a hyperrectangle to a HPolytope + H = Hyperrectangle(N[1, 1], N[2, 2]) + P = convert(HPolytope, H1) + vlist = vertices_list(P) + @test length(vlist) == 4 + @test all([vi ∈ vlist for vi in [N[3, 3], N[3, -1], N[-1, -1], N[-1, 3]]]) + # ----- # V-rep # ----- From ca8504696d605ec54df94387aaaa2c2beb88fd05 Mon Sep 17 00:00:00 2001 From: mforets Date: Tue, 11 Sep 2018 16:42:17 -0300 Subject: [PATCH 09/10] add constraints_list test --- test/unit_Hyperrectangle.jl | 10 +++++++++- test/unit_Polytope.jl | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/test/unit_Hyperrectangle.jl b/test/unit_Hyperrectangle.jl index 492630cee7..c34c36ee3d 100644 --- a/test/unit_Hyperrectangle.jl +++ b/test/unit_Hyperrectangle.jl @@ -141,9 +141,17 @@ for N in [Float64, Rational{Int}, Float32] vl = vertices_list(H) @test length(vl) == 1 && vl[1] == H.center - # transform a hyperrectangle into a polygon + # transform hyperrectangle into a polygon H1pol = convert(HPolygon, H1) vlist = vertices_list(H1pol) @test length(vlist) == 4 @test all([vi ∈ vlist for vi in [N[3, 3], N[3, -1], N[-1, -1], N[-1, 3]]]) + + # test that we can produce the list of constraints + clist = constraints_list(H1) + @test length(clist) == 4 + @test any([HalfSpace(N[1, 0], N(3)) == ci for ci in constraints_list(H1)]) && + any([HalfSpace(N[0, 1], N(3)) == ci for ci in constraints_list(H1)]) && + any([HalfSpace(N[-1, 0], N(1)) == ci for ci in constraints_list(H1)]) && + any([HalfSpace(N[0, -1], N(1)) == ci for ci in constraints_list(H1)]) end diff --git a/test/unit_Polytope.jl b/test/unit_Polytope.jl index 8aaccef81e..6f9967f514 100644 --- a/test/unit_Polytope.jl +++ b/test/unit_Polytope.jl @@ -56,9 +56,9 @@ for N in [Float64, Rational{Int}, Float32] @test length(p.constraints) == length(cl) end - # convert a hyperrectangle to a HPolytope + # convert hyperrectangle to a HPolytope H = Hyperrectangle(N[1, 1], N[2, 2]) - P = convert(HPolytope, H1) + P = convert(HPolytope, H) vlist = vertices_list(P) @test length(vlist) == 4 @test all([vi ∈ vlist for vi in [N[3, 3], N[3, -1], N[-1, -1], N[-1, 3]]]) From a7bbaa5e5b62d77f42d814cdd5ef34b749cf6c33 Mon Sep 17 00:00:00 2001 From: mforets Date: Tue, 11 Sep 2018 17:40:05 -0300 Subject: [PATCH 10/10] revise tests --- src/AbstractHyperrectangle.jl | 2 +- test/unit_Polytope.jl | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/AbstractHyperrectangle.jl b/src/AbstractHyperrectangle.jl index 843aa90712..70998808b2 100644 --- a/src/AbstractHyperrectangle.jl +++ b/src/AbstractHyperrectangle.jl @@ -75,7 +75,7 @@ A list of linear constraints. """ function constraints_list(H::AbstractHyperrectangle{N})::Vector{LinearConstraint{N}} where {N<:Real} n = dim(H) - constraints = Vector{LinearConstraint{N}}(2*n) + constraints = Vector{LinearConstraint{N}}(undef, 2*n) b, c = high(H), -low(H) one_N = one(N) for i in 1:n diff --git a/test/unit_Polytope.jl b/test/unit_Polytope.jl index 6f9967f514..386dbab4e1 100644 --- a/test/unit_Polytope.jl +++ b/test/unit_Polytope.jl @@ -56,12 +56,14 @@ for N in [Float64, Rational{Int}, Float32] @test length(p.constraints) == length(cl) end - # convert hyperrectangle to a HPolytope - H = Hyperrectangle(N[1, 1], N[2, 2]) - P = convert(HPolytope, H) - vlist = vertices_list(P) - @test length(vlist) == 4 - @test all([vi ∈ vlist for vi in [N[3, 3], N[3, -1], N[-1, -1], N[-1, 3]]]) + if test_suite_polyhedra + # convert hyperrectangle to a HPolytope + H = Hyperrectangle(N[1, 1], N[2, 2]) + P = convert(HPolytope, H) + vlist = vertices_list(P) + @test length(vlist) == 4 + @test all([vi ∈ vlist for vi in [N[3, 3], N[3, -1], N[-1, -1], N[-1, 3]]]) + end # ----- # V-rep