Skip to content

Commit

Permalink
Merge pull request #79 from JuliaReach/schillic/58
Browse files Browse the repository at this point in the history
#58 - abstract type hierarchy
  • Loading branch information
schillic committed Dec 19, 2017
2 parents 06e85da + 8ce9c4d commit c056d6f
Show file tree
Hide file tree
Showing 28 changed files with 1,257 additions and 706 deletions.
110 changes: 81 additions & 29 deletions docs/src/lib/representations.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ support_function
support_vector
```

## LazySet interfaces

```@docs
AbstractSingleton
AbstractHPolygon
AbstractPolygon
AbstractHyperrectangle
AbstractPointSymmetric
AbstractPolytope
AbstractPointSymmetricPolytope
```

## Balls

### Euclidean norm ball
Expand All @@ -33,28 +45,36 @@ Ball2
dim(::Ball2)
σ(::AbstractVector{Float64}, ::Ball2)
∈(::AbstractVector{Float64}, ::Ball2{Float64})
center(::Ball2{Float64})
```

### Infinity norm ball

```@docs
BallInf
dim(::BallInf)
σ(::AbstractVector{Float64}, ::BallInf)
vertices_list(::BallInf)
σ(::AbstractVector{Float64}, ::BallInf{Float64})
(::AbstractVector{Float64}, ::BallInf{Float64})
norm(::BallInf, ::Real=Inf)
radius(::BallInf, ::Real=Inf)
diameter(::BallInf, ::Real=Inf)
∈(::AbstractVector{Float64}, ::BallInf{Float64})
vertices_list(::BallInf{Float64})
singleton_list(::BallInf{Float64})
center(::BallInf{Float64})
radius_hyperrectangle(::BallInf{Float64})
radius_hyperrectangle(::BallInf{Float64}, ::Int)
```

### Manhattan norm ball

```@docs
Ball1
dim(::Ball1)
σ(::AbstractVector{Float64}, ::Ball1)
σ(::AbstractVector{Float64}, ::Ball1{Float64})
∈(::AbstractVector{Float64}, ::Ball1{Float64})
vertices_list(::Ball1{Float64})
singleton_list(::Ball1{Float64})
center(::Ball1{Float64})
```

### p-norm ball
Expand All @@ -64,6 +84,7 @@ Ballp
dim(::Ballp)
σ(::AbstractVector{Float64}, ::Ballp)
∈(::AbstractVector{Float64}, ::Ballp{Float64})
center(::Ballp{Float64})
```

## Polygons
Expand All @@ -72,35 +93,41 @@ dim(::Ballp)

```@docs
HPolygon
addconstraint!(::HPolygon{Float64}, ::LinearConstraint{Float64})
dim(::HPolygon)
σ(::AbstractVector{Float64}, ::HPolygon)
σ(::AbstractVector{Float64}, ::HPolygon{Float64})
∈(::AbstractVector{Float64}, ::HPolygon{Float64})
tovrep(::HPolygon)
vertices_list(::HPolygon)
vertices_list(::HPolygon{Float64})
singleton_list(::HPolygon{Float64})
tohrep(::HPolygon{Float64})
tovrep(::HPolygon{Float64})
addconstraint!(::HPolygon{Float64}, ::LinearConstraint{Float64})
```

### Optimized constraint representation

```@docs
HPolygonOpt
addconstraint!(::HPolygonOpt{Float64}, ::LinearConstraint{Float64})
dim(::HPolygonOpt)
σ(::AbstractVector{Float64}, ::HPolygonOpt{Float64})
∈(::AbstractVector{Float64}, ::HPolygonOpt{Float64})
tovrep(::HPolygonOpt)
vertices_list(::HPolygonOpt)
vertices_list(::HPolygonOpt{Float64})
singleton_list(::HPolygonOpt{Float64})
tohrep(::HPolygonOpt{Float64})
tovrep(::HPolygonOpt{Float64})
addconstraint!(::HPolygonOpt{Float64}, ::LinearConstraint{Float64})
```

### Vertex representation

```@docs
VPolygon
dim(::VPolygon)
σ(::AbstractVector{Float64}, ::VPolygon)
vertices_list(::VPolygon)
singleton_list(::VPolygon)
σ(::AbstractVector{Float64}, ::VPolygon{Float64})
∈(::AbstractVector{Float64}, ::VPolygon{Float64})
vertices_list(::VPolygon{Float64})
singleton_list(::VPolygon{Float64})
tohrep(::VPolygon{Float64})
tovrep(::VPolygon{Float64})
```

## Lines and linear constraints
Expand All @@ -117,12 +144,16 @@ intersection(::Line{Float64}, L2::Line{Float64})
Hyperrectangle
Hyperrectangle(;kwargs...)
dim(::Hyperrectangle)
σ(::AbstractVector{Float64}, ::Hyperrectangle)
vertices_list(::Hyperrectangle)
σ(::AbstractVector{Float64}, ::Hyperrectangle{Float64})
(::AbstractVector{Float64}, ::Hyperrectangle{Float64})
norm(::Hyperrectangle, ::Real=Inf)
radius(::Hyperrectangle, ::Real=Inf)
diameter(::Hyperrectangle, ::Real=Inf)
∈(::AbstractVector{Float64}, ::Hyperrectangle{Float64})
vertices_list(::Hyperrectangle{Float64})
singleton_list(::Hyperrectangle{Float64})
center(::Hyperrectangle{Float64})
radius_hyperrectangle(::Hyperrectangle{Float64})
radius_hyperrectangle(::Hyperrectangle{Float64}, ::Int)
high(::Hyperrectangle)
low(::Hyperrectangle)
```
Expand All @@ -136,32 +167,53 @@ dim(::EmptySet)
∈(::AbstractVector{Float64}, ::EmptySet)
```

## ZeroSet

```@docs
ZeroSet
dim(::ZeroSet)
σ(::AbstractVector{Float64}, ::ZeroSet)
∈(::AbstractVector{Float64}, ::ZeroSet)
```

## Singletons

```@docs
Singleton
dim(::Singleton)
σ(::AbstractVector{Float64}, ::Singleton)
σ(::AbstractVector{Float64}, ::Singleton{Float64})
∈(::AbstractVector{Float64}, ::Singleton{Float64})
⊆(::Singleton, ::LazySet)
norm(::Singleton, ::Real=Inf)
diameter(::Singleton, ::Real=Inf)
vertices_list(::Singleton{Float64})
singleton_list(::Singleton{Float64})
center(::Singleton{Float64})
radius_hyperrectangle(::Singleton{Float64})
radius_hyperrectangle(::Singleton{Float64}, ::Int)
element(::Singleton{Float64})
element(::Singleton{Float64}, ::Int)
```

### ZeroSet

```@docs
ZeroSet
dim(::ZeroSet)
σ(::AbstractVector{Float64}, ::ZeroSet)
∈(::AbstractVector{Float64}, ::ZeroSet{Float64})
⊆(::ZeroSet, ::LazySet)
norm(::ZeroSet, ::Real=Inf)
diameter(::ZeroSet, ::Real=Inf)
vertices_list(::ZeroSet{Float64})
singleton_list(::ZeroSet{Float64})
center(::ZeroSet{Float64})
radius_hyperrectangle(::ZeroSet{Float64})
radius_hyperrectangle(::ZeroSet{Float64}, ::Int)
element(::ZeroSet{Float64})
element(::ZeroSet{Float64}, ::Int)
```

## Zonotopes

```@docs
Zonotope
dim(::Zonotope)
σ(d::AbstractVector{Float64}, Z::Zonotope)
σ(::AbstractVector{Float64}, Z::Zonotope)
∈(::AbstractVector{Float64}, ::Zonotope{Float64})
center(::Zonotope{Float64})
vertices_list(::Zonotope{Float64})
singleton_list(::Zonotope{Float64})
order(::Zonotope)
∈(::AbstractVector{Float64}, ::Zonotope{Float64})
```
160 changes: 160 additions & 0 deletions src/AbstractHPolygon.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import Base.∈

export AbstractHPolygon,
addconstraint!

"""
AbstractHPolygon{N<:Real} <: AbstractPolygon{N}
Abstract type for polygons in H-representation (i.e., constraints).
### Notes
Every concrete `AbstractHPolygon` must have the following fields:
- `constraints_list::Vector{LinearConstraint{N}}` -- the constraints
```jldoctest
julia> subtypes(AbstractHPolygon)
2-element Array{Union{DataType, UnionAll},1}:
LazySets.HPolygon
LazySets.HPolygonOpt
```
"""
abstract type AbstractHPolygon{N<:Real} <: AbstractPolygon{N} end


# --- AbstractPolygon interface functions ---


"""
tovrep(P::AbstractHPolygon{N})::VPolygon{N} where {N<:Real}
Build a vertex representation of the given polygon.
### Input
- `P` -- polygon in constraint representation
### Output
The same polygon but in vertex representation, a `VPolygon`.
"""
function tovrep(P::AbstractHPolygon{N})::VPolygon{N} where {N<:Real}
return VPolygon(vertices_list(P))
end


"""
tohrep(P::AbstractHPolygon{N})::AbstractHPolygon{N} where {N<:Real}
Build a contraint representation of the given polygon.
### Input
- `P` -- polygon in constraint representation
### Output
The identity, i.e., the same polygon instance.
"""
function tohrep(P::AbstractHPolygon{N})::AbstractHPolygon{N} where {N<:Real}
return P
end


# --- AbstractPolytope interface functions ---


"""
vertices_list(P::AbstractHPolygon{N})::Vector{Vector{N}} where {N<:Real}
Return the list of vertices of a polygon in constraint representation.
### Input
- `P` -- polygon in constraint representation
### Output
List of vertices.
"""
function vertices_list(P::AbstractHPolygon{N}
)::Vector{Vector{N}} where {N<:Real}
n = length(P.constraints_list)
points = Vector{Vector{N}}(n)
if n == 0
return points
end
@inbounds for i in 1:n-1
points[i] = intersection(Line(P.constraints_list[i]),
Line(P.constraints_list[i+1]))
end
points[n] = intersection(Line(P.constraints_list[n]),
Line(P.constraints_list[1]))
return points
end


# --- LazySet interface functions ---


"""
∈(x::AbstractVector{N}, P::AbstractHPolygon{N})::Bool where {N<:Real}
Check whether a given 2D point is contained in a polygon in constraint
representation.
### Input
- `x` -- two-dimensional point/vector
- `P` -- polygon in constraint representation
### Output
`true` iff ``x ∈ P``.
### Algorithm
This implementation checks if the point lies on the outside of each edge.
"""
function (x::AbstractVector{N}, P::AbstractHPolygon{N})::Bool where {N<:Real}
@assert length(x) == 2

for c in P.constraints_list
if dot(c.a, x) > c.b
return false
end
end
return true
end


# --- common AbstractHPolygon functions ---


"""
addconstraint!(P::AbstractHPolygon{N},
constraint::LinearConstraint{N})::Void where {N<:Real}
Add a linear constraint to a polygon in constraint representation, keeping the
constraints sorted by their normal directions.
### Input
- `P` -- polygon in constraint representation
- `constraint` -- linear constraint to add
### Output
Nothing.
"""
function addconstraint!(P::AbstractHPolygon{N},
constraint::LinearConstraint{N})::Void where {N<:Real}
i = length(P.constraints_list)
while i > 0 && constraint.a <= P.constraints_list[i].a
i -= 1
end
# here P.constraints_list[i] < constraint
insert!(P.constraints_list, i+1, constraint)
return nothing
end

0 comments on commit c056d6f

Please sign in to comment.