-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding HeapedVector.jl and adding benchmarks * trying to import Strategy.jl but cant do it * Error:In Extending the function of StrategyBase.jl * Tests and some bugs are fixed * Benchmarks is shift to another branch * .DS_store is removedand made few changes in Heaped and SortedVector * All .DS_Store files are removed
- Loading branch information
1 parent
93a6cdb
commit b364c38
Showing
9 changed files
with
224 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
__precompile__() | ||
|
||
module HeapedVectors | ||
|
||
import Base: getindex, length, push!, isempty, | ||
pop!, filter!, popfirst! | ||
|
||
export HeapedVector | ||
|
||
import ..StrategyBase:filter_elements! | ||
using ..StrategyBase | ||
|
||
struct HeapedVector{T, F<:Function} <: Strategy | ||
data::Vector{T} | ||
by::F | ||
function HeapedVector(v::Vector{T}, by::F) where {T, F} | ||
new{T, F}(heaping(v, by), by) | ||
end | ||
end | ||
|
||
HeapedVector(data::Vector{T}) where {T} = HeapedVector(data, identity) | ||
|
||
|
||
function heaping(v, by) | ||
ar = typeof(v[1])[] | ||
for i = 1:length(v) | ||
insert!(ar, i, v[i]) | ||
floatup!(ar, length(ar), by) | ||
end | ||
return ar | ||
end | ||
|
||
function floatup!(ar, index, by) | ||
par = convert(Int, floor(index/2)) | ||
if index <= 1 | ||
return ar | ||
end | ||
if by(ar[index]) < by(ar[par]) | ||
ar[par], ar[index] = ar[index], ar[par] | ||
end | ||
floatup!(ar, par, by) | ||
end | ||
|
||
|
||
function push!(v::HeapedVector{T}, x::T) where {T} | ||
insert!(v.data, length(v.data)+1, x) | ||
floatup!(v.data, length(v.data), v.by) | ||
return v | ||
end | ||
|
||
|
||
isempty(v::HeapedVector) = isempty(v.data) | ||
|
||
|
||
function popfirst!(v::HeapedVector{T}) where {T} | ||
if length(v.data) == 0 | ||
return | ||
end | ||
|
||
if length(v.data) > 2 | ||
v.data[length(v.data)], v.data[1] = v.data[1], v.data[length(v.data)] | ||
minm = pop!(v.data) | ||
bubbledown!(v::HeapedVector{T}, 1) | ||
|
||
elseif length(v.data) == 2 | ||
v.data[length(v.data)], v.data[1] = v.data[1], v.data[length(v.data)] | ||
minm = pop!(v.data) | ||
else | ||
minm = pop!(v.data) | ||
end | ||
return minm | ||
end | ||
|
||
|
||
function bubbledown!(v::HeapedVector{T}, index) where{T} | ||
left = index*2 | ||
right = index*2+1 | ||
smallest = index | ||
|
||
if length(v.data)+1 > left && v.by(v.data[smallest]) > v.by(v.data[left]) | ||
smallest = left | ||
end | ||
|
||
if length(v.data)+1 > right && v.by(v.data[smallest]) > v.by(v.data[right]) | ||
smallest = right | ||
end | ||
|
||
if smallest != index | ||
v.data[index], v.data[smallest] = v.data[smallest], v.data[index] | ||
bubbledown!(v, smallest) | ||
end | ||
end | ||
|
||
function filter_elements!(A::HeapedVector{T}, x::T) where{T} | ||
func(y) = A.by(y) < A.by(x) | ||
filter!(func, A.data) | ||
|
||
if length(A.data) == 0 | ||
return A | ||
end | ||
|
||
heaping(A.data, A.by) | ||
return A | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module StrategyBase | ||
|
||
export filter_elements!, Strategy | ||
abstract type Strategy end | ||
function filter_elements!(s::Strategy)end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using IntervalOptimisation | ||
using Test | ||
|
||
const HeapedVector = IntervalOptimisation.HeapedVector | ||
|
||
@testset "heap" begin | ||
|
||
@testset "with standard heaping " begin | ||
|
||
h=HeapedVector([9, 7, 5, 2, 3]) | ||
|
||
@test h.data == [2, 3, 7, 9, 5] | ||
|
||
push!(h, 1) | ||
@test h.data == [1, 3, 2, 9, 5, 7] | ||
|
||
popfirst!(h) | ||
@test h.data == [2, 3, 7, 9, 5] | ||
|
||
end | ||
|
||
@testset "with ordering function" begin | ||
|
||
h=HeapedVector( [(9,"f"), (7,"c"), (5,"e"), (2,"d"), (3,"b")] , x->x[2]) | ||
|
||
@test h.data == [(3, "b"), (7, "c"), (5, "e"), (9, "f"), (2, "d")] | ||
|
||
push!(h, (1,"a")) | ||
@test h.data == [(1, "a"), (7, "c"), (3, "b"), (9, "f"), (2, "d"), (5, "e")] | ||
|
||
popfirst!(h) | ||
@test h.data == [(3, "b"), (7, "c"), (5, "e"), (9, "f"), (2, "d")] | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,62 @@ | ||
using IntervalArithmetic, IntervalOptimisation | ||
using IntervalArithmetic, IntervalOptimisation | ||
using Test | ||
|
||
@testset "IntervalOptimisation tests" begin | ||
|
||
@testset "Minimise in 1D" begin | ||
@testset "Minimise in 1D using default data structure i.e HeapedVector" begin | ||
global_min, minimisers = minimise(x->x, -10..10) | ||
@test global_min ⊆ -10 .. -9.999 | ||
@test length(minimisers) == 1 | ||
@test minimisers[1] ⊆ -10 .. -9.999 | ||
|
||
global_min, minimisers = minimise(x->x^2, -10..11, 1e-10) | ||
global_min, minimisers = minimise(x->x^2, -10..11, tol = 1e-10) | ||
@test global_min ⊆ 0..1e-20 | ||
@test length(minimisers) == 1 | ||
@test minimisers[1] ⊆ -0.1..0.1 | ||
|
||
global_min, minimisers = minimise(x->(x^2-2)^2, -10..11) | ||
@test global_min ⊆ 0..1e-7 | ||
@test length(minimisers) == 2 | ||
@test sqrt(2) ∈ minimisers[1] | ||
@test sqrt(2) ∈ minimisers[2] | ||
end | ||
|
||
for Structure in (SortedVector, HeapedVector) | ||
|
||
@testset "Discontinuous function in 1D" begin | ||
@testset "Minimise in 1D using SoretedVector" begin | ||
global_min, minimisers = minimise(x->x, -10..10, structure = Structure) | ||
@test global_min ⊆ -10 .. -9.999 | ||
@test length(minimisers) == 1 | ||
@test minimisers[1] ⊆ -10 .. -9.999 | ||
|
||
H(x) = (sign(x) + 1) / 2 # Heaviside function except at 0, where H(0) = 0.5 | ||
global_min, minimisers = minimise(x->x^2, -10..11, tol=1e-10, structure = Structure) | ||
@test global_min ⊆ 0..1e-20 | ||
@test length(minimisers) == 1 | ||
@test minimisers[1] ⊆ -0.1..0.1 | ||
|
||
global_min, minimisers = minimise(x -> abs(x) + H(x) - 1, -10..11, 1e-5) | ||
@test global_min ⊆ -1 .. -0.9999 | ||
@test length(minimisers) == 1 | ||
@test 0 ∈ minimisers[1] | ||
@test diam(minimisers[1]) <= 1e-5 | ||
end | ||
global_min, minimisers = minimise(x->(x^2-2)^2, -10..11, structure = Structure) | ||
@test global_min ⊆ 0..1e-7 | ||
@test length(minimisers) == 2 | ||
@test sqrt(2) ∈ max(minimisers[1], minimisers[2]) | ||
end | ||
|
||
|
||
@testset "Smooth function in 2D" begin | ||
global_min, minimisers = minimise( X -> ( (x,y) = X; x^2 + y^2 ), (-10..10) × (-10..10) ) | ||
@test global_min ⊆ 0..1e-7 | ||
@test all(X ⊆ (-1e-3..1e3) × (-1e-3..1e-3) for X in minimisers) | ||
@testset "Discontinuous function in 1D" begin | ||
|
||
H(x) = (sign(x) + 1) / 2 # Heaviside function except at 0, where H(0) = 0.5 | ||
global_min, minimisers = minimise(x -> abs(x) + H(x) - 1, -10..11, tol=1e-5, structure = Structure) | ||
@test global_min ⊆ -1 .. -0.9999 | ||
@test length(minimisers) == 1 | ||
@test 0 ∈ minimisers[1] | ||
@test diam(minimisers[1]) <= 1e-5 | ||
end | ||
|
||
|
||
@testset "Smooth function in 2D" begin | ||
global_min, minimisers = minimise( X -> ( (x,y) = X; x^2 + y^2 ), (-10..10) × (-10..10), structure = Structure ) | ||
@test global_min ⊆ 0..1e-7 | ||
@test all(X ⊆ (-1e-3..1e3) × (-1e-3..1e-3) for X in minimisers) | ||
end | ||
|
||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b364c38
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register()
b364c38
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error while trying to register: File Project.toml not found