Skip to content

Commit

Permalink
Fix a bug in ranking
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnebro committed May 22, 2024
1 parent 592f32c commit 47602ab
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion examples/NSGAIIAsAnEvolutionaryAlgorithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ function main()

solver.variation = CrossoverAndMutationVariation(offspringPopulationSize, crossover, mutation)
solver.selection = BinaryTournamentSelection(solver.variation.matingPoolSize, DefaultDominanceComparator())
solver.replacement = RankingAndDensityEstimatorReplacement(DominanceRanking(DefaultDominanceComparator()), CrowdingDistanceDensityEstimator())

solver.replacement = RankingAndDensityEstimatorReplacement(DominanceRanking(DefaultDominanceComparator()), CrowdingDistanceDensityEstimator())

startingTime = Dates.now()
optimize(solver)
endTime = Dates.now()
Expand Down
1 change: 1 addition & 0 deletions examples/NSGAIIWithStandardSettings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function main()
problem = ZDT1()

solver::NSGAII = NSGAII()
solver.problem = problem
solver.populationSize = 100

solver.termination = TerminationByEvaluations(25000)
Expand Down
1 change: 0 additions & 1 deletion src/algorithm/evolutionaryAlgorithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ function evolutionaryAlgorithm(ea::EvolutionaryAlgorithm)

notify(ea.observable, ea.status)
end

foundSolutions = population
return foundSolutions
end
Expand Down
6 changes: 3 additions & 3 deletions src/component/evolutionaryAlgorithm/replacement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ struct MuPlusLambdaReplacement <: Replacement
comparator::Comparator
end

function replace_(replacement::MuPlusLambdaReplacement, x::Vector{S}, y::Vector{S}) where {S <: Solution}
function replace_(replacement::MuPlusLambdaReplacement, x::Vector{S}, y::Vector{S}) where {S<:Solution}
jointVector = vcat(x, y)
sort!(jointVector, lt=((a, b) -> compare(replacement.comparator, a, b) <= 0))
return jointVector[1:length(x)]
Expand All @@ -12,7 +12,7 @@ struct MuCommaLambdaReplacement <: Replacement
comparator::Comparator
end

function replace_(replacement::MuCommaLambdaReplacement, x::Vector{S}, y::Vector{S}) where {S <: Solution}
function replace_(replacement::MuCommaLambdaReplacement, x::Vector{S}, y::Vector{S}) where {S<:Solution}
@assert length(x) >= length(y) "The length of the x vector is lower than the length of the y vector"

resultVector = Vector(y)
Expand All @@ -29,6 +29,7 @@ struct RankingAndDensityEstimatorReplacement <: Replacement
RankingAndDensityEstimatorReplacement(ranking, densityEstimator) = new(ranking, densityEstimator, RankingAndCrowdingDistanceComparator())
end


function replace_(replacement::RankingAndDensityEstimatorReplacement, x::Vector{T}, y::Vector{T})::Vector{T} where {T<:Solution}
jointVector = vcat(x, y)

Expand All @@ -41,4 +42,3 @@ function replace_(replacement::RankingAndDensityEstimatorReplacement, x::Vector{
sort!(jointVector, lt=((x, y) -> compare(replacement.rankingAndDensityEstimatorComparator, x, y) < 0))
return jointVector[1:length(x)]
end

2 changes: 1 addition & 1 deletion src/metajul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export variate
include("component/evolutionaryAlgorithm/variation.jl")

export MuCommaLambdaReplacement, MuPlusLambdaReplacement, RankingAndDensityEstimatorReplacement
export replace_
export replace_
include("component/evolutionaryAlgorithm/replacement.jl")

export ConstantValueStrategy
Expand Down
2 changes: 1 addition & 1 deletion src/util/densityEstimator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function compute!(densityEstimator::CrowdingDistanceDensityEstimator, solutions:
obj_range_inv = 1 / (maximumObjectiveValue - minimumObjectiveValue)

for j in 2:(num_solutions-1)
@inbounds begin
begin
distance = (solutions[j+1].objectives[i] - solutions[j-1].objectives[i]) * obj_range_inv
distance += getCrowdingDistance(solutions[j])
setCrowdingDistance(solutions[j], distance)
Expand Down
1 change: 1 addition & 0 deletions src/util/ranking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function compare(comparator::DominanceRankingComparator, solution1::Solution, so
end

function compute!(ranking::DominanceRanking, solutions::Array{T}) where {T <: Solution}
ranking.rank = []
solutionsToRank = [solution for solution in solutions]
rankCounter = 1

Expand Down

0 comments on commit 47602ab

Please sign in to comment.