-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use EnsembleProblem instead of custom struct
- Loading branch information
1 parent
4eef579
commit b5709fe
Showing
3 changed files
with
30 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
struct EnsembleOptimizationProblem{T1} <: SciMLBase.AbstractEnsembleProblem | ||
prob::OptimizationProblem{iip, F, T1} where {iip, F} | ||
u0s::Vector{T1} | ||
function SciMLBase.EnsembleProblem(prob::OptimizationProblem, u0s::Vector{Vector{T}}; kwargs...) where {T} | ||
prob_func = (prob, i, repeat = nothing) -> remake(prob, u0 = u0s[i]) | ||
return SciMLBase.EnsembleProblem(prob; prob_func, kwargs...) | ||
end | ||
|
||
function SciMLBase.__init(prob::EnsembleOptimizationProblem{T}, args...; kwargs...) where {T <: OptimizationProblem} | ||
probs = [remake(prob.prob, u0=u0; kwargs...) for u0 in prob.u0s] | ||
return [SciMLBase.__init(prob, args...; kwargs...) for prob in probs] | ||
function SciMLBase.init(prob::EnsembleProblem{T}, args...; kwargs...) where {T <: OptimizationProblem} | ||
SciMLBase.__init(prob, args...; kwargs...) | ||
end | ||
|
||
function SciMLBase.__solve(caches::Vector{OptimizationCache}, args...; kwargs...) | ||
function SciMLBase.__init(prob::EnsembleProblem{T}, args...; trajectories, kwargs...) where {T <: OptimizationProblem} | ||
return [SciMLBase.__init(prob.prob_func(prob.prob, i), args...; kwargs...) for i in 1:trajectories] | ||
end | ||
|
||
function SciMLBase.solve!(cache::Vector{<:OptimizationCache}; kwargs...) | ||
return [SciMLBase.solve!(cache[i]; kwargs...) for i in eachindex(cache)] | ||
end | ||
|
||
function SciMLBase.__solve(caches::Vector{<:OptimizationCache}, args...; kwargs...) | ||
return [SciMLBase.__solve(cache, args...; kwargs...) for cache in caches] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Optimization, OptimizationOptimJL, ForwardDiff | ||
|
||
x0 = zeros(2) | ||
rosenbrock(x, p = nothing) = (1 - x[1])^2 + 100 * (x[2] - x[1]^2)^2 | ||
l1 = rosenbrock(x0) | ||
|
||
optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff()) | ||
prob = OptimizationProblem(optf, x0) | ||
sol1 = Optimization.solve(prob, OptimizationOptimJL.BFGS(), maxiters = 5) | ||
|
||
|
||
ensembleprob = Optimization.EnsembleProblem(prob, [x0, x0 .+ rand(2), x0 .+ rand(2), x0 .+ rand(2)]) | ||
sol = Optimization.solve(ensembleprob, OptimizationOptimJL.BFGS(), trajectories = 4, maxiters = 5) | ||
|
||
@test findmin(i -> sol[i].objective, 1:4) < sol1.objective |