Skip to content

Commit 0b87119

Browse files
committed
use abstol & reltol options to setup convergence metrics tolerance
1 parent ba35327 commit 0b87119

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/api/optimize.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,23 @@ function optimize(objfun::D, constraints::C, method::M, population::AbstractArra
7777
converged, counter_tol = false, 0 # tolerance convergence
7878
is_moo = ismultiobjective(objfun)
7979

80+
# initialize convergence metrics
81+
# use abstol as a tolerance value for metrics
82+
if !isinf(options.abstol)
83+
for m in method.metrics
84+
m.tol = options.abstol
85+
end
86+
end
87+
# if reltol is set, try to add RelDiff metric
88+
if !(isinf(options.reltol) || is_moo)
89+
idx = findfirst(m->isa(m, RelDiff), method.metrics)
90+
if idx === nothing
91+
push!(method.metrics, RelDiff(options.reltol))
92+
else
93+
method.metrics[idx].tol = options.reltol
94+
end
95+
end
96+
8097
options.show_trace && print_header(method)
8198
trace!(tr, iteration, objfun, state, population, method, options, time()-t0)
8299

src/api/types.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ const ConvergenceMetrics = Vector{ConvergenceMetric}
9292
# Options
9393
"""
9494
There are following options available:
95-
- `abstol::Float64`: the absolute tolerance used in the convergence test (*deprecated, use `metrics` parameter of a particular optimization algorithm*)
96-
- `reltol::Float64`: the relative tolerance used in the convergence test (*deprecated, use `metrics` parameter of a particular optimization algorithm*)
95+
- `abstol::Float64`: the absolute tolerance used in the convergence test
96+
- `reltol::Float64`: the relative tolerance used in the convergence test
9797
- `successive_f_tol::Integer`: the additional number of the iterations of the optimization algorithm after the convergence test is satisfied (*default: 10*)
9898
- `iterations::Integer`: the total number of the iterations of the optimization algorithm (*default: 1000*)
9999
- `show_trace::Bool`: enable the trace information display during the optimization (*default: false*).

test/interface.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,5 +182,18 @@
182182
@test Evolutionary.iterations(res) == 7
183183
@test !Evolutionary.converged(res)
184184
@test length(Evolutionary.trace(res)) > 1
185+
@test length(res.metrics) == 1
186+
@test res.metrics[1].tol == 1e-10
187+
188+
o = Evolutionary.Options(abstol=1e-3)
189+
res = Evolutionary.optimize(sum, (()->BitVector(rand(Bool,dimension))), mthd, o)
190+
@test length(res.metrics) == 1
191+
@test res.metrics[1].tol == 1e-3
192+
193+
o = Evolutionary.Options(abstol=1e-3, reltol=1e-4)
194+
res = Evolutionary.optimize(sum, (()->BitVector(rand(Bool,dimension))), mthd, o)
195+
@test length(res.metrics) == 2
196+
@test res.metrics[1].tol == 1e-3
197+
@test res.metrics[2].tol == 1e-4
185198

186199
end

0 commit comments

Comments
 (0)