Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove superfluous methods and extend unit test coverage #30

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions src/dynamics/ParticleDynamics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,6 @@ A constructor helper for `ParticleDynamicCache`. Calls [`ConsensusBasedX.constru
"""
construct_particle_dynamic_cache

@config construct_method_cache(
X₀::AbstractArray,
method::CBXMethod,
particle_dynamic::ParticleDynamic,
) = nothing

function initialise_particle_dynamic_cache!(
X₀::AbstractArray,
particle_dynamic::ParticleDynamic,
Expand Down Expand Up @@ -190,13 +184,3 @@ function set_Δt!(
)
return nothing
end

function initialise_method_cache!(
X₀::AbstractArray,
method::CBXMethod,
method_cache::CBXMethodCache,
particle_dynamic::ParticleDynamic,
particle_dynamic_cache::ParticleDynamicCache,
)
return nothing
end
48 changes: 0 additions & 48 deletions src/dynamics/run_dynamic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,6 @@ function initialise_dynamic!(
return nothing
end

function initialise_method!(
method::CBXMethod,
method_cache::CBXMethodCache,
particle_dynamic::ParticleDynamic,
particle_dynamic_cache::ParticleDynamicCache,
)
return nothing
end

function compute_dynamic!(
particle_dynamic::ParticleDynamic,
particle_dynamic_cache::ParticleDynamicCache{<:Modes, <:Parallelisations},
Expand Down Expand Up @@ -137,16 +128,6 @@ function prepare_method_step!(
return nothing
end

function compute_method_step!(
method::CBXMethod,
method_cache::CBXMethodCache,
particle_dynamic::ParticleDynamic,
particle_dynamic_cache::ParticleDynamicCache,
m::Int,
)
return nothing
end

function update_dynamic!(
particle_dynamic::ParticleDynamic,
particle_dynamic_cache::ParticleDynamicCache,
Expand All @@ -156,16 +137,6 @@ function update_dynamic!(
return nothing
end

function finalise_method_step!(
method::CBXMethod,
method_cache::CBXMethodCache,
particle_dynamic::ParticleDynamic,
particle_dynamic_cache::ParticleDynamicCache,
m::Int,
)
return nothing
end

function finalise_dynamic!(
particle_dynamic::ParticleDynamic,
particle_dynamic_cache::ParticleDynamicCache,
Expand Down Expand Up @@ -201,22 +172,3 @@ function wrap_output(
particle_dynamic_cache,
)
end

function wrap_output(
X₀::AbstractArray,
method::CBXMethod,
method_cache::CBXMethodCache,
particle_dynamic::ParticleDynamic,
particle_dynamic_cache::ParticleDynamicCache,
)
initial_particles = X₀
final_particles = particle_dynamic_cache.X
return (;
initial_particles,
final_particles,
method,
method_cache,
particle_dynamic,
particle_dynamic_cache,
)
end
73 changes: 32 additions & 41 deletions src/interface/parse_config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,35 @@ end
parse_config_default(config::NamedTuple) = merge(DEFAULT_PARSED_CONFIG, config)

function parse_config_mode(config::NamedTuple)
if (haskey(config, :mode))
mode = config.mode
mode = (mode isa String) ? Symbol(mode) : mode
mode = (mode isa Symbol) ? Val(mode) : mode
if mode isa Val
if !(mode isa Modes)
explanation = "The selected `mode` is not recognised as an instance of `ConsensusBasedX.Modes`."
throw(ArgumentError(explanation))
end
else
explanation = "The keyword `mode` should be a `Symbol`, a `String`, or an instance of `ConsensusBasedX.Modes`."
mode = config.mode
mode = (mode isa String) ? Symbol(mode) : mode
mode = (mode isa Symbol) ? Val(mode) : mode
if mode isa Val
if !(mode isa Modes)
explanation = "The selected `mode` is not recognised as an instance of `ConsensusBasedX.Modes`."
throw(ArgumentError(explanation))
end
return merge(config, (; mode))
else
explanation = "The keyword `mode` should be a `Symbol`, a `String`, or an instance of `ConsensusBasedX.Modes`."
throw(ArgumentError(explanation))
end
return config
return merge(config, (; mode))
end

function parse_config_noise(config::NamedTuple)
if (haskey(config, :noise))
noise = config.noise
noise = (noise isa String) ? Symbol(noise) : noise
noise = (noise isa Symbol) ? Val(noise) : noise
if noise isa Val
if !(noise isa Noises)
explanation = "The selected `noise` is not recognised as an instance of `ConsensusBasedX.Noises`."
throw(ArgumentError(explanation))
end
else
explanation = "The keyword `noise` should be a `Symbol`, a `String`, or an instance of `ConsensusBasedX.Noises`."
noise = config.noise
noise = (noise isa String) ? Symbol(noise) : noise
noise = (noise isa Symbol) ? Val(noise) : noise
if noise isa Val
if !(noise isa Noises)
explanation = "The selected `noise` is not recognised as an instance of `ConsensusBasedX.Noises`."
throw(ArgumentError(explanation))
end
return merge(config, (; noise))
else
explanation = "The keyword `noise` should be a `Symbol`, a `String`, or an instance of `ConsensusBasedX.Noises`."
throw(ArgumentError(explanation))
end
return config
return merge(config, (; noise))
end

function parse_config_root(config::NamedTuple)
Expand All @@ -85,22 +79,19 @@ function parse_config_root(config::NamedTuple)
end

function parse_config_parallelisation(config::NamedTuple)
if (haskey(config, :parallelisation))
parallelisation = config.parallelisation
parallelisation =
(parallelisation isa String) ? Symbol(parallelisation) : parallelisation
parallelisation =
(parallelisation isa Symbol) ? Val(parallelisation) : parallelisation
if parallelisation isa Val
if !(parallelisation isa Parallelisations)
explanation = "The selected `parallelisation` is not recognised as an instance of `ConsensusBasedX.Parallelisations`."
throw(ArgumentError(explanation))
end
else
explanation = "The keyword `parallelisation` should be a `Symbol`, a `String`, or an instance of `ConsensusBasedX.Parallelisations`."
parallelisation = config.parallelisation
parallelisation =
(parallelisation isa String) ? Symbol(parallelisation) : parallelisation
parallelisation =
(parallelisation isa Symbol) ? Val(parallelisation) : parallelisation
if parallelisation isa Val
if !(parallelisation isa Parallelisations)
explanation = "The selected `parallelisation` is not recognised as an instance of `ConsensusBasedX.Parallelisations`."
throw(ArgumentError(explanation))
end
return merge(config, (; parallelisation))
else
explanation = "The keyword `parallelisation` should be a `Symbol`, a `String`, or an instance of `ConsensusBasedX.Parallelisations`."
throw(ArgumentError(explanation))
end
return config
return merge(config, (; parallelisation))
end
19 changes: 0 additions & 19 deletions src/utils/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@ end

Base.reshape(x::AbstractArray{<:AbstractArray}, mode::TParticleMode) = x

function reverse_reshape(
x::AbstractVector{<:AbstractVector{<:AbstractVector{<:Number}}},
)
M = length(x)
N = length(x[1])
D = length(x[1][1])
return [x[m][n][d] for d ∈ 1:D, n ∈ 1:N, m ∈ 1:M]
end

function reverse_reshape(x::AbstractVector{<:AbstractMatrix{<:Number}})
M = length(x)
D, N = size(x[1])
return [x[m][d, n] for d ∈ 1:D, n ∈ 1:N, m ∈ 1:M]
end

function deep_add!(
dest::AbstractArray{<:Any, N},
src::AbstractArray{<:Any, N},
Expand Down Expand Up @@ -84,7 +69,3 @@ end
nested_zeros(type::Type, dim) = zeros(type, dim)

nested_zeros(type::Type, dim::Int, dim2) = [zeros(type, dim2) for k ∈ 1:dim]

function nested_zeros(type::Type, dim::Int, dim2::Int, dim3)
return [zeros(type, dim2, dim3) for k ∈ 1:dim]
end
29 changes: 29 additions & 0 deletions test/interface/initialise_particles.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using ConsensusBasedX, Test

import LinearAlgebra

function tests()
config =
(; D = 3, N = 2, M = 1, mode = ConsensusBasedX.ParticleMode, verbosity = 0)
Expand All @@ -14,6 +16,16 @@ function tests()
(; initial_particles = rand(1, 2, 3)),
)))

@test_throws ArgumentError ConsensusBasedX.initialise_particles((merge(
config,
(; initial_particles = rand(3, 2)),
)))

@test_throws ArgumentError ConsensusBasedX.initialise_particles((merge(
config,
(; initial_particles = rand(3, 2, 1, 4)),
)))

X₀ = ConsensusBasedX.initialise_particles((merge(
config,
(; initialisation = :uniform),
Expand Down Expand Up @@ -53,6 +65,13 @@ function tests()
all(map(s -> abs(s - 2) <= 0.1, X₀[2, :, :])) &&
all(map(s -> abs(s - 3) <= 0.1, X₀[3, :, :]))

X₀ = ConsensusBasedX.initialise_particles_uniform(
merge(config, (; initial_guess = [1, 2, 3], initial_diameter = 0.2)),
)
@test all(map(s -> abs(s - 1) <= 0.1, X₀[1, :, :])) &&
all(map(s -> abs(s - 2) <= 0.1, X₀[2, :, :])) &&
all(map(s -> abs(s - 3) <= 0.1, X₀[3, :, :]))

X₀ = ConsensusBasedX.initialise_particles_uniform(
merge(
config,
Expand Down Expand Up @@ -111,6 +130,16 @@ function tests()
all(map(s -> s == 2, X₀[2, :, :])) &&
all(map(s -> s == 3, X₀[3, :, :]))

@test_nowarn ConsensusBasedX.initialise_particles_normal((merge(
config,
(; initial_mean = [1, 2, 3], initial_covariance = [1 0 0; 0 1 0; 0 0 1]),
)))

@test_throws LinearAlgebra.PosDefException ConsensusBasedX.initialise_particles_normal((merge(
config,
(; initial_mean = [1, 2, 3], initial_covariance = zeros(3, 3)),
)))

X₀ = ConsensusBasedX.initialise_particles_normal((merge(
config,
(; initial_mean = [1, 2, 3], initial_variance = [1, 0, 0]),
Expand Down
17 changes: 9 additions & 8 deletions test/interface/maximise.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ using ConsensusBasedX, Test

function tests()
alloc(x) = Base.gc_alloc_count(x.gcstats)
f(x) = -ConsensusBasedX.Quadratic(x, shift = 1)
g(x) = -ConsensusBasedX.Ackley(x, shift = 1)
h(x) = -ConsensusBasedX.Rastrigin(x, shift = 1)

config = (; D = 2, benchmark = true)
config = (; D = 2,)
@test_nowarn maximise(f, config)

f(x) = -ConsensusBasedX.Quadratic(x, shift = 1)
@test alloc(maximise(f, config)) == 0
config = Dict(:D => 2)
@test_nowarn maximise(f, config)

g(x) = -ConsensusBasedX.Ackley(x, shift = 1)
config = (; D = 2, benchmark = true)
@test alloc(maximise(f, config)) == 0
@test alloc(maximise(g, config)) == 0

h(x) = -ConsensusBasedX.Rastrigin(x, shift = 1)
@test alloc(maximise(h, config)) == 0

return config = (; D = 2,)
end

tests()
8 changes: 4 additions & 4 deletions test/interface/minimise.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
using ConsensusBasedX, Test

function tests()
alloc(x) = Base.gc_alloc_count(x.gcstats)
f(x) = ConsensusBasedX.Quadratic(x, shift = 1)
g(x) = ConsensusBasedX.Ackley(x, shift = 1)
h(x) = ConsensusBasedX.Rastrigin(x, shift = 1)

config = (; D = 2,)
@test_nowarn minimise(f, config)

alloc(x) = Base.gc_alloc_count(x.gcstats)
config = (; D = 2, benchmark = true)
config = Dict(:D => 2)
@test_nowarn minimise(f, config)

config = (; D = 2, benchmark = true)
@test alloc(minimise(f, config)) == 0

@test alloc(minimise(g, config)) == 0

@test alloc(minimise(h, config)) == 0
end

Expand Down
6 changes: 6 additions & 0 deletions test/interface/parse_config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ using ConsensusBasedX, ConsensusBasedX.ConsensusBasedXLowLevel, Test
function tests()
@test_throws ArgumentError parse_config(NamedTuple())

@test_nowarn parse_config((; D = 2))

@test_throws ArgumentError parse_config((; D = 2, mode = "wrongMode"))
@test_throws ArgumentError parse_config((; D = 2, mode = :wrongMode))
@test_throws ArgumentError parse_config((; D = 2, mode = 1.0))
Expand All @@ -11,6 +13,10 @@ function tests()
@test_throws ArgumentError parse_config((; D = 2, noise = :wrongNoise))
@test_throws ArgumentError parse_config((; D = 2, noise = 1.0))

@test_throws ArgumentError parse_config((; D = 2, root = "wrongRoot"))
@test_throws ArgumentError parse_config((; D = 2, root = :wrongRoot))
@test_throws ArgumentError parse_config((; D = 2, root = 1.0))

@test_throws ArgumentError parse_config((;
D = 2,
parallelisation = "wrongParallelisation",
Expand Down
13 changes: 11 additions & 2 deletions test/interface/sample.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
using ConsensusBasedX, Test

function tests()
alloc(x) = Base.gc_alloc_count(x.gcstats)
f(x) = ConsensusBasedX.Quadratic(x, shift = 1)

config = (; D = 2,)
@test_nowarn sample(f, config)

config = Dict(:D => 2)
@test_nowarn sample(f, config)

config = (; D = 2, root = :SymmetricRoot)
@test_nowarn sample(f, config)

config = (; D = 2, root = :AsymmetricRoot)
@test_nowarn sample(f, config)

alloc(x) = Base.gc_alloc_count(x.gcstats)
config = (; D = 2, root = :AsymmetricRoot, benchmark = true)
@test alloc(minimise(f, config)) == 0
@test alloc(sample(f, config)) == 0

config = (; D = 2, CBS_mode = :wrongCBSMode)
@test_throws ArgumentError sample(f, config)
end

tests()
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using SafeTestsets, Test
"interface/sample",
"tuples/tuples",
"tuples/types",
"utils/arrays",
"aqua",
"examples",
"format",
Expand Down
Loading
Loading