-
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
7 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/component/particleSwarmOptimization/localBestUpdate.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,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 |
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
1 change: 0 additions & 1 deletion
1
test/component/particleSwarmOptimization/globalBestInitializationTest.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
1 change: 0 additions & 1 deletion
1
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
59 changes: 59 additions & 0 deletions
59
test/component/particleSwarmOptimization/localBestUpdateTest.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,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 |
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