From e4b9111681609d04e2fe6d6e9f544e5ebeeb5fca Mon Sep 17 00:00:00 2001 From: ajnebro Date: Tue, 30 Jan 2024 17:51:10 +0100 Subject: [PATCH] Add example --- ...GAIIExampleWithExternalUnboundedArchive.jl | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 examples/NSGAIIExampleWithExternalUnboundedArchive.jl diff --git a/examples/NSGAIIExampleWithExternalUnboundedArchive.jl b/examples/NSGAIIExampleWithExternalUnboundedArchive.jl new file mode 100644 index 0000000..91eb0db --- /dev/null +++ b/examples/NSGAIIExampleWithExternalUnboundedArchive.jl @@ -0,0 +1,56 @@ +include("../src/bounds.jl") +include("../src/solution.jl") +include("../src/operator.jl") +include("../src/continuousProblem.jl") +include("../src/algorithm.jl") +include("../src/component.jl") +include("../src/utils.jl") + +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() + +solver::EvolutionaryAlgorithm = EvolutionaryAlgorithm() +solver.name = "NSGA-II" +solver.problem = problem +solver.populationSize = 100 +solver.offspringPopulationSize = 100 + +solver.solutionsCreation = DefaultSolutionsCreation((problem = solver.problem, numberOfSolutionsToCreate = solver.populationSize)) + +externalArchive = NonDominatedArchive(ContinuousSolution{Float64}) +solver.evaluation = SequentialEvaluationWithArchive((archive = externalArchive, problem = solver.problem)) + +solver.termination = TerminationByEvaluations((numberOfEvaluationsToStop = 25000, )) + +mutation = PolynomialMutation((probability=1.0/numberOfVariables(problem), distributionIndex=20.0, bounds=problem.bounds)) +""" +crossover = BLXAlphaCrossover((probability=1.0, alpha=0.5, bounds=problem.bounds)) +""" +crossover = SBXCrossover((probability=1.0, distributionIndex=20.0, bounds=problem.bounds)) + +solver.variation = CrossoverAndMutationVariation((offspringPopulationSize = solver.offspringPopulationSize, crossover = crossover, mutation = mutation)) + +solver.selection = BinaryTournamentSelection((matingPoolSize = solver.variation.matingPoolSize, comparator = compareRankingAndCrowdingDistance)) + +solver.replacement = RankingAndDensityEstimatorReplacement((dominanceComparator = compareForDominance, )) + +startingTime = Dates.now() +optimize(solver) +endTime = Dates.now() + +foundSolutions = getSolutions(externalArchive) + +objectivesFileName = "FUN.csv" +variablesFileName = "VAR.csv" + +println("Algorithm: ", name(solver)) + +println("Objectives stored in file ", objectivesFileName) +printObjectivesToCSVFile(objectivesFileName, foundSolutions) + +println("Variavbles stored in file ", variablesFileName) +printVariablesToCSVFile(variablesFileName, foundSolutions) +println("Computing time: ", (endTime - startingTime))