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

Optimizations #88

Merged
merged 2 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
4 changes: 2 additions & 2 deletions .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
push:
branches:
- main
tags: '*'
tags: "*"
pull_request:

jobs:
Expand All @@ -18,7 +18,7 @@ jobs:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@latest
with:
version: '1.7'
version: "1.7"
- name: Install dependencies
run: julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate()'
- name: Build and deploy
Expand Down
4 changes: 2 additions & 2 deletions src/crossover.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ Uniform crossover between parents `a` and `b`. Each gene
of the chromosome is randomly selected from one of the parents.
"""
function cross(::UniformRecombinator, a, b; rng=Random.GLOBAL_RNG)
child = copy(a)
child = similar(a)

for i in eachindex(a)
@inbounds child[i] = rand(rng) < 0.5 ? b[i] : continue
@inbounds child[i] = rand(rng) < 0.5 ? b[i] : a[i]
end

return child
Expand Down
8 changes: 4 additions & 4 deletions src/generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,16 @@ function unif_rand_particle_pop(n, lb, ub; rng=Random.GLOBAL_RNG)
end

"""
normal_rand_particle_pop(n, μ, Σ; rng=Random.GLOBAL_RNG)
normal_rand_particle_pop(n, μ, Σ; y=Inf, rng=Random.GLOBAL_RNG)

Generate a population of `n` [`Particle`](@ref) using a normal distribution with means `μ``
and covariance `Σ`.

`μ` expects a vector of length _l_ (i.e. number of dimensions) while `Σ` expects an _l x l_
matrix of covariances.

`y` is the evalutaion and current best value. `y` is set to `Inf` by default.

# Examples

```julia
Expand All @@ -164,10 +166,8 @@ julia> normal_rand_particle_pop(3, [0, 0], [1 0; 0 1])
Particle([0.5687241357408321, -0.7406267072113427], [0.0, 0.0], Inf, [0.5687241357408321, -0.7406267072113427], Inf)
```
"""
function normal_rand_particle_pop(n, μ, Σ; rng=Random.GLOBAL_RNG)
# TODO: Add y as optional parameter but default to Inf?
function normal_rand_particle_pop(n, μ, Σ; y=Inf, rng=Random.GLOBAL_RNG)
D = MvNormal(μ, Σ)
y = Inf
pop = Vector{Particle}(undef, n)

for i in 1:n
Expand Down
26 changes: 16 additions & 10 deletions src/logbook.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ function compute!(notebooks::Vector{Logbook}, data::AbstractVector)
record = namedtuple(fnames, callables)
push!(logger.records, record)
end

return nothing
end


Expand All @@ -87,14 +89,16 @@ function summarise(logger::Logbook)
data = [logger.records[j][i] for j in 1:n]
printstyled("\n $(eachstat) \n"; bold=true)
print("max: $(maximum(data)) \n" *
"avg: $(mean(data))\n" *
"median: $(median(data)) \n" *
"min: $(minimum(data))\n" *
"std: $(std(data))\n")
"avg: $(mean(data))\n" *
"median: $(median(data)) \n" *
"min: $(minimum(data))\n" *
"std: $(std(data))\n")
plt = lineplot(data;
xlabel="it", ylabel=eachstat)
xlabel="it", ylabel=eachstat)
print(plt)
end

return nothing
end

function summarise(notebooks::Vector{Logbook})
Expand All @@ -104,13 +108,15 @@ function summarise(notebooks::Vector{Logbook})
data = [eachnb.records[j][i] for j in 1:n]
printstyled("\n $(eachstat) \n"; bold=true)
print("max: $(maximum(data)) \n" *
"avg: $(mean(data))\n" *
"median: $(median(data)) \n" *
"min: $(minimum(data))\n" *
"std: $(std(data))\n")
"avg: $(mean(data))\n" *
"median: $(median(data)) \n" *
"min: $(minimum(data))\n" *
"std: $(std(data))\n")
plt = lineplot(data;
xlabel="it", ylabel=eachstat)
xlabel="it", ylabel=eachstat)
print(plt)
end
end

return nothing
end
2 changes: 1 addition & 1 deletion src/mutation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ end
Gaussian mutation with standard deviation `σ`, which should be a real number.
"""
struct GaussianMutator <: ContinuousMutator
σ
σ::Real
end

"""
Expand Down
2 changes: 1 addition & 1 deletion src/selection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract type ParentSelector <: Selector end
Tournament parent selection with tournament size `T`.
"""
struct TournamentSelector <: ParentSelector
T
T::Int
end

"""
Expand Down
26 changes: 13 additions & 13 deletions src/testfunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ f(x) = -a \\exp\\left(-b\\sqrt{\\frac{1}{d} \\sum_{i=1}^d x_i^2}\\right)
"""
@inline function ackley(x::Vector{T} where {T<:Real}; a=20, b=0.2, c=2π)
d = length(x)
return -a * exp(-b * sqrt(sum(x .^ 2) / d)) -
exp(sum(cos.(c * xi) for xi in x) / d) + a + exp(1)
return @fastmath -a * exp(-b * sqrt(sum(x .^ 2) / d)) -
exp(sum(cos.(c * xi) for xi in x) / d) + a + exp(1)
end

"""
Expand All @@ -87,7 +87,7 @@ f(x) = (x_1 + 2x_2 - 7)^2 + (2 x_1 + x_2 - 5)^2
```
"""
@inline function booth(x::Vector{T} where {T<:Real})
return (x[1] + 2 * x[2] - 7)^2 + (2 * x[1] + x[2] - 5)^2
return @fastmath (x[1] + 2 * x[2] - 7)^2 + (2 * x[1] + x[2] - 5)^2
end


Expand All @@ -105,8 +105,8 @@ f(x) = a(x_2 - bx_1^2 + cx_1 - r)^2 + s(1 - t)\\cos(x_1) + s
```
"""
@inline function branin(x::Vector{T} where {T<:Real};
a=1, b=5.1 / (4π^2), c=5 / π, r=6, s=10, t=1 / (8π))
return a * (x[2] - b * x[1]^2 + c * x[1] - r)^2 + s * (1 - t) * cos(x[1]) + s
a=1, b=5.1 / (4π^2), c=5 / π, r=6, s=10, t=1 / (8π))
return @fastmath a * (x[2] - b * x[1]^2 + c * x[1] - r)^2 + s * (1 - t) * cos(x[1]) + s
end


Expand All @@ -119,8 +119,8 @@ with optimiser ``\\mathbf{x}^* = (512, 404.231805)``.
"""
function eggholder(x::Vector{T} where {T<:Real})
n = length(x)
return -sum([(x[i+1]+47) * sin(sqrt(abs(x[i+1] + 47 + x[i]/2))) +
x[i] * sin(sqrt(abs(x[i] - (x[i+1] + 47)))) for i in 1:n-1])
return -sum([(x[i+1] + 47) * sin(sqrt(abs(x[i+1] + 47 + x[i] / 2))) +
x[i] * sin(sqrt(abs(x[i] - (x[i+1] + 47)))) for i in 1:n-1])
end


Expand All @@ -136,7 +136,7 @@ f(x) = -\\sum_{i=1}^{d}\\sin(x_i) \\sin^{2m}\\left(\\frac{ix_i^2}{\\pi}\\right)
```
"""
@inline function michalewicz(x::Vector{T} where {T<:Real}; m=10)
return -sum(sin(v) * sin(i * v^2 / π)^(2m) for (i, v) in enumerate(x))
return @fastmath -sum(sin(v) * sin(i * v^2 / π)^(2m) for (i, v) in enumerate(x))
end


Expand All @@ -149,9 +149,9 @@ optimiser ``\\mathbf{x}^* = `(-488.632577, 512)`.
"""
@inline function rana(x::Vector{T} where {T<:Real})
n = length(x)
return sum([x[i] * cos(sqrt(abs(x[i+1] + x[i] + 1))) * sin(sqrt(abs(x[i+1] - x[i] + 1))) +
(1 + x[i+1]) * sin(sqrt(abs(x[i+1] + x[i] + 1))) * cos(sqrt(abs(x[i+1] - x[i] + 1)))
for i in 1:n-1])
return @fastmath sum([x[i] * cos(sqrt(abs(x[i+1] + x[i] + 1))) * sin(sqrt(abs(x[i+1] - x[i] + 1))) +
(1 + x[i+1]) * sin(sqrt(abs(x[i+1] + x[i] + 1))) * cos(sqrt(abs(x[i+1] - x[i] + 1)))
for i in 1:n-1])
end


Expand All @@ -173,7 +173,7 @@ f(x) = \\sum_{i=1}^{d-1} \\left[b(x_{i+1} - x_i^2)^2 + (x_i - 1)^2 \\right]
"""
@inline function rosenbrock(x::Vector{T} where {T<:Real}; b=100)
n = length(x)
return sum([b * (x[i+1] - x[i]^2)^2 + (x[i] - 1)^2 for i in 1:n-1])
return @fastmath sum([b * (x[i+1] - x[i]^2)^2 + (x[i] - 1)^2 for i in 1:n-1])
end


Expand All @@ -188,5 +188,5 @@ f(x) = - \\exp(- (x_1 x_2 - a)^2 - (x_2 - a)^2 )
```
"""
@inline function wheeler(x::Vector{T} where {T<:Real}; a=1.5)
return -exp(-(x[1] * x[2] - a)^2 - (x[2] - a)^2)
return @fastmath -exp(-(x[1] * x[2] - a)^2 - (x[2] - a)^2)
end
Loading