Skip to content

Commit cc594a8

Browse files
committed
Don't use at-eval
1 parent b10f27e commit cc594a8

File tree

2 files changed

+34
-37
lines changed

2 files changed

+34
-37
lines changed

src/multivariate/optimize/interface.jl

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,91 +53,95 @@ promote_objtype(method::ZerothOrderOptimizer, x, autodiff::Symbol, inplace::Bool
5353
promote_objtype(method::FirstOrderOptimizer, x, autodiff::Symbol, inplace::Bool, td::TwiceDifferentiable) = td
5454
promote_objtype(method::SecondOrderOptimizer, x, autodiff::Symbol, inplace::Bool, td::TwiceDifferentiable) = td
5555

56-
for optimize in [:optimize, :optimizing]
57-
@eval begin
58-
5956
# if no method or options are present
60-
function $optimize(f, initial_x::AbstractArray; inplace = true, autodiff = :finite, kwargs...)
57+
function optimizing(f, initial_x::AbstractArray; inplace = true, autodiff = :finite, kwargs...)
6158
method = fallback_method(f)
6259
checked_kwargs, method = check_kwargs(kwargs, method)
6360
d = promote_objtype(method, initial_x, autodiff, inplace, f)
6461
add_default_opts!(checked_kwargs, method)
6562

6663
options = Options(; checked_kwargs...)
67-
$optimize(d, initial_x, method, options)
64+
optimizing(d, initial_x, method, options)
6865
end
69-
function $optimize(f, g, initial_x::AbstractArray; inplace = true, autodiff = :finite, kwargs...)
66+
function optimizing(f, g, initial_x::AbstractArray; inplace = true, autodiff = :finite, kwargs...)
7067

7168
method = fallback_method(f, g)
7269
checked_kwargs, method = check_kwargs(kwargs, method)
7370
d = promote_objtype(method, initial_x, autodiff, inplace, f, g)
7471
add_default_opts!(checked_kwargs, method)
7572

7673
options = Options(; checked_kwargs...)
77-
$optimize(d, initial_x, method, options)
74+
optimizing(d, initial_x, method, options)
7875
end
79-
function $optimize(f, g, h, initial_x::AbstractArray; inplace = true, autodiff = :finite, kwargs...)
76+
function optimizing(f, g, h, initial_x::AbstractArray; inplace = true, autodiff = :finite, kwargs...)
8077

8178
method = fallback_method(f, g, h)
8279
checked_kwargs, method = check_kwargs(kwargs, method)
8380
d = promote_objtype(method, initial_x, autodiff, inplace, f, g, h)
8481
add_default_opts!(checked_kwargs, method)
8582

8683
options = Options(; checked_kwargs...)
87-
$optimize(d, initial_x, method, options)
84+
optimizing(d, initial_x, method, options)
8885
end
8986

9087
# no method supplied with objective
91-
function $optimize(d::T, initial_x::AbstractArray, options::Options) where T<:AbstractObjective
92-
$optimize(d, initial_x, fallback_method(d), options)
88+
function optimizing(d::T, initial_x::AbstractArray, options::Options) where T<:AbstractObjective
89+
optimizing(d, initial_x, fallback_method(d), options)
9390
end
9491
# no method supplied with inplace and autodiff keywords becauase objective is not supplied
95-
function $optimize(f, initial_x::AbstractArray, options::Options; inplace = true, autodiff = :finite)
92+
function optimizing(f, initial_x::AbstractArray, options::Options; inplace = true, autodiff = :finite)
9693
method = fallback_method(f)
9794
d = promote_objtype(method, initial_x, autodiff, inplace, f)
98-
$optimize(d, initial_x, method, options)
95+
optimizing(d, initial_x, method, options)
9996
end
100-
function $optimize(f, g, initial_x::AbstractArray, options::Options; inplace = true, autodiff = :finite)
97+
function optimizing(f, g, initial_x::AbstractArray, options::Options; inplace = true, autodiff = :finite)
10198

10299
method = fallback_method(f, g)
103100
d = promote_objtype(method, initial_x, autodiff, inplace, f, g)
104-
$optimize(d, initial_x, method, options)
101+
optimizing(d, initial_x, method, options)
105102
end
106-
function $optimize(f, g, h, initial_x::AbstractArray{T}, options::Options; inplace = true, autodiff = :finite) where {T}
103+
function optimizing(f, g, h, initial_x::AbstractArray{T}, options::Options; inplace = true, autodiff = :finite) where {T}
107104

108105
method = fallback_method(f, g, h)
109106
d = promote_objtype(method, initial_x, autodiff, inplace, f, g, h)
110107

111-
$optimize(d, initial_x, method, options)
108+
optimizing(d, initial_x, method, options)
112109
end
113110

114111
# potentially everything is supplied (besides caches)
115-
function $optimize(f, initial_x::AbstractArray, method::AbstractOptimizer,
112+
function optimizing(f, initial_x::AbstractArray, method::AbstractOptimizer,
116113
options::Options = Options(;default_options(method)...); inplace = true, autodiff = :finite)
117114

118115
d = promote_objtype(method, initial_x, autodiff, inplace, f)
119-
$optimize(d, initial_x, method, options)
116+
optimizing(d, initial_x, method, options)
120117
end
121-
function $optimize(f, g, initial_x::AbstractArray, method::AbstractOptimizer,
118+
function optimizing(f, g, initial_x::AbstractArray, method::AbstractOptimizer,
122119
options::Options = Options(;default_options(method)...); inplace = true, autodiff = :finite)
123120

124121
d = promote_objtype(method, initial_x, autodiff, inplace, f, g)
125122

126-
$optimize(d, initial_x, method, options)
123+
optimizing(d, initial_x, method, options)
127124
end
128-
function $optimize(f, g, h, initial_x::AbstractArray{T}, method::AbstractOptimizer,
125+
function optimizing(f, g, h, initial_x::AbstractArray{T}, method::AbstractOptimizer,
129126
options::Options = Options(;default_options(method)...); inplace = true, autodiff = :finite) where T
130127

131128
d = promote_objtype(method, initial_x, autodiff, inplace, f, g, h)
132129

133-
$optimize(d, initial_x, method, options)
130+
optimizing(d, initial_x, method, options)
134131
end
135132

136-
function $optimize(d::D, initial_x::AbstractArray, method::SecondOrderOptimizer,
133+
function optimizing(d::D, initial_x::AbstractArray, method::SecondOrderOptimizer,
137134
options::Options = Options(;default_options(method)...); autodiff = :finite, inplace = true) where {D <: Union{NonDifferentiable, OnceDifferentiable}}
138135
d = promote_objtype(method, initial_x, autodiff, inplace, d)
139-
$optimize(d, initial_x, method, options)
136+
optimizing(d, initial_x, method, options)
140137
end
141138

142-
end # eval
143-
end # for
139+
function optimize(args...; kwargs...)
140+
local istate
141+
for istate′ in optimizing(args...; kwargs...)
142+
istate = istate′
143+
end
144+
# We can safely assume that `istate` is defined at this point. That is to say,
145+
# `OptimIterator` guarantees that `iterate(::OptimIterator) !== nothing`.
146+
return OptimizationResults(istate)
147+
end

src/multivariate/optimize/optimize.jl

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ function Base.iterate(iter::OptimIterator, istate = nothing)
8383
options.show_trace && print_header(method)
8484
trace!(tr, d, state, iteration, method, options, time()-t0)
8585
ls_success::Bool = true
86+
87+
# Note: `optimize` depends on that first iteration always yields something
88+
# (i.e., `iterate` does _not_ return a `nothing` when `istate === nothing`).
8689
else
8790
@unpack_IteratorState istate
8891

@@ -197,13 +200,3 @@ function optimizing(d::D, initial_x::Tx, method::M,
197200
end
198201
return OptimIterator(d, initial_x, method, options, state)
199202
end
200-
201-
function optimize(d::D, initial_x::Tx, method::M,
202-
options::Options = Options(;default_options(method)...),
203-
state = initial_state(method, options, d, initial_x)) where {D<:AbstractObjective, M<:AbstractOptimizer, Tx <: AbstractArray}
204-
local istate
205-
for istate′ in optimizing(d, initial_x, method, options, state)
206-
istate = istate′
207-
end
208-
return OptimizationResults(istate)
209-
end

0 commit comments

Comments
 (0)