22
22
```julia
23
23
SAMIN(; nt::Int = 5 # reduce temperature every nt*ns*dim(x_init) evaluations
24
24
ns::Int = 5 # adjust bounds every ns*dim(x_init) evaluations
25
+ t0::T = 2.0 # Initial temperature
25
26
rt::T = 0.9 # geometric temperature reduction factor: when temp changes, new temp is t=rt*t
27
+ r_expand::T = 10.0 # geometric temperature promotion factor for situation with low coverage: when temp changes, new temp is t=r_expand*t
28
+ bounds_ratio::T = 0.6 # cut-off for bounds increment (1-bounds_ratio for decrease)
26
29
neps::Int = 5 # number of previous best values the final result is compared to
27
- f_tol::T = 1e-12 # the required tolerance level for function value comparisons
28
- x_tol::T = 1e-6 # the required tolerance level for x
29
30
coverage_ok::Bool = false, # if false, increase temperature until initial parameter space is covered
30
31
verbosity::Int = 1) # scalar: 0, 1, 2 or 3 (default = 1).
31
32
```
@@ -41,12 +42,13 @@ algorithm
41
42
@with_kw struct SAMIN{T}<: AbstractConstrainedOptimizer
42
43
nt:: Int = 5 # reduce temperature every nt*ns*dim(x_init) evaluations
43
44
ns:: Int = 5 # adjust bounds every ns*dim(x_init) evaluations
45
+ t0:: T = 2.0 # Initial temperature
44
46
rt:: T = 0.9 # geometric temperature reduction factor: when temp changes, new temp is t=rt*t
47
+ r_expand:: T = 10.0 # geometric temperature promotion factor for situation with low coverage: when temp changes, new temp is t=r_expand*t
48
+ bounds_ratio:: T = 0.6 # cut-off for bounds increment (1-bounds_ratio for decrease)
45
49
neps:: Int = 5 # number of previous best values the final result is compared to
46
- f_tol:: T = 1e-12 # the required tolerance level for function value comparisons
47
- x_tol:: T = 1e-6 # the required tolerance level for x
48
50
coverage_ok:: Bool = false # if false, increase temperature until initial parameter space is covered
49
- verbosity:: Int = 1 # scalar: 0, 1, 2 or 3 (default = 1: see final results).
51
+ verbosity:: Int = 0 # scalar: 0, 1, 2 or 3 (default = 1: see final results).
50
52
end
51
53
# * verbosity: scalar: 0, 1, 2 or 3 (default = 1).
52
54
# * 0 = no screen output
@@ -58,22 +60,24 @@ Base.summary(::SAMIN) = "SAMIN"
58
60
59
61
function optimize (obj_fn, lb:: AbstractArray , ub:: AbstractArray , x:: AbstractArray{Tx} , method:: SAMIN , options:: Options = Options ()) where Tx
60
62
61
- t0 = time () # Initial time stamp used to control early stopping by options.time_limit
63
+ time0 = time () # Initial time stamp used to control early stopping by options.time_limit
62
64
63
65
hline = " =" ^ 80
64
66
d = NonDifferentiable (obj_fn, x)
65
67
66
68
tr = OptimizationTrace {typeof(value(d)), typeof(method)} ()
67
69
tracing = options. store_trace || options. show_trace || options. extended_trace || options. callback != = nothing
68
70
69
- @unpack nt, ns, rt, neps, f_tol, x_tol , coverage_ok, verbosity = method
71
+ @unpack nt, ns, t0, rt, r_expand, bounds_ratio, neps , coverage_ok, verbosity = method
70
72
verbose = verbosity > 0
71
73
74
+ x_tol, f_tol = options. f_abstol, options. x_abstol
75
+
72
76
x0 = copy (x)
73
77
n = size (x,1 ) # dimension of parameter
74
78
# Set initial values
75
79
nacc = 0 # total accepted trials
76
- t = 2.0 # temperature - will initially rise or fall to cover parameter space. Then it will fall
80
+ t = t0 # temperature - will initially rise or fall to cover parameter space. Then it will fall
77
81
converge = 0 # convergence indicator 0 (failure), 1 (normal success), or 2 (convergence but near bounds)
78
82
x_converged = false
79
83
f_converged = false
@@ -96,7 +100,7 @@ function optimize(obj_fn, lb::AbstractArray, ub::AbstractArray, x::AbstractArray
96
100
options. show_trace && print_header (method)
97
101
iteration = 0
98
102
_time = time ()
99
- trace! (tr, d, (x= xopt, iteration= iteration), iteration, method, options, _time- t0 )
103
+ trace! (tr, d, (x= xopt, iteration= iteration), iteration, method, options, _time- time0 )
100
104
stopped_by_callback = false
101
105
# main loop, first increase temperature until parameter space covered, then reduce until convergence
102
106
while converge== 0
@@ -160,12 +164,12 @@ function optimize(obj_fn, lb::AbstractArray, ub::AbstractArray, x::AbstractArray
160
164
161
165
if tracing
162
166
# update trace; callbacks can stop routine early by returning true
163
- stopped_by_callback = trace! (tr, d, (x= xopt,iteration= iteration), iteration, method, options, time ()- t0 )
167
+ stopped_by_callback = trace! (tr, d, (x= xopt,iteration= iteration), iteration, method, options, time ()- time0 )
164
168
end
165
169
166
170
# If options.iterations exceeded, terminate the algorithm
167
171
_time = time ()
168
- if f_calls (d) >= options. iterations || _time- t0 > options. time_limit || stopped_by_callback
172
+ if f_calls (d) >= options. iterations || _time- time0 > options. time_limit || stopped_by_callback
169
173
170
174
if verbose
171
175
println (hline)
@@ -206,7 +210,7 @@ function optimize(obj_fn, lb::AbstractArray, ub::AbstractArray, x::AbstractArray
206
210
h_calls (d),
207
211
true ,
208
212
options. time_limit,
209
- _time- t0 ,NamedTuple ())
213
+ _time- time0 ,NamedTuple ())
210
214
end
211
215
end
212
216
end
@@ -215,8 +219,8 @@ function optimize(obj_fn, lb::AbstractArray, ub::AbstractArray, x::AbstractArray
215
219
for i = 1 : n
216
220
if (lb[i] != ub[i])
217
221
ratio = nacp[i] / ns
218
- if (ratio > 0.6 ) bounds[i] = bounds[i] * (1.0 + 2.0 * (ratio - 0.6 ) / 0.4 ) end
219
- if (ratio < . 4 ) bounds[i] = bounds[i] / (1.0 + 2.0 * ((0.4 - ratio) / 0.4 )) end
222
+ if (ratio > bounds_ratio ) bounds[i] = bounds[i] * (1.0 + 2.0 * (ratio - 0.6 ) / 0.4 ) end
223
+ if (ratio < 1 - bounds_ratio ) bounds[i] = bounds[i] / (1.0 + 2.0 * ((0.4 - ratio) / 0.4 )) end
220
224
# keep within initial bounds
221
225
if (bounds[i] > (ub[i] - lb[i]))
222
226
bounds[i] = ub[i] - lb[i]
@@ -309,7 +313,7 @@ function optimize(obj_fn, lb::AbstractArray, ub::AbstractArray, x::AbstractArray
309
313
f_old = copy (fopt)
310
314
x = copy (xopt)
311
315
else # coverage not ok - increase temperature quickly to expand search area
312
- t *= 10.0
316
+ t *= r_expand
313
317
for i = neps: - 1 : 2
314
318
fstar[i] = fstar[i- 1 ]
315
319
end
@@ -343,7 +347,7 @@ function optimize(obj_fn, lb::AbstractArray, ub::AbstractArray, x::AbstractArray
343
347
h_calls (d),
344
348
true ,
345
349
options. time_limit,
346
- _time- t0 ,
350
+ _time- time0 ,
347
351
NamedTuple ())
348
352
349
353
end
0 commit comments