Skip to content

Commit

Permalink
Refactor local search
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnebro committed May 8, 2024
1 parent 1d40ddf commit 383fd5d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
35 changes: 0 additions & 35 deletions src/algorithm.jl → src/algorithm/algorithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,6 @@ using Dates

###################################

mutable struct LocalSearch <: Algorithm
startingSolution::Solution
problem::Problem
numberOfIterations::Int
mutation::Function
mutationParameters::NamedTuple
foundSolution::Solution

LocalSearch() = new()
end

function optimize(algorithm :: LocalSearch)
algorithm.foundSolution = localSearch(algorithm.startingSolution, algorithm.problem,
algorithm.numberOfIterations, algorithm.mutation, algorithm.mutationParameters)

return Nothing
end

function localSearch(currentSolution::Solution, problem::Problem, numberOfIterations::Int, mutationOperator::Function, mutationParameters)::Solution
for i in 1:numberOfIterations
mutatedSolution = copySolution(currentSolution)
mutatedSolution.variables = mutationOperator(mutatedSolution.variables, mutationParameters)

mutatedSolution = evaluate(mutatedSolution, problem)

if (mutatedSolution.objectives[1] < currentSolution.objectives[1])
currentSolution = mutatedSolution
end
end

return currentSolution
end

#################################

mutable struct EvolutionaryAlgorithm <: Algorithm
name::String
problem::Problem
Expand Down
29 changes: 29 additions & 0 deletions src/algorithm/localSearch.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
mutable struct LocalSearch <: Algorithm
startingSolution::Solution
problem::Problem
numberOfIterations::Int
mutation::MutationOperator
foundSolution::Solution
end

function optimize(algorithm::LocalSearch)::Solution
currentSolution = algorithm.startingSolution
problem = algorithm.problem
numberOfIterations = algorithm.numberOfIterations
mutation = algorithm.mutation

for _ in 1:numberOfIterations
mutatedSolution = copySolution(algorithm.startingSolution)
mutatedSolution.variables = mutate(mutatedSolution.variables, mutation)

mutatedSolution = evaluate(mutatedSolution, problem)

if (mutatedSolution.objectives[1] < currentSolution.objectives[1])
currentSolution = mutatedSolution
end
end

algorithm.foundSolution = foundSolution

return currentSolution
end

0 comments on commit 383fd5d

Please sign in to comment.