Skip to content

Commit

Permalink
Add new problems
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnebro committed Jan 22, 2024
1 parent d6c405a commit 66a80b9
Show file tree
Hide file tree
Showing 8 changed files with 479 additions and 490 deletions.
11 changes: 6 additions & 5 deletions examples/NSGAIIAsAnEvolutionaryAlgorithmExample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ include("../src/utils.jl")
using Dates

# NSGA-II algorithm configured from the evolutionary algorithm template
problem = zdt1Problem()
problem = zdt4Problem()

solver::EvolutionaryAlgorithm = EvolutionaryAlgorithm()
solver.name = "NSGA-II"

solver.problem = problem
solver.populationSize = 100
solver.offspringPopulationSize = 100
solver.offspringPopulationSize = 1

solver.solutionsCreation = DefaultSolutionsCreation((problem = solver.problem, numberOfSolutionsToCreate = solver.populationSize))

Expand All @@ -28,7 +28,7 @@ mutation = PolynomialMutation((probability=1.0/numberOfVariables(problem), distr
"""
crossover = BLXAlphaCrossover((probability=1.0, alpha=0.5, bounds=problem.bounds))
"""
crossover = SBXCrossover((probability=1.0, distributionIndex=20.0, bounds=problem.bounds))
crossover = SBXCrossover((probability=0.9, distributionIndex=20.0, bounds=problem.bounds))

solver.variation = CrossoverAndMutationVariation((offspringPopulationSize = solver.offspringPopulationSize, crossover = crossover, mutation = mutation))

Expand All @@ -37,10 +37,11 @@ solver.selection = BinaryTournamentSelection((matingPoolSize = solver.variation.
solver.replacement = RankingAndDensityEstimatorReplacement((dominanceComparator = compareForDominance, ))

startingTime = Dates.now()
optimize(solver)
#optimize(solver)
foundSolutions = evolutionaryAlgorithm(solver)
endTime = Dates.now()

foundSolutions = solver.foundSolutions
#foundSolutions = solver.foundSolutions

objectivesFileName = "FUN.csv"
variablesFileName = "VAR.csv"
Expand Down
2 changes: 1 addition & 1 deletion examples/NSGAIIExample.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 = zdt1Problem()
problem = zdt4Problem()

solver::NSGAII = NSGAII()
solver.problem = problem
Expand Down
4 changes: 2 additions & 2 deletions examples/NSGAIIExampleWithExternalCrowdingDistanceArchive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ using Dates

# NSGA-II algorithm configured from the evolutionary algorithm template. It incorporates an external archive to store the non-dominated solution found. This archive will be the algorithm output.

problem = zdt2Problem()
problem = zdt4Problem()

solver::EvolutionaryAlgorithm = EvolutionaryAlgorithm()
solver.name = "NSGA-II"
solver.problem = problem
solver.populationSize = 100
solver.offspringPopulationSize = 100
solver.offspringPopulationSize = 1

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 = srinivasProblem()
problem = golinskiProblem()

solver::NSGAII = NSGAII()
solver.problem = problem
Expand Down
676 changes: 276 additions & 400 deletions notebooks/NSGA-II notebook.ipynb

Large diffs are not rendered by default.

41 changes: 17 additions & 24 deletions src/comparator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,32 @@ Compare two numerics vectors `x` and `y` according to the dominance relationship
function compareForDominance(x::Vector{T}, y::Vector{T})::Int where {T<:Number}
@assert length(x) == length(y) "The vectors have a different length"

"""
x==y && return 0
bestIsSolution1 = false
bestIsSolution2 = false

all(t->(t[1]≤t[2]), zip(x, y)) && return -1
all(t->(t[1]≤t[2]), zip(y, x)) && return 1
return 0
"""
bestIsSolution1 = 0
bestIsSolution2 = 0
result = 0

for i in 1:length(x)
if x[i] != y[i]
@inbounds for i in 1:length(x)
if x[i] < y[i]
y[i]
bestIsSolution1 = 1
bestIsSolution1 = true
elseif x[i] > y[i]
bestIsSolution2 = true
end
if x[i] > y[i]
bestIsSolution2 = 1

# Early exit if both flags are true
if bestIsSolution1 && bestIsSolution2
return 0
end
end
end

if bestIsSolution1 > bestIsSolution2
result = -1
elseif bestIsSolution1 < bestIsSolution2
result = 1
if bestIsSolution1
return -1
elseif bestIsSolution2
return 1
else
return 0
end

return result
end


function compareForDominance(solution1::Solution, solution2::Solution)::Int
return compareForDominance(solution1.objectives, solution2.objectives)
end
Expand Down
130 changes: 130 additions & 0 deletions src/constrainedProblem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,133 @@ function srinivasProblem()

return problem
end

function binh2Problem()
binh2 = ContinuousProblem{Float64}("Binh2")

# Setting the variable bounds
addVariable(binh2, Bounds{Float64}(0.0, 5.0))
addVariable(binh2, Bounds{Float64}(0.0, 3.0))

# Objective functions
f1 = x -> 4.0 * x[1] * x[1] + 4.0 * x[2] * x[2]
f2 = x -> (x[1] - 5.0) * (x[1] - 5.0) + (x[2] - 5.0) * (x[2] - 5.0)

addObjective(binh2, f1)
addObjective(binh2, f2)

# Constraints
c1 = x -> -((x[1] - 5.0) * (x[1] - 5.0) + x[2] * x[2] - 25.0)
c2 = x -> (x[1] - 8.0) * (x[1] - 8.0) + (x[2] + 3.0) * (x[2] + 3.0) - 7.7

addConstraint(binh2, c1)
addConstraint(binh2, c2)

return binh2
end

function tanakaProblem()
tanaka = ContinuousProblem{Float64}("Tanaka")

# Setting the variable bounds
for _ in 1:2
addVariable(tanaka, Bounds{Float64}(10e-5, π))
end

# Objective functions
f1 = x -> x[1]
f2 = x -> x[2]

addObjective(tanaka, f1)
addObjective(tanaka, f2)

# Constraints
c1 = x -> x[1] * x[1] + x[2] * x[2] - 1.0 - 0.1 * cos(16.0 * atan(x[1] / x[2]))
c2 = x -> -2.0 * ((x[1] - 0.5) * (x[1] - 0.5) + (x[2] - 0.5) * (x[2] - 0.5) - 0.5)

addConstraint(tanaka, c1)
addConstraint(tanaka, c2)

return tanaka
end

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

# Setting the variable bounds
bounds = [(0.0, 10.0), (0.0, 10.0), (1.0, 5.0), (0.0, 6.0), (1.0, 5.0), (0.0, 10.0)]
for (lower, upper) in bounds
addVariable(osyczka2, Bounds{Float64}(lower, upper))
end

# Objective functions
f1 = x -> -(25.0 * (x[1] - 2.0)^2 + (x[2] - 2.0)^2 + (x[3] - 1.0)^2 + (x[4] - 4.0)^2 + (x[5] - 1.0)^2)
f2 = x -> x[1]^2 + x[2]^2 + x[3]^2 + x[4]^2 + x[5]^2 + x[6]^2

addObjective(osyczka2, f1)
addObjective(osyczka2, f2)

# Constraints
c1 = x -> (x[1] + x[2]) / 2.0 - 1.0
c2 = x -> (6.0 - x[1] - x[2]) / 6.0
c3 = x -> (2.0 - x[2] + x[1]) / 2.0
c4 = x -> (2.0 - x[1] + 3.0 * x[2]) / 2.0
c5 = x -> (4.0 - (x[3] - 3.0)^2 - x[4]) / 4.0
c6 = x -> ((x[5] - 3.0)^2 + x[6] - 4.0) / 4.0

addConstraint(osyczka2, c1)
addConstraint(osyczka2, c2)
addConstraint(osyczka2, c3)
addConstraint(osyczka2, c4)
addConstraint(osyczka2, c5)
addConstraint(osyczka2, c6)

return osyczka2
end

function golinskiProblem()
golinski = ContinuousProblem{Float64}("Golinski")

# Setting the variable bounds
bounds = [(2.6, 3.6), (0.7, 0.8), (17.0, 28.0), (7.3, 8.3), (7.3, 8.3), (2.9, 3.9), (5.0, 5.5)]
for (lower, upper) in bounds
addVariable(golinski, Bounds{Float64}(lower, upper))
end

# Objective functions
f1 = x -> 0.7854 * x[1] * x[2]^2 * ((10 * x[3]^2) / 3.0 + 14.933 * x[3] - 43.0934) -
1.508 * x[1] * (x[6]^2 + x[7]^2) +
7.477 * (x[6]^3 + x[7]^3) +
0.7854 * (x[4] * x[6]^2 + x[5] * x[7]^2)

aux = x -> 745.0 * x[4] / (x[2] * x[3])
f2 = x -> sqrt((aux(x))^2 + 1.69e7) / (0.1 * x[6]^3)

addObjective(golinski, f1)
addObjective(golinski, f2)

# Constraints
c = Vector{Function}(undef, 11)

c[1] = x -> -((1.0 / (x[1] * x[2]^2 * x[3])) - (1.0 / 27.0))
c[2] = x -> -((1.0 / (x[1] * x[2]^2 * x[3]^2)) - (1.0 / 397.5))
c[3] = x -> -((x[4]^4) / (x[2] * x[3]^2 * x[6]^6) - (1.0 / 1.93))
c[4] = x -> -((x[5]^4) / (x[2] * x[3] * x[7]^6) - (1.0 / 1.93))
c[5] = x -> -(x[2] * x[3] - 40.0)
c[6] = x -> -((x[1] / x[2]) - 12.0)
c[7] = x -> -(5.0 - (x[1] / x[2]))
c[8] = x -> -(1.9 - x[4] + 1.5 * x[6])
c[9] = x -> -(1.9 - x[5] + 1.1 * x[7])
c[10] = x -> -(f2(x) - 1300)
c[11] = x -> begin
a = 745.0 * x[5] / (x[2] * x[3])
b = 1.575e8
-(sqrt(a * a + b) / (0.1 * x[7]^3) - 1100.0)
end

for constraint in c
addConstraint(golinski, constraint)
end

return golinski
end
103 changes: 46 additions & 57 deletions src/continuousProblem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,17 @@ function zdt4Problem(numberOfVariables::Int=10)
end

function evalG(x::Vector{Float64})
g = 1.0 +10.0 * (length(x) - 1)+ sum([(^(x[i],2.0) -10.0 * cos(4.0*pi*x[i])) for i in range(2,length(x))])
g = 0.0

return g
for i in 2:length(x)
g += x[i]^2 - 10.0 * cos(4.0 * π * x[i])
end

#g = 1.0 +10.0 * (length(x) - 1)+ sum([(^(x[i],2.0) -10.0 * cos(4.0*pi*x[i])) for i in range(2,length(x))])

constant = 1.0 + 10.0 * (length(x) - 1)

return g + constant
end

function evalH(v::Real, g::Float64)
Expand Down Expand Up @@ -446,68 +454,49 @@ function evaluate(solution::ContinuousSolution{Float64}, problem::DTLZ1)::Contin
return solution
end

struct DTLZ2 <: AbstractContinuousProblem{Float64}
bounds::Vector{Bounds{Float64}}
numberOfObjectives::Int
name::String
end

function dtlz2Problem(numberOfVariables::Int=12, numberOfObjectives::Int=3)
bounds = [Bounds{Float64}(0.0, 1.0) for _ in range(1, numberOfVariables)]

return DTLZ1(bounds,numberOfObjectives,"DTLZ2")
end

function numberOfVariables(problem::DTLZ2)
return length(problem.bounds)
end

function numberOfObjectives(problem::DTLZ2)
return problem.numberOfObjectives
end

function numberOfConstraints(problem::DTLZ2)
return 0
end

function evaluate(solution::ContinuousSolution{Float64}, problem::DTLZ2)::ContinuousSolution{Float64}
x = solution.variables
@assert length(x) == numberOfVariables(problem) "The number of variables of the solution to be evaluated is not correct"
function UF1Problem(numberOfVariables::Int = 30)
uf1 = ContinuousProblem{Float64}("UF1")

k = numberOfVariables(problem) - numberOfObjectives(problem) + 1

g = sum([^(x[i]-0.5,2) for i in range((numberOfVariables(problem)-k+1),numberOfVariables(problem))])

f = [(1.0 + g) for _ in 1:numberOfObjectives(problem)]

#println("G: ", g)
#println("F: ", f)
# Setting the variable bounds
addVariable(uf1, Bounds{Float64}(0.0, 1.0)) # For the first variable
for _ in 2:numberOfVariables
addVariable(uf1, Bounds{Float64}(-1.0, 1.0))
end

for i in 1:numberOfObjectives(problem)
for j in 1:(numberOfObjectives(problem) - i)
f[i] *= cos(x[j] * 0.5 * pi)
#println("F[",i,"] = ", f[i])
# Objective functions
f1 = x -> begin
sum1 = 0.0
count1 = 0
for j in 2:numberOfVariables
yj = x[j] - sin(6.0 * π * x[1] + j * π / numberOfVariables)
yj *= yj
if j % 2 != 0
sum1 += yj
count1 += 1
end
end
#println("----")
if i != 1
aux = numberOfObjectives(problem) - i + 1
#println("AUX: ", aux)
f[i] *= sin(x[aux]*0.5*pi)
#println("F[",i,"] = ", f[i])
return x[1] + 2.0 * sum1 / count1
end

f2 = x -> begin
sum2 = 0.0
count2 = 0
for j in 2:numberOfVariables
yj = x[j] - sin(6.0 * π * x[1] + j * π / numberOfVariables)
yj *= yj
if j % 2 == 0
sum2 += yj
count2 += 1
end
end
return 1.0 - sqrt(x[1]) + 2.0 * sum2 / count2
end

solution.objectives = f
addObjective(uf1, f1)
addObjective(uf1, f2)

return solution
return uf1
end

"""
problem = dtlz1Problem()
solution = createSolution(problem)
solution.variables = [0.5 for i in 1:numberOfVariables(problem)]

println(solution)
evaluate(solution, problem)
println(solution)
"""

0 comments on commit 66a80b9

Please sign in to comment.