-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
61 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/component/particleSwarmOptimization/globalBestUpdate.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
test/component/particleSwarmOptimization/globalBestUpdateTest.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters