Skip to content

Create sub packages for each optimiser #220

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

Merged
merged 22 commits into from
Apr 25, 2022
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
20 changes: 18 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,26 @@ on:
jobs:
test:
runs-on: ubuntu-latest
continue-on-error: ${{ matrix.group == 'Downstream' }}
strategy:
fail-fast: false
matrix:
group:
- All
- Core
- GalacticBBO
- GalacticCMAEvolutionStrategy
- GalacticEvolutionary
- GalacticFlux
- GalacticGCMAES
- GalacticMetaheuristics
- GalacticMOI
- GalacticMultistartOptimization
- GalacticNLopt
- GalacticNOMAD
- GalacticNonconvex
- GalacticOptimJL
- GalacticQuadDIRECT
- GalacticSpeedMapping
version:
- '1'
- '1.6'
Expand All @@ -38,4 +54,4 @@ jobs:
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v1
with:
file: lcov.info
file: lcov.info
17 changes: 17 additions & 0 deletions lib/GalacticBBO/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name = "GalacticBBO"
uuid = "80c49c3a-6557-47d9-8f5b-13d0a2920315"
authors = ["Vaibhav Dixit <[email protected]> and contributors"]
version = "0.1.0"

[deps]
BlackBoxOptim = "a134a8b2-14d6-55f6-9291-3336d3ab0209"
GalacticOptim = "a75be94c-b780-496d-a8a9-0878b188d577"

[compat]
julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
101 changes: 101 additions & 0 deletions lib/GalacticBBO/src/GalacticBBO.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
module GalacticBBO

using BlackBoxOptim, GalacticOptim, GalacticOptim.SciMLBase

abstract type BBO end

for j = string.(BlackBoxOptim.SingleObjectiveMethodNames)
eval(Meta.parse("Base.@kwdef struct BBO_" * j * " <: BBO method=:" * j * " end"))
eval(Meta.parse("export BBO_" * j))
end

decompose_trace(opt::BlackBoxOptim.OptRunController) = BlackBoxOptim.best_candidate(opt)

function __map_optimizer_args(prob::SciMLBase.OptimizationProblem, opt::BBO;
cb=nothing,
maxiters::Union{Number,Nothing}=nothing,
maxtime::Union{Number,Nothing}=nothing,
abstol::Union{Number,Nothing}=nothing,
reltol::Union{Number,Nothing}=nothing,
kwargs...)

if !isnothing(reltol)
@warn "common reltol is currently not used by $(opt)"
end

mapped_args = (; Method=opt.method,
SearchRange=[(prob.lb[i], prob.ub[i]) for i in 1:length(prob.lb)])

if !isnothing(cb)
mapped_args = (; mapped_args..., CallbackFunction=cb, CallbackInterval=0.0)
end

mapped_args = (; mapped_args..., kwargs...)

if !isnothing(maxiters)
mapped_args = (; mapped_args..., MaxSteps=maxiters)
end

if !isnothing(maxtime)
mapped_args = (; mapped_args..., MaxTime=maxtime)
end

if !isnothing(abstol)
mapped_args = (; mapped_args..., MinDeltaFitnessTolerance=abstol)
end

return mapped_args
end

function SciMLBase.__solve(prob::SciMLBase.OptimizationProblem, opt::BBO, data=GalacticOptim.DEFAULT_DATA;
cb=(args...) -> (false),
maxiters::Union{Number,Nothing}=nothing,
maxtime::Union{Number,Nothing}=nothing,
abstol::Union{Number,Nothing}=nothing,
reltol::Union{Number,Nothing}=nothing,
progress=false, kwargs...)

local x, cur, state

if data != GalacticOptim.DEFAULT_DATA
maxiters = length(data)
end

cur, state = iterate(data)

function _cb(trace)
cb_call = cb(decompose_trace(trace), x...)
if !(typeof(cb_call) <: Bool)
error("The callback should return a boolean `halt` for whether to stop the optimization process.")
end
if cb_call == true
BlackBoxOptim.shutdown_optimizer!(trace) #doesn't work
end
cur, state = iterate(data, state)
cb_call
end

maxiters = GalacticOptim._check_and_convert_maxiters(maxiters)
maxtime = GalacticOptim._check_and_convert_maxtime(maxtime)


_loss = function (θ)
x = prob.f(θ, prob.p, cur...)
return first(x)
end

opt_args = __map_optimizer_args(prob, opt, cb=_cb, maxiters=maxiters, maxtime=maxtime, abstol=abstol, reltol=reltol; kwargs...)

opt_setup = BlackBoxOptim.bbsetup(_loss; opt_args...)

t0 = time()
opt_res = BlackBoxOptim.bboptimize(opt_setup)
t1 = time()

opt_ret = Symbol(opt_res.stop_reason)

SciMLBase.build_solution(prob, opt, BlackBoxOptim.best_candidate(opt_res),
BlackBoxOptim.best_fitness(opt_res); original=opt_res, retcode=opt_ret)
end

end
14 changes: 14 additions & 0 deletions lib/GalacticBBO/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using GalacticBBO, GalacticOptim, Zygote
using Test

@testset "GalacticBBO.jl" begin
rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
x0 = zeros(2)
_p = [1.0, 100.0]
l1 = rosenbrock(x0, _p)

optprob = OptimizationFunction(rosenbrock, GalacticOptim.AutoZygote())
prob = GalacticOptim.OptimizationProblem(optprob, x0, _p, lb=[-1.0, -1.0], ub=[0.8, 0.8])
sol = solve(prob, BBO_adaptive_de_rand_1_bin_radiuslimited())
@test 10 * sol.minimum < l1
end
17 changes: 17 additions & 0 deletions lib/GalacticCMAEvolutionStrategy/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name = "GalacticCMAEvolutionStrategy"
uuid = "12fbe61e-72e2-4b68-be42-232f99b30434"
authors = ["Vaibhav Dixit <[email protected]> and contributors"]
version = "0.1.0"

[deps]
CMAEvolutionStrategy = "8d3b24bd-414e-49e0-94fb-163cc3a3e411"
GalacticOptim = "a75be94c-b780-496d-a8a9-0878b188d577"

[compat]
julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
module GalacticCMAEvolutionStrategy

using CMAEvolutionStrategy, GalacticOptim, GalacticOptim.SciMLBase

export CMAEvolutionStrategyOpt

struct CMAEvolutionStrategyOpt end

function __map_optimizer_args(prob::OptimizationProblem, opt::CMAEvolutionStrategyOpt;
cb=nothing,
maxiters::Union{Number, Nothing}=nothing,
maxtime::Union{Number, Nothing}=nothing,
abstol::Union{Number, Nothing}=nothing,
reltol::Union{Number, Nothing}=nothing,
maxiters::Union{Number,Nothing}=nothing,
maxtime::Union{Number,Nothing}=nothing,
abstol::Union{Number,Nothing}=nothing,
reltol::Union{Number,Nothing}=nothing,
kwargs...)

if !isnothing(reltol)
@warn "common reltol is currently not used by $(opt)"
end

mapped_args = (;lower = prob.lb,
upper = prob.ub,
kwargs...)
mapped_args = (; lower=prob.lb,
upper=prob.ub,
kwargs...)

if !isnothing(maxiters)
mapped_args = (; mapped_args..., maxiter=maxiters)
end
Expand All @@ -28,44 +33,44 @@ function __map_optimizer_args(prob::OptimizationProblem, opt::CMAEvolutionStrate
if !isnothing(abstol)
mapped_args = (; mapped_args..., ftol=abstol)
end

return mapped_args
end


function __solve(prob::OptimizationProblem, opt::CMAEvolutionStrategyOpt, data = DEFAULT_DATA;
cb = (args...) -> (false),
maxiters::Union{Number, Nothing} = nothing,
maxtime::Union{Number, Nothing} = nothing,
abstol::Union{Number, Nothing}=nothing,
reltol::Union{Number, Nothing}=nothing,
progress = false, kwargs...)
function SciMLBase.__solve(prob::OptimizationProblem, opt::CMAEvolutionStrategyOpt, data=GalacticOptim.DEFAULT_DATA;
cb=(args...) -> (false),
maxiters::Union{Number,Nothing}=nothing,
maxtime::Union{Number,Nothing}=nothing,
abstol::Union{Number,Nothing}=nothing,
reltol::Union{Number,Nothing}=nothing,
kwargs...)
local x, cur, state

if data != DEFAULT_DATA
if data != GalacticOptim.DEFAULT_DATA
maxiters = length(data)
end

cur, state = iterate(data)

function _cb(trace)
cb_call = cb(decompose_trace(trace).metadata["x"],trace.value...)
cb_call = cb(decompose_trace(trace).metadata["x"], trace.value...)
if !(typeof(cb_call) <: Bool)
error("The callback should return a boolean `halt` for whether to stop the optimization process.")
end
cur, state = iterate(data, state)
cb_call
end

maxiters = _check_and_convert_maxiters(maxiters)
maxtime = _check_and_convert_maxtime(maxtime)
maxiters = GalacticOptim._check_and_convert_maxiters(maxiters)
maxtime = GalacticOptim._check_and_convert_maxtime(maxtime)

_loss = function(θ)
_loss = function (θ)
x = prob.f(θ, prob.p, cur...)
return first(x)
end

opt_args = _map_optimizer_args(prob, opt, cb=_cb, maxiters=maxiters, maxtime=maxtime,abstol=abstol, reltol=reltol; kwargs...)
opt_args = __map_optimizer_args(prob, opt, cb=_cb, maxiters=maxiters, maxtime=maxtime, abstol=abstol, reltol=reltol; kwargs...)

t0 = time()
opt_res = CMAEvolutionStrategy.minimize(_loss, prob.u0, 0.1; opt_args...)
Expand All @@ -75,3 +80,5 @@ function __solve(prob::OptimizationProblem, opt::CMAEvolutionStrategyOpt, data =

SciMLBase.build_solution(prob, opt, opt_res.logger.xbest[end], opt_res.logger.fbest[end]; original=opt_res, retcode=opt_ret)
end

end
13 changes: 13 additions & 0 deletions lib/GalacticCMAEvolutionStrategy/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using GalacticCMAEvolutionStrategy, GalacticOptim, ForwardDiff
using Test

@testset "GalacticCMAEvolutionStrategy.jl" begin
rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
x0 = zeros(2)
_p = [1.0, 100.0]
l1 = rosenbrock(x0, _p)
f = OptimizationFunction(rosenbrock, GalacticOptim.AutoForwardDiff())
prob = OptimizationProblem(f, x0, _p, lb=[-1.0, -1.0], ub=[0.8, 0.8])
sol = solve(prob, CMAEvolutionStrategyOpt())
@test 10 * sol.minimum < l1
end
18 changes: 18 additions & 0 deletions lib/GalacticEvolutionary/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name = "GalacticEvolutionary"
uuid = "e82bed00-89d9-4bf2-a298-ae594e22fac7"
authors = ["Vaibhav Dixit <[email protected]> and contributors"]
version = "0.1.0"

[deps]
Evolutionary = "86b6b26d-c046-49b6-aa0b-5f0f74682bd6"
GalacticOptim = "a75be94c-b780-496d-a8a9-0878b188d577"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"

[compat]
julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
Loading