1
+ module GalacticOptimJL
2
+
3
+ using Optim, GalacticOptim, GalacticOptim. SciMLBase
4
+
1
5
decompose_trace (trace:: Optim.OptimizationTrace ) = last (trace)
6
+ decompose_trace (trace:: Optim.OptimizationState ) = trace
2
7
3
- function __map_optimizer_args (prob:: OptimizationProblem , opt:: Union{Optim.AbstractOptimizer, Optim.Fminbox,Optim.SAMIN, Optim.ConstrainedOptimizer} ;
8
+ function __map_optimizer_args (prob:: OptimizationProblem , opt:: Union{Optim.AbstractOptimizer,Optim.Fminbox,Optim.SAMIN,Optim.ConstrainedOptimizer} ;
4
9
cb= nothing ,
5
- maxiters:: Union{Number, Nothing} = nothing ,
6
- maxtime:: Union{Number, Nothing} = nothing ,
7
- abstol:: Union{Number, Nothing} = nothing ,
8
- reltol:: Union{Number, Nothing} = nothing ,
10
+ maxiters:: Union{Number,Nothing} = nothing ,
11
+ maxtime:: Union{Number,Nothing} = nothing ,
12
+ abstol:: Union{Number,Nothing} = nothing ,
13
+ reltol:: Union{Number,Nothing} = nothing ,
9
14
kwargs... )
10
15
if ! isnothing (abstol)
11
16
@warn " common abstol is currently not used by $(opt) "
12
17
end
13
18
14
- mapped_args = (;extended_trace= true , kwargs... )
15
-
19
+ mapped_args = (; extended_trace= true , kwargs... )
20
+
16
21
if ! isnothing (cb)
17
- mapped_args = (; mapped_args... , callback = cb)
22
+ mapped_args = (; mapped_args... , callback= cb)
18
23
end
19
24
20
25
if ! isnothing (maxiters)
@@ -28,16 +33,18 @@ function __map_optimizer_args(prob::OptimizationProblem, opt::Union{Optim.Abstra
28
33
if ! isnothing (reltol)
29
34
mapped_args = (; mapped_args... , f_tol= reltol)
30
35
end
31
-
32
- return Optim. Options (;mapped_args... )
36
+
37
+ return Optim. Options (; mapped_args... )
33
38
end
34
39
35
- function __solve (prob:: OptimizationProblem , opt:: Optim.AbstractOptimizer , data = DEFAULT_DATA;
40
+ function SciMLBase. __solve (prob:: OptimizationProblem ,
41
+ opt:: Optim.AbstractOptimizer ,
42
+ data= GalacticOptim. DEFAULT_DATA;
36
43
kwargs... )
37
44
if ! isnothing (prob. lb) | ! isnothing (prob. ub)
38
- if ! (opt isa Union{Optim. Fminbox, Optim. SAMIN, Optim. AbstractConstrainedOptimizer})
45
+ if ! (opt isa Union{Optim. Fminbox,Optim. SAMIN,Optim. AbstractConstrainedOptimizer})
39
46
if opt isa Optim. ParticleSwarm
40
- opt = Optim. ParticleSwarm (;lower= prob. lb, upper= prob. ub, n_particles= opt. n_particles)
47
+ opt = Optim. ParticleSwarm (; lower= prob. lb, upper= prob. ub, n_particles= opt. n_particles)
41
48
elseif opt isa Optim. SimulatedAnnealing
42
49
@warn " $(opt) can currently not be wrapped in Fminbox(). The lower and upper bounds thus will be ignored. Consider using a different optimizer or open an issue with Optim.jl"
43
50
else
@@ -50,147 +57,147 @@ function __solve(prob::OptimizationProblem, opt::Optim.AbstractOptimizer, data =
50
57
end
51
58
52
59
function ___solve (prob:: OptimizationProblem , opt:: Optim.AbstractOptimizer ,
53
- data = DEFAULT_DATA;
54
- cb = (args... ) -> (false ),
55
- maxiters:: Union{Number, Nothing} = nothing ,
56
- maxtime:: Union{Number, Nothing} = nothing ,
57
- abstol:: Union{Number, Nothing} = nothing ,
58
- reltol:: Union{Number, Nothing} = nothing ,
59
- progress = false ,
60
- kwargs... )
60
+ data= GalacticOptim . DEFAULT_DATA;
61
+ cb = (args... ) -> (false ),
62
+ maxiters:: Union{Number,Nothing} = nothing ,
63
+ maxtime:: Union{Number,Nothing} = nothing ,
64
+ abstol:: Union{Number,Nothing} = nothing ,
65
+ reltol:: Union{Number,Nothing} = nothing ,
66
+ progress= false ,
67
+ kwargs... )
61
68
local x, cur, state
62
69
63
- if data != DEFAULT_DATA
70
+ if data != GalacticOptim . DEFAULT_DATA
64
71
maxiters = length (data)
65
72
end
66
73
67
74
cur, state = iterate (data)
68
75
69
76
function _cb (trace)
70
- cb_call = opt == Optim. NelderMead () ? cb (decompose_trace (trace). metadata[" centroid" ],x... ) : cb (decompose_trace (trace). metadata[" x" ],x... )
77
+ cb_call = opt == Optim. NelderMead () ? cb (decompose_trace (trace). metadata[" centroid" ], x... ) : cb (decompose_trace (trace). metadata[" x" ], x... )
71
78
if ! (typeof (cb_call) <: Bool )
72
79
error (" The callback should return a boolean `halt` for whether to stop the optimization process." )
73
80
end
74
81
cur, state = iterate (data, state)
75
82
cb_call
76
83
end
77
84
78
- maxiters = _check_and_convert_maxiters (maxiters)
79
- maxtime = _check_and_convert_maxtime (maxtime)
85
+ maxiters = GalacticOptim . _check_and_convert_maxiters (maxiters)
86
+ maxtime = GalacticOptim . _check_and_convert_maxtime (maxtime)
80
87
81
- f = instantiate_function (prob. f,prob. u0,prob. f. adtype,prob. p)
88
+ f = GalacticOptim . instantiate_function (prob. f, prob. u0, prob. f. adtype, prob. p)
82
89
83
90
! (opt isa Optim. ZerothOrderOptimizer) && f. grad === nothing && error (" Use OptimizationFunction to pass the derivatives or automatically generate them with one of the autodiff backends" )
84
91
85
- _loss = function (θ)
92
+ _loss = function (θ)
86
93
x = f. f (θ, prob. p, cur... )
87
94
__x = first (x)
88
- return prob. sense === MaxSense ? - __x : __x
95
+ return prob. sense === GalacticOptim . MaxSense ? - __x : __x
89
96
end
90
97
91
- fg! = function (G,θ)
98
+ fg! = function (G, θ)
92
99
if G != = nothing
93
100
f. grad (G, θ, cur... )
94
- if prob. sense === MaxSense
101
+ if prob. sense === GalacticOptim . MaxSense
95
102
G .*= false
96
103
end
97
104
end
98
105
return _loss (θ)
99
106
end
100
107
101
108
if opt isa Optim. KrylovTrustRegion
102
- hv = function (H,θ, v)
103
- f. hv (H,θ,v, cur... )
104
- if prob. sense === MaxSense
109
+ hv = function (H, θ, v)
110
+ f. hv (H, θ, v, cur... )
111
+ if prob. sense === GalacticOptim . MaxSense
105
112
H .*= false
106
113
end
107
114
end
108
115
optim_f = Optim. TwiceDifferentiableHV (_loss, fg!, hv, prob. u0)
109
116
else
110
117
gg = function (G, θ)
111
118
f. grad (G, θ, cur... )
112
- if prob. sense === MaxSense
119
+ if prob. sense === GalacticOptim . MaxSense
113
120
G .*= false
114
121
end
115
122
end
116
-
117
- hh = function (H,θ)
118
- f. hess (H,θ, cur... )
119
- if prob. sense === MaxSense
123
+
124
+ hh = function (H, θ)
125
+ f. hess (H, θ, cur... )
126
+ if prob. sense === GalacticOptim . MaxSense
120
127
H .*= false
121
128
end
122
129
end
123
130
optim_f = Optim. TwiceDifferentiable (_loss, gg, fg!, hh, prob. u0)
124
131
end
125
132
126
- opt_args = _map_optimizer_args (prob, opt, cb= _cb, maxiters= maxiters, maxtime= maxtime,abstol= abstol, reltol= reltol; kwargs... )
133
+ opt_args = __map_optimizer_args (prob, opt, cb= _cb, maxiters= maxiters, maxtime= maxtime, abstol= abstol, reltol= reltol; kwargs... )
127
134
128
135
t0 = time ()
129
136
opt_res = Optim. optimize (optim_f, prob. u0, opt, opt_args)
130
137
t1 = time ()
131
138
opt_ret = Symbol (Optim. converged (opt_res))
132
139
133
- SciMLBase. build_solution (prob, opt, opt_res. minimizer, prob. sense === MaxSense ? - opt_res. minimum : opt_res. minimum; original= opt_res, retcode= opt_ret)
140
+ SciMLBase. build_solution (prob, opt, opt_res. minimizer, prob. sense === GalacticOptim . MaxSense ? - opt_res. minimum : opt_res. minimum; original= opt_res, retcode= opt_ret)
134
141
end
135
142
136
143
function ___solve (prob:: OptimizationProblem , opt:: Union{Optim.Fminbox,Optim.SAMIN} ,
137
- data = DEFAULT_DATA;
138
- cb = (args... ) -> (false ),
139
- maxiters:: Union{Number, Nothing} = nothing ,
140
- maxtime:: Union{Number, Nothing} = nothing ,
141
- abstol:: Union{Number, Nothing} = nothing ,
142
- reltol:: Union{Number, Nothing} = nothing ,
143
- progress = false ,
144
- kwargs... )
144
+ data= GalacticOptim . DEFAULT_DATA;
145
+ cb = (args... ) -> (false ),
146
+ maxiters:: Union{Number,Nothing} = nothing ,
147
+ maxtime:: Union{Number,Nothing} = nothing ,
148
+ abstol:: Union{Number,Nothing} = nothing ,
149
+ reltol:: Union{Number,Nothing} = nothing ,
150
+ progress= false ,
151
+ kwargs... )
145
152
146
153
local x, cur, state
147
154
148
- if data != DEFAULT_DATA
155
+ if data != GalacticOptim . DEFAULT_DATA
149
156
maxiters = length (data)
150
157
end
151
158
152
159
cur, state = iterate (data)
153
160
154
161
function _cb (trace)
155
- cb_call = ! (opt isa Optim. SAMIN) && opt. method == Optim. NelderMead () ? cb (decompose_trace (trace). metadata[" centroid" ],x... ) : cb (decompose_trace (trace). metadata[" x" ],x... )
162
+ cb_call = ! (opt isa Optim. SAMIN) && opt. method == Optim. NelderMead () ? cb (decompose_trace (trace). metadata[" centroid" ], x... ) : cb (decompose_trace (trace). metadata[" x" ], x... )
156
163
if ! (typeof (cb_call) <: Bool )
157
164
error (" The callback should return a boolean `halt` for whether to stop the optimization process." )
158
165
end
159
166
cur, state = iterate (data, state)
160
167
cb_call
161
168
end
162
169
163
- maxiters = _check_and_convert_maxiters (maxiters)
164
- maxtime = _check_and_convert_maxtime (maxtime)
170
+ maxiters = GalacticOptim . _check_and_convert_maxiters (maxiters)
171
+ maxtime = GalacticOptim . _check_and_convert_maxtime (maxtime)
165
172
166
- f = instantiate_function (prob. f,prob. u0,prob. f. adtype,prob. p)
173
+ f = GalacticOptim . instantiate_function (prob. f, prob. u0, prob. f. adtype, prob. p)
167
174
168
175
! (opt isa Optim. ZerothOrderOptimizer) && f. grad === nothing && error (" Use OptimizationFunction to pass the derivatives or automatically generate them with one of the autodiff backends" )
169
176
170
- _loss = function (θ)
177
+ _loss = function (θ)
171
178
x = f. f (θ, prob. p, cur... )
172
179
__x = first (x)
173
- return prob. sense === MaxSense ? - __x : __x
180
+ return prob. sense === GalacticOptim . MaxSense ? - __x : __x
174
181
end
175
- fg! = function (G,θ)
182
+ fg! = function (G, θ)
176
183
if G != = nothing
177
184
f. grad (G, θ, cur... )
178
- if prob. sense === MaxSense
185
+ if prob. sense === GalacticOptim . MaxSense
179
186
G .*= false
180
187
end
181
188
end
182
189
return _loss (θ)
183
190
end
184
-
191
+
185
192
gg = function (G, θ)
186
193
f. grad (G, θ, cur... )
187
- if prob. sense === MaxSense
194
+ if prob. sense === GalacticOptim . MaxSense
188
195
G .*= false
189
196
end
190
197
end
191
198
optim_f = Optim. OnceDifferentiable (_loss, gg, fg!, prob. u0)
192
199
193
- opt_args = _map_optimizer_args (prob, opt, cb= _cb, maxiters= maxiters, maxtime= maxtime,abstol= abstol, reltol= reltol; kwargs... )
200
+ opt_args = __map_optimizer_args (prob, opt, cb= _cb, maxiters= maxiters, maxtime= maxtime, abstol= abstol, reltol= reltol; kwargs... )
194
201
195
202
t0 = time ()
196
203
opt_res = Optim. optimize (optim_f, prob. lb, prob. ub, prob. u0, opt, opt_args)
@@ -202,87 +209,87 @@ end
202
209
203
210
204
211
function ___solve (prob:: OptimizationProblem , opt:: Optim.ConstrainedOptimizer ,
205
- data = DEFAULT_DATA;
206
- cb = (args... ) -> (false ),
207
- maxiters:: Union{Number, Nothing} = nothing ,
208
- maxtime:: Union{Number, Nothing} = nothing ,
209
- abstol:: Union{Number, Nothing} = nothing ,
210
- reltol:: Union{Number, Nothing} = nothing ,
211
- progress = false ,
212
- kwargs... )
212
+ data= GalacticOptim . DEFAULT_DATA;
213
+ cb = (args... ) -> (false ),
214
+ maxiters:: Union{Number,Nothing} = nothing ,
215
+ maxtime:: Union{Number,Nothing} = nothing ,
216
+ abstol:: Union{Number,Nothing} = nothing ,
217
+ reltol:: Union{Number,Nothing} = nothing ,
218
+ progress= false ,
219
+ kwargs... )
213
220
214
221
local x, cur, state
215
222
216
- if data != DEFAULT_DATA
223
+ if data != GalacticOptim . DEFAULT_DATA
217
224
maxiters = length (data)
218
225
end
219
226
220
227
cur, state = iterate (data)
221
228
222
- function _cb (trace)
223
- cb_call = cb (decompose_trace (trace). metadata[" x" ],x... )
224
- if ! (typeof (cb_call) <: Bool )
225
- error (" The callback should return a boolean `halt` for whether to stop the optimization process." )
226
- end
227
- cur, state = iterate (data, state)
228
- cb_call
229
+ function _cb (trace)
230
+ cb_call = cb (decompose_trace (trace). metadata[" x" ], x... )
231
+ if ! (typeof (cb_call) <: Bool )
232
+ error (" The callback should return a boolean `halt` for whether to stop the optimization process." )
233
+ end
234
+ cur, state = iterate (data, state)
235
+ cb_call
229
236
end
230
237
231
- maxiters = _check_and_convert_maxiters (maxiters)
232
- maxtime = _check_and_convert_maxtime (maxtime)
238
+ maxiters = GalacticOptim . _check_and_convert_maxiters (maxiters)
239
+ maxtime = GalacticOptim . _check_and_convert_maxtime (maxtime)
233
240
234
- f = instantiate_function (prob. f,prob. u0,prob. f. adtype,prob. p,prob. ucons === nothing ? 0 : length (prob. ucons))
241
+ f = GalacticOptim . instantiate_function (prob. f, prob. u0, prob. f. adtype, prob. p, prob. ucons === nothing ? 0 : length (prob. ucons))
235
242
236
- f. cons_j === nothing && error (" This optimizer requires derivative definitions for nonlinear constraints. If the problem does not have nonlinear constraints, choose a different optimizer. Otherwise define the derivative for cons using OptimizationFunction either directly or automatically generate them with one of the autodiff backends" )
243
+ f. cons_j === nothing && error (" This optimizer requires derivative definitions for nonlinear constraints. If the problem does not have nonlinear constraints, choose a different optimizer. Otherwise define the derivative for cons using OptimizationFunction either directly or automatically generate them with one of the autodiff backends" )
237
244
238
- _loss = function (θ)
245
+ _loss = function (θ)
239
246
x = f. f (θ, prob. p, cur... )
240
247
__x = first (x)
241
- return prob. sense === MaxSense ? - __x : __x
248
+ return prob. sense === GalacticOptim . MaxSense ? - __x : __x
242
249
end
243
- fg! = function (G,θ)
250
+ fg! = function (G, θ)
244
251
if G != = nothing
245
252
f. grad (G, θ, cur... )
246
- if prob. sense === MaxSense
253
+ if prob. sense === GalacticOptim . MaxSense
247
254
G .*= false
248
255
end
249
256
end
250
257
return _loss (θ)
251
258
end
252
259
gg = function (G, θ)
253
260
f. grad (G, θ, cur... )
254
- if prob. sense === MaxSense
261
+ if prob. sense === GalacticOptim . MaxSense
255
262
G .*= false
256
263
end
257
264
end
258
-
259
- hh = function (H,θ)
260
- f. hess (H,θ, cur... )
261
- if prob. sense === MaxSense
265
+
266
+ hh = function (H, θ)
267
+ f. hess (H, θ, cur... )
268
+ if prob. sense === GalacticOptim . MaxSense
262
269
H .*= false
263
270
end
264
271
end
265
272
optim_f = Optim. TwiceDifferentiable (_loss, gg, fg!, hh, prob. u0)
266
273
267
- cons! = (res, θ) -> res .= f. cons (θ);
274
+ cons! = (res, θ) -> res .= f. cons (θ)
268
275
269
- cons_j! = function (J, x)
276
+ cons_j! = function (J, x)
270
277
f. cons_j (J, x)
271
278
end
272
279
273
280
cons_hl! = function (h, θ, λ)
274
281
res = [similar (h) for i in 1 : length (λ)]
275
282
f. cons_h (res, θ)
276
283
for i in 1 : length (λ)
277
- h .+ = λ[i]* res[i]
284
+ h .+ = λ[i] * res[i]
278
285
end
279
286
end
280
287
281
288
lb = prob. lb === nothing ? [] : prob. lb
282
289
ub = prob. ub === nothing ? [] : prob. ub
283
290
optim_fc = Optim. TwiceDifferentiableConstraints (cons!, cons_j!, cons_hl!, lb, ub, prob. lcons, prob. ucons)
284
291
285
- opt_args = _map_optimizer_args (prob, opt, cb= _cb, maxiters= maxiters, maxtime= maxtime,abstol= abstol, reltol= reltol; kwargs... )
292
+ opt_args = __map_optimizer_args (prob, opt, cb= _cb, maxiters= maxiters, maxtime= maxtime, abstol= abstol, reltol= reltol; kwargs... )
286
293
287
294
t0 = time ()
288
295
opt_res = Optim. optimize (optim_f, optim_fc, prob. u0, opt, opt_args)
@@ -293,4 +300,4 @@ function ___solve(prob::OptimizationProblem, opt::Optim.ConstrainedOptimizer,
293
300
end
294
301
295
302
296
-
303
+ end
0 commit comments