diff --git a/src/component/particleSwarmOptimization/localBestUpdate.jl b/src/component/particleSwarmOptimization/localBestUpdate.jl new file mode 100644 index 0000000..0ba1c4a --- /dev/null +++ b/src/component/particleSwarmOptimization/localBestUpdate.jl @@ -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 diff --git a/src/core/coreTypes.jl b/src/core/coreTypes.jl index f6dbaa3..a50c991 100644 --- a/src/core/coreTypes.jl +++ b/src/core/coreTypes.jl @@ -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 diff --git a/src/metajul.jl b/src/metajul.jl index 4a2855b..0dda2e3 100644 --- a/src/metajul.jl +++ b/src/metajul.jl @@ -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") diff --git a/test/component/particleSwarmOptimization/globalBestInitializationTest.jl b/test/component/particleSwarmOptimization/globalBestInitializationTest.jl index 0c49f2d..c487317 100644 --- a/test/component/particleSwarmOptimization/globalBestInitializationTest.jl +++ b/test/component/particleSwarmOptimization/globalBestInitializationTest.jl @@ -1,6 +1,5 @@ # Unit tests for DefaultGlobalBestInitialization - function inializeDefaultGlobalBestWithASolutionInitializationReturnTheRightResult() swarmSize = 1 swarm = [createContinuousSolution([1.0, 3.0]) for _ in 1:swarmSize] diff --git a/test/component/particleSwarmOptimization/globalBestUpdateTest.jl b/test/component/particleSwarmOptimization/globalBestUpdateTest.jl index ebcc40c..a6f4ebd 100644 --- a/test/component/particleSwarmOptimization/globalBestUpdateTest.jl +++ b/test/component/particleSwarmOptimization/globalBestUpdateTest.jl @@ -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] diff --git a/test/component/particleSwarmOptimization/localBestUpdateTest.jl b/test/component/particleSwarmOptimization/localBestUpdateTest.jl new file mode 100644 index 0000000..f639e2c --- /dev/null +++ b/test/component/particleSwarmOptimization/localBestUpdateTest.jl @@ -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 \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 17129de..75403ee 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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