Skip to content

Commit 3476f34

Browse files
ericphansondpsanders
authored andcommitted
Allow optimizing over Bigfloat intervals (#45)
* Allow bigfloat intervals * Add tests for `numeric_type` * Bump version * Remove unneeded dependencies, clean up
1 parent 8c69987 commit 3476f34

File tree

6 files changed

+46
-23
lines changed

6 files changed

+46
-23
lines changed

Project.toml

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
name = "IntervalOptimisation"
22
uuid = "c7c68f13-a4a2-5b9a-b424-07d005f8d9d2"
3-
version = "0.4.0"
4-
5-
[compat]
6-
DataStructures = "≥ 0.9.0"
7-
ForwardDiff = "≥ 0.8.0"
8-
IntervalArithmetic = "≥ 0.15.0"
9-
IntervalConstraintProgramming = "≥ 0.9.0"
10-
IntervalRootFinding = "≥ 0.4.0"
11-
julia = "≥ 1.0.0"
3+
version = "0.4.1"
124

135
[deps]
14-
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
15-
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
166
IntervalArithmetic = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253"
17-
IntervalConstraintProgramming = "138f1668-1576-5ad7-91b9-7425abbf3153"
18-
IntervalRootFinding = "d2bf35a9-74e0-55ec-b149-d360ff49b807"
7+
8+
[compat]
9+
IntervalArithmetic = "0.15, 0.16"
10+
julia = "1.0"
1911

2012
[extras]
2113
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

src/IntervalOptimisation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using .SortedVectors
1313
include("HeapedVectors.jl")
1414
using .HeapedVectors
1515

16-
using IntervalArithmetic, IntervalRootFinding
16+
using IntervalArithmetic
1717

1818
include("optimise.jl")
1919

src/optimise.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
"""
1+
numeric_type(::Interval{T}) where {T} = T
2+
numeric_type(::IntervalBox{N, T}) where {N, T} = T
3+
4+
"""
25
minimise(f, X, structure = SortedVector, tol=1e-3)
36
or
47
minimise(f, X, structure = HeapedVector, tol=1e-3)
@@ -16,11 +19,12 @@ For higher-dimensional functions ``f:\\mathbb{R}^n \\to \\mathbb{R}``, `f` must
1619
Returns an interval containing the global minimum, and a list of boxes that contain the minimisers.
1720
"""
1821
function minimise(f, X::T; structure = HeapedVector, tol=1e-3) where {T}
19-
22+
nT = numeric_type(X)
23+
2024
# list of boxes with corresponding lower bound, arranged according to selected structure :
21-
working = structure([(X, )], x->x[2])
25+
working = structure([(X, nT(∞))], x->x[2])
2226
minimizers = T[]
23-
global_min = # upper bound
27+
global_min = nT(∞) # upper bound
2428

2529
num_bisections = 0
2630

test/heaped_vector.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using IntervalOptimisation
22
using Test
33

4-
const HeapedVector = IntervalOptimisation.HeapedVector
4+
using IntervalOptimisation: HeapedVector
55

66
@testset "heap" begin
77

@@ -33,4 +33,4 @@ const HeapedVector = IntervalOptimisation.HeapedVector
3333

3434
end
3535

36-
end
36+
end

test/optimise.jl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
using IntervalArithmetic, IntervalOptimisation
22
using Test
3+
using IntervalOptimisation: numeric_type
34

45
@testset "IntervalOptimisation tests" begin
6+
@testset "numeric_type" begin
7+
x = -10..10
8+
big_x = big(x)
9+
@test numeric_type(x) == Float64
10+
@test numeric_type(big_x) == BigFloat
11+
@test numeric_type(IntervalBox(x, x)) == Float64
12+
@test numeric_type(IntervalBox(big_x, big_x)) == BigFloat
13+
end
514

615
@testset "Minimise in 1D using default data structure i.e HeapedVector" begin
716
global_min, minimisers = minimise(x->x, -10..10)
@@ -15,6 +24,12 @@ using Test
1524
@test length(maximisers) == 1
1625
@test maximisers[1] 9.999 .. 10
1726

27+
# same but with BigFloats
28+
global_min, minimisers = minimise(x->x, -big(10.0)..big(10.0))
29+
@test global_min -10 .. -9.999
30+
@test length(minimisers) == 1
31+
@test minimisers[1] -10 .. -9.999
32+
1833
global_min, minimisers = minimise(x->x^2, -10..11, tol = 1e-10)
1934
@test global_min 0..1e-20
2035
@test length(minimisers) == 1
@@ -28,7 +43,7 @@ using Test
2843

2944
for Structure in (SortedVector, HeapedVector)
3045

31-
@testset "Minimise in 1D using SoretedVector" begin
46+
@testset "Minimise in 1D using SortedVector" begin
3247
global_min, minimisers = minimise(x->x, -10..10, structure = Structure)
3348
@test global_min -10 .. -9.999
3449
@test length(minimisers) == 1
@@ -40,6 +55,12 @@ using Test
4055
@test length(maximisers) == 1
4156
@test maximisers[1] 9.999 .. 10
4257

58+
# same but with BigFloats
59+
global_min, minimisers = minimise(x->x, -big(10.0)..big(10.0), structure = Structure)
60+
@test global_min -10 .. -9.999
61+
@test length(minimisers) == 1
62+
@test minimisers[1] -10 .. -9.999
63+
4364
global_min, minimisers = minimise(x->x^2, -10..11, tol=1e-10, structure = Structure)
4465
@test global_min 0..1e-20
4566
@test length(minimisers) == 1
@@ -73,6 +94,12 @@ using Test
7394
@test global_max 199.9..200
7495
m = (9.99..10)
7596
@test all(X m × m || X -m × m || X m × -m || X -m × -m for X in maximisers)
97+
98+
# same but with BigFloats
99+
global_min, minimisers = minimise( X -> ( (x,y) = X; x^2 + y^2 ), (-big(10.0)..big(10.0)) × (-big(10.0)..big(10.0)), structure = Structure )
100+
@test global_min 0..1e-7
101+
@test all(X big(-1e-3..1e3) × big(-1e-3..1e-3) for X in minimisers)
102+
76103
end
77104

78105
end

test/sorted_vector.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using IntervalOptimisation
22
using Test
33

4-
const SortedVector = IntervalOptimisation.SortedVector
4+
using IntervalOptimisation: SortedVector
55

66
@testset "SortedVector" begin
77

@@ -47,4 +47,4 @@ const SortedVector = IntervalOptimisation.SortedVector
4747
@test v.data == [(1, "a"), (4, "c"), (3, "x"), (5, "y"), (2, "z")]
4848
end
4949

50-
end
50+
end

0 commit comments

Comments
 (0)