Skip to content

Commit

Permalink
#755 - Refactor Zonotopes' reduce_order (#1766)
Browse files Browse the repository at this point in the history
* refactor reduce_order

* remove export

* restore `reduce_order`

* add type to `r`

* change type restriction for `r`

* fix function signature in docs
  • Loading branch information
SebastianGuadalupe authored and mforets committed Oct 16, 2019
1 parent 2376e92 commit 608c78e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 33 deletions.
2 changes: 1 addition & 1 deletion docs/src/lib/representations.md
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ generators(Z::Zonotope)
genmat(Z::Zonotope)
scale(::Real, ::Zonotope)
ngens(::Zonotope)
reduce_order(::Zonotope, r)
reduce_order(::Zonotope, ::Union{Integer, Rational})
split(::Zonotope, ::Int)
```

Expand Down
52 changes: 52 additions & 0 deletions src/Approximations/overapproximate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1316,3 +1316,55 @@ function overapproximate(cap::Intersection{N,
::Type{CartesianProductArray}, oa) where {N}
overapproximate(Intersection(cap.Y, cap.X), oa)
end

"""
overapproximate(Z::Zonotope{N}, ::Type{<:Zonotope}, r::Union{Integer, Rational}) where {N<:Real}
Reduce the order of a zonotope by overapproximating with a zonotope with less
generators.
### Input
- `Z` -- zonotope
- `Zonotope` -- desired type for dispatch
- `r` -- desired order
### Output
A new zonotope with less generators, if possible.
### Algorithm
This function implements the algorithm described in A. Girard's
*Reachability of Uncertain Linear Systems Using Zonotopes*, HSCC. Vol. 5. 2005.
If the desired order is smaller than one, the zonotope is *not* reduced.
"""
function overapproximate(Z::Zonotope{N}, ::Type{<:Zonotope}, r::Union{Integer, Rational}) where {N<:Real}
c, G = Z.center, Z.generators
d, p = dim(Z), ngens(Z)

if r * d >= p || r < 1
# do not reduce
return Z
end

h = zeros(N, p)
for i in 1:p
h[i] = norm(G[:, i], 1) - norm(G[:, i], Inf)
end
ind = sortperm(h)

m = p - floor(Int, d * (r - 1)) # subset of ngens that are reduced
rg = G[:, ind[1:m]] # reduced generators

# interval hull computation of reduced generators
Gbox = Diagonal(sum(abs.(rg), dims=2)[:])
if m < p
Gnotred = G[:, ind[m+1:end]]
Gred = [Gnotred Gbox]
else
Gred = Gbox
end
return Zonotope(c, Gred)
end
36 changes: 4 additions & 32 deletions src/Sets/Zonotope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function scale(α::Real, Z::Zonotope)
end

"""
reduce_order(Z::Zonotope, r)::Zonotope
reduce_order(Z::Zonotope, r::Union{Integer, Rational})::Zonotope
Reduce the order of a zonotope by overapproximating with a zonotope with less
generators.
Expand All @@ -273,38 +273,10 @@ A new zonotope with less generators, if possible.
### Algorithm
This function implements the algorithm described in A. Girard's
*Reachability of Uncertain Linear Systems Using Zonotopes*, HSCC. Vol. 5. 2005.
If the desired order is smaller than one, the zonotope is *not* reduced.
See `overapproximate(Z::Zonotope{N}, ::Type{<:Zonotope}, r::Union{Integer, Rational}) where {N<:Real}` for details.
"""
function reduce_order(Z::Zonotope{N}, r)::Zonotope{N} where {N<:Real}
c, G = Z.center, Z.generators
d, p = dim(Z), ngens(Z)

if r * d >= p || r < 1
# do not reduce
return Z
end

h = zeros(N, p)
for i in 1:p
h[i] = norm(G[:, i], 1) - norm(G[:, i], Inf)
end
ind = sortperm(h)

m = p - floor(Int, d * (r - 1)) # subset of ngens that are reduced
rg = G[:, ind[1:m]] # reduced generators

# interval hull computation of reduced generators
Gbox = Diagonal(sum(abs.(rg), dims=2)[:])
if m < p
Gnotred = G[:, ind[m+1:end]]
Gred = [Gnotred Gbox]
else
Gred = Gbox
end
return Zonotope(c, Gred)
function reduce_order(Z::Zonotope{N}, r::Union{Integer, Rational})::Zonotope{N} where {N<:Real}
return overapproximate(Z, Zonotope, r)
end

"""
Expand Down

0 comments on commit 608c78e

Please sign in to comment.