Skip to content

Commit

Permalink
Add Local Best Update component
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnebro committed May 16, 2024
1 parent 9b12539 commit 2991c5b
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/component/particleSwarmOptimization/localBestUpdate.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
struct DefaultLocalBestUpdate <: LocalBestUpdate
dominanceComparator::Comparator
end

function update(localBestUpdate::DefaultLocalBestUpdate, swarm, localBest)
@assert length(swarm) > 0

for i in 1:length(swarm)
result = compare(localBestUpdate.dominanceComparator, swarm[i], localBest[i])
println(result)
if result != 1
localBest[i] = copySolution(swarm[i])
end
end

return Nothing
end
1 change: 1 addition & 0 deletions src/core/coreTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ abstract type GlobalBestInitialization <: Component end
abstract type GlobalBestSelection <: Component end
abstract type GlobalBestUpdate <: Component end
abstract type LocalBestInitialization <: Component end
abstract type LocalBestUpdate <: Component end

abstract type Ranking end
abstract type DensityEstimator end
Expand Down
4 changes: 4 additions & 0 deletions src/metajul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export DefaultLocalBestInitialization
export initialize
include("component/particleSwarmOptimization/localBestInitialization.jl")

export DefaultLocalBestUpdate
export update
include("component/particleSwarmOptimization/localBestUpdate.jl")

export normalizeObjectives, distanceBasedSubsetSelection, printObjectivesToCSVFile, printVariablesToCSVFile
include("util/utils.jl")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Unit tests for DefaultGlobalBestInitialization


function inializeDefaultGlobalBestWithASolutionInitializationReturnTheRightResult()
swarmSize = 1
swarm = [createContinuousSolution([1.0, 3.0]) for _ in 1:swarmSize]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Unit tests for default global test update


function defaultGlobalBestUpdateWorksProperlyWhenTheSwarmHasASolution()
swarmSize = 1
swarm = [createContinuousSolution([1.0, 3.0]) for _ in 1:swarmSize]
Expand Down
59 changes: 59 additions & 0 deletions test/component/particleSwarmOptimization/localBestUpdateTest.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Unit tests for DefaultLocalBestUpdate

function constructorWorksProperly()
dominanceComparator = DefaultDominanceComparator()
localBestUpdate = DefaultLocalBestUpdate(dominanceComparator)

return dominanceComparator == localBestUpdate.dominanceComparator
end

"""
Case A: the swarm has a particle which is dominated by its local best, so the local best remains unchanged
"""
function udpateWorksProperlyCaseA()
swarm = [createContinuousSolution([6.0, 7.0])]
localBest = [createContinuousSolution([2.0, 5.0])]

dominanceComparator = DefaultDominanceComparator()
localBestUpdate = DefaultLocalBestUpdate(dominanceComparator)

update(localBestUpdate, swarm, localBest)

return [2.0, 5.0] == localBest[1].objectives
end

"""
Case B: the swarm has a particle which dominates its local best, so the local best is updated
"""
function udpateWorksProperlyCaseB()
swarm = [createContinuousSolution([2.0, 3.0])]
localBest = [createContinuousSolution([5.0, 6.0])]

dominanceComparator = DefaultDominanceComparator()
localBestUpdate = DefaultLocalBestUpdate(dominanceComparator)

update(localBestUpdate, swarm, localBest)

return [2.0, 3.0] == localBest[1].objectives
end

"""
Case B: the swarm has a particle which is non-dominated with its local best, so the local best is updated
"""
function udpateWorksProperlyCaseC()
swarm = [createContinuousSolution([7.0, 3.0])]
localBest = [createContinuousSolution([5.0, 6.0])]

dominanceComparator = DefaultDominanceComparator()
localBestUpdate = DefaultLocalBestUpdate(dominanceComparator)

update(localBestUpdate, swarm, localBest)

return [7.0, 3.0] == localBest[1].objectives
end

@testset "Default local best initialization tests" begin
@test constructorWorksProperly()
@test udpateWorksProperlyCaseA()
@test udpateWorksProperlyCaseC()
end
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ componentTests = [
"component/particleSwarmOptimization/globalBestInitializationTest.jl",
"component/particleSwarmOptimization/globalBestSelectionTest.jl",
"component/particleSwarmOptimization/globalBestUpdateTest.jl",
"component/particleSwarmOptimization/localBestInitializationTest.jl"
"component/particleSwarmOptimization/localBestInitializationTest.jl",
"component/particleSwarmOptimization/localBestUpdateTest.jl"
]

for testProgram in componentTests
Expand Down

0 comments on commit 2991c5b

Please sign in to comment.