Skip to content

Commit

Permalink
Working on MIC 2024 paper
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnebro committed Jan 27, 2024
1 parent 66a80b9 commit ce0b84f
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 37 deletions.
9 changes: 6 additions & 3 deletions examples/NSGAIIAsAnEvolutionaryAlgorithmExample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion examples/NSGAIISolvingAConstrainedProblem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/bounds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion src/constrainedProblem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function tanakaProblem()
return tanaka
end

function ozyczka2Problem()
function osyczka2Problem()
osyczka2 = ContinuousProblem{Float64}("Osyczka2")

# Setting the variable bounds
Expand Down
80 changes: 51 additions & 29 deletions src/continuousProblem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)])

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -363,6 +365,8 @@ end
# DTLZ benchmark
#######################



struct DTLZ1 <: AbstractContinuousProblem{Float64}
bounds::Vector{Bounds{Float64}}
numberOfObjectives::Int
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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])
Expand All @@ -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")

Expand Down Expand Up @@ -500,3 +521,4 @@ end




2 changes: 1 addition & 1 deletion src/operator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ce0b84f

Please sign in to comment.