Skip to content

Commit

Permalink
Add GlobalBestUpdate component
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnebro committed May 12, 2024
1 parent e9117d8 commit 1a263d3
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function initialize(globalBestInitialization::DefaultGlobalBestInitialization)
@assert length(globalBestInitialization.swarm) > 0

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

return globalBestInitialization.globalBest
Expand Down
14 changes: 14 additions & 0 deletions src/component/particleSwarmOptimization/globalBestUpdate.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
struct DefaultGlobalBestUpdate <: GlobalBestUpdate
swarm::Vector{ContinuousSolution}
globalBest::CrowdingDistanceArchive
end

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

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

return globalBestUpdate.globalBest
end
1 change: 1 addition & 0 deletions src/core/coreTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ abstract type InertiaWeightComputingStrategy <: Component end
abstract type VelocityInitialization <: Component end
abstract type GlobalBestInitialization <: Component end
abstract type GlobalBestSelection <: Component end
abstract type GlobalBestUpdate <: Component end

abstract type Archive end
3 changes: 3 additions & 0 deletions src/metajul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ export BinaryTournamentGlobalBestSelection
export select
include("component/particleSwarmOptimization/globalBestSelection.jl")

export DefaultGlobalBestUpdate
export update
include("component/particleSwarmOptimization/globalBestUpdate.jl")

export normalizeObjectives, distanceBasedSubsetSelection, printObjectivesToCSVFile, printVariablesToCSVFile
include("util/utils.jl")
Expand Down
40 changes: 40 additions & 0 deletions test/component/particleSwarmOptimization/globalBestUpdateTest.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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)

update(globalBestUpdate)

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

function defaultGlobalBestUpdateWorksProperlyWhenTheSwarmHasTwoNonDominatedSolutions()
swarm = [createContinuousSolution([1.0, 3.0]), createContinuousSolution([3.0, 1.0])]

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

update(globalBestUpdate)

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

@testset "Global best update tests" begin
@test constructorOfDefaultGlobalBestUpdateWorksProperly()
@test defaultGlobalBestUpdateWorksProperlyWhenTheSwarmHasASolution()
@test defaultGlobalBestUpdateWorksProperlyWhenTheSwarmHasTwoNonDominatedSolutions()
end
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ componentTests = [
"component/particleSwarmOptimization/inertiaWeightComputingStrategyTest.jl",
"component/particleSwarmOptimization/velocityInitializationTest.jl",
"component/particleSwarmOptimization/globalBestInitializationTest.jl",
"component/particleSwarmOptimization/globalBestSelectionTest.jl"
"component/particleSwarmOptimization/globalBestSelectionTest.jl",
"component/particleSwarmOptimization/globalBestUpdateTest.jl"
]

for testProgram in componentTests
Expand Down

0 comments on commit 1a263d3

Please sign in to comment.