From dcbffe110cfd834c7aefabfda70c25eceeee2bc1 Mon Sep 17 00:00:00 2001 From: Robert Feldt Date: Thu, 22 Sep 2016 10:10:55 +0200 Subject: [PATCH] included test and code for halton sequence, can be used in diverse pseudo-random gen --- src/BlackBoxOptim.jl | 1 + src/utilities/halton_sequence.jl | 4 +-- test/runtests.jl | 1 + test/test_halton_sequence.jl | 44 -------------------------- test/utilities/test_halton_sequence.jl | 44 ++++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 46 deletions(-) delete mode 100644 test/test_halton_sequence.jl create mode 100644 test/utilities/test_halton_sequence.jl diff --git a/src/BlackBoxOptim.jl b/src/BlackBoxOptim.jl index 859a8567..4b5ebad5 100644 --- a/src/BlackBoxOptim.jl +++ b/src/BlackBoxOptim.jl @@ -93,6 +93,7 @@ export Optimizer, AskTellOptimizer, SteppingOptimizer, PopulationOptimizer, module Utils include("utilities/latin_hypercube_sampling.jl") include("utilities/assign_ranks.jl") + include("utilities/halton_sequence.jl") end include("search_space.jl") diff --git a/src/utilities/halton_sequence.jl b/src/utilities/halton_sequence.jl index d5f00d5e..3b5c9330 100644 --- a/src/utilities/halton_sequence.jl +++ b/src/utilities/halton_sequence.jl @@ -16,14 +16,14 @@ end Implementation is based on the psudo code in: http://en.wikipedia.org/wiki/Halton_sequence """ -function haltonnumber(base, index) +function haltonnumber(base::Integer, index::Integer) res = 0 f = 1 / base i = index while (i > 0) res += f * (i % base) - i = ifloor(i / base) + i = floor(Integer, i / base) f = f / base end diff --git a/test/runtests.jl b/test/runtests.jl index d9609aa4..09b920e3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,6 +6,7 @@ import Compat.String my_tests = [ "utilities/test_latin_hypercube_sampling.jl", + "utilities/test_halton_sequence.jl", "utilities/test_assign_ranks.jl", "test_parameters.jl", diff --git a/test/test_halton_sequence.jl b/test/test_halton_sequence.jl deleted file mode 100644 index 99434677..00000000 --- a/test/test_halton_sequence.jl +++ /dev/null @@ -1,44 +0,0 @@ -haltonnumber = BlackBoxOptim.Utils.haltonnumber -haltonsequence = BlackBoxOptim.Utils.haltonsequence - -# The Halton sequence with base 2 according to http://en.wikipedia.org/wiki/Halton_sequence -HaltonSequence2 = [ - 1/2, 1/4, 3/4, 1/8, 5/8, 3/8, 7/8, 1/16, 9/16 -] - -# The Halton sequence with base 2 according to http://en.wikipedia.org/wiki/Halton_sequence -HaltonSequence3 = [ - 1/3, 2/3, 1/9, 4/9, 7/9, 2/9, 5/9, 8/9, 1/27 -] - -facts("Halton numbers and sequence") do - - context("Halton numbers") do - - map(1:length(HaltonSequence2)) do i - @fact isapprox(haltonnumber(2, i), HaltonSequence2[i]) --> true - end - - map(1:length(HaltonSequence3)) do i - @fact isapprox(haltonnumber(3, i), HaltonSequence3[i]) --> true - end - - end - - context("Halton sequences") do - - hseq2 = haltonsequence(2, length(HaltonSequence2)) - @fact length(hseq2) --> length(HaltonSequence2) - map(1:length(HaltonSequence2)) do i - @fact isapprox(hseq2[i], HaltonSequence2[i]) --> true - end - - hseq3 = haltonsequence(3, length(HaltonSequence3)) - @fact length(hseq3) --> length(HaltonSequence3) - map(1:length(HaltonSequence3)) do i - @fact isapprox(hseq3[i], HaltonSequence3[i]) --> true - end - - end - -end \ No newline at end of file diff --git a/test/utilities/test_halton_sequence.jl b/test/utilities/test_halton_sequence.jl new file mode 100644 index 00000000..d27d862d --- /dev/null +++ b/test/utilities/test_halton_sequence.jl @@ -0,0 +1,44 @@ +haltonnumber = BlackBoxOptim.Utils.haltonnumber +haltonsequence = BlackBoxOptim.Utils.haltonsequence + +# The Halton sequence with base 2 according to http://en.wikipedia.org/wiki/Halton_sequence +HaltonSequence2 = [ + 1/2, 1/4, 3/4, 1/8, 5/8, 3/8, 7/8, 1/16, 9/16 +] + +# The Halton sequence with base 2 according to http://en.wikipedia.org/wiki/Halton_sequence +HaltonSequence3 = [ + 1/3, 2/3, 1/9, 4/9, 7/9, 2/9, 5/9, 8/9, 1/27 +] + +@testset "Halton numbers and sequence" begin + + @testset "Halton numbers" begin + + map(1:length(HaltonSequence2)) do i + @test isapprox(haltonnumber(2, i), HaltonSequence2[i]) + end + + map(1:length(HaltonSequence3)) do i + @test isapprox(haltonnumber(3, i), HaltonSequence3[i]) + end + + end + + @testset "Halton sequences" begin + + hseq2 = haltonsequence(2, length(HaltonSequence2)) + @test length(hseq2) == length(HaltonSequence2) + map(1:length(HaltonSequence2)) do i + @test isapprox(hseq2[i], HaltonSequence2[i]) + end + + hseq3 = haltonsequence(3, length(HaltonSequence3)) + @test length(hseq3) == length(HaltonSequence3) + map(1:length(HaltonSequence3)) do i + @test isapprox(hseq3[i], HaltonSequence3[i]) + end + + end + +end \ No newline at end of file