Skip to content

Commit

Permalink
Add tests for components
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnebro committed May 18, 2024
1 parent 9c794cf commit f160f9e
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/component/particleSwarmOptimization/velocityUpdate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/core/coreTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/problem/multiObjective/fonseca.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
53 changes: 53 additions & 0 deletions test/component/particleSwarmOptimization/positionUpdateTest.jl
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions test/component/particleSwarmOptimization/velocityUpdateTest.jl
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f160f9e

Please sign in to comment.