From ce0b84ffdaf93be9cefaeb5f1889e4256288a149 Mon Sep 17 00:00:00 2001 From: ajnebro Date: Sat, 27 Jan 2024 11:12:31 +0100 Subject: [PATCH] Working on MIC 2024 paper --- .../NSGAIIAsAnEvolutionaryAlgorithmExample.jl | 9 ++- ...mpleWithExternalCrowdingDistanceArchive.jl | 2 +- examples/NSGAIISolvingAConstrainedProblem.jl | 2 +- src/bounds.jl | 3 +- src/constrainedProblem.jl | 2 +- src/continuousProblem.jl | 80 ++++++++++++------- src/operator.jl | 2 +- 7 files changed, 63 insertions(+), 37 deletions(-) diff --git a/examples/NSGAIIAsAnEvolutionaryAlgorithmExample.jl b/examples/NSGAIIAsAnEvolutionaryAlgorithmExample.jl index 72f49e5..6425f12 100644 --- a/examples/NSGAIIAsAnEvolutionaryAlgorithmExample.jl +++ b/examples/NSGAIIAsAnEvolutionaryAlgorithmExample.jl @@ -9,22 +9,25 @@ include("../src/utils.jl") using Dates # NSGA-II algorithm configured from the evolutionary algorithm template -problem = zdt4Problem() +problem = zdt1Problem(100) solver::EvolutionaryAlgorithm = EvolutionaryAlgorithm() solver.name = "NSGA-II" solver.problem = problem solver.populationSize = 100 -solver.offspringPopulationSize = 1 +solver.offspringPopulationSize = 100 solver.solutionsCreation = DefaultSolutionsCreation((problem = solver.problem, numberOfSolutionsToCreate = solver.populationSize)) solver.evaluation = SequentialEvaluation((problem = solver.problem, )) -solver.termination = TerminationByEvaluations((numberOfEvaluationsToStop = 25000, )) +solver.termination = TerminationByEvaluations((numberOfEvaluationsToStop = 200000, )) mutation = PolynomialMutation((probability=1.0/numberOfVariables(problem), distributionIndex=20.0, bounds=problem.bounds)) + +#mutation = UniformMutation((probability=1.0/numberOfVariables(problem), perturbation=20.0, bounds=problem.bounds)) + """ crossover = BLXAlphaCrossover((probability=1.0, alpha=0.5, bounds=problem.bounds)) """ diff --git a/examples/NSGAIIExampleWithExternalCrowdingDistanceArchive.jl b/examples/NSGAIIExampleWithExternalCrowdingDistanceArchive.jl index 9e94e09..7e9f88a 100644 --- a/examples/NSGAIIExampleWithExternalCrowdingDistanceArchive.jl +++ b/examples/NSGAIIExampleWithExternalCrowdingDistanceArchive.jl @@ -16,7 +16,7 @@ solver::EvolutionaryAlgorithm = EvolutionaryAlgorithm() solver.name = "NSGA-II" solver.problem = problem solver.populationSize = 100 -solver.offspringPopulationSize = 1 +solver.offspringPopulationSize = 100 solver.solutionsCreation = DefaultSolutionsCreation((problem = solver.problem, numberOfSolutionsToCreate = solver.populationSize)) diff --git a/examples/NSGAIISolvingAConstrainedProblem.jl b/examples/NSGAIISolvingAConstrainedProblem.jl index 064eac5..2ca22fa 100644 --- a/examples/NSGAIISolvingAConstrainedProblem.jl +++ b/examples/NSGAIISolvingAConstrainedProblem.jl @@ -10,7 +10,7 @@ using Dates # NSGA-II algorithm example configured from the NSGA-II template -problem = golinskiProblem() +problem = tanakaProblem() solver::NSGAII = NSGAII() solver.problem = problem diff --git a/src/bounds.jl b/src/bounds.jl index 753fbc3..72ed15d 100644 --- a/src/bounds.jl +++ b/src/bounds.jl @@ -16,7 +16,7 @@ function createBounds(lowerBounds::Vector{T}, upperBounds::Vector{T}) where {T < end -function restrict(value::Number, bounds::Bounds)::Number +function restrict(value::T, bounds::Bounds{T}) where {T <: Number} result = value if (value < bounds.lowerBound) result = bounds.lowerBound @@ -26,6 +26,7 @@ function restrict(value::Number, bounds::Bounds)::Number return result end + function restrict(values::Vector{T}, bounds::Vector{Bounds{T}}) where {T <: Number} for i in 1:length(values) values[i] = restrict(values[i], bounds[i]) diff --git a/src/constrainedProblem.jl b/src/constrainedProblem.jl index 80ac27b..1b6825a 100644 --- a/src/constrainedProblem.jl +++ b/src/constrainedProblem.jl @@ -93,7 +93,7 @@ function tanakaProblem() return tanaka end -function ozyczka2Problem() +function osyczka2Problem() osyczka2 = ContinuousProblem{Float64}("Osyczka2") # Setting the variable bounds diff --git a/src/continuousProblem.jl b/src/continuousProblem.jl index 948cbdd..87a2902 100644 --- a/src/continuousProblem.jl +++ b/src/continuousProblem.jl @@ -65,6 +65,8 @@ function evaluate(solution::ContinuousSolution{T}, problem::ContinuousProblem{T} solution.objectives[i] = problem.objectives[i](solution.variables) end + #print(solution.objectives) + #solution.objectives = [f(solution.variables) for f in problem.objectives] #map!(x -> problem.objectives[x](solution.variables), solution.objectives, [_ for _ in 1:length(problem.objectives)]) @@ -285,7 +287,7 @@ function zdt4Problem(numberOfVariables::Int=10) return g + constant end - function evalH(v::Real, g::Float64) + function evalH(v::Float64, g::Float64) return 1.0 - sqrt(v/g) end @@ -363,6 +365,8 @@ end # DTLZ benchmark ####################### + + struct DTLZ1 <: AbstractContinuousProblem{Float64} bounds::Vector{Bounds{Float64}} numberOfObjectives::Int @@ -394,8 +398,11 @@ function evaluate(solution::ContinuousSolution{Float64}, problem::DTLZ1)::Contin k = numberOfVariables(problem) - numberOfObjectives(problem) + 1 #println("x: " , solution.variables) - g = sum([(x[i]-0.5)*(x[i]-0.5) - cos(20.0 * pi * (x[i]-0.5)) for i in range((numberOfVariables(problem)-k+1),numberOfVariables(problem))]) - + g = 0.0 + for i in (numberOfVariables(problem) - k + 1):numberOfVariables(problem) + g += (x[i] - 0.5) * (x[i] - 0.5) - cos(20.0 * π * (x[i] - 0.5)) + end + #println("G: ",g) g = 100 * (k + g) #println("G: ",g) @@ -408,32 +415,7 @@ function evaluate(solution::ContinuousSolution{Float64}, problem::DTLZ1)::Contin #println("F: ", f) - """ - M = 3 - n = 7 - k = 5 - f1 = - f2 = - f3 = - - i = 1 - j = 1 - f1 = x1 x2 (M - 1 = 3 - 1 = 2) - - i = 2 - j = 1 - f2 = x1 - aux = 1? - i = 3 - aux = 0? - - - f1 = 0.5 x1 x2 - f2 = 0.5 x1 (1 - x2) aux = 2 - f3 = 0.5 (1 - x1) aux = 1 - - """ for i in 1:numberOfObjectives(problem) #println("i: ",i) for j in 1:(numberOfObjectives(problem) - i) @@ -442,7 +424,13 @@ function evaluate(solution::ContinuousSolution{Float64}, problem::DTLZ1)::Contin #println("F[",i,"] = ", f[i]) end if i != 1 - aux = numberOfObjectives(problem) - i + 1 + #println("I: ", i) + #println("Objs: ", numberOfObjectives(problem)) + aux = numberOfObjectives(problem) + 1 - i + """ + 2 : 2 + 3 : 1 + """ #println("AUX: ", aux) f[i] *= 1 - x[aux] #println("F[",i,"] = ", f[i]) @@ -454,6 +442,39 @@ function evaluate(solution::ContinuousSolution{Float64}, problem::DTLZ1)::Contin return solution end +""" +function dtlz1Problem(numberOfVariables::Int=7, numberOfObjectives::Int=3) + dtlz1 = ContinuousProblem{Float64}("DTLZ1") + + # Add variables + for _ in 1:numberOfVariables + addVariable(dtlz1, Bounds{Float64}(0.0, 1.0)) + end + + # Function to evaluate g + function evalG(x::Vector{Float64}) + return 100 * (length(x) - numberOfObjectives + 1 + sum((xi - 0.5)^2 - cos(20 * π * (xi - 0.5)) for xi in x[numberOfObjectives:end])) + end + + # Objective functions + for m in 1:numberOfObjectives + addObjective(dtlz1, x -> begin + g = evalG(x) + f = 0.5 * (1 + g) + for i in 1:(numberOfObjectives - m) + f *= x[i] + end + if m > 1 + f *= (1 - x[numberOfObjectives - m]) + end + return f + end) + end + + return dtlz1 +end +""" + function UF1Problem(numberOfVariables::Int = 30) uf1 = ContinuousProblem{Float64}("UF1") @@ -500,3 +521,4 @@ end + diff --git a/src/operator.jl b/src/operator.jl index 60dc192..5c5e12d 100644 --- a/src/operator.jl +++ b/src/operator.jl @@ -108,7 +108,7 @@ function polynomialMutation(x::Vector{T}, parameters)::Vector{T} where {T<:Real} x[i] = y end end - x = randomRestrict(x, bounds) + x = restrict(x, bounds) return x end