Skip to content

Commit bec2590

Browse files
christianhauschelavik-pal
authored andcommitted
added time_limit for univariate optimization (JuliaNLSolvers#1092)
* added time_limit for univariate optimization * time_limit tests for univariate solvers
1 parent 45be0e0 commit bec2590

File tree

7 files changed

+48
-7
lines changed

7 files changed

+48
-7
lines changed

src/api.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,8 @@ abs_tol(r::UnivariateOptimizationResults) = r.abs_tol
134134
time_limit(r::MultivariateOptimizationResults) = r.time_limit
135135
time_run( r::MultivariateOptimizationResults) = r.time_run
136136

137+
time_limit(r::UnivariateOptimizationResults) = r.time_limit
138+
time_run( r::UnivariateOptimizationResults) = r.time_run
139+
137140
time_limit(r::OptimizationResults) = error("time_limit is not implemented for $(summary(r)).")
138141
time_run( r::OptimizationResults) = error("time_run is not implemented for $(summary(r)).")

src/univariate/printing.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,16 @@ function Base.show(io::IO, t::OptimizationState{<:Real, GoldenSection})
3939
end
4040

4141
function Base.show(io::IO, r::UnivariateOptimizationResults)
42+
43+
status_string = ""
44+
if time_run(r) > time_limit(r)
45+
status_string *= "failure (exceeded time limit of $(time_limit(r)))"
46+
else
47+
status_string = "success"
48+
end
49+
4250
@printf io "Results of Optimization Algorithm\n"
51+
@printf io " * Status: %s\n" status_string
4352
@printf io " * Algorithm: %s\n" summary(r)
4453
@printf io " * Search Interval: [%f, %f]\n" lower_bound(r) upper_bound(r)
4554
@printf io " * Minimizer: %e\n" minimizer(r)

src/univariate/solvers/brent.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ function optimize(
2626
rel_tol::T = sqrt(eps(T)),
2727
abs_tol::T = eps(T),
2828
iterations::Integer = 1_000,
29+
time_limit::Float64 = Inf,
2930
store_trace::Bool = false,
3031
show_trace::Bool = false,
3132
show_warnings::Bool = true,
3233
callback = nothing,
3334
show_every = 1,
3435
extended_trace::Bool = false) where T <: AbstractFloat
3536
t0 = time()
36-
options = (store_trace=store_trace, show_trace=show_trace, show_warnings=show_warnings, show_every=show_every, callback=callback)
37+
options = (store_trace=store_trace, show_trace=show_trace, show_warnings=show_warnings, show_every=show_every, callback=callback, time_limit=time_limit)
3738
if x_lower > x_upper
3839
error("x_lower must be less than x_upper")
3940
end
@@ -73,8 +74,8 @@ function optimize(
7374
new_minimum=new_minimum)
7475
stopped_by_callback = trace!(tr, nothing, state, iteration, mo, options, time()-t0)
7576
end
76-
77-
while iteration < iterations && !stopped_by_callback
77+
_time = time() - t0
78+
while iteration < iterations && !stopped_by_callback && _time < time_limit
7879

7980
p = zero(T)
8081
q = zero(T)
@@ -170,6 +171,7 @@ function optimize(
170171
new_minimum=new_minimum)
171172
stopped_by_callback = trace!(tr, nothing, state, iteration, mo, options, time()-t0)
172173
end
174+
_time = time() - t0
173175
end
174176

175177
return UnivariateOptimizationResults(mo,
@@ -183,7 +185,10 @@ function optimize(
183185
rel_tol,
184186
abs_tol,
185187
tr,
186-
f_calls)
188+
f_calls,
189+
time_limit,
190+
_time,
191+
)
187192
end
188193

189194

src/univariate/solvers/golden_section.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function optimize(f, x_lower::T, x_upper::T,
2323
rel_tol::T = sqrt(eps(T)),
2424
abs_tol::T = eps(T),
2525
iterations::Integer = 1_000,
26+
time_limit::Float64 = Inf,
2627
store_trace::Bool = false,
2728
show_trace::Bool = false,
2829
show_warnings::Bool = true,
@@ -34,7 +35,7 @@ function optimize(f, x_lower::T, x_upper::T,
3435
error("x_lower must be less than x_upper")
3536
end
3637
t0 = time()
37-
options = (store_trace=store_trace, show_trace=show_trace, show_warnings=show_warnings, show_every=show_every, callback=callback)
38+
options = (store_trace=store_trace, show_trace=show_trace, show_warnings=show_warnings, show_every=show_every, callback=callback, time_limit=time_limit)
3839
# Save for later
3940
initial_lower = x_lower
4041
initial_upper = x_upper
@@ -63,7 +64,9 @@ function optimize(f, x_lower::T, x_upper::T,
6364
stopped_by_callback = trace!(tr, nothing, state, iteration, mo, options, time()-t0)
6465
end
6566

66-
while iteration < iterations && !stopped_by_callback
67+
_time = time() - t0
68+
69+
while iteration < iterations && !stopped_by_callback && _time < time_limit
6770

6871
x_tol = rel_tol * abs(new_minimizer) + abs_tol
6972

@@ -113,6 +116,7 @@ function optimize(f, x_lower::T, x_upper::T,
113116
new_minimum=new_minimum)
114117
stopped_by_callback = trace!(tr, nothing, state, iteration, mo, options, time()-t0)
115118
end
119+
_time = time() - t0
116120
end
117121

118122
return UnivariateOptimizationResults(mo,
@@ -126,7 +130,9 @@ function optimize(f, x_lower::T, x_upper::T,
126130
rel_tol,
127131
abs_tol,
128132
tr,
129-
f_calls)
133+
f_calls,
134+
time_limit,
135+
_time)
130136
end
131137

132138

src/univariate/types.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ mutable struct UnivariateOptimizationResults{Tb,Tt,Tf, Tx,M,O<:UnivariateOptimiz
1111
abs_tol::Tt
1212
trace::OptimizationTrace{M}
1313
f_calls::Int
14+
time_limit::Float64
15+
time_run::Float64
1416
end

test/univariate/solvers/brent.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,13 @@
2727
@test Optim.converged(result)
2828
@test Optim.minimum(result) == -1.0
2929

30+
## time limit
31+
function slow_obj(x)
32+
sleep(0.05)
33+
return sin(x)
34+
end
35+
result = optimize(x -> slow_obj(x), 0, 2π, Brent(); time_limit=0.01)
36+
@test result.f_calls == 1
37+
3038
result = Optim.optimize(x->sin(x), 0, 2π, Optim.Brent(); abs_tol=1e-4, store_trace=false, show_trace=true, iterations=2)
3139
end

test/univariate/solvers/golden_section.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,13 @@
1616
@test Optim.minimum(result) == 2.0
1717
@test_throws ErrorException optimize(identity, 2.0, 1.0, GoldenSection())
1818

19+
## time limit
20+
function slow_obj(x)
21+
sleep(0.05)
22+
return sin(x)
23+
end
24+
result = optimize(x -> slow_obj(x), 0, 2π, GoldenSection(); time_limit=0.01)
25+
@test result.f_calls == 1
26+
1927
result = Optim.optimize(x->sin(x), 0, 2π, Optim.GoldenSection(); abs_tol=1e-4, store_trace=false, show_trace=true, iterations=2)
2028
end

0 commit comments

Comments
 (0)