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

[WIP #191] HPolygon/HPolytope conversion #227

Merged
merged 5 commits into from
Feb 10, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ makedocs(
"Set Interfaces" => "lib/interfaces.md",
"Common Set Representations" => "lib/representations.md",
"Common Set Operations" => "lib/operations.md",
"Conversion between set representations" => "lib/conversion.md",
"Binary Functions on Sets" => "lib/binary_functions.md",
"Approximations" => "lib/approximations.md",
"Utility Functions" => "lib/utils.md"],
Expand Down
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ Pages = [
"lib/interfaces.md",
"lib/representations.md",
"lib/operations.md",
"lib/conversion.md",
"lib/approximations.md",
"lib/utils.md"
]
Expand Down
20 changes: 20 additions & 0 deletions docs/src/lib/conversion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Conversion between set representations

This section of the manual lists the conversion functions between set
representations.

```@contents
Pages = ["conversion.md"]
Depth = 3
```

```@meta
CurrentModule = LazySets
DocTestSetup = quote
using LazySets
end
```

```@docs
convert
```
2 changes: 2 additions & 0 deletions src/AbstractHPolygon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Abstract type for polygons in H-representation (i.e., constraints).
Every concrete `AbstractHPolygon` must have the following fields:
- `constraints::Vector{LinearConstraint{N}}` -- the constraints

New subtypes should be added to the `convert` method in order to be convertible.

```jldoctest
julia> subtypes(AbstractHPolygon)
2-element Array{Union{DataType, UnionAll},1}:
Expand Down
5 changes: 5 additions & 0 deletions src/HPolygon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ Use `addconstraint!` to iteratively add the edges in a sorted way.
-- default constructor
- `HPolygon()`
-- constructor with no constraints
- `HPolygon(S::LazySet)` -- constructor from another set
"""
struct HPolygon{N<:Real} <: AbstractHPolygon{N}
constraints::Vector{LinearConstraint{N}}
end
# constructor for an HPolygon with no constraints
HPolygon{N}() where {N<:Real} = HPolygon{N}(Vector{N}(0))

# constructor for an HPolygon with no constraints of type Float64
HPolygon() = HPolygon{Float64}()

# conversion constructor
HPolygon(S::LazySet) = convert(HPolygon, S)


# --- LazySet interface functions ---

Expand Down
24 changes: 8 additions & 16 deletions src/HPolygonOpt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,26 @@ The default constructor assumes that the given list of edges is sorted.
It *does not perform* any sorting.
Use `addconstraint!` to iteratively add the edges in a sorted way.

- `HPolygonOpt(constraints::Vector{LinearConstraint{<:Real}}, ind::Int)`
-- default constructor
- `HPolygonOpt(constraints::Vector{LinearConstraint{<:Real}})`
-- constructor without index
- `HPolygonOpt(H::HPolygon{<:Real})`
-- constructor from an HPolygon
- `HPolygonOpt(constraints::Vector{LinearConstraint{<:Real}}, [ind]::Int)`
-- default constructor with optional index
- `HPolygonOpt(S::LazySet)` -- constructor from another set
"""
mutable struct HPolygonOpt{N<:Real} <: AbstractHPolygon{N}
constraints::Vector{LinearConstraint{N}}
ind::Int

# default constructor
HPolygonOpt{N}(constraints::Vector{LinearConstraint{N}},
ind::Int) where {N<:Real} =
ind::Int=1) where {N<:Real} =
new{N}(constraints, ind)
end
# type-less convenience constructor
# type-less convenience constructor with optional index
HPolygonOpt(constraints::Vector{LinearConstraint{N}},
ind::Int) where {N<:Real} =
ind::Int=1) where {N<:Real} =
HPolygonOpt{N}(constraints, ind)

# type-less convenience constructor without index
HPolygonOpt(constraints::Vector{LinearConstraint{N}}) where {N<:Real} =
HPolygonOpt{N}(constraints, 1)

# constructor from an HPolygon
HPolygonOpt(H::HPolygon{N}) where {N<:Real} =
HPolygonOpt{N}(H.constraints, 1)
# conversion constructor
HPolygonOpt(S::LazySet) = convert(HPolygonOpt, S)


# --- LazySet interface functions ---
Expand Down
4 changes: 2 additions & 2 deletions src/HPolytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ HPolytope{N}() where {N<:Real} = HPolytope{N}(Vector{N}(0))
# constructor for a HPolytope with no constraints of type Float64
HPolytope() = HPolytope{Float64}()

# constructor from a polygon in H-representation
HPolytope(P::HPolygon) = HPolytope(P.constraints)
# conversion constructor
HPolytope(S::LazySet) = convert(HPolytope, S)

# constructor for a HPolytope from a simple H-representation
function HPolytope(A::Matrix{N}, b::Vector{N}) where {N<:Real}
Expand Down
5 changes: 5 additions & 0 deletions src/LazySets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ include("LinearMap.jl")
include("MinkowskiSum.jl")
include("SymmetricIntervalHull.jl")

# =============================
# Conversions between set types
# =============================
include("convert.jl")

# =======================================
# Algorithms for check operations on sets
# =======================================
Expand Down
18 changes: 0 additions & 18 deletions src/Zonotope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -386,21 +386,3 @@ function reduce_order(Z::Zonotope{N}, r)::Zonotope{N} where {N<:Real}
end
return Zonotope(c, Gred)
end

"""
convert(::Type{Zonotope}, H::AbstractHyperrectangle{N}) where {N}

Converts a hyperrectangular set to a zonotope.

### Input

- `Zonotope`
- `H` -- hyperrectangular set

### Output

A zonotope.
"""
function convert(::Type{Zonotope}, H::AbstractHyperrectangle{N}) where {N}
return Zonotope{N}(center(H), diagm(radius_hyperrectangle(H)))
end
85 changes: 85 additions & 0 deletions src/convert.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#= conversion between set types =#

"""
convert(::Type{HPOLYGON1}, P::HPOLYGON2) where
{HPOLYGON1<:Union{HPolygon, HPolygonOpt}, HPOLYGON2<:AbstractHPolygon}

Convert between polygon types in H-representation.

### Input

- `type` -- target type
- `P` -- source polygon

### Output

The polygon represented as the target type.

### Notes

We need the `Union` type for `HPOLYGON1` because the target type must be
concrete.
"""
function convert(::Type{HPOLYGON1},
P::HPOLYGON2) where {HPOLYGON1<:Union{HPolygon, HPolygonOpt},
HPOLYGON2<:AbstractHPolygon}
return HPOLYGON1(P.constraints)
end

"""
convert(::Type{HPolytope}, P::AbstractHPolygon)

Convert from polygon in H-representation to polytope in H-representation.

### Input

- `type` -- target type
- `P` -- source polygon

### Output

The polygon represented as 2D polytope.
"""
function convert(::Type{HPolytope}, P::AbstractHPolygon)
return HPolytope(P.constraints)
end

"""
convert(::Type{HPOLYGON}, P::HPolytope) where {HPOLYGON<:AbstractHPolygon}

Convert from 2D polytope in H-representation to polygon in H-representation.

### Input

- `type` -- target type
- `P` -- source polytope (must be 2D)

### Output

The 2D polytope represented as polygon.
"""
function convert(::Type{HPOLYGON},
P::HPolytope) where {HPOLYGON<:AbstractHPolygon}
if dim(P) != 2
error("polytope must be 2D for conversion")
end
return HPOLYGON(P.constraints)
end

"""
convert(::Type{Zonotope}, H::AbstractHyperrectangle{N}) where {N}

Converts a hyperrectangular set to a zonotope.

### Input

- `Zonotope`
- `H` -- hyperrectangular set

### Output

A zonotope.
"""
function convert(::Type{Zonotope}, H::AbstractHyperrectangle{N}) where {N}
return Zonotope{N}(center(H), diagm(radius_hyperrectangle(H)))
end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ include("to_N.jl")
@time @testset "LazySets.BallInf" begin include("unit_BallInf.jl") end
@time @testset "LazySets.Hyperrectangle" begin include("unit_Hyperrectangle.jl") end
@time @testset "LazySets.Polygon" begin include("unit_Polygon.jl") end
@time @testset "LazySets.HPolytope" begin include("unit_Polytope.jl") end
@time @testset "LazySets.Polytope" begin include("unit_Polytope.jl") end
@time @testset "LazySets.Zonotope" begin include("unit_Zonotope.jl") end
@time @testset "LazySets.ZeroSet" begin include("unit_ZeroSet.jl") end
@time @testset "LazySets.EmptySet" begin include("unit_EmptySet.jl") end
Expand Down
16 changes: 14 additions & 2 deletions test/unit_Polygon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,21 @@ for N in [Float64, Float32, Rational{Int}]
@test p.constraints[3] == c3
@test p.constraints[4] == c4

# Optimized polygon
# conversion to optimized polygon
po = HPolygonOpt(p)
# conversion back
HPolygon(po)
# conversion from HPolytope
polytope = HPolytope{N}()
addconstraint!(polytope, c1)
addconstraint!(polytope, c2)
addconstraint!(polytope, c3)
addconstraint!(polytope, c4)
HPolygon(polytope)
HPolygonOpt(polytope)
# conversion to HPolytope
HPolytope(p)
HPolytope(po)

# HPolygon/HPolygonOpt tests
for p in [p, po]
Expand Down Expand Up @@ -122,5 +135,4 @@ for N in [Float64, Float32, Rational{Int}]
@test vi <= v[j]
end
end

end