Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/EvoLP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ include("testfunctions.jl")

include("algorithms/ga.jl")
include("algorithms/ea.jl")
include("algorithms/sa.jl")
include("algorithms/swarm.jl")

include("deprecated.jl")
Expand All @@ -32,6 +33,7 @@ export Particle, normal_rand_particle_pop, unif_rand_particle_pop # Particles

# Algorithms
export GA, GA!
export SA
export oneplusone, oneplusone!
export PSO, PSO!

Expand Down
54 changes: 54 additions & 0 deletions src/algorithms/sa.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
SA(f, pop, k_max, S, C, M; T_init)

Simulated annealing.

## Arguments
- `f::Function`: objective function to **minimise**.
- `population::AbstractVector`: a list of vector individuals.
- `k_max::Integer`: number of iterations.
- `S::ParentSelector`: one of the available [`ParentSelector`](@ref).
- `C::CrossoverMethod`: one of the available [`CrossoverMethod`](@ref).
- `M::MutationMethod`: one of the available [`MutationMethod`](@ref).
- `T₀::Real=100.0`: initial temperature
- `α::Real=0.99`: cooling rate

Returns a [`Result`](@ref).
"""
function SA(
f::Function,
population::AbstractVector,
k_max::Integer,
S::ParentSelector,
C::Recombinator,
M::Mutator;
T₀::Real=100.0,
α::Real=0.99
)
fx = Inf
best_ind = copy(population)
T = T₀

runtime = @elapsed begin
for _ in 1:k_max
c = mutate(M, population)
fc = f(c)
ΔE = fc - fx # Calculate the difference in objective function values

# Acceptance probability function
if ΔE < 0 || rand() < exp(-ΔE / T)
population = copy(c)
fx = fc
end

fx < f(best_ind) && (best_ind = copy(population))

T *= α
end
end

n_evals = k_max

return Result(fx, best_ind, [best_ind], k_max, n_evals, runtime)
end