/
Restart.jl
94 lines (62 loc) · 2.42 KB
/
Restart.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
abstract type AbstractRestart <: AbstractParameters end
struct Restart{T} <: AbstractRestart
alg::T
every::Int
end
"""
Restart(optimizer, every=100)
Resets the `optimizer` every specified number of iterations (100 by default).
### Example
```julia-repl
julia> f, bounds, _ = Metaheuristics.TestProblems.rastrigin();
julia> optimize(f, bounds, Restart(ECA(), every=200))
```
### Customization
The restart condition can be updated by overloading the `restart_condition` method:
```julia
function Metaheuristics.restart_condition(status, restart::Restart, information, options)
st.iteration % params.every == 0
end
```
"""
function Restart(base::Algorithm; every = 100)
parameters = Restart(base.parameters, every)
Algorithm(
parameters,
information = base.information,
options = base.options
)
end
restart_condition(st, params::Restart, args...) = st.iteration % params.every == 0
function restart_population!(st, params::AbstractRestart, problem, info, opts, args...; kargs...)
if !restart_condition(st, params, info, opts)
return
end
opts.debug && @info "Restarting population..."
# initialize population (not params.alg)
st_tmp = State(nothing, [])
st_new = initialize!(st_tmp, params.alg, problem, info, opts, args...; kargs...)
# current population is replaced by the new one
st.population = st_new.population
opts.debug && @info "Restarted population."
end
function update_state!(st, params::AbstractRestart, problem, info, opts, args...; kargs...)
# restart population if necessary
restart_population!(st, params, problem, info, opts, args...; kargs...)
# perform the optimization step at current population
update_state!(st, params.alg, problem, info, opts, args...; kargs...)
end
#########################################
# implement each step for AbstractRestart
#########################################
#
function initialize!(st, params::AbstractRestart, problem, info, opts, args...; kargs...)
initialize!(st, params.alg, problem, info, opts, args...; kargs...)
end
function final_stage!(st, params::AbstractRestart, problem, info, opts, args...; kargs...)
final_stage!(st, params.alg, problem, info, opts, args...; kargs...)
end
function stop_criteria!(st, params::AbstractRestart, problem, info, opts, args...; kargs...)
stop_criteria!(st, params.alg, problem, info, opts, args...; kargs...)
return
end