Skip to content

Commit

Permalink
improve resuming
Browse files Browse the repository at this point in the history
  • Loading branch information
AStupidBear committed Jun 9, 2020
1 parent 4da4b0b commit 497f57d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GCMAES"
uuid = "4aa9d100-eb0f-11e8-15f1-25748831eb3b"
authors = ["Yao Lu <[email protected]>"]
version = "0.1.12"
version = "0.1.13"

[deps]
BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"
Expand Down
35 changes: 18 additions & 17 deletions src/GCMAES.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ mutable struct CMAESOpt{T, F, G, CO, TR}
μ::Int
w::Vector{T}
μeff::T
χₙ::T
# strategy parameter setting: adaptation
σ::T
cc::T
Expand All @@ -46,7 +47,6 @@ mutable struct CMAESOpt{T, F, G, CO, TR}
B::Matrix{T}
BD::Matrix{T}
C::Matrix{T}
χₙ::T
arx::Matrix{T}
ary::Matrix{T}
arz::Matrix{T}
Expand Down Expand Up @@ -102,9 +102,11 @@ function CMAESOpt(f, g, x0, σ0, lo = -fill(1, size(x0)), hi = fill(1, size(x0))
T, F, G = eltype(x̄), typeof(f′), typeof(g′)
CO, TR = typeof(constr), typeof(trans)
return CMAESOpt{T, F, G, CO, TR}(
f′, g′, N, σ0, lo, hi, constr, trans, rng,
λ, μ, w, μeff, σ, cc, cσ, c1, cμ, dσ,
x̄, pc, pσ, D, B, BD, C, χₙ,
f′, g′, N, σ0, lo, hi,
constr, trans, rng,
λ, μ, w, μeff, χₙ,
σ, cc, cσ, c1, cμ, dσ,
x̄, pc, pσ, D, B, BD, C,
arx, ary, arz, arfitness, arindex,
xmin, fmin, T[], T[], T[],
time(), 0, 0, 0, 0, "CMAES.bson")
Expand Down Expand Up @@ -266,39 +268,38 @@ function load!(opt::CMAESOpt, resume)
(resume == false || !isfile(opt.file)) && return
data = BSON.load(opt.file)
data[:N] != opt.N && return
loadvars = [, :cc, :cσ, :c1, :cμ, :dσ, :x̄, :pc, :pσ, :D, :B, :BD, :C, :χₙ]
loadvars = [, :cc, :cσ, :c1, :cμ, :dσ, :x̄, :pc, :pσ, :D, :B, :BD, :C]
resume == :full && append!(loadvars, [:xmin, :fmin, :fmins, :fmeds, :feqls])
for s in loadvars
haskey(data, s) && setfield!(opt, s, data[s])
end
end

function save(opt::CMAESOpt, saveall = false)
data = Dict{Symbol, Any}()
if saveall || Base.summarysize(opt) <= 1024^2 * 20
for fn in fieldnames(CMAESOpt)
x = getfield(opt, fn)
isa(x, Union{Number, Array}) && setindex!(data, copy(x), fn)
end
else
data[:xmin] = copy(opt.xmin)
data[:x̄] = copy(opt.x̄)
data[:N] = opt.N
data = Dict{Symbol, Any}(:N => opt.N)
savevars = [, :cc, :cσ, :c1, :cμ, :dσ, :x̄, :pc, :pσ, :D, :B, :BD, :C, :xmin, :fmin, :fmins, :fmeds, :feqls]
if !saveall && Base.summarysize(opt) >= 1024^2 * 20
savevars = setdiff(savevars, [:D, :B, :BD, :C])
end
for s in savevars
data[s] = copy(getfield(opt, s))
end
BSON.bson(opt.file, data)
end

function minimize(fg, x0, a...; maxfevals = 0, gcitr = false, maxiter = 0,
resume = false, cb = [], seed = nothing, autodiff = false,
saveall = false, lazydecomp = false, equal_best = Inf, ka...)
rng = bcast(MersenneTwister(seed), 0)
rng = bcast(MersenneTwister(seed))
f, g = fg isa Tuple ? fg : autodiff ? (fg, fg') : (fg, zero)
opt = CMAESOpt(f, g, bcast(x0), a...; rng = rng, ka...)
cb = runall([throttle(x -> save(opt, saveall), 60); cb])
maxfevals = (maxfevals == 0) ? 1e3 * length(x0)^2 : maxfevals
maxfevals = maxiter != 0 ? maxiter * opt.λ : maxfevals
load!(opt, resume)
fcount = iter = 0; status = 0
iter = length(opt.fmins)
fcount = iter * opt.λ
status = 0
while fcount < maxfevals
iter += 1; fcount += opt.λ
if opt.λ == 1
Expand Down
4 changes: 3 additions & 1 deletion src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,6 @@ deigen(x) = eigen(Symmetric(x, :U))

myrank() = myid() - 1

bcast(x) = x
bcast(x, root = 0) = x

allequal(x) = length(unique(x)) == 1

2 comments on commit 497f57d

@AStupidBear
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/16103

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.13 -m "<description of version>" 497f57d04f89072ad94297a8516d15aff7e9f5d1
git push origin v0.1.13

Please sign in to comment.