-
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
11 changed files
with
43 additions
and
94 deletions.
There are no files selected for viewing
15 changes: 6 additions & 9 deletions
15
src/component/particleSwarmOptimization/globalBestInitialization.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 |
---|---|---|
@@ -1,15 +1,12 @@ | ||
struct DefaultGlobalBestInitialization <: GlobalBestInitialization | ||
swarm::Vector{ContinuousSolution} | ||
globalBest::CrowdingDistanceArchive | ||
end | ||
struct DefaultGlobalBestInitialization <: GlobalBestInitialization end | ||
|
||
function initialize(globalBestInitialization::DefaultGlobalBestInitialization) | ||
@assert length(globalBestInitialization.swarm) > 0 | ||
function initialize(globalBestInitialization::DefaultGlobalBestInitialization, swarm, globalBest) | ||
@assert length(swarm) > 0 | ||
|
||
for particle in globalBestInitialization.swarm | ||
add!(globalBestInitialization.globalBest, copySolution(particle)) | ||
for particle in swarm | ||
add!(globalBest, copySolution(particle)) | ||
end | ||
|
||
return globalBestInitialization.globalBest | ||
return 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
15 changes: 6 additions & 9 deletions
15
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 |
---|---|---|
@@ -1,14 +1,11 @@ | ||
struct DefaultGlobalBestUpdate <: GlobalBestUpdate | ||
swarm::Vector{ContinuousSolution} | ||
globalBest::CrowdingDistanceArchive | ||
end | ||
struct DefaultGlobalBestUpdate <: GlobalBestUpdate end | ||
|
||
function update(globalBestUpdate::DefaultGlobalBestUpdate) | ||
@assert length(globalBestUpdate.swarm) > 0 | ||
function update(globalBestUpdate::DefaultGlobalBestUpdate, swarm, globalBest) | ||
@assert length(swarm) > 0 | ||
|
||
for solution in globalBestUpdate.swarm | ||
add!(globalBestUpdate.globalBest, copySolution(solution)) | ||
for solution in swarm | ||
add!(globalBest, copySolution(solution)) | ||
end | ||
|
||
return globalBestUpdate.globalBest | ||
return globalBest | ||
end |
10 changes: 4 additions & 6 deletions
10
src/component/particleSwarmOptimization/localBestInitialization.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 |
---|---|---|
@@ -1,11 +1,9 @@ | ||
struct DefaultLocalBestInitialization <: LocalBestInitialization | ||
swarm::Vector{ContinuousSolution} | ||
end | ||
struct DefaultLocalBestInitialization <: LocalBestInitialization end | ||
|
||
function initialize(localBestInitialization::DefaultLocalBestInitialization) | ||
@assert length(localBestInitialization.swarm) > 0 | ||
function initialize(localBestInitialization::DefaultLocalBestInitialization, swarm) | ||
@assert length(swarm) > 0 | ||
|
||
localBest = [copySolution(solution) for solution in localBestInitialization.swarm] | ||
localBest = [copySolution(solution) for solution in swarm] | ||
|
||
return localBest | ||
end |
10 changes: 4 additions & 6 deletions
10
src/component/particleSwarmOptimization/velocityInitialization.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 |
---|---|---|
@@ -1,9 +1,7 @@ | ||
struct DefaultVelocityInitialization <: VelocityInitialization | ||
swarm::Vector{ContinuousSolution} | ||
end | ||
struct DefaultVelocityInitialization <: VelocityInitialization end | ||
|
||
function initialize(velocityInitialization::DefaultVelocityInitialization) | ||
numberOfVariables = length(velocityInitialization.swarm[1].variables) | ||
function initialize(velocityInitialization::DefaultVelocityInitialization, swarm) | ||
numberOfVariables = length(swarm[1].variables) | ||
|
||
return zeros(length(velocityInitialization.swarm), numberOfVariables) | ||
return zeros(length(swarm), numberOfVariables) | ||
end |
16 changes: 3 additions & 13 deletions
16
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,19 @@ | ||
# Unit tests for DefaultGlobalBestInitialization | ||
|
||
function constructorOfDefaultGlobalBestInitializationWorksProperly() | ||
swarmSize = 10 | ||
swarm = [createContinuousSolution([1.0, 3.0]) for _ in 1:swarmSize] | ||
|
||
globalBest = CrowdingDistanceArchive(20, ContinuousSolution) | ||
globalBestInitialization = DefaultGlobalBestInitialization(swarm, globalBest) | ||
|
||
return swarm == globalBestInitialization.swarm && globalBest == globalBestInitialization.globalBest | ||
end | ||
|
||
function inializeDefaultGlobalBestWithASolutionInitializationReturnTheRightResult() | ||
swarmSize = 1 | ||
swarm = [createContinuousSolution([1.0, 3.0]) for _ in 1:swarmSize] | ||
|
||
globalBest = CrowdingDistanceArchive(20, ContinuousSolution) | ||
globalBestInitialization = DefaultGlobalBestInitialization(swarm, globalBest) | ||
globalBestInitialization = DefaultGlobalBestInitialization() | ||
|
||
initialize(globalBestInitialization) | ||
initialize(globalBestInitialization, swarm, globalBest) | ||
|
||
return 1 == length(globalBestInitialization.globalBest) | ||
return 1 == length(globalBest) | ||
end | ||
|
||
|
||
@testset "Default global best initialization tests" begin | ||
@test constructorOfDefaultGlobalBestInitializationWorksProperly() | ||
@test inializeDefaultGlobalBestWithASolutionInitializationReturnTheRightResult() | ||
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
19 changes: 4 additions & 15 deletions
19
test/component/particleSwarmOptimization/localBestInitializationTest.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 |
---|---|---|
@@ -1,39 +1,28 @@ | ||
# Unit tests for DefaultLocalBestInitialization | ||
|
||
function constructorOfLocalBestInitializationWorksProperly() | ||
swarmSize = 2 | ||
swarm = [createContinuousSolution([1.0, 3.0]) for _ in 1:swarmSize] | ||
|
||
localBestInitialization = DefaultLocalBestInitialization(swarm) | ||
|
||
return swarm == localBestInitialization.swarm | ||
end | ||
|
||
|
||
function initializeDefaultLocalBestWithASwarmWithASolutionWorksProperly() | ||
swarmSize = 1 | ||
swarm = [createContinuousSolution([1.0, 3.0]) for _ in 1:swarmSize] | ||
|
||
localBestInitialization = DefaultLocalBestInitialization(swarm) | ||
localBestInitialization = DefaultLocalBestInitialization() | ||
|
||
localBest = initialize(localBestInitialization) | ||
localBest = initialize(localBestInitialization, swarm) | ||
|
||
return 1 == length(localBest) && isequal(swarm[1], localBest[1]) | ||
end | ||
|
||
function initializeDefaultLocalBestWithASwarmWithThreeSolutionsWorksProperly() | ||
swarm = [createContinuousSolution([1.0, 3.0]), createContinuousSolution([2.0, 5.0]), createContinuousSolution([3.0, 1.0])] | ||
|
||
localBestInitialization = DefaultLocalBestInitialization(swarm) | ||
localBestInitialization = DefaultLocalBestInitialization() | ||
|
||
localBest = initialize(localBestInitialization) | ||
localBest = initialize(localBestInitialization, swarm) | ||
|
||
return 3 == length(localBest) && isequal(swarm[1], localBest[1]) && isequal(swarm[2], localBest[2]) && isequal(swarm[3], localBest[3]) | ||
end | ||
|
||
|
||
@testset "Default local best initialization tests" begin | ||
@test constructorOfLocalBestInitializationWorksProperly() | ||
@test initializeDefaultLocalBestWithASwarmWithASolutionWorksProperly() | ||
@test initializeDefaultLocalBestWithASwarmWithThreeSolutionsWorksProperly() | ||
end |
14 changes: 2 additions & 12 deletions
14
test/component/particleSwarmOptimization/velocityInitializationTest.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 |
---|---|---|
@@ -1,27 +1,17 @@ | ||
# Unit tests for DefaultVelocityInitialization | ||
|
||
function constructorOfDefaultVelocityInitializationWorksProperly() | ||
swarmSize = 10 | ||
swarm = [createContinuousSolution([1.0, 3.0]) for _ in 1:swarmSize] | ||
|
||
velocityInitialization = DefaultVelocityInitialization(swarm) | ||
|
||
return swarmSize == length(velocityInitialization.swarm) | ||
end | ||
|
||
function inializeDefaultVelocityInitializationReturnTheRightResult() | ||
swarmSize = 10 | ||
variables = [1.0, 0.1, 3.9] | ||
swarm = [ContinuousSolution{Float64}(variables, [1,1,3], [], Dict(), [Bounds{Float64}(1.0, 10.0), Bounds{Float64}(1.0, 10.0)]) for _ in 1:swarmSize] | ||
|
||
velocityInitialization = DefaultVelocityInitialization(swarm) | ||
speed = initialize(velocityInitialization) | ||
velocityInitialization = DefaultVelocityInitialization() | ||
speed = initialize(velocityInitialization, swarm) | ||
|
||
return zeros(swarmSize, length(variables)) == speed | ||
end | ||
|
||
@testset "Default velocity initialization tests" begin | ||
@test constructorOfDefaultVelocityInitializationWorksProperly() | ||
@test inializeDefaultVelocityInitializationReturnTheRightResult() | ||
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