From 15eba9d3da12a893fb45ce87177db09f109bde46 Mon Sep 17 00:00:00 2001 From: Marcelo Forets Date: Thu, 12 Dec 2019 11:38:18 -0300 Subject: [PATCH] #1802 - Concretely type linear constraint arrays (#1803) * type constraints_list of hyperrectangular set * type constraints list of Ball1 * constraints list of cpa * type for constraints list of ResetMap * add more type annotations * more type annotations * update doctest * fix last doctest --- src/Approximations/decompositions.jl | 2 +- src/Approximations/iterative_refinement.jl | 7 ++++--- src/Approximations/overapproximate.jl | 2 +- src/Interfaces/AbstractHyperrectangle.jl | 2 +- src/LazyOperations/CartesianProduct.jl | 5 ++--- src/LazyOperations/ResetMap.jl | 2 +- src/LazyOperations/Translation.jl | 2 +- src/Sets/Ball1.jl | 2 +- src/Sets/HPolytope.jl | 3 ++- src/convert.jl | 2 +- 10 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Approximations/decompositions.jl b/src/Approximations/decompositions.jl index bc4e7e85fe..5848abd1de 100644 --- a/src/Approximations/decompositions.jl +++ b/src/Approximations/decompositions.jl @@ -555,7 +555,7 @@ Now let's take a ball in the infinity norm and remove some constraints: julia> B = BallInf(zeros(4), 1.0); julia> c = constraints_list(B)[1:2] -2-element Array{HalfSpace{Float64,VN} where VN<:AbstractArray{Float64,1},1}: +2-element Array{HalfSpace{Float64,LazySets.Arrays.SingleEntryVector{Float64}},1}: HalfSpace{Float64,LazySets.Arrays.SingleEntryVector{Float64}}([1.0, 0.0, 0.0, 0.0], 1.0) HalfSpace{Float64,LazySets.Arrays.SingleEntryVector{Float64}}([0.0, 1.0, 0.0, 0.0], 1.0) diff --git a/src/Approximations/iterative_refinement.jl b/src/Approximations/iterative_refinement.jl index c2a7887633..35fb05b7cb 100644 --- a/src/Approximations/iterative_refinement.jl +++ b/src/Approximations/iterative_refinement.jl @@ -59,10 +59,11 @@ Type that represents the polygonal approximation of a convex set. struct PolygonalOverapproximation{N<:Real} S::LazySet{N} approx_stack::Vector{LocalApproximation{N}} - constraints::Vector{LinearConstraint{N}} + constraints::Vector{LinearConstraint{N, Vector{N}}} - PolygonalOverapproximation(S::LazySet{N}) where {N<:Real} = new{N}( - S, Vector{LocalApproximation{N}}(), Vector{LinearConstraint{N}}()) + function PolygonalOverapproximation(S::LazySet{N}) where {N<:Real} + return new{N}(S, Vector{LocalApproximation{N}}(), Vector{LinearConstraint{N,Vector{N}}}()) + end end """ diff --git a/src/Approximations/overapproximate.jl b/src/Approximations/overapproximate.jl index 9a7a627fd1..514fbfaba8 100644 --- a/src/Approximations/overapproximate.jl +++ b/src/Approximations/overapproximate.jl @@ -48,7 +48,7 @@ function overapproximate(S::LazySet{N}, )::HPolygon where {N<:Real} @assert dim(S) == 2 if ε == Inf - constraints = Vector{LinearConstraint{N}}(undef, 4) + constraints = Vector{LinearConstraint{N, Vector{N}}}(undef, 4) constraints[1] = LinearConstraint(DIR_EAST(N), ρ(DIR_EAST(N), S)) constraints[2] = LinearConstraint(DIR_NORTH(N), ρ(DIR_NORTH(N), S)) constraints[3] = LinearConstraint(DIR_WEST(N), ρ(DIR_WEST(N), S)) diff --git a/src/Interfaces/AbstractHyperrectangle.jl b/src/Interfaces/AbstractHyperrectangle.jl index 69999ac689..4afed6dd96 100644 --- a/src/Interfaces/AbstractHyperrectangle.jl +++ b/src/Interfaces/AbstractHyperrectangle.jl @@ -238,7 +238,7 @@ A list of linear constraints. """ function constraints_list(H::AbstractHyperrectangle{N}) where {N<:Real} n = dim(H) - constraints = Vector{LinearConstraint{N}}(undef, 2*n) + constraints = Vector{LinearConstraint{N, SingleEntryVector{N}}}(undef, 2*n) b, c = high(H), -low(H) one_N = one(N) for i in 1:n diff --git a/src/LazyOperations/CartesianProduct.jl b/src/LazyOperations/CartesianProduct.jl index 0e1a823f66..551d7b6fd0 100644 --- a/src/LazyOperations/CartesianProduct.jl +++ b/src/LazyOperations/CartesianProduct.jl @@ -588,7 +588,7 @@ number of sets. A list of constraints. """ function constraints_list(cpa::CartesianProductArray{N}) where {N<:Real} - clist = Vector{LinearConstraint{N}}() + clist = Vector{LinearConstraint{N, SparseVector{N, Int}}}() n = dim(cpa) sizehint!(clist, n) prev_step = 1 @@ -599,8 +599,7 @@ function constraints_list(cpa::CartesianProductArray{N}) where {N<:Real} indices = prev_step : (dim(c_low_list[1]) + prev_step - 1) end for constr in c_low_list - new_constr = LinearConstraint(sparsevec(indices, constr.a, n), - constr.b) + new_constr = LinearConstraint(sparsevec(indices, constr.a, n), constr.b) push!(clist, new_constr) end prev_step += dim(c_low_list[1]) diff --git a/src/LazyOperations/ResetMap.jl b/src/LazyOperations/ResetMap.jl index 5acfc5ad29..492a6ec008 100644 --- a/src/LazyOperations/ResetMap.jl +++ b/src/LazyOperations/ResetMap.jl @@ -359,7 +359,7 @@ function constraints_list(rm::ResetMap{N, S} ) where {N<:Real, S<:AbstractHyperrectangle} H = rm.X n = dim(H) - constraints = Vector{LinearConstraint{N}}(undef, 2*n) + constraints = Vector{LinearConstraint{N, SingleEntryVector{N}}}(undef, 2*n) j = 1 for i in 1:n ei = SingleEntryVector(i, n, one(N)) diff --git a/src/LazyOperations/Translation.jl b/src/LazyOperations/Translation.jl index e845c493f7..ae236707c3 100644 --- a/src/LazyOperations/Translation.jl +++ b/src/LazyOperations/Translation.jl @@ -130,7 +130,7 @@ whose `constraints_list` is available) can be computed from a lazy translation: ```jldoctest translation julia> constraints_list(tr) -6-element Array{HalfSpace{Float64,VN} where VN<:AbstractArray{Float64,1},1}: +6-element Array{HalfSpace{Float64,LazySets.Arrays.SingleEntryVector{Float64}},1}: HalfSpace{Float64,LazySets.Arrays.SingleEntryVector{Float64}}([1.0, 0.0, 0.0], 5.0) HalfSpace{Float64,LazySets.Arrays.SingleEntryVector{Float64}}([0.0, 1.0, 0.0], 3.0) HalfSpace{Float64,LazySets.Arrays.SingleEntryVector{Float64}}([0.0, 0.0, 1.0], 3.0) diff --git a/src/Sets/Ball1.jl b/src/Sets/Ball1.jl index aeada4c425..326e1166f8 100644 --- a/src/Sets/Ball1.jl +++ b/src/Sets/Ball1.jl @@ -245,7 +245,7 @@ all possible ``d_i``, the function `Iterators.product` is used. function constraints_list(B::Ball1{N}) where {N<:Real} n = LazySets.dim(B) c, r = B.center, B.radius - clist = Vector{LinearConstraint{N}}(undef, 2^n) + clist = Vector{LinearConstraint{N, Vector{N}}}(undef, 2^n) for (i, di) in enumerate(Iterators.product([[one(N), -one(N)] for i = 1:n]...)) d = collect(di) # tuple -> vector clist[i] = LinearConstraint(d, dot(d, c) + r) diff --git a/src/Sets/HPolytope.jl b/src/Sets/HPolytope.jl index 4bb6f7681e..9dea2560be 100644 --- a/src/Sets/HPolytope.jl +++ b/src/Sets/HPolytope.jl @@ -147,7 +147,8 @@ return quote # see the interface file AbstractPolytope.jl for the imports function convert(::Type{HPolytope{N}}, P::HRep{N}) where {N} - constraints = LinearConstraint{N}[] + VT = Polyhedra.hvectortype(P) + constraints = Vector{LinearConstraint{N, VT}}() for hi in Polyhedra.allhalfspaces(P) a, b = hi.a, hi.β if isapproxzero(norm(a)) diff --git a/src/convert.jl b/src/convert.jl index dd616dd1fa..d91a6beade 100644 --- a/src/convert.jl +++ b/src/convert.jl @@ -402,7 +402,7 @@ A polygon in constraint representation with the minimal number of constraints """ function convert(::Type{HPOLYGON}, S::AbstractSingleton{N} ) where {N<:Real, HPOLYGON<:AbstractHPolygon} - constraints_list = Vector{LinearConstraint{N}}(undef, 3) + constraints_list = Vector{LinearConstraint{N, Vector{N}}}(undef, 3) o = one(N) z = zero(N) v = element(S)