Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#755 - Refactor Zonotopes' reduce_order #1766

Merged
merged 6 commits into from Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/lib/representations.md
Expand Up @@ -698,7 +698,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
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
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