diff --git a/src/component/particleSwarmOptimization/velocityUpdate.jl b/src/component/particleSwarmOptimization/velocityUpdate.jl index 6089c8a..a2695db 100644 --- a/src/component/particleSwarmOptimization/velocityUpdate.jl +++ b/src/component/particleSwarmOptimization/velocityUpdate.jl @@ -47,8 +47,8 @@ struct ConstrainedVelocityUpdate <: VelocityUpdate deltaMax = Array{Float64}(undef, numberOfVariables(problem)) for i in 1:numberOfVariables(problem) - bounds = bounds(problem)[i] - deltaMax[i] = (bounds.upperBound - bounds.lowerBound) / 2.0 + problemBounds = bounds(problem)[i] + deltaMax[i] = (problemBounds.upperBound - problemBounds.lowerBound) / 2.0 deltaMin[i] = -deltaMax[i] end diff --git a/src/core/coreTypes.jl b/src/core/coreTypes.jl index 06b488d..eef3555 100644 --- a/src/core/coreTypes.jl +++ b/src/core/coreTypes.jl @@ -13,6 +13,7 @@ abstract type Component end abstract type SolutionsCreation <: Component end abstract type Evaluation <: Component end abstract type Termination <: Component end + abstract type Selection <: Component end abstract type Variation <: Component end abstract type Replacement <: Component end diff --git a/src/problem/multiObjective/fonseca.jl b/src/problem/multiObjective/fonseca.jl index 42978d7..f3def05 100644 --- a/src/problem/multiObjective/fonseca.jl +++ b/src/problem/multiObjective/fonseca.jl @@ -6,7 +6,6 @@ function fonseca() addVariable(problem, Bounds{Float64}(-4.0, 4.0)) end - f1 = x -> begin sum1 = 0.0 for i in range(1, numberOfVariables) diff --git a/test/component/particleSwarmOptimization/positionUpdateTest.jl b/test/component/particleSwarmOptimization/positionUpdateTest.jl new file mode 100644 index 0000000..cac55c3 --- /dev/null +++ b/test/component/particleSwarmOptimization/positionUpdateTest.jl @@ -0,0 +1,53 @@ +# Unit tests for DefaultPositionUpdate + +function defaultPositionUpdateConstructorIsCorrectlyInitialized() + velocityChangeWhenLowerLimitIsReached = -1.0 + velocityChangeWhenUpperLimitIsReached = 0.25 + positionBounds = [Bounds{Float64}(1.0, 10.0)] + + positionUpdate = DefaultPositionUpdate(velocityChangeWhenLowerLimitIsReached, velocityChangeWhenUpperLimitIsReached, positionBounds) + + return velocityChangeWhenLowerLimitIsReached == positionUpdate.velocityChangeWhenLowerLimitIsReached && velocityChangeWhenUpperLimitIsReached == positionUpdate.velocityChangeWhenUpperLimitIsReached && positionBounds == positionUpdate.positionBounds +end + + +function positionUpdateWorksProperlyWithASingleParticle() + velocityChangeWhenLowerLimitIsReached = -1.0 + velocityChangeWhenUpperLimitIsReached = 0.25 + positionBounds = [Bounds{Float64}(-10.0, 10.0), Bounds{Float64}(-15.0, 15.0)] + speed = [[0.5, -0.5]] + variables = [2.0, 4.0] + + particle = ContinuousSolution{Float64}(copy(variables), [1.5, 2.5], [], Dict(), positionBounds) + swarm = [particle] + + positionUpdate = DefaultPositionUpdate(velocityChangeWhenLowerLimitIsReached, velocityChangeWhenUpperLimitIsReached, positionBounds) + + update(positionUpdate, swarm, speed) + + return speed[1] .+ variables == particle.variables +end + +function positionUpdateWorksProperlyWithASingleParticleWithPositionsOutOfBounds() + velocityChangeWhenLowerLimitIsReached = -1.0 + velocityChangeWhenUpperLimitIsReached = 1 + positionBounds = [Bounds{Float64}(-10.0, 10.0), Bounds{Float64}(-15.0, 15.0)] + speed = [[-1.0, 1.0]] + variables = [-9.5, 14.5] + + particle = ContinuousSolution{Float64}(copy(variables), [1.5, 2.5], [], Dict(), positionBounds) + swarm = [particle] + + positionUpdate = DefaultPositionUpdate(velocityChangeWhenLowerLimitIsReached, velocityChangeWhenUpperLimitIsReached, positionBounds) + + update(positionUpdate, swarm, speed) + + return [-10.0, 15.0] == particle.variables +end + + +@testset "Default position update tests" begin + @test defaultPositionUpdateConstructorIsCorrectlyInitialized() + @test positionUpdateWorksProperlyWithASingleParticle() + @test positionUpdateWorksProperlyWithASingleParticleWithPositionsOutOfBounds() +end diff --git a/test/component/particleSwarmOptimization/velocityUpdateTest.jl b/test/component/particleSwarmOptimization/velocityUpdateTest.jl new file mode 100644 index 0000000..6c89b75 --- /dev/null +++ b/test/component/particleSwarmOptimization/velocityUpdateTest.jl @@ -0,0 +1,40 @@ +# Unit tests for DefaultVelocityUpdate + +function defaultVelocityUpdateIsCorrectlyInitialized() + c1Min = 0.1 + c1Max = 0.5 + c2Min = 0.2 + c2Max = 0.6 + + velocityUpdate = DefaultVelocityUpdate(c1Min, c1Max, c2Min, c2Max) + + return c1Min == velocityUpdate.c1Min && c1Max == velocityUpdate.c1Max && c2Min == velocityUpdate.c2Min && c2Max == velocityUpdate.c2Max +end + + +@testset "DefaultVelocityUpdate tests" begin + @test defaultVelocityUpdateIsCorrectlyInitialized() +end + +# Unit tests for ConstrainedVelocityUpdate + +function constrainedVelocityUpdateIsCorrectlyInitialized() + c1Min = 0.1 + c1Max = 0.5 + c2Min = 0.2 + c2Max = 0.6 + problem = fonseca() + + velocityUpdate = ConstrainedVelocityUpdate(c1Min, c1Max, c2Min, c2Max, problem) + + # Bounds for the tree variables of problen Fonseca: [-4.0, 4.0] + deltaMax = [(4.0 - -4.0)/2.0 for _ in 1:3] + deltaMin = -deltaMax + + + return c1Min == velocityUpdate.c1Min && c1Max == velocityUpdate.c1Max && c2Min == velocityUpdate.c2Min && c2Max == velocityUpdate.c2Max && deltaMax == velocityUpdate.deltaMax && deltaMin == velocityUpdate.deltaMin +end + +@testset "ConstrainedVelocityUpdate tests" begin + @test constrainedVelocityUpdateIsCorrectlyInitialized() +end \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 85c3188..59328f7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -36,7 +36,9 @@ componentTests = [ "component/particleSwarmOptimization/globalBestUpdateTest.jl", "component/particleSwarmOptimization/localBestInitializationTest.jl", "component/particleSwarmOptimization/localBestUpdateTest.jl", - "component/particleSwarmOptimization/perturbationTest.jl" + "component/particleSwarmOptimization/perturbationTest.jl", + "component/particleSwarmOptimization/positionUpdateTest.jl", + "component/particleSwarmOptimization/velocityUpdateTest.jl" ] for testProgram in componentTests