-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimlib_docs_de.html
359 lines (278 loc) · 14.8 KB
/
optimlib_docs_de.html
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="OptimLib: a C++ numerical optimization library.">
<meta name="author" content="Keith O'Hara">
<meta name="keywords" content="Optimization, C++, C++11, Differential Evolution, Particle Swarm Optimization, Root Finding, OpenMP, Parallel Optimization, BFGS, L-BFGS, Keith O'Hara, Economics, Econometrics, Research, NYU, New York University" />
<link rel="shortcut icon" type="image/x-icon" href="siteicon.ico">
<title>OptimLib: Differential Evolution</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/modern-business.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- Additional Settings -->
<link href="css/yuxuan_settings.css" rel="stylesheet">
<!-- Syntax Highlighter -->
<script type="text/javascript" src="js/syntaxhighlighter.js"></script>
<link type="text/css" rel="stylesheet" href="css/swift_theme.css">
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-93902857-1', 'auto');
ga('send', 'pageview');
</script>
<!-- MathJax -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript" async
src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML">
</script>
<script async defer src="https://buttons.github.io/buttons.js"></script>
<script src="js/jquery.js"></script>
<script>
$(function(){
$("#mynavbar").load("navbar.html")
$("#optimhead").load("optimlib_header.html")
$("#myfooter").load("footer.html")
});
</script>
</head>
<style>
pre {
display: inline-block;
}
</style>
<body>
<!-- Navigation -->
<div id="mynavbar"></div>
<!-- Page Content -->
<div class="container">
<!-- Page Heading/Breadcrumbs -->
<div id="optimhead"></div>
<br>
<!-- -->
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<a data-toggle="collapse" href="#collapse1"><h4><strong style="font-size: 120%;">OptimLib: Differential Evolution</strong></h4></a>
</div>
<div id="collapse1" class="panel-collapse collapse">
<div class="panel-body">
<a href="#definition">Definition</a> <br>
<a href="#details">Details</a> <br>
<a href="#examples">Examples</a>
</div>
</div>
</div>
</div>
</div>
<!-- -->
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<p>Differential Evolution (DE) is a stochastic genetic search algorithm for global optimization of potentially ill-behaved nonlinear functions.</p>
<hr style="height:2px;border-width:0;background-color:black">
<h3 style="text-align: left;" id="definition"><strong style="font-size: 100%;">Definition and Syntax</strong></h3>
<pre class="brush: cpp;">
bool de(arma::vec& init_out_vals, std::function<double (const arma::vec& vals_inp, arma::vec* grad_out, void* opt_data)> opt_objfn, void* opt_data);
bool de(arma::vec& init_out_vals, std::function<double (const arma::vec& vals_inp, arma::vec* grad_out, void* opt_data)> opt_objfn, void* opt_data, algo_settings_t& settings);
</pre>
<p><strong>Function arguments:</strong></p>
<ul>
<li><code>init_out_vals</code> a column vector of initial values; will be replaced by the solution values.</li>
<li><code>opt_objfn</code> the function to be minimized, taking three arguments:
<ul>
<li><code>vals_inp</code> a vector of inputs;</li>
<li><code>grad_out</code> an empty vector, as DE does not require the gradient to be known/exist; and</li>
<li><code>opt_data</code> additional parameters passed to the function.</li>
</ul>
<li><code>opt_data</code> additional parameters passed to the function.</li>
<li><code>settings</code> parameters controlling the optimization routine; see below.</li>
</ul>
<p><strong>Optimization control parameters:</strong></p>
<ul>
<li><code>bool vals_bound</code> whether the search space is bounded. If true, then</li>
<ul>
<li><code>arma::vec lower_bounds</code> this defines the lower bounds.</li>
<li><code>arma::vec upper_bounds</code> this defines the upper bounds.</li>
</ul>
<br>
<li><code>int de_n_pop</code> population size of each generation.</li>
<li><code>int de_n_gen</code> number of vectors to generate.</li>
<li><code>int de_check_freq</code> number of generations between convergence checks.</li>
<br>
<li><code>int de_mutation_method</code> which method of mutation to apply:</li>
<ul>
<li><code>de_mutation_method = 1</code> applies the 'rand' policy.</li>
<li><code>de_mutation_method = 2</code> applies the 'best' policy.</li>
</ul>
<li><code>double de_par_F</code> the mutation parameter $F$ in the details section below.</li>
<li><code>double de_par_CR</code> the crossover parameter $CR$ in the details section below.</li>
<br>
<li><code>arma::vec de_initial_lb</code> lower bounds on the initial population; defaults to <code>init_out_vals</code> $- \ 0.5$.</li>
<li><code>arma::vec de_initial_ub</code> upper bounds on the initial population; defaults to <code>init_out_vals</code> $+ \ 0.5$.</li>
</ul>
<hr style="height:2px;border-width:0;background-color:black">
<h3 style="text-align: left;" id="details"><strong style="font-size: 100%;">Details</strong></h3>
<p>The DE method in OptimLib comes in two varieties: the simple version, outlined first, and the more advanced method of Zamuda and Brest (2015).</p>
<hr>
<h4> Basic Differential Evolution (DE)</h4>
<p>Let $X^{(i)}$ denote a $N_p \times d$-dimensional array of values at stage $i$ of the algorithm. The basic DE algorithm is comprised of three steps.</p>
<ul>
<li><strong>The Mutation Step.</strong> For unique random indices $a,b,c$:</li>
<ul>
<li>if <code>de_mutation_method = 1</code>, use the 'rand' method:</li>
$$X^* = X^{(i)}(c,:) + F \times (X^{(i)}(a,:) - X^{(i)}(b,:))$$
<li>if <code>de_mutation_method = 2</code>, use the 'best' method:</li>
$$X^* = X^{(i)}(\text{best},:) + F \times (X^{(i)}(a,:) - X^{(i)}(b,:))$$
where $X^{(i)}(\text{best},:) := \arg \min \{ f(X^{(i)}(1,:)), \ldots, f(X^{(i)}(N,:)) \}$.</li>
</ul>
<li><strong>The Crossover Step.</strong> For a random integer $r_k \in \{1, \ldots, d\}$,</li>
$$X_c^* (j,k) = \begin{cases} X^*(j,k) & \text{ if } u_k \leq CR \text{ or } k = r_k \\ X^{(i)} (j,k) & \text{ else } \end{cases}$$
<li><strong>The Update Step.</strong></li>
$$X^{(i+1)} (j,:) = \begin{cases} X_c^*(j,:) & \text{ if } f(X_c^*(j)) < f(X^{(i)}(j)) \\ X^{(i)} (j) & \text{ else } \end{cases}$$
</ul>
<p>The algorithm stops when the relative improvement in the objective function is less than <code>err_tol</code> between <code>de_check_freq</code> number of generations, or when the total number of 'generations' exceeds <code>n_gen</code>.</p>
<hr>
<h4> Differential Evolution with Population Reduction and Multiple Mutation Strategies (DE-PRMM)</h4>
<br>
<p>Let $X^{(i)}$ denote a $N_p \times d$-dimensional array of values at stage $i$ of the algorithm.</p>
<ul>
<li>Let $M$ denote the maximum number of function evaluations (of which there are $N_p$ per generation).</li>
<li>Each generation is split into two sub-populations of size $N_{p}^{(m)}$ and $N_{p}^{(b)} = N_p - N_{p}^{(m)}$, labelled the 'main' population and the 'best' population, respectively.</li>
<li>The population is reduced $p_{\text{max}}-1$ number of times.</li>
</ul>
<p>The steps are as follows.</p>
<ul>
<li><strong>Population Reduction Step.</strong> <br>If
$$i = \dfrac{M}{p_{\text{max}} N_p}$$
reduce the population by half ($N_p /2$) and double the number of generations ($2 \times N_g$).
<br>
Values are chosen based on a simple pairwise comparison:
$$X_{i,\text{new}} (j,:) = \begin{cases} X^{(i)} (j,:) &\text{ if } f(X^{(i)} (j,:)) < f(X^{(i)} (j+N_p/2,:)) \\ X^{(i)} (j+N_p/2,:) & \text{ else} \end{cases}$$
Reset $i = 1$.</li>
<!-- <strong>Otherwise go to the Mutation Step.</strong></li> -->
<li><strong>The Mutation Step.</strong> <br>Sample $F_i$ and $CR_i$ values according to:
$$F_i = \begin{cases} F_l + (F_u - F_l) \times u_2 & \text{ if } u_1 < \tau_F \\ F_{i-1} & \text{ else} \end{cases}$$
$$CR_i = \begin{cases} u_4 & \text{ if } u_3 < \tau_{CR} \\ CR_{i-1} & \text{ else} \end{cases}$$
If $i \leq N_p - N_p^{(b)}$:
<ul>
<li>then for unique random indices $a,b,c$:</li>
$$X^* = \begin{cases} X^{(i)}(c,:) + F_i \times (X^{(i)}(a,:) - X^{(i)}(b,:)) & \text{ if } r_s < 0.75 \text{ or } N_p \geq 100 \\ X_{i,\text{best}}^{(m)} + F_i \times (X^{(i)}(a,:) - X^{(i)}(b,:)) & \text{ else} \end{cases}$$
</ul>
Else, if $N_p - N_p^{(b)} < i \leq N_p$:
<ul>
<li>then for unique random indices $a,b$:</li>
$$X^* = X_{i,\text{best}}^{(b)} + F_i \times (X^{(i)}(a,:) - X^{(i)}(b,:))$$
</ul>
<li><strong>The Crossover Step.</strong> <br>For a random integer $r_k \in \{1, \ldots, d\}$,</li>
$$X_c^* (j,k) = \begin{cases} X^*(j,k) & \text{ if } u_k \leq CR_i \text{ or } k = r_k \\ X^{(i)} (j,k) & \text{ else } \end{cases}$$
<li><strong>The Update Step.</strong></li>
$$X^{(i+1)} (j,:) = \begin{cases} X_c^*(j,:) & \text{ if } f(X_c^*(j)) < f(X^{(i)}(j)) \\ X^{(i)} (j) & \text{ else } \end{cases}$$
<li><strong>Setting the 'best' vector.</strong> <br> Let $X_{i+1,\text{best}}^{(m)} := \arg \min \{ f(X^{(i+1)}(1,:)), \ldots, f(X^{(i+1)}(N_p^{(m)},:)) \}$ and $X_{i+1,\text{best}}^{(b)} := \arg \min \{ f(X^{(i+1)}(N_p^{(m)}+1,:)), \ldots, f(X^{(i+1)}(N_p,:)) \}$.
<ul>
<li>If $i = N_p^{(m)}$ and $f(X_{i+1,\text{best}}^{(m)}) < f(X_{i,\text{best}}^{(b)})$, set</li>
$$X_{\text{xchg}} := X_{i+1,\text{best}}^{(m)}$$
<li>If $i = N_p$ and $f(X_{i+1,\text{best}}^{(b)}) < f(X_{i+1,\text{best}}^{(m)})$, and</li>
$$\left| \frac{1}{d} \sum_{j=1}^d \frac{X_{i+1,\text{best}}^{(b)} (j) - X_{\text{min}}(j)}{X_{\text{xchg}} (j) - X_{\text{min}}(j)} - 1 \right| > 0.5$$
then
$$X_{i+1,\text{best}}^{(m)} := X_{i+1,\text{best}}^{(b)}$$
</ul>
</ul>
<hr style="height:2px;border-width:0;background-color:black">
<h3 style="text-align: left;" id="examples"><strong style="font-size: 100%;">Examples</strong></h3>
<p>To illustrate the effectiveness of DE, we will tackle two well-known performance tests from the numerical optimization literature:</p>
<ul>
<li>The Rastrigin function:</li>
$$\min_{x \in [-5,5]^n} \left\{ A n + \sum_{i=1}^n \left( x_i^2 - A \cos(2 \pi x_i) \right) \right\}$$
<li>Ackley's function:</li>
$$\min_{x \in [-5,5]^2} \left\{ -20 \exp \left( -0.2 \sqrt{0.5(x_1^2 + x_2^2)} \right) - \exp \left(0.5[\cos(2 \pi x_1) + \cos(2 \pi x_2)]\right) + e + 20 \right\}$$
</ul>
<p>Both functions are 'bumpy' and contain many local minima. Plots of both functions are given below.</p>
<!-- <img class="img-responsive center-block" src="pics/rastrigin_fn.png" alt=""> -->
<!-- <img class="img-responsive pull-left" src="pics/rastrigin_fn.png" alt=""> <img class="img-responsive pull-right" src="pics/ackley_fn_3d.png" alt=""> -->
<div class="row">
<div class="col-md-6">
<h3 style="text-align: center;"><strong style="font-size: 110%;">Rastrigin</strong></h3>
<!-- <img class="img-responsive" src="pics/wsp_2.jpg" alt=""> -->
<img class="img-responsive center-block" src="pics/rastrigin_fn.png" alt="">
</div>
<div class="col-md-6">
<h3 style="text-align: center;"><strong style="font-size: 110%;">Ackley</strong></h3>
<!-- <img class="img-responsive" src="pics/wsp_2.jpg" alt=""> -->
<!-- <img src="pics/rastrigin_fn.png" class="img-rounded pull-right" alt="Sphere" width="304" height="236"> -->
<img class="img-responsive center-block" src="pics/ackley_fn_3d.png" alt="">
</div>
</div>
<br>
<p>Both minimization problems possess a unique global minimum: the zero vector. Code implementing these examples using OptimLib is given below.</p>
<pre class="brush: cpp;">
#include "optim.hpp"
struct rastrigin_fn_data {
double A;
};
double rastrigin_fn(const arma::vec& vals_inp, arma::vec* grad_out, void* opt_data)
{
const int n = vals_inp.n_elem;
rastrigin_fn_data* objfn_data = reinterpret_cast<rastrigin_fn_data*>(opt_data);
const double A = objfn_data->A;
double obj_val = A*n + arma::accu( arma::pow(vals_inp,2) - A*arma::cos(2*arma::datum::pi*vals_inp) );
//
return obj_val;
}
double ackley_fn(const arma::vec& vals_inp, arma::vec* grad_out, void* opt_data)
{
const double x = vals_inp(0);
const double y = vals_inp(1);
const double pi = arma::datum::pi;
double obj_val = -20*std::exp( -0.2*std::sqrt(0.5*(x*x + y*y)) ) - std::exp( 0.5*(std::cos(2*pi*x) + std::cos(2*pi*y)) ) + std::exp(1) + 20;
//
return obj_val;
}
int main()
{
//
// Rastrigin function
rastrigin_fn_data test_data;
test_data.A = 10;
arma::vec x = arma::ones(2,1) + 1.0; // initial values: (2,2)
bool success = optim::de(x,rastrigin_fn,&test_data);
if (success) {
std::cout << "de: Rastrigin test completed successfully." << std::endl;
} else {
std::cout << "de: Rastrigin test completed unsuccessfully." << std::endl;
}
arma::cout << "de: solution to Rastrigin test:\n" << x << arma::endl;
//
// Ackley function
arma::vec x_2 = arma::ones(2,1) + 1.0; // initial values: (2,2)
bool success_2 = optim::de(x_2,ackley_fn,nullptr);
if (success_2) {
std::cout << "de: Ackley test completed successfully." << std::endl;
} else {
std::cout << "de: Ackley test completed unsuccessfully." << std::endl;
}
arma::cout << "de: solution to Ackley test:\n" << x_2 << arma::endl;
return 0;
}
</pre>
</div>
</div>
</div>
<div id="myfooter"></div>
<!-- jQuery -->
<!--<script src="js/jquery.js"></script>-->
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>