diff --git a/src/ode_pso.jl b/src/ode_pso.jl index 202fa57..9ed9162 100644 --- a/src/ode_pso.jl +++ b/src/ode_pso.jl @@ -15,8 +15,8 @@ @set! particle.velocity = updated_velocity @set! particle.position = particle.position + particle.velocity - update_pos = max(particle.position, lb) - update_pos = min(update_pos, ub) + update_pos = max.(particle.position, lb) + update_pos = min.(update_pos, ub) @set! particle.position = update_pos @inbounds gpu_particles[i] = particle diff --git a/test/ode_pso.jl b/test/ode_pso.jl new file mode 100644 index 0000000..3669d9f --- /dev/null +++ b/test/ode_pso.jl @@ -0,0 +1,21 @@ +using ParallelParticleSwarms, KernelAbstractions, StaticArrays, Test + +include("./utils.jl") + +@testset "parameter_estim_ode! particle update clamps each coordinate to the box" begin + lb = SVector(-10.0f0, -10.0f0) + ub = SVector(10.0f0, 10.0f0) + positions = [SVector(5.0f0, -20.0f0), SVector(-20.0f0, 5.0f0), SVector(20.0f0, -20.0f0)] + update! = ParallelParticleSwarms.ode_update_particle_states!(backend) + for x in positions + particle = ParallelParticleSwarms.SPSOParticle(x, zero(x), Inf32, x, Inf32) + single = KernelAbstractions.allocate(backend, typeof(particle), 1) + copyto!(single, [particle]) + update!( + single, lb, ub, ParallelParticleSwarms.SPSOGBest(x, Inf32), 0.0f0; + ndrange = 1 + ) + KernelAbstractions.synchronize(backend) + @test Array(single)[1].position == clamp.(x, lb, ub) + end +end diff --git a/test/runtests.jl b/test/runtests.jl index 6a93248..22ecd29 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -16,6 +16,7 @@ run_tests(; #TODO: Current throws warning for redefinition with the use of @testset multiple times. Migrate to TestItemRunners.jl return @testset for BACKEND in unique(("CPU", CI_GROUP)) global GROUP = BACKEND + @testset "$(BACKEND) ODE parameter estimation particle update" include("./ode_pso.jl") @testset "$(BACKEND) optimizers tests" include("./gpu.jl") GC.gc(true) @testset "$(BACKEND) optimizers with constraints tests" include("./constraints.jl")