Skip to content

Commit

Permalink
Refactor PSO components
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnebro committed May 16, 2024
1 parent a9b060c commit 9b12539
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
struct DefaultGlobalBestInitialization <: GlobalBestInitialization
swarm::Vector{ContinuousSolution}
globalBest::CrowdingDistanceArchive
end
struct DefaultGlobalBestInitialization <: GlobalBestInitialization end

function initialize(globalBestInitialization::DefaultGlobalBestInitialization)
@assert length(globalBestInitialization.swarm) > 0
function initialize(globalBestInitialization::DefaultGlobalBestInitialization, swarm, globalBest)
@assert length(swarm) > 0

for particle in globalBestInitialization.swarm
add!(globalBestInitialization.globalBest, copySolution(particle))
for particle in swarm
add!(globalBest, copySolution(particle))
end

return globalBestInitialization.globalBest
return globalBest
end

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
struct BinaryTournamentGlobalBestSelection <: GlobalBestSelection
dominanceComparator::Function
dominanceComparator::Comparator
end

function select(globalBestSolutions::Vector{ContinuousSolution{Float64}}, globalBestSelection::BinaryTournamentGlobalBestSelection)
function select(globalBestSelection::BinaryTournamentGlobalBestSelection, globalBestSolutions::Vector{ContinuousSolution{Float64}}, )
position1 = rand(1:length(globalBestSolutions))
position2 = rand(1:length(globalBestSolutions))

Expand All @@ -11,7 +11,7 @@ function select(globalBestSolutions::Vector{ContinuousSolution{Float64}}, global

globalBest = solution2

if globalBestSelection.dominanceComparator(solution1, solution2) < 1
if compare(globalBestSelection.dominanceComparator, solution1, solution2) < 1
globalBest = copySolution(solution1)
else
globalBest = copySolution(solution2)
Expand Down
15 changes: 6 additions & 9 deletions src/component/particleSwarmOptimization/globalBestUpdate.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
struct DefaultGlobalBestUpdate <: GlobalBestUpdate
swarm::Vector{ContinuousSolution}
globalBest::CrowdingDistanceArchive
end
struct DefaultGlobalBestUpdate <: GlobalBestUpdate end

function update(globalBestUpdate::DefaultGlobalBestUpdate)
@assert length(globalBestUpdate.swarm) > 0
function update(globalBestUpdate::DefaultGlobalBestUpdate, swarm, globalBest)
@assert length(swarm) > 0

for solution in globalBestUpdate.swarm
add!(globalBestUpdate.globalBest, copySolution(solution))
for solution in swarm
add!(globalBest, copySolution(solution))
end

return globalBestUpdate.globalBest
return globalBest
end
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
struct DefaultLocalBestInitialization <: LocalBestInitialization
swarm::Vector{ContinuousSolution}
end
struct DefaultLocalBestInitialization <: LocalBestInitialization end

function initialize(localBestInitialization::DefaultLocalBestInitialization)
@assert length(localBestInitialization.swarm) > 0
function initialize(localBestInitialization::DefaultLocalBestInitialization, swarm)
@assert length(swarm) > 0

localBest = [copySolution(solution) for solution in localBestInitialization.swarm]
localBest = [copySolution(solution) for solution in swarm]

return localBest
end
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
struct DefaultVelocityInitialization <: VelocityInitialization
swarm::Vector{ContinuousSolution}
end
struct DefaultVelocityInitialization <: VelocityInitialization end

function initialize(velocityInitialization::DefaultVelocityInitialization)
numberOfVariables = length(velocityInitialization.swarm[1].variables)
function initialize(velocityInitialization::DefaultVelocityInitialization, swarm)
numberOfVariables = length(swarm[1].variables)

return zeros(length(velocityInitialization.swarm), numberOfVariables)
return zeros(length(swarm), numberOfVariables)
end
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
# Unit tests for DefaultGlobalBestInitialization

function constructorOfDefaultGlobalBestInitializationWorksProperly()
swarmSize = 10
swarm = [createContinuousSolution([1.0, 3.0]) for _ in 1:swarmSize]

globalBest = CrowdingDistanceArchive(20, ContinuousSolution)
globalBestInitialization = DefaultGlobalBestInitialization(swarm, globalBest)

return swarm == globalBestInitialization.swarm && globalBest == globalBestInitialization.globalBest
end

function inializeDefaultGlobalBestWithASolutionInitializationReturnTheRightResult()
swarmSize = 1
swarm = [createContinuousSolution([1.0, 3.0]) for _ in 1:swarmSize]

globalBest = CrowdingDistanceArchive(20, ContinuousSolution)
globalBestInitialization = DefaultGlobalBestInitialization(swarm, globalBest)
globalBestInitialization = DefaultGlobalBestInitialization()

initialize(globalBestInitialization)
initialize(globalBestInitialization, swarm, globalBest)

return 1 == length(globalBestInitialization.globalBest)
return 1 == length(globalBest)
end


@testset "Default global best initialization tests" begin
@test constructorOfDefaultGlobalBestInitializationWorksProperly()
@test inializeDefaultGlobalBestWithASolutionInitializationReturnTheRightResult()
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Unit tests for BinaryTournamentGlobalBestSelection

function constructorOfBinaryTournamentGlobalBestSelectionWorksProperly()
dominanceComparator = compareForDominance
dominanceComparator = DefaultDominanceComparator()

globalBestSelection = BinaryTournamentGlobalBestSelection(dominanceComparator)

Expand All @@ -13,9 +13,9 @@ function binaryTournamentGlobalBestSelectionWorksProperlyWhenTheSwarmHasASolutio
swarmSize = 1
swarm = [createContinuousSolution([1.0, 3.0]) for _ in 1:swarmSize]

globalBestSelection = BinaryTournamentGlobalBestSelection(compareForDominance)
globalBestSelection = BinaryTournamentGlobalBestSelection(DefaultDominanceComparator())

solution = select(swarm, globalBestSelection)
solution = select(globalBestSelection, swarm)

return isequal(swarm[1], solution)
end
Expand Down
18 changes: 4 additions & 14 deletions test/component/particleSwarmOptimization/globalBestUpdateTest.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
# Unit tests for default global test update

function constructorOfDefaultGlobalBestUpdateWorksProperly()
swarmSize = 10
swarm = [createContinuousSolution([1.0, 3.0]) for _ in 1:swarmSize]

globalBest = CrowdingDistanceArchive(20, ContinuousSolution)
globalBestUpdate = DefaultGlobalBestUpdate(swarm, globalBest)

return swarm == globalBestUpdate.swarm && globalBest == globalBestUpdate.globalBest
end

function defaultGlobalBestUpdateWorksProperlyWhenTheSwarmHasASolution()
swarmSize = 1
swarm = [createContinuousSolution([1.0, 3.0]) for _ in 1:swarmSize]

globalBest = CrowdingDistanceArchive(20, ContinuousSolution)
globalBestUpdate = DefaultGlobalBestUpdate(swarm, globalBest)
globalBestUpdate = DefaultGlobalBestUpdate()

update(globalBestUpdate)
update(globalBestUpdate, swarm, globalBest)

return 1 == length(getSolutions(globalBest))
end
Expand All @@ -26,15 +17,14 @@ function defaultGlobalBestUpdateWorksProperlyWhenTheSwarmHasTwoNonDominatedSolut
swarm = [createContinuousSolution([1.0, 3.0]), createContinuousSolution([3.0, 1.0])]

globalBest = CrowdingDistanceArchive(20, ContinuousSolution)
globalBestUpdate = DefaultGlobalBestUpdate(swarm, globalBest)
globalBestUpdate = DefaultGlobalBestUpdate()

update(globalBestUpdate)
update(globalBestUpdate, swarm, globalBest)

return 2 == length(getSolutions(globalBest))
end

@testset "Global best update tests" begin
@test constructorOfDefaultGlobalBestUpdateWorksProperly()
@test defaultGlobalBestUpdateWorksProperlyWhenTheSwarmHasASolution()
@test defaultGlobalBestUpdateWorksProperlyWhenTheSwarmHasTwoNonDominatedSolutions()
end
Original file line number Diff line number Diff line change
@@ -1,39 +1,28 @@
# Unit tests for DefaultLocalBestInitialization

function constructorOfLocalBestInitializationWorksProperly()
swarmSize = 2
swarm = [createContinuousSolution([1.0, 3.0]) for _ in 1:swarmSize]

localBestInitialization = DefaultLocalBestInitialization(swarm)

return swarm == localBestInitialization.swarm
end


function initializeDefaultLocalBestWithASwarmWithASolutionWorksProperly()
swarmSize = 1
swarm = [createContinuousSolution([1.0, 3.0]) for _ in 1:swarmSize]

localBestInitialization = DefaultLocalBestInitialization(swarm)
localBestInitialization = DefaultLocalBestInitialization()

localBest = initialize(localBestInitialization)
localBest = initialize(localBestInitialization, swarm)

return 1 == length(localBest) && isequal(swarm[1], localBest[1])
end

function initializeDefaultLocalBestWithASwarmWithThreeSolutionsWorksProperly()
swarm = [createContinuousSolution([1.0, 3.0]), createContinuousSolution([2.0, 5.0]), createContinuousSolution([3.0, 1.0])]

localBestInitialization = DefaultLocalBestInitialization(swarm)
localBestInitialization = DefaultLocalBestInitialization()

localBest = initialize(localBestInitialization)
localBest = initialize(localBestInitialization, swarm)

return 3 == length(localBest) && isequal(swarm[1], localBest[1]) && isequal(swarm[2], localBest[2]) && isequal(swarm[3], localBest[3])
end


@testset "Default local best initialization tests" begin
@test constructorOfLocalBestInitializationWorksProperly()
@test initializeDefaultLocalBestWithASwarmWithASolutionWorksProperly()
@test initializeDefaultLocalBestWithASwarmWithThreeSolutionsWorksProperly()
end
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
# Unit tests for DefaultVelocityInitialization

function constructorOfDefaultVelocityInitializationWorksProperly()
swarmSize = 10
swarm = [createContinuousSolution([1.0, 3.0]) for _ in 1:swarmSize]

velocityInitialization = DefaultVelocityInitialization(swarm)

return swarmSize == length(velocityInitialization.swarm)
end

function inializeDefaultVelocityInitializationReturnTheRightResult()
swarmSize = 10
variables = [1.0, 0.1, 3.9]
swarm = [ContinuousSolution{Float64}(variables, [1,1,3], [], Dict(), [Bounds{Float64}(1.0, 10.0), Bounds{Float64}(1.0, 10.0)]) for _ in 1:swarmSize]

velocityInitialization = DefaultVelocityInitialization(swarm)
speed = initialize(velocityInitialization)
velocityInitialization = DefaultVelocityInitialization()
speed = initialize(velocityInitialization, swarm)

return zeros(swarmSize, length(variables)) == speed
end

@testset "Default velocity initialization tests" begin
@test constructorOfDefaultVelocityInitializationWorksProperly()
@test inializeDefaultVelocityInitializationReturnTheRightResult()
end

8 changes: 4 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ componentTests = [
"component/evolutionaryAlgorithm/replacementTest.jl",
"component/particleSwarmOptimization/inertiaWeightComputingStrategyTest.jl",
"component/particleSwarmOptimization/velocityInitializationTest.jl",
"component/particleSwarmOptimization/globalBestInitializationTest.jl"
#"component/particleSwarmOptimization/globalBestSelectionTest.jl",
#"component/particleSwarmOptimization/globalBestUpdateTest.jl",
#"component/particleSwarmOptimization/localBestInitializationTest.jl"
"component/particleSwarmOptimization/globalBestInitializationTest.jl",
"component/particleSwarmOptimization/globalBestSelectionTest.jl",
"component/particleSwarmOptimization/globalBestUpdateTest.jl",
"component/particleSwarmOptimization/localBestInitializationTest.jl"
]

for testProgram in componentTests
Expand Down

0 comments on commit 9b12539

Please sign in to comment.