Skip to content

Commit

Permalink
cleanup enclose dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcelo Forets committed Sep 29, 2023
1 parent 1595413 commit 149397c
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 31 deletions.
6 changes: 3 additions & 3 deletions paper/paper.tex
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ \subsection{The enclose API}
\end{figure}

In \emph{\RE}, the \texttt{solver} is an instance of a struct that must be a subtype of \texttt{AbstractEnclosureAlgorithm}.
If a user wants to add a new solver, they just have to add a new struct, say, \texttt{MyEnclosure} and implement the method \texttt{\_enclose}, which is automatically called by \texttt{enclose}.
If a user wants to add a new solver, they just have to add a new struct, say, \texttt{MyEnclosure} and extend the method \texttt{enclose}.
%
\begin{lstlisting}[language=Julia]
_enclose(solver::MyEnclosure, f::Function,
D::Union{Interval, IntervalBox}; kwargs...)
enclose(f::Function, D::Union{Interval, IntervalBox},
solver::MyEnclosure; kwargs...)
\end{lstlisting}

Note that \texttt{D} can be of type \texttt{Interval} for univariate ($n = 1$) functions or of type \texttt{IntervalBox} for multivariate ($n > 1$) functions.
Expand Down
1 change: 0 additions & 1 deletion src/RangeEnclosures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ include("intervalarithmetic.jl")
include("branchandbound.jl")
include("taylormodels.jl")
include("affine.jl")
include("sdp_header.jl")
include("intervaloptimisation.jl")

# ================
Expand Down
4 changes: 2 additions & 2 deletions src/affine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ function load_affinearithmetic()
end # load_affinearithmetic()

# univariate
function _enclose(::AffineArithmeticEnclosure, f::Function, dom::Interval)
function enclose(f::Function, dom::Interval, ::AffineArithmeticEnclosure)
require(@__MODULE__, :AffineArithmetic; fun_name="enclose")

x = Aff(dom, 1, 1)
return interval(f(x))
end

# multivariate
function _enclose(::AffineArithmeticEnclosure, f::Function, dom::IntervalBox{N}) where {N}
function enclose(f::Function, dom::IntervalBox{N}, ::AffineArithmeticEnclosure) where {N}
require(@__MODULE__, :AffineArithmetic; fun_name="enclose")

x = [Aff(dom[i], N, i) for i in 1:N]
Expand Down
7 changes: 3 additions & 4 deletions src/enclose.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ julia> enclose(x -> 1 - x^4 + x^5, 0..1, [TaylorModelsEnclosure(), NaturalEnclos
```
"""
function enclose(f::Function, dom::Interval_or_IntervalBox,
solver::AbstractEnclosureAlgorithm=NaturalEnclosure(); kwargs...)
return _enclose(solver, f, dom; kwargs...)
function enclose(f::Function, dom::Interval_or_IntervalBox; kwargs...)
return enclose(f, dom, NaturalEnclosure())
end

function enclose(f::Function, dom::Interval_or_IntervalBox,
method::Vector; kwargs...)
return mapreduce-> _enclose(ξ, f, dom; kwargs...), , method)
return mapreduce-> enclose(f, dom, ξ; kwargs...), , method)
end

"""
Expand Down
8 changes: 4 additions & 4 deletions src/intervalarithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
# Methods using interval arithmetic
# =================================

function _enclose(::NaturalEnclosure, f::Function, dom::Interval_or_IntervalBox; kwargs...)
function enclose(f::Function, dom::Interval_or_IntervalBox, ::NaturalEnclosure; kwargs...)
return f(dom)
end

function _enclose(::MeanValueEnclosure, f::Function, dom::Interval;
df=Base.Fix1(ForwardDiff.derivative, f))
function enclose(f::Function, dom::Interval, ::MeanValueEnclosure;
df=Base.Fix1(ForwardDiff.derivative, f))
return f(mid(dom)) + df(dom) * (dom - mid(dom))
end

function _enclose(::MeanValueEnclosure, f::Function, dom::IntervalBox;
function enclose(f::Function, dom::IntervalBox, ::MeanValueEnclosure;
df=t -> ForwardDiff.gradient(f, t.v))
return f(mid.(dom)) + dot(df(dom), dom - mid.(dom))
end
3 changes: 1 addition & 2 deletions src/intervaloptimisation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
# Methods using interval optimization
# ===================================

function _enclose(mse::MooreSkelboeEnclosure, f::Function, dom::Interval_or_IntervalBox;
kwargs...)
function enclose(f::Function, dom::Interval_or_IntervalBox, mse::MooreSkelboeEnclosure; kwargs...)
require(@__MODULE__, :IntervalOptimisation; fun_name="enclose")

global_min, _ = minimise(f, dom; structure=mse.structure, tol=mse.tol)
Expand Down
12 changes: 9 additions & 3 deletions src/polynomials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ using .MultivariatePolynomials
function enclose(p::AbstractPolynomialLike, dom::Interval_or_IntervalBox,
solver::AbstractEnclosureAlgorithm=NaturalEnclosure(); kwargs...)
f(x) = p(x...)
return _enclose(solver, f, dom; kwargs...)
return enclose(f, dom, solver; kwargs...)
end

# ======================================
# Methods using semidefinite programming
# ======================================

function enclose(p::AbstractPolynomialLike, dom::Interval_or_IntervalBox,
solver::SumOfSquaresEnclosure; kwargs...)
return _enclose(solver, p, dom; kwargs...)
sose::SumOfSquaresEnclosure; kwargs...)
require(@__MODULE__, :SumOfSquares; fun_name="enclose")

return _enclose_sos(sose, p, dom; kwargs...)
end
10 changes: 0 additions & 10 deletions src/sdp_header.jl

This file was deleted.

3 changes: 1 addition & 2 deletions src/taylormodels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
# Methods using Taylor models
# ===========================

function _enclose(tme::TaylorModelsEnclosure, f::Function, dom::Interval_or_IntervalBox;
kwargs...)
function enclose(f::Function, dom::Interval_or_IntervalBox, tme::TaylorModelsEnclosure; kwargs...)
require(@__MODULE__, :TaylorModels; fun_name="enclose")

if tme.normalize
Expand Down

0 comments on commit 149397c

Please sign in to comment.