Skip to content

Commit 76943ab

Browse files
Merge pull request #263 from SciML/polyalgorithms
Create GalacticPolyalgorithms.jl
2 parents 154431e + f610698 commit 76943ab

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
- GalacticOptimisers
3131
- GalacticQuadDIRECT
3232
- GalacticSpeedMapping
33+
- GalacticPolyalgorithms
3334
version:
3435
- '1'
3536
- '1.6'

lib/GalacticPolyalgorithms/LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2022 Julia Computing
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name = "GalacticPolyalgorithms"
2+
uuid = "8202cac9-28d3-4ced-94fb-69829a1553b4"
3+
authors = ["Vaibhav Dixit <vaibhavyashdixit@gmail.com> and contributors"]
4+
version = "0.1.0"
5+
6+
[deps]
7+
GalacticOptim = "a75be94c-b780-496d-a8a9-0878b188d577"
8+
GalacticOptimJL = "9d3c5eb1-403b-401b-8c0f-c11105342e6b"
9+
GalacticOptimisers = "86b7a833-eb4b-49e2-87ed-89357ad7afa2"
10+
11+
[compat]
12+
GalacticOptim = "3"
13+
GalacticOptimJL = "0.1"
14+
GalacticOptimisers = "0.1"
15+
julia = "1.6"
16+
17+
[extras]
18+
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
19+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
20+
21+
[targets]
22+
test = ["ForwardDiff", "Test"]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module GalacticPolyalgorithms
2+
3+
using GalacticOptim, GalacticOptim.SciMLBase, GalacticOptimJL, GalacticOptimisers
4+
5+
struct PolyOpt end
6+
7+
function SciMLBase.__solve(prob::OptimizationProblem,
8+
opt::PolyOpt,
9+
args...;
10+
maxiters = nothing,
11+
kwargs...)
12+
13+
loss, θ = x -> prob.f(x, prob.p), prob.u0
14+
deterministic = first(loss(θ)) == first(loss(θ))
15+
16+
if (!isempty(args) || !deterministic) && maxiters === nothing
17+
error("Automatic optimizer determination requires deterministic loss functions (and no data) or maxiters must be specified.")
18+
end
19+
20+
if isempty(args) && deterministic && prob.lb === nothing && prob.ub === nothing
21+
# If determinsitic then ADAM -> finish with BFGS
22+
if maxiters === nothing
23+
res1 = GalacticOptim.solve(prob, Optimisers.ADAM(0.01), args...; maxiters=300, kwargs...)
24+
else
25+
res1 = GalacticOptim.solve(prob, Optimisers.ADAM(0.01), args...; maxiters, kwargs...)
26+
end
27+
28+
optprob2 = remake(prob,u0 = res1.u)
29+
res1 = GalacticOptim.solve(
30+
optprob2, BFGS(initial_stepnorm=0.01), args...; maxiters, kwargs...)
31+
elseif isempty(args) && deterministic
32+
res1 = GalacticOptim.solve(
33+
prob, BFGS(initial_stepnorm=0.01), args...; maxiters, kwargs...)
34+
else
35+
res1 = GalacticOptim.solve(prob, Optimisers.ADAM(0.1), args...; maxiters, kwargs...)
36+
end
37+
38+
end
39+
40+
export PolyOpt
41+
42+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using GalacticPolyalgorithms, GalacticOptim, ForwardDiff
2+
using Test
3+
4+
@testset "GalacticPolyalgorithms.jl" begin
5+
rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
6+
x0 = zeros(2)
7+
_p = [1.0, 100.0]
8+
l1 = rosenbrock(x0, _p)
9+
10+
optprob = OptimizationFunction(rosenbrock, GalacticOptim.AutoForwardDiff())
11+
prob = OptimizationProblem(optprob, x0, _p)
12+
sol = GalacticOptim.solve(prob, PolyOpt(), maxiters=1000)
13+
@test 10 * sol.minimum < l1
14+
end

0 commit comments

Comments
 (0)