From 9c794cf1ace67242dbc7b947ea0fd23cbab095d5 Mon Sep 17 00:00:00 2001 From: ajnebro Date: Fri, 17 May 2024 19:15:49 +0200 Subject: [PATCH] Refactor examples --- examples/NSGAIIAsAnEvolutionaryAlgorithm.jl | 62 ++++++++++--------- examples/NSGAIISolvingABinaryProblem.jl | 41 ++++++------ examples/NSGAIISolvingAConstrainedProblem.jl | 43 +++++++------ ...GAIIWithExternalCrowdingDistanceArchive.jl | 59 +++++++++--------- .../NSGAIIWithExternalUnboundedArchive.jl | 58 ++++++++--------- examples/NSGAIIWithObserver.jl | 45 +++++++------- examples/NSGAIIWithStandardSettings.jl | 46 +++++++------- examples/localSearchForOneMaxProblem.jl | 34 +++++----- examples/localSearchWithPolynomialMutation.jl | 34 +++++----- examples/localSearchWithUniformMutation.jl | 28 +++++---- .../singleObjectiveGeneticAlgorithmOneMax.jl | 49 ++++++++------- .../singleObjectiveGeneticAlgorithmSphere.jl | 54 ++++++++-------- ...eticAlgorithmTerminatingByComputingTime.jl | 54 ++++++++-------- ...leObjectiveGeneticAlgorithmWithObserver.jl | 54 ++++++++-------- 14 files changed, 345 insertions(+), 316 deletions(-) diff --git a/examples/NSGAIIAsAnEvolutionaryAlgorithm.jl b/examples/NSGAIIAsAnEvolutionaryAlgorithm.jl index 0521786..63524ff 100644 --- a/examples/NSGAIIAsAnEvolutionaryAlgorithm.jl +++ b/examples/NSGAIIAsAnEvolutionaryAlgorithm.jl @@ -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 \ No newline at end of file diff --git a/examples/NSGAIISolvingABinaryProblem.jl b/examples/NSGAIISolvingABinaryProblem.jl index 252475c..a211929 100644 --- a/examples/NSGAIISolvingABinaryProblem.jl +++ b/examples/NSGAIISolvingABinaryProblem.jl @@ -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)) \ No newline at end of file + println("Variables stored in file ", variablesFileName) + printVariablesToCSVFile(variablesFileName, foundSolutions) + println("Computing time: ", (endTime - startingTime)) +end \ No newline at end of file diff --git a/examples/NSGAIISolvingAConstrainedProblem.jl b/examples/NSGAIISolvingAConstrainedProblem.jl index d2d1ef6..b56bf97 100644 --- a/examples/NSGAIISolvingAConstrainedProblem.jl +++ b/examples/NSGAIISolvingAConstrainedProblem.jl @@ -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)) \ No newline at end of file + println("Objectives stored in file ", objectivesFileName) + printObjectivesToCSVFile(objectivesFileName, foundSolutions) + + println("Variables stored in file ", variablesFileName) + printVariablesToCSVFile(variablesFileName, foundSolutions) + println("Computing time: ", (endTime - startingTime)) +end \ No newline at end of file diff --git a/examples/NSGAIIWithExternalCrowdingDistanceArchive.jl b/examples/NSGAIIWithExternalCrowdingDistanceArchive.jl index d0e2457..17d9e31 100644 --- a/examples/NSGAIIWithExternalCrowdingDistanceArchive.jl +++ b/examples/NSGAIIWithExternalCrowdingDistanceArchive.jl @@ -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 \ No newline at end of file diff --git a/examples/NSGAIIWithExternalUnboundedArchive.jl b/examples/NSGAIIWithExternalUnboundedArchive.jl index 60a2320..5312e33 100644 --- a/examples/NSGAIIWithExternalUnboundedArchive.jl +++ b/examples/NSGAIIWithExternalUnboundedArchive.jl @@ -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 \ No newline at end of file diff --git a/examples/NSGAIIWithObserver.jl b/examples/NSGAIIWithObserver.jl index bd3986d..99eb638 100644 --- a/examples/NSGAIIWithObserver.jl +++ b/examples/NSGAIIWithObserver.jl @@ -2,35 +2,36 @@ using metajul using Dates # NSGA-II algorithm example configured from the NSGA-II template +function main() + problem = ZDT1() -problem = ZDT1() + 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 = PolynomialMutation(1.0 / numberOfVariables(problem), 20.0, problem.bounds) + solver.crossover = SBXCrossover(1.0, 20.0, problem.bounds) -solver.mutation = PolynomialMutation(1.0/numberOfVariables(problem),20.0, problem.bounds) -solver.crossover = SBXCrossover(1.0, 20.0, problem.bounds) + observer = EvaluationObserver(500) + register!(getObservable(solver), observer) -observer = EvaluationObserver(500) -register!(getObservable(solver), observer) + 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 \ No newline at end of file diff --git a/examples/NSGAIIWithStandardSettings.jl b/examples/NSGAIIWithStandardSettings.jl index dc6c0fc..a871ba7 100644 --- a/examples/NSGAIIWithStandardSettings.jl +++ b/examples/NSGAIIWithStandardSettings.jl @@ -4,35 +4,37 @@ using Dates # NSGA-II algorithm example configured from the NSGA-II template -problem = ZDT1() +function main() + problem = ZDT1() -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 = PolynomialMutation(1.0/numberOfVariables(problem),20.0, problem.bounds) -solver.crossover = SBXCrossover(1.0, 20.0, problem.bounds) + solver.mutation = PolynomialMutation(1.0 / numberOfVariables(problem), 20.0, problem.bounds) + solver.crossover = SBXCrossover(1.0, 20.0, problem.bounds) -""" -solver.crossover = BLXAlphaCrossover(1.0, 0.5, problem.bounds)) -""" + """ + solver.crossover = BLXAlphaCrossover(1.0, 0.5, problem.bounds)) + """ -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)) \ No newline at end of file + println("Variables stored in file ", variablesFileName) + printVariablesToCSVFile(variablesFileName, foundSolutions) + println("Computing time: ", (endTime - startingTime)) +end \ No newline at end of file diff --git a/examples/localSearchForOneMaxProblem.jl b/examples/localSearchForOneMaxProblem.jl index 3aa7dc1..b17bffa 100644 --- a/examples/localSearchForOneMaxProblem.jl +++ b/examples/localSearchForOneMaxProblem.jl @@ -3,23 +3,25 @@ using metajul using Dates # Local search example -problem = oneMax(512) -solution::Solution = createSolution(problem) -solution = evaluate(solution, problem) +function main() + problem = oneMax(512) + solution::Solution = createSolution(problem) + solution = evaluate(solution, problem) -solver::LocalSearch = LocalSearch() -solver.startingSolution = solution -solver.problem = problem -solver.numberOfIterations = 20000 -solver.mutation = BitFlipMutation(1.0/numberOfVariables(problem)) + solver::LocalSearch = LocalSearch() + solver.startingSolution = solution + solver.problem = problem + solver.numberOfIterations = 20000 + solver.mutation = BitFlipMutation(1.0 / numberOfVariables(problem)) -startingTime = Dates.now() -optimize(solver) -endTime = Dates.now() + startingTime = Dates.now() + optimize(solver) + endTime = Dates.now() -foundSolution = solver.foundSolution + foundSolution = solver.foundSolution -println("Local search result: ", foundSolution) -println("Fitness of the starting solution: ", solution.objectives[1]) -println("Fitness of the found solution: ", foundSolution.objectives[1]) -println("Computing time: ", (endTime - startingTime)) + println("Local search result: ", foundSolution) + println("Fitness of the starting solution: ", solution.objectives[1]) + println("Fitness of the found solution: ", foundSolution.objectives[1]) + println("Computing time: ", (endTime - startingTime)) +end diff --git a/examples/localSearchWithPolynomialMutation.jl b/examples/localSearchWithPolynomialMutation.jl index 429864d..c9a6f4c 100644 --- a/examples/localSearchWithPolynomialMutation.jl +++ b/examples/localSearchWithPolynomialMutation.jl @@ -2,23 +2,25 @@ using metajul using Dates # Local search example -problem = sphere(20) -solution::Solution = createSolution(problem) -solution = evaluate(solution, problem) +function main() + problem = sphere(20) + solution::Solution = createSolution(problem) + solution = evaluate(solution, problem) -solver::LocalSearch = LocalSearch() -solver.startingSolution = solution -solver.problem = problem -solver.numberOfIterations = 100000 -solver.mutation = PolynomialMutation(1.0/numberOfVariables(problem), 20.0, problem.bounds) + solver::LocalSearch = LocalSearch() + solver.startingSolution = solution + solver.problem = problem + solver.numberOfIterations = 100000 + solver.mutation = PolynomialMutation(1.0 / numberOfVariables(problem), 20.0, problem.bounds) -startingTime = Dates.now() -optimize(solver) -endTime = Dates.now() + startingTime = Dates.now() + optimize(solver) + endTime = Dates.now() -foundSolution = solver.foundSolution + foundSolution = solver.foundSolution -println("Local search result: ", foundSolution) -println("Fitness of the starting solution: ", solution.objectives[1]) -println("Fitness of the found solution: ", foundSolution.objectives[1]) -println("Computing time: ", (endTime - startingTime)) + println("Local search result: ", foundSolution) + println("Fitness of the starting solution: ", solution.objectives[1]) + println("Fitness of the found solution: ", foundSolution.objectives[1]) + println("Computing time: ", (endTime - startingTime)) +end \ No newline at end of file diff --git a/examples/localSearchWithUniformMutation.jl b/examples/localSearchWithUniformMutation.jl index ceded5b..05dc3c3 100644 --- a/examples/localSearchWithUniformMutation.jl +++ b/examples/localSearchWithUniformMutation.jl @@ -1,20 +1,22 @@ using metajul # Local search example -problem = sphere(10) -solution::Solution = createSolution(problem) -solution = evaluate(solution, problem) +function main() + problem = sphere(10) + solution::Solution = createSolution(problem) + solution = evaluate(solution, problem) -solver::LocalSearch = LocalSearch() -solver.startingSolution = solution -solver.problem = problem -solver.numberOfIterations = 10000 -solver.mutation = UniformMutation(0.1, 0.5, problem.bounds) + solver::LocalSearch = LocalSearch() + solver.startingSolution = solution + solver.problem = problem + solver.numberOfIterations = 10000 + solver.mutation = UniformMutation(0.1, 0.5, problem.bounds) -optimize(solver) + optimize(solver) -foundSolution = solver.foundSolution + foundSolution = solver.foundSolution -println("Local search result: ", foundSolution) -println("Fitness of the starting solution: ", solution.objectives[1]) -println("Fitness of the found solution: ", foundSolution.objectives[1]) \ No newline at end of file + println("Local search result: ", foundSolution) + println("Fitness of the starting solution: ", solution.objectives[1]) + println("Fitness of the found solution: ", foundSolution.objectives[1]) +end \ No newline at end of file diff --git a/examples/singleObjectiveGeneticAlgorithmOneMax.jl b/examples/singleObjectiveGeneticAlgorithmOneMax.jl index 61340a3..412940a 100644 --- a/examples/singleObjectiveGeneticAlgorithmOneMax.jl +++ b/examples/singleObjectiveGeneticAlgorithmOneMax.jl @@ -2,38 +2,41 @@ using metajul using Dates # Genetic algorithm example applied to problem OneMax -problem = oneMax(512) -solver::EvolutionaryAlgorithm = EvolutionaryAlgorithm() -solver.problem = problem -solver.populationSize = 100 -solver.offspringPopulationSize = 100 +function main() + problem = oneMax(512) -solver.solutionsCreation = DefaultSolutionsCreation(solver.problem, solver.populationSize) + solver::EvolutionaryAlgorithm = EvolutionaryAlgorithm() + solver.problem = problem + solver.populationSize = 100 + solver.offspringPopulationSize = 100 -solver.evaluation = SequentialEvaluation(solver.problem) + solver.solutionsCreation = DefaultSolutionsCreation(solver.problem, solver.populationSize) -solver.termination = TerminationByEvaluations(40000) + solver.evaluation = SequentialEvaluation(solver.problem) -mutation = BitFlipMutation(1.0/numberOfVariables(problem)) -crossover = SinglePointCrossover(1.0) -solver.variation = CrossoverAndMutationVariation(solver.offspringPopulationSize, crossover, mutation) + solver.termination = TerminationByEvaluations(40000) -solver.selection = BinaryTournamentSelection(solver.variation.matingPoolSize, compareIthObjective) + mutation = BitFlipMutation(1.0 / numberOfVariables(problem)) + crossover = SinglePointCrossover(1.0) + solver.variation = CrossoverAndMutationVariation(solver.offspringPopulationSize, crossover, mutation) -solver.replacement = MuPlusLambdaReplacement(compareIthObjective) + solver.selection = BinaryTournamentSelection(solver.variation.matingPoolSize, compareIthObjective) -startingTime = Dates.now() -optimize(solver) -endTime = Dates.now() + solver.replacement = MuPlusLambdaReplacement(compareIthObjective) -foundSolutions = solver.foundSolutions + startingTime = Dates.now() + optimize(solver) + endTime = Dates.now() -println("Algorithm: ", name(solver)) + foundSolutions = solver.foundSolutions -printObjectivesToCSVFile("FUN.csv", [foundSolutions[1]]) -printVariablesToCSVFile("VAR.csv", [foundSolutions[1]]) + println("Algorithm: ", name(solver)) -println("Fitness: ", -1.0 * foundSolutions[1].objectives[1]) -println("Solution: ", foundSolutions[1].variables) -println("Computing time: ", (endTime - startingTime)) + printObjectivesToCSVFile("FUN.csv", [foundSolutions[1]]) + printVariablesToCSVFile("VAR.csv", [foundSolutions[1]]) + + println("Fitness: ", -1.0 * foundSolutions[1].objectives[1]) + println("Solution: ", foundSolutions[1].variables) + println("Computing time: ", (endTime - startingTime)) +end \ No newline at end of file diff --git a/examples/singleObjectiveGeneticAlgorithmSphere.jl b/examples/singleObjectiveGeneticAlgorithmSphere.jl index e141758..697da8a 100644 --- a/examples/singleObjectiveGeneticAlgorithmSphere.jl +++ b/examples/singleObjectiveGeneticAlgorithmSphere.jl @@ -2,42 +2,44 @@ using metajul using Dates # Genetic algorithm example applied to problem Sphere -problem = sphere(100) +function main() + problem = sphere(100) -solver::EvolutionaryAlgorithm = EvolutionaryAlgorithm() -solver.name = "GA" + solver::EvolutionaryAlgorithm = EvolutionaryAlgorithm() + solver.name = "GA" -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(700000) + solver.termination = TerminationByEvaluations(700000) -mutation = PolynomialMutation(1.0/numberOfVariables(problem), 20.0, problem.bounds) -""" -solver.crossover = BLXAlphaCrossover((probability=1.0, alpha=0.5, bounds=problem.bounds)) -""" -crossover = SBXCrossover(1.0, 20.0, problem.bounds) -solver.variation = CrossoverAndMutationVariation(solver.offspringPopulationSize, crossover, mutation) + mutation = PolynomialMutation(1.0 / numberOfVariables(problem), 20.0, problem.bounds) + """ + solver.crossover = BLXAlphaCrossover((probability=1.0, alpha=0.5, bounds=problem.bounds)) + """ + crossover = SBXCrossover(1.0, 20.0, problem.bounds) + solver.variation = CrossoverAndMutationVariation(solver.offspringPopulationSize, crossover, mutation) -solver.selection = BinaryTournamentSelection(solver.variation.matingPoolSize, compareIthObjective) + solver.selection = BinaryTournamentSelection(solver.variation.matingPoolSize, compareIthObjective) -solver.replacement = MuPlusLambdaReplacement(compareIthObjective) + solver.replacement = MuPlusLambdaReplacement(compareIthObjective) -startingTime = Dates.now() -optimize(solver) -endTime = Dates.now() + startingTime = Dates.now() + optimize(solver) + endTime = Dates.now() -foundSolutions = solver.foundSolutions + foundSolutions = solver.foundSolutions -println("Algorithm: ", name(solver)) + println("Algorithm: ", name(solver)) -printObjectivesToCSVFile("FUN.csv", [foundSolutions[1]]) -printVariablesToCSVFile("VAR.csv", [foundSolutions[1]]) + printObjectivesToCSVFile("FUN.csv", [foundSolutions[1]]) + printVariablesToCSVFile("VAR.csv", [foundSolutions[1]]) -println("Best solution found: ", foundSolutions[1].objectives[1]) -println("Computing time: ", (endTime - startingTime)) + println("Best solution found: ", foundSolutions[1].objectives[1]) + println("Computing time: ", (endTime - startingTime)) +end \ No newline at end of file diff --git a/examples/singleObjectiveGeneticAlgorithmTerminatingByComputingTime.jl b/examples/singleObjectiveGeneticAlgorithmTerminatingByComputingTime.jl index 8917cd5..791ce1a 100644 --- a/examples/singleObjectiveGeneticAlgorithmTerminatingByComputingTime.jl +++ b/examples/singleObjectiveGeneticAlgorithmTerminatingByComputingTime.jl @@ -2,42 +2,44 @@ using metajul using Dates # Genetic algorithm example applied to problem Sphere -problem = sphere(100) +function main() + problem = sphere(100) -solver::EvolutionaryAlgorithm = EvolutionaryAlgorithm() -solver.name = "GA" + solver::EvolutionaryAlgorithm = EvolutionaryAlgorithm() + solver.name = "GA" -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 = TerminationByComputingTime(Dates.Millisecond(6000)) + solver.termination = TerminationByComputingTime(Dates.Millisecond(6000)) -mutation = PolynomialMutation(1.0/numberOfVariables(problem), 20.0, problem.bounds) -""" -solver.crossover = BLXAlphaCrossover((probability=1.0, alpha=0.5, bounds=problem.bounds)) -""" -crossover = SBXCrossover(1.0, 20.0, problem.bounds) -solver.variation = CrossoverAndMutationVariation(solver.offspringPopulationSize, crossover, mutation) + mutation = PolynomialMutation(1.0 / numberOfVariables(problem), 20.0, problem.bounds) + """ + solver.crossover = BLXAlphaCrossover((probability=1.0, alpha=0.5, bounds=problem.bounds)) + """ + crossover = SBXCrossover(1.0, 20.0, problem.bounds) + solver.variation = CrossoverAndMutationVariation(solver.offspringPopulationSize, crossover, mutation) -solver.selection = BinaryTournamentSelection(solver.variation.matingPoolSize, compareIthObjective) + solver.selection = BinaryTournamentSelection(solver.variation.matingPoolSize, compareIthObjective) -solver.replacement = MuPlusLambdaReplacement(compareIthObjective) + solver.replacement = MuPlusLambdaReplacement(compareIthObjective) -startingTime = Dates.now() -optimize(solver) -endTime = Dates.now() + startingTime = Dates.now() + optimize(solver) + endTime = Dates.now() -foundSolutions = solver.foundSolutions + foundSolutions = solver.foundSolutions -println("Algorithm: ", name(solver)) + println("Algorithm: ", name(solver)) -printObjectivesToCSVFile("FUN.csv", [foundSolutions[1]]) -printVariablesToCSVFile("VAR.csv", [foundSolutions[1]]) + printObjectivesToCSVFile("FUN.csv", [foundSolutions[1]]) + printVariablesToCSVFile("VAR.csv", [foundSolutions[1]]) -println("Best solution found: ", foundSolutions[1].objectives[1]) -println("Computing time: ", (endTime - startingTime)) + println("Best solution found: ", foundSolutions[1].objectives[1]) + println("Computing time: ", (endTime - startingTime)) +end \ No newline at end of file diff --git a/examples/singleObjectiveGeneticAlgorithmWithObserver.jl b/examples/singleObjectiveGeneticAlgorithmWithObserver.jl index f6d80db..06530e0 100644 --- a/examples/singleObjectiveGeneticAlgorithmWithObserver.jl +++ b/examples/singleObjectiveGeneticAlgorithmWithObserver.jl @@ -2,42 +2,44 @@ using metajul using Dates # Genetic algorithm example applied to problem OneMax -problem = oneMax(512) +function main() + problem = oneMax(512) -solver::EvolutionaryAlgorithm = EvolutionaryAlgorithm() -solver.problem = problem -solver.populationSize = 100 -solver.offspringPopulationSize = 100 + solver::EvolutionaryAlgorithm = EvolutionaryAlgorithm() + 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(40000) + solver.termination = TerminationByEvaluations(40000) -mutation = BitFlipMutation(1.0/numberOfVariables(problem)) -crossover = SinglePointCrossover(1.0) -solver.variation = CrossoverAndMutationVariation(solver.offspringPopulationSize, crossover, mutation) + mutation = BitFlipMutation(1.0 / numberOfVariables(problem)) + crossover = SinglePointCrossover(1.0) + solver.variation = CrossoverAndMutationVariation(solver.offspringPopulationSize, crossover, mutation) -solver.selection = BinaryTournamentSelection(solver.variation.matingPoolSize, compareIthObjective) + solver.selection = BinaryTournamentSelection(solver.variation.matingPoolSize, compareIthObjective) -solver.replacement = MuPlusLambdaReplacement(compareIthObjective) + solver.replacement = MuPlusLambdaReplacement(compareIthObjective) -#observer = EvaluationObserver(4000) -observer = FitnessObserver(500) -register!(getObservable(solver), observer) + #observer = EvaluationObserver(4000) + observer = FitnessObserver(500) + register!(getObservable(solver), observer) -startingTime = Dates.now() -optimize(solver) -endTime = Dates.now() + startingTime = Dates.now() + optimize(solver) + endTime = Dates.now() -foundSolutions = solver.foundSolutions + foundSolutions = solver.foundSolutions -println("Algorithm: ", name(solver)) + println("Algorithm: ", name(solver)) -printObjectivesToCSVFile("FUN.csv", [foundSolutions[1]]) -printVariablesToCSVFile("VAR.csv", [foundSolutions[1]]) + printObjectivesToCSVFile("FUN.csv", [foundSolutions[1]]) + printVariablesToCSVFile("VAR.csv", [foundSolutions[1]]) -println("Fitness: ", -1.0 * foundSolutions[1].objectives[1]) -println("Solution: ", foundSolutions[1].variables) -println("Computing time: ", (endTime - startingTime)) + println("Fitness: ", -1.0 * foundSolutions[1].objectives[1]) + println("Solution: ", foundSolutions[1].variables) + println("Computing time: ", (endTime - startingTime)) +end \ No newline at end of file