Skip to content

Commit

Permalink
update to Julia 1.0 (second attempt) (#25)
Browse files Browse the repository at this point in the history
* update to julia 1.0
  • Loading branch information
Andrew Keenan authored and wildart committed Aug 28, 2018
1 parent d1465f1 commit 37e1716
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 72 deletions.
11 changes: 4 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
language: julia
os:
- linux
# - osx
julia:
- 0.6
- 1.0
- nightly
notifications:
email: false
email: false
matrix:
allow_failures:
- julia: nightly
script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- julia --check-bounds=yes -e 'Pkg.clone(pwd()); Pkg.test("Evolutionary"; coverage=true)'
sudo: false
after_success:
- julia -e 'cd(Pkg.dir("Evolutionary")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
- julia -e 'using Pkg; Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
6 changes: 3 additions & 3 deletions src/Evolutionary.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Evolutionary

using Random
export Strategy, strategy, inverse, mutationwrapper,
# ES mutations
isotropic, anisotropic, isotropicSigma, anisotropicSigma,
Expand All @@ -17,7 +17,7 @@ module Evolutionary
es, cmaes, ga

const Strategy = Dict{Symbol,Any}
const Individual = Union{Vector, Matrix, Function, Void}
const Individual = Union{Vector, Matrix, Function, Nothing}

# Wrapping function for strategy
function strategy(; kwargs...)
Expand All @@ -30,7 +30,7 @@ module Evolutionary

# Inverse function for reversing optimization direction
function inverseFunc(f::Function)
function fitnessFunc{T <: Vector}(x::T)
function fitnessFunc(x::T) where {T <: Vector}
return 1.0/(f(x)+eps())
end
return fitnessFunc
Expand Down
17 changes: 10 additions & 7 deletions src/cmaes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# μ is the number of parents
# λ is the number of offspring.
#
using LinearAlgebra
using Statistics
function cmaes( objfun::Function, N::Int;
initPopulation::Individual = ones(N),
initStrategy::Strategy = strategy= sqrt(N), τ_c = N^2, τ_σ = sqrt(N)),
Expand All @@ -20,12 +22,12 @@ function cmaes( objfun::Function, N::Int;
# Initialize parent population
individual = getIndividual(initPopulation, N)
population = fill(individual, μ)
offspring = Array{typeof(individual)}(λ)
offspring = Array{typeof(individual)}(undef, λ)
fitpop = fill(Inf, μ)
fitoff = fill(Inf, λ)

parent = copy(individual)
C = eye(N)
C = Diagonal{Float64}(I, N)
s = zeros(N)
s_σ = zeros(N)
σ = 1.0
Expand All @@ -35,10 +37,11 @@ function cmaes( objfun::Function, N::Int;
# Generation cycle
count = 0
while true
SqrtC = (C + C')/2.0
SqrtC = (C + C') / 2.0
try
SqrtC = chol(SqrtC)
SqrtC = cholesky(SqrtC).U
catch
println("Break on Cholesky")
break
end

Expand All @@ -57,8 +60,8 @@ function cmaes( objfun::Function, N::Int;
fitpop[i] = fitoff[idx[i]]
end

w = vec(mean(W[:,idx], 2))
ɛ = vec(mean(E[:,idx], 2))
w = vec(mean(W[:,idx], dims=2))
ɛ = vec(mean(E[:,idx], dims=2))
parent += w # forming recombinant perent for next generation (L2)
s = (1.0 - 1.0/initStrategy[])*s +
(sqrt/initStrategy[] * (2.0 - 1.0/initStrategy[]))/σ)*w # (L3)
Expand All @@ -76,4 +79,4 @@ function cmaes( objfun::Function, N::Int;
end

return population[1], fitpop[1], count
end
end
4 changes: 2 additions & 2 deletions src/es.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function es( objfun::Function, N::Int;
fitness[i] = objfun(population[i])
debug && println("INIT $(i): $(population[i]) : $(fitness[i])")
end
offspring = Array{typeof(individual)}(λ)
offspring = Array{typeof(individual)}(undef, λ)
fitoff = fill(Inf, λ)
stgpop = fill(initStrategy, μ)
stgoff = fill(initStrategy, λ)
Expand Down Expand Up @@ -112,4 +112,4 @@ function es( objfun::Function, N::Int;
end

return population[1], fitness[1], count, store
end
end
6 changes: 3 additions & 3 deletions src/ga.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#
function ga(objfun::Function, N::Int;
initPopulation::Individual = ones(N),
lowerBounds::Union{Void, Vector} = nothing,
upperBounds::Union{Void, Vector} = nothing,
lowerBounds::Union{Nothing, Vector} = nothing,
upperBounds::Union{Nothing, Vector} = nothing,
populationSize::Int = 50,
crossoverRate::Float64 = 0.8,
mutationRate::Float64 = 0.1,
Expand All @@ -39,7 +39,7 @@ function ga(objfun::Function, N::Int;
# Initialize population
individual = getIndividual(initPopulation, N)
fitness = zeros(populationSize)
population = Array{typeof(individual)}(populationSize)
population = Array{typeof(individual)}(undef, populationSize)
offspring = similar(population)

# Generate population
Expand Down
26 changes: 13 additions & 13 deletions src/mutations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# ==================

# Isotropic mutation operator y' := y + σ(N_1(0, 1), ..., N_N(0, 1))
function isotropic{T <: Vector, S <: Strategy}(recombinant::T, s::S)
function isotropic(recombinant::T, s::S) where {T <: Vector, S <: Strategy}
vals = randn(length(recombinant)) * s[]
recombinant += vals
return recombinant
end

# Anisotropic mutation operator y' := y + σ(N_1(0, 1), ..., N_N(0, 1))
function anisotropic{T <: Vector, S <: Strategy}(recombinant::T, s::S)
function anisotropic(recombinant::T, s::S) where {T <: Vector, S <: Strategy}
@assert length(s[]) == length(recombinant) "Sigma parameters must be defined for every dimension of objective parameter"
vals = randn(length(recombinant)) .* s[]
recombinant += vals
Expand All @@ -21,13 +21,13 @@ end
# ===========================

# Isotropic strategy mutation σ' := σ exp(τ N(0, 1))
function isotropicSigma{S <: Strategy}(s::S)
function isotropicSigma(s::S) where {S <: Strategy}
@assert keys(s) && keys(s) "Strategy must have parameters: σ, τ"
return strategy= s[] * exp(s[]*randn()), τ = s[])
end

# Anisotropic strategy mutation σ' := exp(τ0 N_0(0, 1))(σ_1 exp(τ N_1(0, 1)), ..., σ_N exp(τ N_N(0, 1)))
function anisotropicSigma{S <: Strategy}(s::S)
function anisotropicSigma(s::S) where {S <: Strategy}
@assert keys(s) && keys(s) && :τ0 keys(s) "Strategy must have parameters: σ, τ0, τ"
@assert isa(s[], Vector) "Sigma must be a vector of parameters"
#σ = exp(s[:τ0]*randn())*exp(s[:τ]*randn(length(s[:σ])))
Expand All @@ -53,7 +53,7 @@ end
# --------------------
function domainrange(valrange::Vector, m::Int = 20)
prob = 1.0 / m
function mutation{T <: Vector}(recombinant::T)
function mutation(recombinant::T) where {T <: Vector}
d = length(recombinant)
@assert length(valrange) == d "Range matrix must have $(d) columns"
δ = zeros(m)
Expand All @@ -75,7 +75,7 @@ end

# Combinatorial mutations (applicable to binary vectors)
# ------------------------------------------------------
function inversion{T <: Vector}(recombinant::T)
function inversion(recombinant::T) where {T <: Vector}
l = length(recombinant)
from, to = rand(1:l, 2)
from, to = from > to ? (to, from) : (from, to)
Expand All @@ -86,22 +86,22 @@ function inversion{T <: Vector}(recombinant::T)
return recombinant
end

function insertion{T <: Vector}(recombinant::T)
function insertion(recombinant::T) where {T <: Vector}
l = length(recombinant)
from, to = rand(1:l, 2)
val = recombinant[from]
deleteat!(recombinant, from)
return insert!(recombinant, to, val)
end

function swap2{T <: Vector}(recombinant::T)
function swap2(recombinant::T) where {T <: Vector}
l = length(recombinant)
from, to = rand(1:l, 2)
swap!(recombinant, from, to)
return recombinant
end

function scramble{T <: Vector}(recombinant::T)
function scramble(recombinant::T) where {T <: Vector}
l = length(recombinant)
from, to = rand(1:l, 2)
from, to = from > to ? (to, from) : (from, to)
Expand All @@ -117,7 +117,7 @@ function scramble{T <: Vector}(recombinant::T)
return recombinant
end

function shifting{T <: Vector}(recombinant::T)
function shifting(recombinant::T) where {T <: Vector}
l = length(recombinant)
from, to, where = sort(rand(1:l, 3))
patch = recombinant[from:to]
Expand All @@ -139,13 +139,13 @@ end

# Utils
# =====
function swap!{T <: Vector}(v::T, from::Int, to::Int)
function swap!(v::T, from::Int, to::Int) where {T <: Vector}
val = v[from]
v[from] = v[to]
v[to] = val
end

function mutationwrapper(gamutation::Function)
wrapper{T <: Vector, S <: Strategy}(recombinant::T, s::S) = gamutation(recombinant)
wrapper(recombinant::T, s::S) where {T <: Vector, S <: Strategy} = gamutation(recombinant)
return wrapper
end
end
Loading

0 comments on commit 37e1716

Please sign in to comment.