/
DE.jl
212 lines (164 loc) · 5.06 KB
/
DE.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
abstract type AbstractDifferentialEvolution <: AbstractParameters end
mutable struct DE <: AbstractDifferentialEvolution
N::Int
F::Float64
CR::Float64
CR_min::Float64
CR_max::Float64
F_min::Float64
F_max::Float64
strategy::Symbol
end
include("epsilonDE.jl")
"""
DE(;
N = 0,
F = 1.0,
CR = 0.5,
strategy = :rand1,
information = Information(),
options = Options()
)
Parameters for Differential Evolution (DE) algorithm: step-size `F`,`CR` controlls the binomial
crossover, `N` is the population size. The parameter `strategy` is related to the variation
operator (`:rand1`, `:rand2`, `:best1`, `:best2`, `:randToBest1`).
# Example
```jldoctest
julia> f(x) = sum(x.^2)
f (generic function with 1 method)
julia> optimize(f, [-1 -1 -1; 1 1 1.0], DE())
+=========== RESULT ==========+
iteration: 1000
minimum: 0
minimizer: [0.0, 0.0, 0.0]
f calls: 30000
total time: 0.0437 s
+============================+
julia> optimize(f, [-1 -1 -1; 1 1 1.0], DE(N=50, F=1.5, CR=0.8))
+=========== RESULT ==========+
iteration: 600
minimum: 8.68798e-25
minimizer: [3.2777877981303293e-13, 3.7650459509488005e-13, -7.871487597385812e-13]
f calls: 30000
total time: 0.0319 s
+============================+
```
"""
function DE(;
N::Int = 0,
F = 0.7,
CR = 0.5,
CR_min = CR,
CR_max = CR,
F_min = F,
F_max = F,
strategy::Symbol = :rand1,
kargs...
)
parameters = DE(N, promote(F, CR, CR_min, CR_max, F_min, F_max)..., strategy)
Algorithm(parameters; kargs...)
end
function update_state!(
status,
parameters::AbstractDifferentialEvolution,
problem::AbstractProblem,
information::Information,
options::Options,
args...;
kargs...
)
# stepsize
if parameters.F_min < parameters.F_max
parameters.F = parameters.F_min + (parameters.F_max - parameters.F_min) * rand(options.rng)
end
if parameters.CR_min < parameters.CR_max
parameters.CR = parameters.CR_min + (parameters.CR_max - parameters.CR_min) * rand(options.rng)
end
new_vectors = reproduction(status, parameters, problem)
# evaluate solutions
new_solutions = create_solutions(new_vectors, problem,ε=options.h_tol)
append!(status.population, new_solutions)
environmental_selection!(status.population, parameters)
status.best_sol = get_best(status.population)
end
function environmental_selection(population, parameters::AbstractDifferentialEvolution)
@assert length(population) == 2*parameters.N
new_solutions = population[parameters.N+1:end]
population = population[1:parameters.N]
survivals = Int[]
# select survival
for (i, h) in enumerate(new_solutions)
if is_better(h, population[i], parameters)
push!(survivals, parameters.N + i)
else
push!(survivals, i)
end
end
return survivals
end
function environmental_selection!(population, parameters::AbstractDifferentialEvolution)
mask = environmental_selection(population, parameters)
ignored = ones(Bool, length(population))
ignored[mask] .= false
deleteat!(population, ignored)
return
end
function initialize!(
status,
parameters::AbstractDifferentialEvolution,
problem::AbstractProblem,
information::Information,
options::Options,
args...;
kargs...
)
D = getdim(problem)
if parameters.N <= 5
parameters.N = 10 * D
end
if parameters.CR < 0 || parameters.CR > 1
parameters.CR = 0.5
options.debug &&
@warn("CR should be from interval [0,1]; set to default value 0.5")
end
if options.f_calls_limit == 0
options.f_calls_limit = 10000D
options.debug &&
@warn( "f_calls_limit increased to $(options.f_calls_limit)")
end
if options.iterations == 0
options.iterations = div(options.f_calls_limit, parameters.N) + 1
end
return gen_initial_state(problem,parameters,information,options,status)
end
function final_stage!(
status,
parameters::AbstractDifferentialEvolution,
problem::AbstractProblem,
information::Information,
options::Options,
args...;
kargs...
)
status.final_time = time()
end
function reproduction(status, parameters::AbstractDifferentialEvolution, problem)
population = status.population
@assert !isempty(population)
N = parameters.N
D = length(get_position(population[1]))
strategy = parameters.strategy
xBest = get_position(status.best_sol)
F = parameters.F
CR = parameters.CR
X = zeros(eltype(xBest), N, D)
for i in 1:N
x = get_position(population[i])
u = DE_mutation(population, F, strategy, 1)
v = DE_crossover(x, u, CR)
evo_boundary_repairer!(v, xBest, problem.search_space)
X[i,:] .= _fix_type(v, problem.search_space)
end
X
end
is_better(a, b, parameters::AbstractDifferentialEvolution) = is_better(a, b)