Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update algorithms folder #90

Merged
merged 3 commits into from
May 20, 2024
Merged
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
8 changes: 4 additions & 4 deletions src/algorithms/ea.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Returns a [`Result`](@ref).
"""
function oneplusone(f::Function, ind::AbstractVector, k_max::Integer, M::Mutator)
fx = Inf # works only on minimisation problems
runtime = @elapsed @inbounds for _ in 1:k_max
runtime = @elapsed for _ in 1:k_max
c = mutate(M, ind)
fx, fc = f(ind), f(c)
if fc <= fx
Expand All @@ -24,7 +24,7 @@ function oneplusone(f::Function, ind::AbstractVector, k_max::Integer, M::Mutator
end
end

n_evals = 2 * k_max
n_evals = 2k_max

return Result(fx, ind, [ind], k_max, n_evals, runtime)
end
Expand All @@ -34,7 +34,7 @@ function oneplusone!(
logger::Logbook, f::Function, ind::AbstractVector, k_max::Integer, M::Mutator
)
fx = Inf # works only on minimisation problems
runtime = @elapsed @inbounds for _ in 1:k_max
runtime = @elapsed for _ in 1:k_max
c = mutate(M, ind)
fx, fc = f(ind), f(c)
if fc <= fx # O(2 * k_max) # minimisation problem
Expand All @@ -45,7 +45,7 @@ function oneplusone!(
compute!(logger, [fx])
end

n_evals = 2 * k_max
n_evals = 2k_max

return Result(fx, ind, [ind], k_max, n_evals, runtime)
end
6 changes: 3 additions & 3 deletions src/algorithms/ga.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function GA(

fitnesses = Vector{Float64}(undef, n)

runtime = @elapsed @inbounds for _ in 1:k_max
runtime = @elapsed for _ in 1:k_max
fitnesses = f.(population) # O(k_max * n)
parents = [select(S, fitnesses) for _ in eachindex(population)]
offspring = [cross(C, population[p[1]], population[p[2]]) for p in parents]
Expand Down Expand Up @@ -55,7 +55,7 @@ function GA!(

fitnesses = Vector{Float64}(undef, n)

runtime = @elapsed @inbounds for _ in 1:k_max
runtime = @elapsed for _ in 1:k_max
fitnesses = f.(population) # O(k_max * n)
parents = [select(S, fitnesses) for _ in eachindex(population)]
offspring = [cross(C, population[p[1]], population[p[2]]) for p in parents]
Expand Down Expand Up @@ -85,7 +85,7 @@ function GA!(

fitnesses = Vector{Float64}(undef, n)

runtime = @elapsed @inbounds for _ in 1:k_max
runtime = @elapsed for _ in 1:k_max
fitnesses = f.(population) # O(k_max * n)
parents = [select(S, fitnesses) for _ in eachindex(population)]
offspring = [cross(C, population[p[1]], population[p[2]]) for p in parents]
Expand Down
4 changes: 2 additions & 2 deletions src/algorithms/swarm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function PSO(
for P in population
r1, r2 = rand(d), rand(d)
P.x += P.v
P.v = w * P.v + c1 * r1 .* (P.x_best - P.x) + c2 * r2 .* (x_best - P.x)
P.v = @fastmath w * P.v + c1 * r1 .* (P.x_best - P.x) + c2 * r2 .* (x_best - P.x)
P.y = f(P.x) # O(k_max * pop)

if P.y < y_best
Expand Down Expand Up @@ -93,7 +93,7 @@ function PSO!(
end
end

@inbounds for _ in 1:k_max
for _ in 1:k_max
for P in population
r1, r2 = rand(d), rand(d)
P.x += P.v
Expand Down
Loading