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

#684 - Lazy intersection of polytope and hyperplane (Optim) #685

Merged
merged 3 commits into from
Oct 1, 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
2 changes: 1 addition & 1 deletion docs/src/lib/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ dim(::Intersection)
σ(::AbstractVector{Real}, ::Intersection{Real})
∈(::AbstractVector{Real}, ::Intersection{Real})
isempty(::Intersection)
ρ(::AbstractVector{N}, ::Intersection{N, <:LazySet, <:HalfSpace}) where {N<:AbstractFloat}
ρ(::AbstractVector{N}, ::Intersection{N, <:LazySet, S}) where {N<:AbstractFloat, S<:Union{HalfSpace, Hyperplane}}
```

Inherited from [`LazySet`](@ref):
Expand Down
44 changes: 26 additions & 18 deletions src/Intersection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -290,18 +290,18 @@ end

"""
ρ(d::AbstractVector{N},
cap::Intersection{N, <:LazySet, <:HalfSpace};
cap::Intersection{N, <:LazySet, S};
algorithm::String="line_search",
check_intersection::Bool=true,
kwargs...) where {N<:AbstractFloat}
kwargs...) where {N<:AbstractFloat, S<:Union{HalfSpace, Hyperplane}}

Return the support function of the intersection of a compact set and a half-space
in a given direction.
or a hyperplane in a given direction.

### Input

- `d` -- direction
- `cap` -- lazy intersection of a compact set and a half-space
- `cap` -- lazy intersection of a compact set and a half-space or hyperplane
- `algorithm` -- (optional, default: `"line_search"`): the algorithm to calculate
the support function, valid options are:

Expand Down Expand Up @@ -330,8 +330,10 @@ keyword arguments.
The algorithms are based on solving the associated optimization problem

```math
\\min_\\{ λ ≥ 0 \\} ρ(ℓ - λa, X) + λb.
\\min_\\{ λ ∈ D_h \\} ρ(ℓ - λa, X) + λb.
```
where ``D_h = \\{ λ : λ ≥ 0 \\}`` if ``H`` is a half-space or
``D_h = \\{ λ : λ ∈ \\mathbb{R} \\}`` if ``H`` is a hyperplane.

For additional information we refer to:

Expand All @@ -343,13 +345,13 @@ For additional information we refer to:
Variational Analysis](https://www.springer.com/us/book/9783540627722).
"""
function ρ(d::AbstractVector{N},
cap::Intersection{N, <:LazySet, <:HalfSpace};
cap::Intersection{N, <:LazySet, S};
algorithm::String="line_search",
check_intersection::Bool=true,
kwargs...) where {N<:AbstractFloat}
kwargs...) where {N<:AbstractFloat, S<:Union{HalfSpace, Hyperplane}}

X = cap.X # compact set
H = cap.Y # halfspace
H = cap.Y # halfspace or hyperplane

# if the intersection is empty => stop
if check_intersection
Expand All @@ -374,18 +376,20 @@ using Optim
"""
_line_search(ℓ, X, H; kwargs...)

Given a compact and convex set ``X`` and a hyperplane ``H = \\{x: a^T x ≤ b \\}``,
calculate:
Given a compact and convex set ``X`` and a halfspace ``H = \\{x: a^T x ≤ b \\}``
or a hyperplane ``H = \\{x: a^T x = b \\}``, calculate:

```math
\\min_\\{ λ ≥ 0 \\} ρ(ℓ - λa, X) + λb.
\\min_\\{ λ ∈ D_h \\} ρ(ℓ - λa, X) + λb.
```
where ``D_h = \\{ λ : λ ≥ 0 \\}`` if ``H`` is a half-space or
``D_h = \\{ λ : λ ∈ \\mathbb{R} \\}`` if ``H`` is a hyperplane.

### Input

- `ℓ` -- direction
- `X` -- set
- `H` -- hyperplane
- `H` -- halfspace or hyperplane

### Output

Expand Down Expand Up @@ -430,23 +434,27 @@ julia> _line_search([1.0, 0.0], X, H, upper=1e3, method=GoldenSection())
(1.0, 381.9660112501051)
```
"""
function _line_search(ℓ, X, H; kwargs...)
function _line_search(ℓ, X, H::Union{HalfSpace, Hyperplane}; kwargs...)
options = Dict(kwargs)

# Initialization
a, b = H.a, H.b
f(λ) = ρ(ℓ - λ[1] * a, X) + λ[1] * b

if haskey(options, :lower)
lower = pop!(options, :lower)
else
lower = 0.0
if H isa HalfSpace
lower = 0.0
elseif H isa Hyperplane
lower = -1e6 # "big": TODO relate with f(λ)
end
end

if haskey(options, :upper)
upper = pop!(options, :upper)
else
upper = 1e6 # TODO: relate with f(λ)
upper = 1e6 # "big": TODO relate with f(λ)
end

if haskey(options, :method)
Expand All @@ -466,7 +474,7 @@ end # quote
end # load_optim

# Symmetric case
ρ(ℓ::AbstractVector{N}, cap::Intersection{N, <:HalfSpace, <:LazySet};
ρ(ℓ::AbstractVector{N}, cap::Intersection{N, S, <:LazySet};
algorithm::String="line_search", check_intersection::Bool=true,
kwargs...) where {N<:AbstractFloat} = ρ(ℓ, cap.Y ∩ cap.X;
kwargs...) where {N<:AbstractFloat, S<:Union{HalfSpace, Hyperplane}} = ρ(ℓ, cap.Y ∩ cap.X;
algorithm=algorithm, check_intersection=check_intersection, kwargs...)
8 changes: 6 additions & 2 deletions test/unit_Intersection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ end
# Tests for Float64 only
# ======================

# halfspace - ball1 intersection
# Half-space - Ball1 intersection
X = Ball1(zeros(2), 1.0);
H = HalfSpace([-1.0, 0.0], -1.0); # x >= 0
H = HalfSpace([-1.0, 0.0], -1.0); # x >= 0
d = normalize([1.0, 0.0])

# line search using Optim
using Optim
@test ρ(d, X ∩ H) == ρ(d, H ∩ X) == 1.0

# Hyperplane - Ball1 intersection
H = HalfSpace([1.0, 0.0], 0.0); # x = 0
@test ρ(d, X ∩ H) < 1e-6 && ρ(d, H ∩ X) < 1e-6