Skip to content

Commit

Permalink
Refactor examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnebro committed May 17, 2024
1 parent c9487a3 commit 9c794cf
Show file tree
Hide file tree
Showing 14 changed files with 345 additions and 316 deletions.
62 changes: 32 additions & 30 deletions examples/NSGAIIAsAnEvolutionaryAlgorithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,52 @@ using metajul
using Dates

# NSGA-II algorithm configured from the evolutionary algorithm template
problem = ZDT1()
function main()
problem = ZDT1()

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

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

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

solver.evaluation = SequentialEvaluation(solver.problem)
solver.evaluation = SequentialEvaluation(solver.problem)

solver.termination = TerminationByEvaluations(25000)
solver.termination = TerminationByEvaluations(25000)

mutation = PolynomialMutation(1.0 / numberOfVariables(problem), 20.0, problem.bounds)
mutation = PolynomialMutation(1.0 / numberOfVariables(problem), 20.0, problem.bounds)

"""
mutation = UniformMutation(1.0/numberOfVariables(problem), 20.0, problem.bounds)
"""
mutation = UniformMutation(1.0/numberOfVariables(problem), 20.0, problem.bounds)
crossover = BLXAlphaCrossover(1.0, 0.5, problem.bounds)
"""
crossover = SBXCrossover(0.9, 20.0, problem.bounds)
crossover = BLXAlphaCrossover(1.0, 0.5, problem.bounds)
"""
crossover = SBXCrossover(0.9, 20.0, problem.bounds)

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

solver.selection = BinaryTournamentSelection(solver.variation.matingPoolSize, DefaultDominanceComparator())
solver.selection = BinaryTournamentSelection(solver.variation.matingPoolSize, DefaultDominanceComparator())

solver.replacement = RankingAndDensityEstimatorReplacement(DominanceRanking{ContinuousSolution{Float64}}(DefaultDominanceComparator()), CrowdingDistanceDensityEstimator())
solver.replacement = RankingAndDensityEstimatorReplacement(DominanceRanking{ContinuousSolution{Float64}}(DefaultDominanceComparator()), CrowdingDistanceDensityEstimator())

startingTime = Dates.now()
optimize(solver)
endTime = Dates.now()
startingTime = Dates.now()
optimize(solver)
endTime = Dates.now()

foundSolutions = solver.foundSolutions
foundSolutions = solver.foundSolutions

objectivesFileName = "FUN.csv"
variablesFileName = "VAR.csv"
objectivesFileName = "FUN.csv"
variablesFileName = "VAR.csv"

println("Algorithm: ", name(solver))
println("Algorithm: ", name(solver))

println("Objectives stored in file ", objectivesFileName)
printObjectivesToCSVFile(objectivesFileName, foundSolutions)
println("Objectives stored in file ", objectivesFileName)
printObjectivesToCSVFile(objectivesFileName, foundSolutions)

println("Variables stored in file ", variablesFileName)
printVariablesToCSVFile(variablesFileName, foundSolutions)
println("Computing time: ", (endTime - startingTime))
println("Variables stored in file ", variablesFileName)
printVariablesToCSVFile(variablesFileName, foundSolutions)
println("Computing time: ", (endTime - startingTime))
end
41 changes: 21 additions & 20 deletions examples/NSGAIISolvingABinaryProblem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,33 @@ using metajul
using Dates

# NSGA-II algorithm example configured from the NSGA-II template
function main()
problem = oneZeroMax(512)

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

solver::NSGAII = NSGAII()
solver.problem = problem
solver.populationSize = 100
solver.termination = TerminationByEvaluations(25000)

solver.termination = TerminationByEvaluations(25000)
solver.mutation = BitFlipMutation(1.0 / numberOfVariables(problem))
solver.crossover = SinglePointCrossover(1.0)

solver.mutation = BitFlipMutation(1.0/numberOfVariables(problem))
solver.crossover = SinglePointCrossover(1.0)
startingTime = Dates.now()
optimize(solver)
endTime = Dates.now()

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

foundSolutions = solver.foundSolutions
objectivesFileName = "FUN.csv"
variablesFileName = "VAR.csv"

objectivesFileName = "FUN.csv"
variablesFileName = "VAR.csv"
println("Algorithm: ", name(solver))

println("Algorithm: ", name(solver))
println("Objectives stored in file ", objectivesFileName)
printObjectivesToCSVFile(objectivesFileName, foundSolutions)

println("Objectives stored in file ", objectivesFileName)
printObjectivesToCSVFile(objectivesFileName, foundSolutions)

println("Variables stored in file ", variablesFileName)
printVariablesToCSVFile(variablesFileName, foundSolutions)
println("Computing time: ", (endTime - startingTime))
println("Variables stored in file ", variablesFileName)
printVariablesToCSVFile(variablesFileName, foundSolutions)
println("Computing time: ", (endTime - startingTime))
end
43 changes: 23 additions & 20 deletions examples/NSGAIISolvingAConstrainedProblem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,36 @@ using Dates

# NSGA-II algorithm example configured from the NSGA-II template

problem = srinivas()
function main()

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

solver.termination = TerminationByEvaluations(25000)
solver::NSGAII = NSGAII()
solver.problem = problem
solver.populationSize = 100

solver.mutation = PolynomialMutation(1.0/numberOfVariables(problem), 20.0, problem.bounds)
solver.crossover = SBXCrossover(1.0, 20.0, problem.bounds)
solver.termination = TerminationByEvaluations(25000)

solver.dominanceComparator = ConstraintsAndDominanceComparator()
solver.mutation = PolynomialMutation(1.0 / numberOfVariables(problem), 20.0, problem.bounds)
solver.crossover = SBXCrossover(1.0, 20.0, problem.bounds)

startingTime = Dates.now()
optimize(solver)
endTime = Dates.now()
solver.dominanceComparator = ConstraintsAndDominanceComparator()

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

objectivesFileName = "FUN.csv"
variablesFileName = "VAR.csv"
foundSolutions = solver.foundSolutions

println("Algorithm: ", name(solver))
objectivesFileName = "FUN.csv"
variablesFileName = "VAR.csv"

println("Objectives stored in file ", objectivesFileName)
printObjectivesToCSVFile(objectivesFileName, foundSolutions)
println("Algorithm: ", name(solver))

println("Variables stored in file ", variablesFileName)
printVariablesToCSVFile(variablesFileName, foundSolutions)
println("Computing time: ", (endTime - startingTime))
println("Objectives stored in file ", objectivesFileName)
printObjectivesToCSVFile(objectivesFileName, foundSolutions)

println("Variables stored in file ", variablesFileName)
printVariablesToCSVFile(variablesFileName, foundSolutions)
println("Computing time: ", (endTime - startingTime))
end
59 changes: 31 additions & 28 deletions examples/NSGAIIWithExternalCrowdingDistanceArchive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,50 @@ 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 = ZDT1()
function main()

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

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

externalArchive = CrowdingDistanceArchive(solver.populationSize, ContinuousSolution{Float64})
solver.solutionsCreation = DefaultSolutionsCreation(solver.problem, solver.populationSize)

solver.evaluation = SequentialEvaluationWithArchive(solver.problem, externalArchive)
externalArchive = CrowdingDistanceArchive(solver.populationSize, ContinuousSolution{Float64})

solver.termination = TerminationByEvaluations(25000)
solver.evaluation = SequentialEvaluationWithArchive(solver.problem, externalArchive)

mutation = PolynomialMutation(1.0 / numberOfVariables(problem), 20.0, problem.bounds)
solver.termination = TerminationByEvaluations(25000)

crossover = SBXCrossover(0.9, 20.0, problem.bounds)
mutation = PolynomialMutation(1.0 / numberOfVariables(problem), 20.0, problem.bounds)

solver.variation = CrossoverAndMutationVariation(solver.offspringPopulationSize, crossover, mutation)
crossover = SBXCrossover(0.9, 20.0, problem.bounds)

solver.selection = BinaryTournamentSelection(solver.variation.matingPoolSize, DefaultDominanceComparator())
solver.variation = CrossoverAndMutationVariation(solver.offspringPopulationSize, crossover, mutation)

solver.replacement = RankingAndDensityEstimatorReplacement(DominanceRanking(DefaultDominanceComparator()), CrowdingDistanceDensityEstimator())
solver.selection = BinaryTournamentSelection(solver.variation.matingPoolSize, DefaultDominanceComparator())

startingTime = Dates.now()
optimize(solver)
foundSolutions = solver.foundSolutions
endTime = Dates.now()
solver.replacement = RankingAndDensityEstimatorReplacement(DominanceRanking(DefaultDominanceComparator()), CrowdingDistanceDensityEstimator())

foundSolutions = getSolutions(externalArchive)
startingTime = Dates.now()
optimize(solver)
foundSolutions = solver.foundSolutions
endTime = Dates.now()

objectivesFileName = "FUN.csv"
variablesFileName = "VAR.csv"
foundSolutions = getSolutions(externalArchive)

println("Algorithm: ", name(solver))
objectivesFileName = "FUN.csv"
variablesFileName = "VAR.csv"

println("Objectives stored in file ", objectivesFileName)
printObjectivesToCSVFile(objectivesFileName, foundSolutions)
println("Algorithm: ", name(solver))

println("Variavbles stored in file ", variablesFileName)
printVariablesToCSVFile(variablesFileName, foundSolutions)
println("Computing time: ", (endTime - startingTime))
println("Objectives stored in file ", objectivesFileName)
printObjectivesToCSVFile(objectivesFileName, foundSolutions)

println("Variavbles stored in file ", variablesFileName)
printVariablesToCSVFile(variablesFileName, foundSolutions)
println("Computing time: ", (endTime - startingTime))
end
58 changes: 30 additions & 28 deletions examples/NSGAIIWithExternalUnboundedArchive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,48 @@ 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 = ZDT1()
function main()
problem = ZDT1()

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

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

externalArchive = NonDominatedArchive(ContinuousSolution{Float64})
solver.evaluation = SequentialEvaluationWithArchive(solver.problem, externalArchive)
externalArchive = NonDominatedArchive(ContinuousSolution{Float64})
solver.evaluation = SequentialEvaluationWithArchive(solver.problem, externalArchive)

solver.termination = TerminationByEvaluations(25000)
solver.termination = TerminationByEvaluations(25000)

mutation = PolynomialMutation(1.0 / numberOfVariables(problem), 20.0, problem.bounds)
mutation = PolynomialMutation(1.0 / numberOfVariables(problem), 20.0, problem.bounds)

crossover = SBXCrossover(0.9, 20.0, problem.bounds)
crossover = SBXCrossover(0.9, 20.0, problem.bounds)

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

solver.selection = BinaryTournamentSelection(solver.variation.matingPoolSize, DefaultDominanceComparator())
solver.selection = BinaryTournamentSelection(solver.variation.matingPoolSize, DefaultDominanceComparator())

solver.replacement = RankingAndDensityEstimatorReplacement(DominanceRanking{ContinuousSolution{Float64}}(DefaultDominanceComparator()), CrowdingDistanceDensityEstimator())
solver.replacement = RankingAndDensityEstimatorReplacement(DominanceRanking{ContinuousSolution{Float64}}(DefaultDominanceComparator()), CrowdingDistanceDensityEstimator())

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

foundSolutions = getSolutions(externalArchive)
foundSolutions = getSolutions(externalArchive)

objectivesFileName = "FUN.csv"
variablesFileName = "VAR.csv"
objectivesFileName = "FUN.csv"
variablesFileName = "VAR.csv"

println("Algorithm: ", name(solver))
println("Algorithm: ", name(solver))

println("Objectives stored in file ", objectivesFileName)
printObjectivesToCSVFile(objectivesFileName, foundSolutions)
println("Objectives stored in file ", objectivesFileName)
printObjectivesToCSVFile(objectivesFileName, foundSolutions)

println("Variavbles stored in file ", variablesFileName)
printVariablesToCSVFile(variablesFileName, foundSolutions)
println("Computing time: ", (endTime - startingTime))
println("Variavbles stored in file ", variablesFileName)
printVariablesToCSVFile(variablesFileName, foundSolutions)
println("Computing time: ", (endTime - startingTime))
end
Loading

0 comments on commit 9c794cf

Please sign in to comment.