Content-Length: 351837 | pFad | http://github.com/wildart/Evolutionary.jl/commit/0955613292cac377d3ca9febc185bdf43cdd3bc3

52 added time limit, closes #64 · wildart/Evolutionary.jl@0955613 · GitHub
Skip to content

Commit

Permalink
added time limit, closes #64
Browse files Browse the repository at this point in the history
  • Loading branch information
wildart committed Mar 19, 2021
1 parent 287d1f2 commit 0955613
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/api/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function optimize(objfun::D, constraints::C, population::AbstractArray,
options.show_trace && print_header(method)
trace!(tr, iteration, objfun, state, population, method, options, time()-t0)

_time = time()
while !converged && !stopped && iteration < options.iterations
iteration += 1

Expand All @@ -86,10 +87,14 @@ function optimize(objfun::D, constraints::C, population::AbstractArray,
stopped_by_callback = trace!(tr, iteration, objfun, state, population, method, options, time()-t0)
end

if stopped_by_callback
_time = time()
stopped_by_time_limit = _time-t0 > options.time_limit

if stopped_by_callback || stopped_by_time_limit
stopped = true
end
end

after_while!(objfun, state, method, options)

return EvolutionaryOptimizationResults(
Expand All @@ -101,6 +106,8 @@ function optimize(objfun::D, constraints::C, population::AbstractArray,
converged,
options.abstol,
tr,
f_calls(objfun)
f_calls(objfun),
options.time_limit,
_time-t0,
)
end
18 changes: 17 additions & 1 deletion src/api/results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ f_calls(r::OptimizationResults) = r.f_calls
Returns an absolute tollerance value of the optimization `result`.
"""
tol(r::OptimizationResults) = error("tol is not implemented for $(summary(r)).")
tol(r::OptimizationResults) = error("`tol` is not implemented for $(summary(r)).")

time_limit(r::OptimizationResults) = r.time_limit
time_run( r::OptimizationResults) = r.time_run


"""
Evolutionary optimization result type
Expand All @@ -72,6 +76,8 @@ mutable struct EvolutionaryOptimizationResults{O<:AbstractOptimizer, T, Tx, Tf}
abstol::T
trace::OptimizationTrace
f_calls::Int
time_limit::Float64
time_run::Float64
end

"""
Expand All @@ -88,6 +94,9 @@ function show(io::IO, r::EvolutionaryOptimizationResults)
if iteration_limit_reached(r)
failure_string *= " (reached maximum number of iterations)"
end
if time_run(r) > time_limit(r)
failure_string *= " (exceeded time limit of $(time_limit(r)))"
end
print(io, "\n")
print(io, " * Status: ", converged(r) ? "success" : failure_string, "\n\n")
print(io, " * Candidate solution\n")
Expand All @@ -107,5 +116,12 @@ function show(io::IO, r::EvolutionaryOptimizationResults)
print(io, "\n")
print(io, " * Found with\n")
print(io, " Algorithm: $(summary(r))\n")
print(io, "\n")
print(io, " * Work counters\n")
tr = round(time_run(r); digits=4)
tl = isnan(time_limit(r)) ? Inf : round(time_limit(r); digits=4)
print(io, " Seconds run: $tr (vs limit $tl)\n")
print(io, " Iterations: $(iterations(r))\n")
print(io, " f(x) calls: $(f_calls(r))\n")
return
end
2 changes: 2 additions & 0 deletions src/api/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ store_trace::Bool = false
show_trace::Bool = false
show_every::Integer = 1
callback::TCallback = nothing
time_limit::Float64 = NaN
```
"""
@kwdef struct Options{TCallback <: Union{Nothing, Function}}
Expand All @@ -32,6 +33,7 @@ callback::TCallback = nothing
show_trace::Bool = false
show_every::Integer = 1
callback::TCallback = nothing
time_limit::Float64 = NaN
end
function show(io::IO, o::Options)
for k in fieldnames(typeof(o))
Expand Down
4 changes: 3 additions & 1 deletion test/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
res = Evolutionary.EvolutionaryOptimizationResults(
mthd, individual, value(objfun),
opts.iterations, opts.show_trace, opts.store_trace,
opts.abstol, tr, f_calls(objfun)
opts.abstol, tr, f_calls(objfun), 1.0, 1.0
);
@test summary(res) == summary(mthd)
@test Evolutionary.minimizer(res) == individual
Expand All @@ -104,6 +104,8 @@
@test Evolutionary.tol(res) == opts.abstol
@test Evolutionary.converged(res) == opts.store_trace
@test f_calls(res) == f_calls(objfun)
@test Evolutionary.time_run(res) == 1.0
@test Evolutionary.time_limit(res) == 1.0
@test length(Evolutionary.trace(res)) >= 1
show(IOBuffer(), res)

Expand Down

0 comments on commit 0955613

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/wildart/Evolutionary.jl/commit/0955613292cac377d3ca9febc185bdf43cdd3bc3

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy