Skip to content

Commit 69a18bd

Browse files
authored
Remove kwargs for options (#1186)
* Remove kwargs for options * fix some dispatches * Update fminbox.jl * Update fminbox.jl * fix fminbox * debug newton * Update runtests.jl * Update runtests.jl * Update runtests.jl * Update runtests.jl * Update runtests.jl * Update runtests.jl * Update runtests.jl * Update runtests.jl * Update runtests.jl * Update runtests.jl * Update runtests.jl * Update runtests.jl * Update runtests.jl * Update runtests.jl * Update runtests.jl * does it fail if this doesn't run * Fix methods Fminbox vs IPNewton
1 parent 520dad3 commit 69a18bd

File tree

10 files changed

+150
-277
lines changed

10 files changed

+150
-277
lines changed

ext/OptimMOIExt.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,7 @@ function MOI.optimize!(model::Optimizer{T}) where {T}
336336
)
337337
else
338338
d = Optim.promote_objtype(method, initial_x, :finite, true, f, g!, h!)
339-
Optim.add_default_opts!(options, method)
340-
options = Optim.Options(; options...)
339+
options = Optim.Options(; Optim.default_options(method)..., options...)
341340
if nl_constrained || has_bounds
342341
if nl_constrained
343342
lc = [b.lower for b in nlp_data.constraint_bounds]

src/deprecate.jl

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +0,0 @@
1-
Base.@deprecate method(x) summary(x)
2-
3-
const has_deprecated_fminbox = Ref(false)
4-
function optimize(
5-
df::OnceDifferentiable,
6-
initial_x::Array{T},
7-
l::Array{T},
8-
u::Array{T},
9-
::Type{Fminbox};
10-
x_tol::T = eps(T),
11-
f_tol::T = sqrt(eps(T)),
12-
g_tol::T = sqrt(eps(T)),
13-
allow_f_increases::Bool = true,
14-
iterations::Integer = 1_000,
15-
store_trace::Bool = false,
16-
show_trace::Bool = false,
17-
extended_trace::Bool = false,
18-
show_warnings::Bool = true,
19-
callback = nothing,
20-
show_every::Integer = 1,
21-
linesearch = LineSearches.HagerZhang{T}(),
22-
eta::Real = convert(T, 0.4),
23-
mu0::T = convert(T, NaN),
24-
mufactor::T = convert(T, 0.001),
25-
precondprep = (P, x, l, u, mu) -> precondprepbox!(P, x, l, u, mu),
26-
optimizer = ConjugateGradient,
27-
optimizer_o = Options(
28-
store_trace = store_trace,
29-
show_trace = show_trace,
30-
extended_trace = extended_trace,
31-
show_warnings = show_warnings,
32-
),
33-
nargs...,
34-
) where {T<:AbstractFloat}
35-
if !has_deprecated_fminbox[]
36-
@warn(
37-
"Fminbox with the optimizer keyword is deprecated, construct Fminbox{optimizer}() and pass it to optimize(...) instead."
38-
)
39-
has_deprecated_fminbox[] = true
40-
end
41-
optimize(
42-
df,
43-
initial_x,
44-
l,
45-
u,
46-
Fminbox{optimizer}();
47-
allow_f_increases = allow_f_increases,
48-
iterations = iterations,
49-
store_trace = store_trace,
50-
show_trace = show_trace,
51-
extended_trace = extended_trace,
52-
show_warnings = show_warnings,
53-
show_every = show_every,
54-
callback = callback,
55-
linesearch = linesearch,
56-
eta = eta,
57-
mu0 = mu0,
58-
mufactor = mufactor,
59-
precondprep = precondprep,
60-
optimizer_o = optimizer_o,
61-
)
62-
end
63-
64-
function optimize(::AbstractObjective)
65-
throw(
66-
ErrorException(
67-
"Optimizing an objective `obj` without providing an initial `x` has been deprecated without backwards compatability. Please explicitly provide an `x`: `optimize(obj, x)``",
68-
),
69-
)
70-
end
71-
function optimize(::AbstractObjective, ::Method)
72-
throw(
73-
ErrorException(
74-
"Optimizing an objective `obj` without providing an initial `x` has been deprecated without backwards compatability. Please explicitly provide an `x`: `optimize(obj, x, method)``",
75-
),
76-
)
77-
end
78-
function optimize(::AbstractObjective, ::Method, ::Options)
79-
throw(
80-
ErrorException(
81-
"Optimizing an objective `obj` without providing an initial `x` has been deprecated without backwards compatability. Please explicitly provide an `x`: `optimize(obj, x, method, options)``",
82-
),
83-
)
84-
end
85-
function optimize(::AbstractObjective, ::Options)
86-
throw(
87-
ErrorException(
88-
"Optimizing an objective `obj` without providing an initial `x` has been deprecated without backwards compatability. Please explicitly provide an `x`: `optimize(obj, x, options)``",
89-
),
90-
)
91-
end
92-
93-
function optimize(
94-
df::OnceDifferentiable,
95-
l::Array{T},
96-
u::Array{T},
97-
F::Fminbox{O};
98-
kwargs...,
99-
) where {T<:AbstractFloat,O<:AbstractOptimizer}
100-
throw(
101-
ErrorException(
102-
"Optimizing an objective `obj` without providing an initial `x` has been deprecated without backwards compatability. Please explicitly provide an `x`: `optimize(obj, x, l, u, method, options)``",
103-
),
104-
)
105-
end

src/maximize.jl

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,33 @@ function maximize(f, lb::Real, ub::Real; kwargs...)
1818
MaximizationWrapper(optimize(fmax, lb, ub; kwargs...))
1919
end
2020

21+
2122
# ==============================================================================
2223
# Multivariate warppers
2324
# ==============================================================================
24-
function maximize(f, x0::AbstractArray; kwargs...)
25+
function maximize(f, x0::AbstractArray, options::Options = Options())
2526
fmax = x -> -f(x)
26-
MaximizationWrapper(optimize(fmax, x0; kwargs...))
27+
MaximizationWrapper(optimize(fmax, x0, options))
2728
end
2829
function maximize(
2930
f,
3031
x0::AbstractArray,
3132
method::AbstractOptimizer,
32-
options = Optim.Options();
33-
kwargs...,
33+
options = Optim.Options()
3434
)
3535
fmax = x -> -f(x)
36-
MaximizationWrapper(optimize(fmax, x0, method, options; kwargs...))
36+
MaximizationWrapper(optimize(fmax, x0, method, options))
3737
end
3838
function maximize(
3939
f,
4040
g,
4141
x0::AbstractArray,
4242
method::AbstractOptimizer,
43-
options = Optim.Options();
44-
kwargs...,
43+
options = Optim.Options()
4544
)
4645
fmax = x -> -f(x)
4746
gmax = (G, x) -> (g(G, x); G .= -G)
48-
MaximizationWrapper(optimize(fmax, gmax, x0, method, options; kwargs...))
47+
MaximizationWrapper(optimize(fmax, gmax, x0, method, options))
4948
end
5049

5150
function maximize(
@@ -54,13 +53,12 @@ function maximize(
5453
h,
5554
x0::AbstractArray,
5655
method::AbstractOptimizer,
57-
options = Optim.Options();
58-
kwargs...,
56+
options = Optim.Options()
5957
)
6058
fmax = x -> -f(x)
6159
gmax = (G, x) -> (g(G, x); G .= -G)
6260
hmax = (H, x) -> (h(H, x); H .= -H)
63-
MaximizationWrapper(optimize(fmax, gmax, hmax, x0, method, options; kwargs...))
61+
MaximizationWrapper(optimize(fmax, gmax, hmax, x0, method, options))
6462
end
6563

6664
minimum(r::MaximizationWrapper) = throw(MethodError())

src/multivariate/optimize/interface.jl

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,5 @@
1-
# Multivariate optimization
2-
function check_kwargs(kwargs, fallback_method)
3-
kws = Dict{Symbol,Any}()
4-
method = nothing
5-
for kwarg in kwargs
6-
if kwarg[1] != :method
7-
kws[kwarg[1]] = kwarg[2]
8-
else
9-
method = kwarg[2]
10-
end
11-
end
12-
13-
if method === nothing
14-
method = fallback_method
15-
end
16-
kws, method
17-
end
18-
191
default_options(method::AbstractOptimizer) = NamedTuple()
202

21-
function add_default_opts!(opts::Dict{Symbol,Any}, method::AbstractOptimizer)
22-
for newopt in pairs(default_options(method))
23-
if !haskey(opts, newopt[1])
24-
opts[newopt[1]] = newopt[2]
25-
end
26-
end
27-
end
28-
293
fallback_method(f) = NelderMead()
304
fallback_method(f, g!) = LBFGS()
315
fallback_method(f, g!, h!) = Newton()
@@ -164,32 +138,26 @@ function optimize(
164138
initial_x::AbstractArray;
165139
inplace = true,
166140
autodiff = :finite,
167-
kwargs...,
168141
)
169142
method = fallback_method(f)
170-
checked_kwargs, method = check_kwargs(kwargs, method)
171-
172143
d = promote_objtype(method, initial_x, autodiff, inplace, f)
173-
add_default_opts!(checked_kwargs, method)
174144

175-
options = Options(; checked_kwargs...)
145+
options = Options(; default_options(method)...)
176146
optimize(d, initial_x, method, options)
177147
end
178148
function optimize(
179149
f,
180150
g,
181151
initial_x::AbstractArray;
182-
inplace = true,
183152
autodiff = :finite,
184-
kwargs...,
153+
inplace = true,
185154
)
186155

187156
method = fallback_method(f, g)
188-
checked_kwargs, method = check_kwargs(kwargs, method)
189-
d = promote_objtype(method, initial_x, autodiff, inplace, f, g)
190-
add_default_opts!(checked_kwargs, method)
191157

192-
options = Options(; checked_kwargs...)
158+
d = promote_objtype(method, initial_x, autodiff, inplace, f, g)
159+
160+
options = Options(; default_options(method)...)
193161
optimize(d, initial_x, method, options)
194162
end
195163
function optimize(
@@ -198,16 +166,12 @@ function optimize(
198166
h,
199167
initial_x::AbstractArray;
200168
inplace = true,
201-
autodiff = :finite,
202-
kwargs...,
169+
autodiff = :finite
203170
)
204-
205171
method = fallback_method(f, g, h)
206-
checked_kwargs, method = check_kwargs(kwargs, method)
207172
d = promote_objtype(method, initial_x, autodiff, inplace, f, g, h)
208-
add_default_opts!(checked_kwargs, method)
209173

210-
options = Options(; checked_kwargs...)
174+
options = Options(; default_options(method)...)
211175
optimize(d, initial_x, method, options)
212176
end
213177

@@ -253,7 +217,6 @@ function optimize(
253217
inplace = true,
254218
autodiff = :finite,
255219
) where {T}
256-
257220
method = fallback_method(f, g, h)
258221
d = promote_objtype(method, initial_x, autodiff, inplace, f, g, h)
259222

@@ -269,7 +232,6 @@ function optimize(
269232
inplace = true,
270233
autodiff = :finite,
271234
)
272-
273235
d = promote_objtype(method, initial_x, autodiff, inplace, f)
274236
optimize(d, initial_x, method, options)
275237
end
@@ -295,7 +257,6 @@ function optimize(
295257
inplace = true,
296258
autodiff = :finite,
297259
)
298-
299260
d = promote_objtype(method, initial_x, autodiff, inplace, f, g)
300261

301262
optimize(d, initial_x, method, options)
@@ -304,13 +265,13 @@ function optimize(
304265
f,
305266
g,
306267
h,
307-
initial_x::AbstractArray{T},
268+
initial_x::AbstractArray,
308269
method::AbstractOptimizer,
309270
options::Options = Options(; default_options(method)...);
310271
inplace = true,
311272
autodiff = :finite,
312-
) where {T}
313-
273+
274+
)
314275
d = promote_objtype(method, initial_x, autodiff, inplace, f, g, h)
315276

316277
optimize(d, initial_x, method, options)
@@ -321,8 +282,8 @@ function optimize(
321282
initial_x::AbstractArray,
322283
method::SecondOrderOptimizer,
323284
options::Options = Options(; default_options(method)...);
324-
autodiff = :finite,
325285
inplace = true,
286+
autodiff = :finite,
326287
) where {D<:Union{NonDifferentiable,OnceDifferentiable}}
327288
d = promote_objtype(method, initial_x, autodiff, inplace, d)
328289
optimize(d, initial_x, method, options)

0 commit comments

Comments
 (0)