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

add f_calls_limit parameter to minimise #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 16 additions & 9 deletions src/optimise.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
numeric_type(::IntervalBox{N, T}) where {N, T} = T

"""
minimise(f, X, structure = SortedVector, tol=1e-3)
minimise(f, X, structure = SortedVector, tol=1e-3, f_calls_limit=Inf)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inf is a Float64. Presumably the number of calls should be an integer. So this should be replaced by some large integer like 10^20, or given a sentinel value like -1 (probably preferable).

or
minimise(f, X, structure = HeapedVector, tol=1e-3)
minimise(f, X, structure = HeapedVector, tol=1e-3, f_calls_limit=Inf)
or
minimise(f, X, tol=1e-3) in this case the default value of "structure" is "HeapedVector"
minimise(f, X, tol=1e-3, f_calls_limit=Inf) in this case the default value of "structure" is "HeapedVector"

Find the global minimum of the function `f` over the `Interval` or `IntervalBox` `X`
using the Moore-Skelboe algorithm. The way in which vector elements are
Expand All @@ -18,7 +18,7 @@ For higher-dimensional functions ``f:\\mathbb{R}^n \\to \\mathbb{R}``, `f` must

Returns an interval containing the global minimum, and a list of boxes that contain the minimisers.
"""
function minimise(f, X::T; structure = HeapedVector, tol=1e-3) where {T}
function minimise(f, X::T; structure = HeapedVector, tol=1e-3, f_calls_limit=Inf) where {T}
nT = numeric_type(X)

# list of boxes with corresponding lower bound, arranged according to selected structure :
Expand All @@ -27,6 +27,7 @@ function minimise(f, X::T; structure = HeapedVector, tol=1e-3) where {T}
global_min = nT(∞) # upper bound

num_bisections = 0
f_calls = 0

while !isempty(working)

Expand Down Expand Up @@ -55,6 +56,12 @@ function minimise(f, X::T; structure = HeapedVector, tol=1e-3) where {T}
num_bisections += 1
end

f_calls += 3 # we called the function 3 times
# we still need length(minimizers) + 1 evaluations to compute lower_bound
if ((f_calls + length(minimizers) + 1) >= f_calls_limit)
push!(minimizers, X)
break
end
end

lower_bound = minimum(inf.(f.(minimizers)))
Expand All @@ -63,17 +70,17 @@ function minimise(f, X::T; structure = HeapedVector, tol=1e-3) where {T}
end

"""
maximise(f, X, structure = SortedVector, tol=1e-3)
maximise(f, X, structure = SortedVector, tol=1e-3, f_calls_limit=Inf)
or
maximise(f, X, structure = HeapedVector, tol=1e-3)
maximise(f, X, structure = HeapedVector, tol=1e-3, f_calls_limit=Inf)
or
maximise(f, X, tol=1e-3) in this case the default value of "structure" is "HeapedVector"
maximise(f, X, tol=1e-3, f_calls_limit=Inf) in this case the default value of "structure" is "HeapedVector"

Find the global maximum of the function `f` over the `Interval` or `IntervalBox` `X`
using the Moore-Skelboe algorithm. See [`minimise`](@ref) for a description
of the available options.
"""
function maximise(f, X::T; structure=HeapedVector, tol=1e-3) where {T}
bound, minimizers = minimise(x -> -f(x), X, structure=structure, tol=tol)
function maximise(f, X::T; structure=HeapedVector, tol=1e-3, f_calls_limit=Inf) where {T}
bound, minimizers = minimise(x -> -f(x), X, structure=structure, tol=tol, f_calls_limit=f_calls_limit)
return -bound, minimizers
end
5 changes: 5 additions & 0 deletions test/optimise.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ using IntervalOptimisation: numeric_type
@test length(minimisers) == 1
@test minimisers[1] ⊆ -0.1..0.1

global n_calls = 0
f = x -> begin global n_calls += 1; x^2 end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably use a let block instead of the global variable.

global_min, minimisers = minimise(f, -10..11, tol = 1e-10, f_calls_limit = 10)
@test n_calls == 10 #note : for other values we can overshoot, since we call 3 times per iteration
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the minimise function should return the total number of calls.


global_min, minimisers = minimise(x->(x^2-2)^2, -10..11)
@test global_min ⊆ 0..1e-7
@test length(minimisers) == 2
Expand Down