From c7de3d4e7b243a064423d531ad2e55c584896845 Mon Sep 17 00:00:00 2001 From: Suavesito Date: Sun, 23 Jan 2022 04:11:19 -0600 Subject: [PATCH] Avoid emptying all the minimisers if hitting minimum at the begining (#63) * Avoid emptying all the minimisers if hitting minimum at the begining * Add tests to edge cases of minimise * Modify test to avoid passing test if `global_min` is empty * bump patch version to `0.4.6` Co-authored-by: lucaferranti <49938764+lucaferranti@users.noreply.github.com> --- Project.toml | 2 +- src/HeapedVectors.jl | 2 +- src/SortedVectors.jl | 5 ++++- test/optimise.jl | 6 ++++++ 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 32f6a90..ed1664e 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "IntervalOptimisation" uuid = "c7c68f13-a4a2-5b9a-b424-07d005f8d9d2" -version = "0.4.5" +version = "0.4.6" [deps] IntervalArithmetic = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253" diff --git a/src/HeapedVectors.jl b/src/HeapedVectors.jl index 98f2092..f01a1ea 100644 --- a/src/HeapedVectors.jl +++ b/src/HeapedVectors.jl @@ -92,7 +92,7 @@ function bubbledown!(v::HeapedVector{T}, index) where{T} end function filter_elements!(A::HeapedVector{T}, x::T) where{T} - func(y) = A.by(y) < A.by(x) + func(y) = A.by(y) <= A.by(x) filter!(func, A.data) if length(A.data) == 0 diff --git a/src/SortedVectors.jl b/src/SortedVectors.jl index c42dea7..d5b587f 100644 --- a/src/SortedVectors.jl +++ b/src/SortedVectors.jl @@ -43,7 +43,10 @@ popfirst!(v::SortedVector) = popfirst!(v.data) function filter_elements!(v::SortedVector{T}, x::T) where {T} cutoff = searchsortedfirst(v.data, x, by=v.by) - resize!(v.data, cutoff-1) + if cutoff >= length(v.data) + return v + end + resize!(v.data, cutoff) return v end diff --git a/test/optimise.jl b/test/optimise.jl index 3377040..9c43319 100644 --- a/test/optimise.jl +++ b/test/optimise.jl @@ -102,6 +102,12 @@ using IntervalOptimisation: numeric_type end + @testset "Non smooth function in 2D" begin + global_min, minimisers = minimise( X -> ( (x,y) = X; abs(x-y) + abs(x) ), (-1..1) × (-1..1), structure = Structure ) + @test global_min == 0..0 # it must return the exact minimum (if mid is used to find the first global_min) + @test all(X ⊆ (-2e-3.. 2e-3) × (-2e-3..2e-3) for X in minimisers) + end + end end