Skip to content

Commit

Permalink
included test and code for halton sequence, can be used in diverse ps…
Browse files Browse the repository at this point in the history
…eudo-random gen
  • Loading branch information
robertfeldt committed Sep 22, 2016
1 parent 280b19c commit dcbffe1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 46 deletions.
1 change: 1 addition & 0 deletions src/BlackBoxOptim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/utilities/halton_sequence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
44 changes: 0 additions & 44 deletions test/test_halton_sequence.jl

This file was deleted.

44 changes: 44 additions & 0 deletions test/utilities/test_halton_sequence.jl
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dcbffe1

Please sign in to comment.