Skip to content
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

[WIP] Ensemble #620

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
ProgressLogging = "33c8b6b6-d38a-422a-b730-caa89a2f386c"
QuasiMonteCarlo = "8a4e6c94-4038-4cdc-81c3-7e6ffdb2a71b"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
Expand Down
3 changes: 2 additions & 1 deletion src/Optimization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if !isdefined(Base, :get_extension)
end

using Logging, ProgressLogging, ConsoleProgressMonitor, TerminalLoggers, LoggingExtras
using ArrayInterface, Base.Iterators, SparseArrays, LinearAlgebra
using ArrayInterface, Base.Iterators, SparseArrays, LinearAlgebra, QuasiMonteCarlo
using Pkg

import SciMLBase: OptimizationProblem, OptimizationFunction, ObjSense,
Expand All @@ -23,6 +23,7 @@ include("utils.jl")
include("function.jl")
include("adtypes.jl")
include("cache.jl")
include("ensemble.jl")

@static if !isdefined(Base, :get_extension)
function __init__()
Expand Down
19 changes: 19 additions & 0 deletions src/ensemble.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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...)

Check warning on line 3 in src/ensemble.jl

View check run for this annotation

Codecov / codecov/patch

src/ensemble.jl#L1-L3

Added lines #L1 - L3 were not covered by tests
end

function SciMLBase.EnsembleProblem(prob::OptimizationProblem, trajectories::Int; kwargs...)
Copy link
Member

Choose a reason for hiding this comment

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

I'd just keep to the interface. This doesn't seem like a necessary shorthand to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd like to provide an automated way of doing the solves with initial points sampled, it would be useful. I can probably do it another way if you are opposed to this, but do note that the other dispatch is already not conforming fully with the current interface of creating these problems

Copy link
Member

Choose a reason for hiding this comment

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

it would be good to come up with such a shorthand at a higher level of the interface, since NonlinearProblem, NonlinearLeastSquaresProblem, etc. would want to do similar things.

if prob.lb != nothing && prob.ub != nothing
u0s = QuasiMonteCarlo.sample(trajectories, prob.lb, prob.ub, LatinHypercubeSample())
prob_func = (prob, i, repeat = nothing) -> remake(prob, u0 = u0s[:, i])

Check warning on line 9 in src/ensemble.jl

View check run for this annotation

Codecov / codecov/patch

src/ensemble.jl#L6-L9

Added lines #L6 - L9 were not covered by tests
else
error("EnsembleProblem requires either initial points as second argument or lower and upper bounds to be defined with the trajectories second argument method.")

Check warning on line 11 in src/ensemble.jl

View check run for this annotation

Codecov / codecov/patch

src/ensemble.jl#L11

Added line #L11 was not covered by tests
end
return SciMLBase.EnsembleProblem(prob; prob_func, kwargs...)

Check warning on line 13 in src/ensemble.jl

View check run for this annotation

Codecov / codecov/patch

src/ensemble.jl#L13

Added line #L13 was not covered by tests
end


function SciMLBase.solve(prob::EnsembleProblem{T}, args...; kwargs...) where {T <: OptimizationProblem}
return SciMLBase.__solve(prob, args...; kwargs...)

Check warning on line 18 in src/ensemble.jl

View check run for this annotation

Codecov / codecov/patch

src/ensemble.jl#L17-L18

Added lines #L17 - L18 were not covered by tests
end
18 changes: 18 additions & 0 deletions test/ensemble.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Optimization, OptimizationOptimJL, ForwardDiff, Test

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(), EnsembleThreads(), trajectories = 4, maxiters = 5)
@test findmin(i -> sol[i].objective, 1:4)[1] < sol1.objective

sol = Optimization.solve(ensembleprob, OptimizationOptimJL.BFGS(), EnsembleDistributed(), trajectories = 4, maxiters = 5)
@test findmin(i -> sol[i].objective, 1:4)[1] < sol1.objective
Loading