Skip to content

Commit d2b18a4

Browse files
authored
Merge pull request #3244 from JuliaReach/schillic/move
Move some code to interface files
2 parents 7e21373 + f62e30c commit d2b18a4

File tree

5 files changed

+99
-95
lines changed

5 files changed

+99
-95
lines changed

src/Interfaces/AbstractPolyhedron_functions.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,28 @@ isconvextype(::Type{<:AbstractPolyhedron}) = true
4545

4646
is_polyhedral(::AbstractPolyhedron) = true
4747

48+
"""
49+
constraints_list(A::AbstractMatrix{N}, b::AbstractVector)
50+
51+
Convert a matrix-vector representation to a linear-constraint representation.
52+
53+
### Input
54+
55+
- `A` -- matrix
56+
- `b` -- vector
57+
58+
### Output
59+
60+
A list of linear constraints.
61+
"""
62+
function constraints_list(A::AbstractMatrix, b::AbstractVector)
63+
m = size(A, 1)
64+
@assert m == length(b) "a matrix with $m rows is incompatible with a " *
65+
"vector of length $(length(b))"
66+
67+
return [HalfSpace(A[i, :], b[i]) for i in 1:m]
68+
end
69+
4870
"""
4971
∈(x::AbstractVector, P::AbstractPolyhedron)
5072

src/Interfaces/AbstractZonotope.jl

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,19 +421,91 @@ function vertices_list(Z::AbstractZonotope; apply_convex_hull::Bool=true)
421421

422422
elseif n == 2
423423
if p == 1
424-
return _vertices_list_2D_order_one_half(c, G, apply_convex_hull=apply_convex_hull)
424+
return _vertices_list_zonotope_2D_order_one_half(c, G, apply_convex_hull=apply_convex_hull)
425425
elseif p == 2
426-
return _vertices_list_2D_order_one(c, G, apply_convex_hull=apply_convex_hull)
426+
return _vertices_list_zonotope_2D_order_one(c, G, apply_convex_hull=apply_convex_hull)
427427
else
428-
return _vertices_list_2D(c, G, apply_convex_hull=apply_convex_hull)
428+
return _vertices_list_zonotope_2D(c, G, apply_convex_hull=apply_convex_hull)
429429
end
430430

431431
else
432432
Gred = remove_zero_columns(G)
433-
return _vertices_list_iterative(c, Gred, apply_convex_hull=apply_convex_hull)
433+
return _vertices_list_zonotope_iterative(c, Gred, apply_convex_hull=apply_convex_hull)
434434
end
435435
end
436436

437+
function _vertices_list_zonotope_2D(c::AbstractVector{N}, G::AbstractMatrix{N};
438+
apply_convex_hull::Bool) where {N}
439+
if same_sign(G)
440+
return _vertices_list_zonotope_2D_positive(c, G, apply_convex_hull=apply_convex_hull)
441+
else
442+
# FIXME generalized 2D vertices list function is not implemented yet
443+
# See LazySets#2209
444+
return _vertices_list_zonotope_iterative(c, G, apply_convex_hull=apply_convex_hull)
445+
end
446+
end
447+
448+
function _vertices_list_zonotope_2D_positive(c::AbstractVector{N}, G::AbstractMatrix{N};
449+
apply_convex_hull::Bool) where {N}
450+
n, p = size(G)
451+
452+
# TODO special case p = 1 or p = 2 ?
453+
454+
sorted_G = sortslices(G, dims=2, by=x->atan(x[2], x[1]))
455+
index = ones(N, p, 2*p)
456+
@inbounds for i in 1:p
457+
index[i, i+1:i+p-1] .= -one(N)
458+
end
459+
index[:, 1] .= -one(N)
460+
V = sorted_G * index .+ c
461+
vlist = [V[:, i] for i in 1:2*p]
462+
463+
if apply_convex_hull
464+
convex_hull!(vlist)
465+
end
466+
return vlist
467+
end
468+
469+
function _vertices_list_zonotope_iterative(c::VN, G::MN; apply_convex_hull::Bool
470+
) where {N, VN<:AbstractVector{N}, MN<:AbstractMatrix{N}}
471+
p = size(G, 2)
472+
vlist = Vector{VN}()
473+
sizehint!(vlist, 2^p)
474+
475+
for ξi in Iterators.product([(1, -1) for i = 1:p]...)
476+
push!(vlist, c .+ G * collect(ξi))
477+
end
478+
479+
return apply_convex_hull ? convex_hull!(vlist) : vlist
480+
end
481+
482+
# special case 2D zonotope of order 1/2
483+
function _vertices_list_zonotope_2D_order_one_half(c::VN, G::MN;
484+
apply_convex_hull::Bool) where {N, VN<:AbstractVector{N}, MN}
485+
vlist = Vector{VN}(undef, 2)
486+
g = view(G, :, 1)
487+
@inbounds begin
488+
vlist[1] = c .+ g
489+
vlist[2] = c .- g
490+
end
491+
return apply_convex_hull ? _two_points_2d!(vlist) : vlist
492+
end
493+
494+
# special case 2D zonotope of order 1
495+
function _vertices_list_zonotope_2D_order_one(c::VN, G::MN;
496+
apply_convex_hull::Bool) where {N, VN<:AbstractVector{N}, MN}
497+
vlist = Vector{VN}(undef, 4)
498+
a = [one(N), one(N)]
499+
b = [one(N), -one(N)]
500+
@inbounds begin
501+
vlist[1] = c .+ G * a
502+
vlist[2] = c .- G * a
503+
vlist[3] = c .+ G * b
504+
vlist[4] = c .- G * b
505+
end
506+
return apply_convex_hull ? _four_points_2d!(vlist) : vlist
507+
end
508+
437509
"""
438510
constraints_list(P::AbstractZonotope)
439511

src/Sets/HalfSpace.jl

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -310,28 +310,6 @@ function constraints_list(hs::HalfSpace)
310310
return [hs]
311311
end
312312

313-
"""
314-
constraints_list(A::AbstractMatrix{N}, b::AbstractVector)
315-
316-
Convert a matrix-vector representation to a linear-constraint representation.
317-
318-
### Input
319-
320-
- `A` -- matrix
321-
- `b` -- vector
322-
323-
### Output
324-
325-
A list of linear constraints.
326-
"""
327-
function constraints_list(A::AbstractMatrix, b::AbstractVector)
328-
m = size(A, 1)
329-
@assert m == length(b) "a matrix with $m rows is incompatible with a " *
330-
"vector of length $(length(b))"
331-
332-
return [HalfSpace(A[i, :], b[i]) for i in 1:m]
333-
end
334-
335313
"""
336314
constrained_dimensions(hs::HalfSpace)
337315

src/Sets/Zonotope.jl

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -458,74 +458,6 @@ function _bound_intersect_2D(Z::Zonotope, L::Line2D)
458458
return element(singleton)[2]
459459
end
460460

461-
function _vertices_list_2D(c::AbstractVector{N}, G::AbstractMatrix{N};
462-
apply_convex_hull::Bool) where {N}
463-
if same_sign(G)
464-
return _vertices_list_2D_positive(c, G, apply_convex_hull=apply_convex_hull)
465-
else
466-
# FIXME generalized 2D vertices list function is not implemented yet
467-
# See LazySets#2209
468-
return _vertices_list_iterative(c, G, apply_convex_hull=apply_convex_hull)
469-
end
470-
end
471-
472-
function _vertices_list_2D_positive(c::AbstractVector{N}, G::AbstractMatrix{N};
473-
apply_convex_hull::Bool) where {N}
474-
n, p = size(G)
475-
476-
# TODO special case p = 1 or p = 2 ?
477-
478-
sorted_G = sortslices(G, dims=2, by=x->atan(x[2], x[1]))
479-
index = ones(N, p, 2*p)
480-
@inbounds for i in 1:p
481-
index[i, i+1:i+p-1] .= -one(N)
482-
end
483-
index[:, 1] .= -one(N)
484-
V = sorted_G * index .+ c
485-
vlist = [V[:, i] for i in 1:2*p]
486-
487-
if apply_convex_hull
488-
convex_hull!(vlist)
489-
end
490-
return vlist
491-
end
492-
493-
function _vertices_list_iterative(c::VN, G::MN; apply_convex_hull::Bool) where {N, VN<:AbstractVector{N}, MN<:AbstractMatrix{N}}
494-
p = size(G, 2)
495-
vlist = Vector{VN}()
496-
sizehint!(vlist, 2^p)
497-
498-
for ξi in Iterators.product([(1, -1) for i = 1:p]...)
499-
push!(vlist, c .+ G * collect(ξi))
500-
end
501-
502-
return apply_convex_hull ? convex_hull!(vlist) : vlist
503-
end
504-
505-
# special case 2D zonotope of order 1/2
506-
function _vertices_list_2D_order_one_half(c::VN, G::MN; apply_convex_hull::Bool) where {N, VN<:AbstractVector{N}, MN}
507-
vlist = Vector{VN}(undef, 2)
508-
g = view(G, :, 1)
509-
@inbounds begin
510-
vlist[1] = c .+ g
511-
vlist[2] = c .- g
512-
end
513-
return apply_convex_hull ? _two_points_2d!(vlist) : vlist
514-
end
515-
516-
# special case 2D zonotope of order 1
517-
function _vertices_list_2D_order_one(c::VN, G::MN; apply_convex_hull::Bool) where {N, VN<:AbstractVector{N}, MN}
518-
vlist = Vector{VN}(undef, 4)
519-
a = [one(N), one(N)]
520-
b = [one(N), -one(N)]
521-
@inbounds begin
522-
vlist[1] = c .+ G * a
523-
vlist[2] = c .- G * a
524-
vlist[3] = c .+ G * b
525-
vlist[4] = c .- G * b
526-
end
527-
return apply_convex_hull ? _four_points_2d!(vlist) : vlist
528-
end
529461

530462
"""
531463
remove_redundant_generators(Z::Zonotope{N}) where {N}

test/Sets/Zonotope.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ for N in [Float64]
359359
@test length(vlistZ) == 6
360360

361361
# option to not apply the convex hull operation
362-
vlistZ = LazySets._vertices_list_iterative(Z.center, Z.generators, apply_convex_hull=false)
362+
vlistZ = LazySets._vertices_list_zonotope_iterative(Z.center, Z.generators, apply_convex_hull=false)
363363
@test length(vlistZ) == 8
364364
@test ispermutation(convex_hull(vlistZ), [N[-2, -2], N[0, -2], N[2, 0], N[2, 2], N[0, 2], N[-2, 0]])
365365

0 commit comments

Comments
 (0)