Skip to content

Commit

Permalink
scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
AStupidBear committed May 29, 2020
1 parent 98c9c38 commit 4324eeb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
19 changes: 11 additions & 8 deletions src/transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ export BoxLinQuadTransform

abstract type Transform end

transform(trans::Transform, x) = x
transform(t::Transform, x) = x

inverse(trans::Transform, x) = x
inverse(t::Transform, x) = x

struct NoTransform <: Transform
end
Expand All @@ -14,16 +14,18 @@ struct BoxLinQuadTransform{T}
ub::Vector{T}
al::Vector{T}
au::Vector{T}
scaling::Bool
end

function BoxLinQuadTransform(lb, ub)
function BoxLinQuadTransform(lb, ub; scaling = false)
al = @. min((ub - lb) / 2, (1 + abs(lb)) / 20)
au = @. min((ub - lb) / 2, (1 + abs(ub)) / 20)
BoxLinQuadTransform(lb, ub, al, au)
BoxLinQuadTransform(lb, ub, al, au, scaling)
end

function transform(trans::BoxLinQuadTransform, x)
map(x, trans.lb, trans.ub, trans.al, trans.au) do y, lb, ub, al, au
function transform(tr::BoxLinQuadTransform, x)
map(tr.scaling ? tr.lb .+ (tr.ub .- tr.lb) .* x : x,
tr.lb, tr.ub, tr.al, tr.au) do y, lb, ub, al, au
yl = lb - 2 * al - (ub - lb) / 2
yu = ub + 2 * au + (ub - lb) / 2
z, r = y, 2 * (ub - lb + al + au)
Expand All @@ -37,11 +39,12 @@ function transform(trans::BoxLinQuadTransform, x)
end
end

function inverse(trans::BoxLinQuadTransform, x)
map(x, trans.lb, trans.ub, trans.al, trans.au) do y, lb, ub, al, au
function inverse(tr::BoxLinQuadTransform, x)
y = map(x, tr.lb, tr.ub, tr.al, tr.au) do y, lb, ub, al, au
z, y = y, clamp(y, lb, ub)
y = ifelse(y < lb + al, (lb - al) + 2 * sqrt(al * (y - lb)),
ifelse(y > ub - au, (ub + au) - 2 * sqrt(au * (ub - y)), y))
oftype(z, y)
end
tr.scaling ? (y .- tr.lb) ./ (tr.ub .- tr.lb) : y
end
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ x0 = fill(0.3, D)
lo = fill(-5.12, D)
hi = fill(5.12, D)

GCMAES.minimize(rastrigin, x0, σ0, lo, hi, maxiter = 200)
GCMAES.minimize(rastrigin, x0, σ0, lo, hi, maxiter = 200, autodiff = false)
GCMAES.minimize(rastrigin, x0, σ0, lo, hi, maxiter = 200, autodiff = true)
GCMAES.minimize(rastrigin, x0, σ0, lo, hi, maxiter = 200, constr = BoxConstraint(lo, hi, 2))
GCMAES.minimize(rastrigin, x0, σ0, lo, hi, maxiter = 200, constr = NormConstraint(2))
GCMAES.minimize(rastrigin, x0, σ0, lo, hi, maxiter = 200, trans = true)
GCMAES.minimize(rastrigin, x0, σ0, lo, hi, maxiter = 200, trans = BoxLinQuadTransform(lo, hi))
GCMAES.minimize(rastrigin, x0, σ0, lo, hi, maxiter = 200, trans = BoxLinQuadTransform(lo, hi, scaling = false))
GCMAES.minimize(rastrigin, x0, σ0, lo, hi, maxiter = 200, trans = BoxLinQuadTransform(lo, hi, scaling = true))

rm("CMAES.bson", force = true)

2 comments on commit 4324eeb

@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/15564

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.9 -m "<description of version>" 4324eebc59007f8023fa2e997a172892b388a020
git push origin v0.1.9

Please sign in to comment.