Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ terminated.
- `cache::LocalSearchSolutionCache{T}`: The solution cache for storing the solution from
solving with NonlinearSolve.jl.
"""
struct NonlinearSolveLocalSearch{T,A,UTO<:Union{Val{true},Val{false}}} <: GlobalOptimization.NonlinearSolveLocalSearch{T}
struct NonlinearSolveLocalSearch{T,A,UTO<:Union{Val{true},Val{false}}} <:
GlobalOptimization.NonlinearSolveLocalSearch{T}
# Tollerance on percent decrease of objective function for performing another local search
percent_decrease_tolerance::T

Expand Down Expand Up @@ -115,7 +116,9 @@ function nonlinear_solve!(
return true
end

function GlobalOptimization.get_solve_fun(evaluator, ls::NonlinearSolveLocalSearch{T,A}) where {T,A}
function GlobalOptimization.get_solve_fun(
evaluator, ls::NonlinearSolveLocalSearch{T,A}
) where {T,A}
@unpack prob = evaluator
@unpack alg, abs_tol, max_solve_iters, cache = ls
solve! =
Expand Down
25 changes: 19 additions & 6 deletions ext/OptimLocalSearchExt/OptimLocalSearchExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ Note that this method employs the `LBFGS` algorithm with the `Fminbox` wrapper f
used. Can be any of the autodiff methods from
[ADTypes.jl](https://github.com/SciML/ADTypes.jl).
"""
struct LBFGSLocalSearch{T,AT,OT,AD<:Union{AbstractADType,Nothing},UTO<:Union{Val{true},Val{false}}} <: GlobalOptimization.LBFGSLocalSearch{T}
struct LBFGSLocalSearch{
T,AT,OT,AD<:Union{AbstractADType,Nothing},UTO<:Union{Val{true},Val{false}}
} <: GlobalOptimization.LBFGSLocalSearch{T}

# Tollerance on percent decrease of objective function for performing another local search
percent_decrease_tolerance::T
Expand Down Expand Up @@ -117,7 +119,7 @@ struct LBFGSLocalSearch{T,AT,OT,AD<:Union{AbstractADType,Nothing},UTO<:Union{Val
)
end
end
function GlobalOptimization.LBFGSLocalSearch{T}(args...; kwargs...) where T
function GlobalOptimization.LBFGSLocalSearch{T}(args...; kwargs...) where {T}
return LBFGSLocalSearch{T}(args...; kwargs...)
end

Expand All @@ -126,15 +128,24 @@ function GlobalOptimization.initialize!(ls::LBFGSLocalSearch, num_dims)
return nothing
end

function optim_solve!(cache::GlobalOptimization.LocalSearchSolutionCache, prob, x0, alg, options)
function optim_solve!(
cache::GlobalOptimization.LocalSearchSolutionCache, prob, x0, alg, options
)
res = Optim.optimize(
GlobalOptimization.get_scalar_function(prob), prob.ss.dim_min, prob.ss.dim_max, x0, alg, options;
GlobalOptimization.get_scalar_function(prob),
prob.ss.dim_min,
prob.ss.dim_max,
x0,
alg,
options;
)
cache.x .= Optim.minimizer(res)
cache.cost = Optim.minimum(res)
return true
end
function optim_solve!(cache::GlobalOptimization.LocalSearchSolutionCache, prob, x0, alg, ad, options)
function optim_solve!(
cache::GlobalOptimization.LocalSearchSolutionCache, prob, x0, alg, ad, options
)
res = Optim.optimize(
GlobalOptimization.get_scalar_function(prob),
prob.ss.dim_min,
Expand All @@ -152,7 +163,9 @@ end
HasAD(::LBFGSLocalSearch{T,AT,OT,Nothing}) where {T,AT,OT} = Val{false}()
HasAD(::LBFGSLocalSearch{T,AT,OT,<:AbstractADType}) where {T,AT,OT} = Val{true}()

GlobalOptimization.get_solve_fun(eval,ls::LBFGSLocalSearch) = get_solve_fun(eval, ls, HasAD(ls))
function GlobalOptimization.get_solve_fun(eval, ls::LBFGSLocalSearch)
get_solve_fun(eval, ls, HasAD(ls))
end
function get_solve_fun(evaluator, ls::LBFGSLocalSearch, ::Val{false})
@unpack prob = evaluator
@unpack alg, options, cache = ls
Expand Down
25 changes: 12 additions & 13 deletions ext/PRIMALocalSearchExt/PRIMALocalSearchExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ A local search algorithm that uses the BOBYQA algorithm from SSDC_PRIMA.jl to lo
- `max_solve_time::Float64=0.1`: Max solve time in seconds.

"""
struct BOBYQALocalSearch{T,UTO<:Union{Val{true},Val{false}}} <: GlobalOptimization.BOBYQALocalSearch{T}
struct BOBYQALocalSearch{T,UTO<:Union{Val{true},Val{false}}} <:
GlobalOptimization.BOBYQALocalSearch{T}
# Tollerance on percent decrease of objective function for performing another local search
percent_decrease_tolerance::T

Expand All @@ -45,7 +46,7 @@ struct BOBYQALocalSearch{T,UTO<:Union{Val{true},Val{false}}} <: GlobalOptimizati
function BOBYQALocalSearch{T}(;
percent_decrease_tol::Number=50.0,
max_fevals::Int=1000,
use_timeout::VT= Val{true}(),
use_timeout::VT=Val{true}(),
max_solve_time::Float64=0.1,
) where {T<:AbstractFloat,VT<:Union{Val{true},Val{false}}}
return new{T,VT}(
Expand All @@ -57,7 +58,7 @@ struct BOBYQALocalSearch{T,UTO<:Union{Val{true},Val{false}}} <: GlobalOptimizati
)
end
end
function GlobalOptimization.BOBYQALocalSearch{T}(args...; kwargs...) where T
function GlobalOptimization.BOBYQALocalSearch{T}(args...; kwargs...) where {T}
return BOBYQALocalSearch{T}(args...; kwargs...)
end

Expand All @@ -77,20 +78,18 @@ function bobyqa_solve!(
lbx = x0[i] - dmin <= eps() * max(1.0, abs(dmin))
ubx = x0[i] - dmax >= eps() * max(1.0, abs(dmax))
if !lbx && !ubx
rhobeg = max(
eps(),
min(rhobeg, x0[i] - dmin, dmax - x0[i]),
)
rhobeg = max(eps(), min(rhobeg, x0[i] - dmin, dmax - x0[i]))
end
end

x, info = SSDC_PRIMA.bobyqa(
GlobalOptimization.get_scalar_function(prob), x0;
maxfun = max_fevals,
xl = prob.ss.dim_min,
xu = prob.ss.dim_max,
rhobeg = rhobeg,
honour_x0 = true,
GlobalOptimization.get_scalar_function(prob),
x0;
maxfun=max_fevals,
xl=prob.ss.dim_min,
xu=prob.ss.dim_max,
rhobeg=rhobeg,
honour_x0=true,
)
cache.x .= x
cache.cost = info.fx
Expand Down
5 changes: 1 addition & 4 deletions scripts/mbh_testing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ lsgb = LBFGSLocalSearch{Float64}(;
max_solve_time=0.5,
ad=AutoForwardDiff(),
)
lsbobyqa = BOBYQALocalSearch{Float64}(;
max_fevals = 500,
use_timeout = Val{false}(),
)
lsbobyqa = BOBYQALocalSearch{Float64}(; max_fevals=500, use_timeout=Val{false}())
lss = LocalStochasticSearch{Float64}(1e-2, 1000)
nls = GlobalOptimization.NonlinearSolveLocalSearch{Float64}(
NonlinearSolve.NewtonRaphson();
Expand Down
4 changes: 2 additions & 2 deletions src/DE/DE.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ end
function get_save_trace_elements(opt::DE, trace_mode::Val{:all})
# get minimal elements
min_elements = get_save_trace_elements(opt, Val{:minimal}())

# get candidate elements (only for :all trace mode)
cand_elements = get_save_trace_elements(opt.population.current_generation, trace_mode)

return (min_elements..., cand_elements...)
end
end
14 changes: 3 additions & 11 deletions src/DE/crossover.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,7 @@ struct UncorrelatedCovarianceTransformation <: LinearOperatorCrossoverTransforma
end

initialize!(transformation::NoTransformation, population_size) = nothing
function initialize!(
transformation::LinearOperatorCrossoverTransformation, population_size
)
function initialize!(transformation::LinearOperatorCrossoverTransformation, population_size)
resize!(transformation.idxs, population_size)
transformation.idxs .= 1:population_size
return nothing
Expand Down Expand Up @@ -334,9 +332,7 @@ function to_transformed(transformation::LinearOperatorCrossoverTransformation, c
end

from_transformed!(transformation::NoTransformation, mt, m) = nothing
function from_transformed!(
transformation::LinearOperatorCrossoverTransformation, mt, m
)
function from_transformed!(transformation::LinearOperatorCrossoverTransformation, mt, m)
mul!(m, transformation.B, mt)
return nothing
end
Expand Down Expand Up @@ -610,11 +606,7 @@ function crossover!(
# Perform crossover
mbr_i = rand(1:length(mutant_t))
for j in eachindex(mutant_t)
mutant_t[j] = ifelse(
rand() > CR && j != mbr_i,
candidate_t[j],
mutant_t[j]
)
mutant_t[j] = ifelse(rand() > CR && j != mbr_i, candidate_t[j], mutant_t[j])
end

# Transform back to original space
Expand Down
3 changes: 2 additions & 1 deletion src/GlobalOptimization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ using Base: Base

# Needed to pass Aqua.jl tests (Stale Deps) until package extensions can have their own
# dependencies
import ADTypes, LineSearches
using ADTypes: ADTypes
using LineSearches: LineSearches

# Base
include("utils.jl")
Expand Down
8 changes: 4 additions & 4 deletions src/MBH/LocalSearch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ end
function get_solve_fun(eval, ::Any) end

function call(
solve!::F, candidate, use_timeout::Val{true}, max_solve_time,
) where F <: Function
solve!::F, candidate, use_timeout::Val{true}, max_solve_time
) where {F<:Function}
# Call the solve function with a timeout
return timeout(solve!, candidate, max_solve_time, false)
end
function call(
solve!::F, candidate, use_timeout::Val{false}, max_solve_time,
) where F <: Function
solve!::F, candidate, use_timeout::Val{false}, max_solve_time
) where {F<:Function}
solve!(candidate)
return true
end
Expand Down
2 changes: 1 addition & 1 deletion src/PSO/PSO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,4 @@ function get_save_trace_elements(opt::PSO, trace_mode::Val{:all})
pop_elements = get_save_trace_elements(opt.swarm, trace_mode)

return (minimal_elements..., vu_elements..., pop_elements...)
end
end
4 changes: 1 addition & 3 deletions src/Population.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ function initialize_population_vector!(
return nothing
end


function get_save_trace_elements(pop::AbstractPopulation, trace_mode::Val{:all})
@unpack candidates = pop

Expand All @@ -207,5 +206,4 @@ function get_save_trace_elements(pop::AbstractPopulation, trace_mode::Val{:all})
end

return tevec

end
end
6 changes: 4 additions & 2 deletions test/mbh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,16 @@ end
optimum = [1.0, 1.0]
step_size = 0.1
direction = optimum .- GlobalOptimization.candidate(hopper)
norm_dir = sqrt(sum(direction.^2))
norm_dir = sqrt(sum(direction .^ 2))
if norm_dir > 0
direction ./= norm_dir
new_candidate = GlobalOptimization.candidate(hopper) .+ step_size .* direction

# Check feasibility and evaluate fitness like LocalStochasticSearch does
if GlobalOptimization.feasible(new_candidate, evaluator.prob.ss)
fitness, penalty = GlobalOptimization.evaluate_with_penalty(evaluator, new_candidate)
fitness, penalty = GlobalOptimization.evaluate_with_penalty(
evaluator, new_candidate
)
if abs(penalty) - eps() <= 0.0
# Update hopper candidate
hopper.candidate_step .+= new_candidate .- hopper.candidate
Expand Down