File: _global_optimizer_8cpp-example.html

package info (click to toggle)
quantlib-refman-html 1.20-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 103,140 kB
  • sloc: javascript: 13,408; makefile: 35
file content (481 lines) | stat: -rw-r--r-- 51,240 bytes parent folder | download
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.20"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>QuantLib: GlobalOptimizer.cpp</title>
<link href='https://fonts.googleapis.com/css?family=Merriweather+Sans:800' rel='stylesheet' type='text/css'>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="quantlibextra.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname"><a href="http://quantlib.org">
       <img alt="QuantLib" src="QL-title.jpg"></a>
   <div id="projectbrief">A free/open-source library for quantitative finance</div>
   <div id="projectnumber">Reference manual - version 1.20</div>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.20 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">GlobalOptimizer.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>This example shows the use of several different optimizers: firefly algorithm, hybrid simulated annealing, particle swarm optimization, simulated annealing, and differential evolution.</p>
<div class="fragment"><div class="line"><span class="comment">/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include &lt;ql/qldefines.hpp&gt;</span></div>
<div class="line"><span class="preprocessor">#ifdef BOOST_MSVC</span></div>
<div class="line"><span class="preprocessor">#  include &lt;ql/auto_link.hpp&gt;</span></div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"><span class="preprocessor">#include &lt;ql/math/optimization/differentialevolution.hpp&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;ql/math/optimization/simulatedannealing.hpp&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;ql/experimental/math/fireflyalgorithm.hpp&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;ql/experimental/math/hybridsimulatedannealing.hpp&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;ql/experimental/math/particleswarmoptimization.hpp&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;ql/functional.hpp&gt;</span></div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include &lt;ql/tuple.hpp&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iomanip&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">using namespace </span><a class="code" href="namespace_quant_lib.html">QuantLib</a>;</div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#if defined(QL_ENABLE_SESSIONS)</span></div>
<div class="line"><span class="keyword">namespace </span><a class="code" href="namespace_quant_lib.html">QuantLib</a> {</div>
<div class="line"> </div>
<div class="line">    ThreadKey sessionId() { <span class="keywordflow">return</span> 0; }</div>
<div class="line"> </div>
<div class="line">}</div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> seed = 127;</div>
<div class="line"> </div>
<div class="line"><span class="comment">/*</span></div>
<div class="line"><span class="comment">    Some benchmark functions taken from</span></div>
<div class="line"><span class="comment">    https://en.wikipedia.org/wiki/Test_functions_for_optimization</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">    Global optimizers have generally a lot of hyper-parameters, and one</span></div>
<div class="line"><span class="comment">    * usually requires some hyper-parameter optimization to find appropriate values</span></div>
<div class="line"><span class="comment">*/</span></div>
<div class="line"> </div>
<div class="line"><a name="a0"></a><a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> ackley(<span class="keyword">const</span> <a name="_a1"></a><a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>&amp; x) {</div>
<div class="line">    <span class="comment">//Minimum is found at 0</span></div>
<div class="line">    <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> p1 = 0.0, p2 = 0.0;</div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> i = 0; i &lt; x.<a name="a2"></a><a class="code" href="class_quant_lib_1_1_array.html#acce0ab2cacc475b2434f24c65c91685a" title="dimension of the array">size</a>(); i++) {</div>
<div class="line">        p1 += x[i] * x[i];</div>
<div class="line">        p2 += std::cos(M_TWOPI*x[i]);</div>
<div class="line">    }</div>
<div class="line">    p1 = -0.2*std::sqrt(0.5*p1);</div>
<div class="line">    p2 *= 0.5;</div>
<div class="line">    <span class="keywordflow">return</span> M_E + 20.0 - 20.0*std::exp(p1)-std::exp(p2);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><a name="_a3"></a><a class="code" href="class_quant_lib_1_1_disposable.html" title="generic disposable object with move semantics">Disposable&lt;Array&gt;</a> ackleyValues(<span class="keyword">const</span> <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>&amp; x) {</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a> y(x.<a class="code" href="class_quant_lib_1_1_array.html#acce0ab2cacc475b2434f24c65c91685a" title="dimension of the array">size</a>());</div>
<div class="line">    <span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> i = 0; i &lt; x.<a class="code" href="class_quant_lib_1_1_array.html#acce0ab2cacc475b2434f24c65c91685a" title="dimension of the array">size</a>(); i++) {</div>
<div class="line">        <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> p1 = x[i] * x[i];</div>
<div class="line">        p1 = -0.2*std::sqrt(0.5*p1);</div>
<div class="line">        <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> p2 = 0.5*std::cos(M_TWOPI*x[i]);</div>
<div class="line">        y[i] = M_E + 20.0 - 20.0*std::exp(p1)-std::exp(p2);</div>
<div class="line">    }</div>
<div class="line">    <span class="keywordflow">return</span> y;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> sphere(<span class="keyword">const</span> <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>&amp; x) {</div>
<div class="line">    <span class="comment">//Minimum is found at 0</span></div>
<div class="line">    <span class="keywordflow">return</span> DotProduct(x, x);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><a class="code" href="class_quant_lib_1_1_disposable.html" title="generic disposable object with move semantics">Disposable&lt;Array&gt;</a> sphereValues(<span class="keyword">const</span> <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>&amp; x) {</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a> y(x.<a class="code" href="class_quant_lib_1_1_array.html#acce0ab2cacc475b2434f24c65c91685a" title="dimension of the array">size</a>());</div>
<div class="line">    <span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> i = 0; i &lt; x.<a class="code" href="class_quant_lib_1_1_array.html#acce0ab2cacc475b2434f24c65c91685a" title="dimension of the array">size</a>(); i++) {</div>
<div class="line">        y[i] = x[i]*x[i];</div>
<div class="line">    }</div>
<div class="line">    <span class="keywordflow">return</span> y;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> rosenbrock(<span class="keyword">const</span> <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>&amp; x) {</div>
<div class="line">    <span class="comment">//Minimum is found at f(1, 1, ...)</span></div>
<div class="line">    QL_REQUIRE(x.<a class="code" href="class_quant_lib_1_1_array.html#acce0ab2cacc475b2434f24c65c91685a" title="dimension of the array">size</a>() &gt; 1, <span class="stringliteral">&quot;Input size needs to be higher than 1&quot;</span>);</div>
<div class="line">    <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> result = 0.0;</div>
<div class="line">    <span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> i = 0; i &lt; x.<a class="code" href="class_quant_lib_1_1_array.html#acce0ab2cacc475b2434f24c65c91685a" title="dimension of the array">size</a>() - 1; i++) {</div>
<div class="line">        <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> temp = (x[i + 1] - x[i] * x[i]);</div>
<div class="line">        result += (x[i] - 1.0)*(x[i] - 1.0) + 100.0*temp*temp;</div>
<div class="line">    }</div>
<div class="line">    <span class="keywordflow">return</span> result;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> easom(<span class="keyword">const</span> <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>&amp; x) {</div>
<div class="line">    <span class="comment">//Minimum is found at f(\pi, \pi, ...)</span></div>
<div class="line">    <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> p1 = 1.0, p2 = 0.0;</div>
<div class="line">    <span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> i = 0; i &lt; x.<a class="code" href="class_quant_lib_1_1_array.html#acce0ab2cacc475b2434f24c65c91685a" title="dimension of the array">size</a>(); i++) {</div>
<div class="line">        p1 *= std::cos(x[i]);</div>
<div class="line">        p2 += (x[i] - M_PI)*(x[i] - M_PI);</div>
<div class="line">    }</div>
<div class="line">    <span class="keywordflow">return</span> -p1*std::exp(-p2);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><a class="code" href="class_quant_lib_1_1_disposable.html" title="generic disposable object with move semantics">Disposable&lt;Array&gt;</a> easomValues(<span class="keyword">const</span> <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>&amp; x) {</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a> y(x.<a class="code" href="class_quant_lib_1_1_array.html#acce0ab2cacc475b2434f24c65c91685a" title="dimension of the array">size</a>());</div>
<div class="line">    <span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> i = 0; i &lt; x.<a class="code" href="class_quant_lib_1_1_array.html#acce0ab2cacc475b2434f24c65c91685a" title="dimension of the array">size</a>(); i++) {</div>
<div class="line">        <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> p1 = std::cos(x[i]);</div>
<div class="line">        <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> p2 = (x[i] - M_PI)*(x[i] - M_PI);</div>
<div class="line">        y[i] = -p1*std::exp(-p2);</div>
<div class="line">    }</div>
<div class="line">    <span class="keywordflow">return</span> y;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> eggholder(<span class="keyword">const</span> <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>&amp; x) {</div>
<div class="line">    <span class="comment">//Minimum is found at f(512, 404.2319)</span></div>
<div class="line">    QL_REQUIRE(x.<a class="code" href="class_quant_lib_1_1_array.html#acce0ab2cacc475b2434f24c65c91685a" title="dimension of the array">size</a>() == 2, <span class="stringliteral">&quot;Input size needs to be equal to 2&quot;</span>);</div>
<div class="line">    <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> p = (x[1] + 47.0);</div>
<div class="line">    <span class="keywordflow">return</span> -p*std::sin(std::sqrt(std::abs(0.5*x[0] + p))) -</div>
<div class="line">        x[0] * std::sin(std::sqrt(std::abs(x[0] - p)));</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> printFunction(<a name="_a4"></a><a class="code" href="class_quant_lib_1_1_problem.html" title="Constrained optimization problem.">Problem</a>&amp; p, <span class="keyword">const</span> <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>&amp; x) {</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot; f(&quot;</span> &lt;&lt; x[0];</div>
<div class="line">    <span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> i = 1; i &lt; x.size(); i++) {</div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;, &quot;</span> &lt;&lt; x[i];</div>
<div class="line">    }</div>
<div class="line">    <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> val = p.<a name="a5"></a><a class="code" href="class_quant_lib_1_1_problem.html#ab5a7fc79ae2c7a09f08d7ab169a52dc7" title="call cost function computation and increment evaluation counter">value</a>(x);</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;) = &quot;</span> &lt;&lt; val &lt;&lt; std::endl;</div>
<div class="line">    <span class="keywordflow">return</span> val;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keyword">class </span>TestFunction : <span class="keyword">public</span> <a name="_a6"></a><a class="code" href="class_quant_lib_1_1_cost_function.html" title="Cost function abstract class for optimization problem.">CostFunction</a> {</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line">    <span class="keyword">typedef</span> ext::function&lt;<a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a>(<span class="keyword">const</span> <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>&amp;)&gt; RealFunc;</div>
<div class="line">    <span class="keyword">typedef</span> ext::function&lt;Disposable&lt;Array&gt;(<span class="keyword">const</span> <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>&amp;)&gt; ArrayFunc;</div>
<div class="line">    <span class="keyword">explicit</span> TestFunction(<span class="keyword">const</span> RealFunc &amp; f, <span class="keyword">const</span> ArrayFunc &amp; fs = ArrayFunc()) : f_(f), fs_(fs) {}</div>
<div class="line">    <span class="keyword">explicit</span> TestFunction(<a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a>(*f)(<span class="keyword">const</span> <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>&amp;), <a class="code" href="class_quant_lib_1_1_disposable.html" title="generic disposable object with move semantics">Disposable&lt;Array&gt;</a>(*fs)(<span class="keyword">const</span> <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>&amp;) = NULL) : f_(f), fs_(fs) {}</div>
<div class="line">    <span class="keyword">virtual</span> ~TestFunction(){}</div>
<div class="line">    <span class="keyword">virtual</span> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> value(<span class="keyword">const</span> <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>&amp; x)<span class="keyword"> const </span>{</div>
<div class="line">        <span class="keywordflow">return</span> f_(x);</div>
<div class="line">    }</div>
<div class="line">    <span class="keyword">virtual</span> <a class="code" href="class_quant_lib_1_1_disposable.html" title="generic disposable object with move semantics">Disposable&lt;Array&gt;</a> values(<span class="keyword">const</span> <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>&amp; x)<span class="keyword"> const </span>{</div>
<div class="line">        <span class="keywordflow">if</span>(!fs_)</div>
<div class="line">            <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">&quot;Invalid function&quot;</span>);</div>
<div class="line">        <span class="keywordflow">return</span> fs_(x);</div>
<div class="line">    }</div>
<div class="line"><span class="keyword">private</span>:</div>
<div class="line">    RealFunc f_;</div>
<div class="line">    ArrayFunc fs_;</div>
<div class="line">};</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> test(<a name="_a7"></a><a class="code" href="class_quant_lib_1_1_optimization_method.html" title="Abstract class for constrained optimization method.">OptimizationMethod</a>&amp; method, <a class="code" href="class_quant_lib_1_1_cost_function.html" title="Cost function abstract class for optimization problem.">CostFunction</a>&amp; f, <span class="keyword">const</span> <a name="_a8"></a><a class="code" href="class_quant_lib_1_1_end_criteria.html" title="Criteria to end optimization process:">EndCriteria</a>&amp; endCriteria,</div>
<div class="line">          <span class="keyword">const</span> <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>&amp; start, <span class="keyword">const</span> <a name="_a9"></a><a class="code" href="class_quant_lib_1_1_constraint.html" title="Base constraint class.">Constraint</a>&amp; constraint = <a class="code" href="class_quant_lib_1_1_constraint.html" title="Base constraint class.">Constraint</a>(),</div>
<div class="line">          <span class="keyword">const</span> <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>&amp; optimum = <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>()) {</div>
<div class="line">    QL_REQUIRE(!start.<a name="a10"></a><a class="code" href="class_quant_lib_1_1_array.html#a644718bb2fb240de962dc3c9a1fdf0dc" title="whether the array is empty">empty</a>(), <span class="stringliteral">&quot;Input size needs to be at least 1&quot;</span>);</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;Starting point: &quot;</span>;</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_constraint.html" title="Base constraint class.">Constraint</a> c;</div>
<div class="line">    <span class="keywordflow">if</span> (!constraint.empty())</div>
<div class="line">        c = constraint;</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_problem.html" title="Constrained optimization problem.">Problem</a> p(f, c, start);</div>
<div class="line">    printFunction(p, start);</div>
<div class="line">    method.<a name="a11"></a><a class="code" href="class_quant_lib_1_1_optimization_method.html#a6bf9f0581475e7a28bdf478f941f116e" title="minimize the optimization problem P">minimize</a>(p, endCriteria);</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;End point: &quot;</span>;</div>
<div class="line">    <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> val = printFunction(p, p.<a name="a12"></a><a class="code" href="class_quant_lib_1_1_problem.html#a2640a08249de104b1944e52e352400a9" title="current value of the local minimum">currentValue</a>());</div>
<div class="line">    <span class="keywordflow">if</span>(!optimum.empty())</div>
<div class="line">    {</div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;Global optimum: &quot;</span>;</div>
<div class="line">        <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> optimVal = printFunction(p, optimum);</div>
<div class="line">        <span class="keywordflow">if</span>(std::abs(optimVal) &lt; 1e-13)</div>
<div class="line">            <span class="keywordflow">return</span> <span class="keyword">static_cast&lt;</span><span class="keywordtype">int</span><span class="keyword">&gt;</span>(std::abs(val - optimVal) &lt; 1e-6);</div>
<div class="line">        <span class="keywordflow">else</span></div>
<div class="line">            <span class="keywordflow">return</span> <span class="keyword">static_cast&lt;</span><span class="keywordtype">int</span><span class="keyword">&gt;</span>(std::abs((val - optimVal) / optimVal) &lt; 1e-6);</div>
<div class="line">    }</div>
<div class="line">    <span class="keywordflow">return</span> 1;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> testFirefly() {</div>
<div class="line">    <span class="comment">/*</span></div>
<div class="line"><span class="comment">    The Eggholder function is only in 2 dimensions, it has a multitude</span></div>
<div class="line"><span class="comment">    * of local minima, and they are not symmetric necessarily</span></div>
<div class="line"><span class="comment">    */</span></div>
<div class="line">    <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> n = 2;</div>
<div class="line">    <a name="_a13"></a><a class="code" href="class_quant_lib_1_1_nonhomogeneous_boundary_constraint.html" title="Constraint imposing i-th argument to be in [low_i,high_i] for all i">NonhomogeneousBoundaryConstraint</a> constraint(<a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>(n, -512.0), <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>(n, 512.0));</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a> x(n, 0.0);</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a> optimum(n);</div>
<div class="line">    optimum[0] = 512.0;</div>
<div class="line">    optimum[1] = 404.2319;</div>
<div class="line">    <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> agents = 150;</div>
<div class="line">    <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> vola = 1.5;</div>
<div class="line">    <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> intense = 1.0;</div>
<div class="line">    ext::shared_ptr&lt;FireflyAlgorithm::Intensity&gt; intensity =</div>
<div class="line">        ext::make_shared&lt;ExponentialIntensity&gt;(10.0, 1e-8, intense);</div>
<div class="line">    ext::shared_ptr&lt;FireflyAlgorithm::RandomWalk&gt; randomWalk =</div>
<div class="line">        ext::make_shared&lt;LevyFlightWalk&gt;(vola, 0.5, 1.0, seed);</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;Function eggholder, Agents: &quot;</span> &lt;&lt; agents</div>
<div class="line">            &lt;&lt; <span class="stringliteral">&quot;, Vola: &quot;</span> &lt;&lt; vola &lt;&lt; <span class="stringliteral">&quot;, Intensity: &quot;</span> &lt;&lt; intense &lt;&lt; std::endl;</div>
<div class="line">    TestFunction f(eggholder);</div>
<div class="line">    <a name="_a14"></a><a class="code" href="class_quant_lib_1_1_firefly_algorithm.html">FireflyAlgorithm</a> fa(agents, intensity, randomWalk, 40);</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_end_criteria.html" title="Criteria to end optimization process:">EndCriteria</a> ec(5000, 1000, 1.0e-8, 1.0e-8, 1.0e-8);</div>
<div class="line">    test(fa, f, ec, x, constraint, optimum);</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;================================================================&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> testSimulatedAnnealing(<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> dimension, <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> maxSteps, <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> staticSteps){</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/*The ackley function has a large amount of local minima, but the</span></div>
<div class="line"><span class="comment">      structure is symmetric, so if one could simply just ignore the</span></div>
<div class="line"><span class="comment">      walls separating the local minima, it would look like almost</span></div>
<div class="line"><span class="comment">      like a parabola</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">      Andres Hernandez: I could not find a configuration that was able</span></div>
<div class="line"><span class="comment">      to fix the problem</span></div>
<div class="line"><span class="comment">    */</span></div>
<div class="line"> </div>
<div class="line">    <span class="comment">//global minimum is at 0.0</span></div>
<div class="line">    TestFunction f(ackley, ackleyValues);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">//Starting point</span></div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a> x(dimension, 1.5);</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a> optimum(dimension, 0.0);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">//Constraint for local optimizer</span></div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a> lower(dimension, -5.0);</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a> upper(dimension, 5.0);</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_nonhomogeneous_boundary_constraint.html" title="Constraint imposing i-th argument to be in [low_i,high_i] for all i">NonhomogeneousBoundaryConstraint</a> constraint(lower, upper);</div>
<div class="line"> </div>
<div class="line">    <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> lambda = 0.1;</div>
<div class="line">    <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> temperature = 350;</div>
<div class="line">    <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> epsilon = 0.99;</div>
<div class="line">    <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> ms = 1000;</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;Function ackley, Lambda: &quot;</span> &lt;&lt; lambda</div>
<div class="line">            &lt;&lt; <span class="stringliteral">&quot;, Temperature: &quot;</span> &lt;&lt; temperature</div>
<div class="line">            &lt;&lt; <span class="stringliteral">&quot;, Epsilon: &quot;</span> &lt;&lt; epsilon</div>
<div class="line">            &lt;&lt; <span class="stringliteral">&quot;, Iterations: &quot;</span> &lt;&lt; ms</div>
<div class="line">            &lt;&lt; std::endl;</div>
<div class="line"> </div>
<div class="line">    <a name="_a15"></a><a class="code" href="class_quant_lib_1_1_mersenne_twister_uniform_rng.html" title="Uniform random number generator.">MersenneTwisterUniformRng</a> rng(seed);</div>
<div class="line">    <a name="_a16"></a><a class="code" href="class_quant_lib_1_1_simulated_annealing.html" title="Simulated Annealing.">SimulatedAnnealing&lt;MersenneTwisterUniformRng&gt;</a> sa(lambda, temperature, epsilon, ms, rng);</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_end_criteria.html" title="Criteria to end optimization process:">EndCriteria</a> ec(maxSteps, staticSteps, 1.0e-8, 1.0e-8, 1.0e-8);</div>
<div class="line">    test(sa, f, ec, x, constraint, optimum);</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;================================================================&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> testGaussianSA(<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> dimension,</div>
<div class="line">                    <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> maxSteps,</div>
<div class="line">                    <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> staticSteps,</div>
<div class="line">                    <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> initialTemp,</div>
<div class="line">                    <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> finalTemp,</div>
<div class="line">                    GaussianSimulatedAnnealing::ResetScheme resetScheme =</div>
<div class="line">                        GaussianSimulatedAnnealing::ResetToBestPoint,</div>
<div class="line">                    <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> resetSteps = 150,</div>
<div class="line">                    GaussianSimulatedAnnealing::LocalOptimizeScheme optimizeScheme =</div>
<div class="line">                        GaussianSimulatedAnnealing::EveryBestPoint,</div>
<div class="line">                    <span class="keyword">const</span> ext::shared_ptr&lt;OptimizationMethod&gt;&amp; localOptimizer =</div>
<div class="line">                        ext::make_shared&lt;LevenbergMarquardt&gt;()) {</div>
<div class="line"> </div>
<div class="line">    <span class="comment">/*The ackley function has a large amount of local minima, but the</span></div>
<div class="line"><span class="comment">     * structure is symmetric, so if one could simply just ignore the</span></div>
<div class="line"><span class="comment">     * walls separating the local minima, it would look like almost like</span></div>
<div class="line"><span class="comment">     * a parabola*/</span></div>
<div class="line"> </div>
<div class="line">    <span class="comment">//global minimum is at 0.0</span></div>
<div class="line">    TestFunction f(ackley, ackleyValues);</div>
<div class="line"> </div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;Function: ackley, Dimensions: &quot;</span> &lt;&lt; dimension</div>
<div class="line">              &lt;&lt; <span class="stringliteral">&quot;, Initial temp:&quot;</span> &lt;&lt; initialTemp</div>
<div class="line">              &lt;&lt; <span class="stringliteral">&quot;, Final temp:&quot;</span> &lt;&lt; finalTemp</div>
<div class="line">              &lt;&lt; <span class="stringliteral">&quot;, Reset scheme:&quot;</span> &lt;&lt; resetScheme</div>
<div class="line">              &lt;&lt; <span class="stringliteral">&quot;, Reset steps:&quot;</span> &lt;&lt; resetSteps</div>
<div class="line">              &lt;&lt; std::endl;</div>
<div class="line">    <span class="comment">//Starting point</span></div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a> x(dimension, 1.5);</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a> optimum(dimension, 0.0);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">//Constraint for local optimizer</span></div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a> lower(dimension, -5.0);</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a> upper(dimension, 5.0);</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_nonhomogeneous_boundary_constraint.html" title="Constraint imposing i-th argument to be in [low_i,high_i] for all i">NonhomogeneousBoundaryConstraint</a> constraint(lower, upper);</div>
<div class="line"> </div>
<div class="line">    <span class="comment">//Simulated annealing setup</span></div>
<div class="line">    <a name="_a17"></a><a class="code" href="class_quant_lib_1_1_sampler_gaussian.html" title="Gaussian Sampler.">SamplerGaussian</a> sampler(seed);</div>
<div class="line">    <a name="_a18"></a><a class="code" href="class_quant_lib_1_1_probability_boltzmann_downhill.html" title="Boltzmann Downhill Probability.">ProbabilityBoltzmannDownhill</a> probability(seed);</div>
<div class="line">    TemperatureExponential temperature(initialTemp, dimension);</div>
<div class="line">    <a name="_a19"></a><a class="code" href="class_quant_lib_1_1_hybrid_simulated_annealing.html">GaussianSimulatedAnnealing</a> sa(sampler, probability, temperature, <a name="_a20"></a><a class="code" href="struct_quant_lib_1_1_reannealing_trivial.html" title="Reannealing Trivial.">ReannealingTrivial</a>(),</div>
<div class="line">                                  initialTemp, finalTemp, 50, resetScheme,</div>
<div class="line">                                  resetSteps, localOptimizer,</div>
<div class="line">                                  optimizeScheme);</div>
<div class="line"> </div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_end_criteria.html" title="Criteria to end optimization process:">EndCriteria</a> ec(maxSteps, staticSteps, 1.0e-8, 1.0e-8, 1.0e-8);</div>
<div class="line">    test(sa, f, ec, x, constraint, optimum);</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;================================================================&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> testPSO(<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> n){</div>
<div class="line">    <span class="comment">/*The Rosenbrock function has a global minima at (1.0, ...) and a local minima at (-1.0, 1.0, ...)</span></div>
<div class="line"><span class="comment">    The difficulty lies in the weird shape of the function*/</span></div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_nonhomogeneous_boundary_constraint.html" title="Constraint imposing i-th argument to be in [low_i,high_i] for all i">NonhomogeneousBoundaryConstraint</a> constraint(<a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>(n, -1.0), <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>(n, 4.0));</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a> x(n, 0.0);</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a> optimum(n, 1.0);</div>
<div class="line">    <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> agents = 100;</div>
<div class="line">    <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> kneighbor = 25;</div>
<div class="line">    <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> threshold = 500;</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;Function: rosenbrock, Dimensions: &quot;</span> &lt;&lt; n</div>
<div class="line">            &lt;&lt; <span class="stringliteral">&quot;, Agents: &quot;</span> &lt;&lt; agents &lt;&lt; <span class="stringliteral">&quot;, K-neighbors: &quot;</span> &lt;&lt; kneighbor</div>
<div class="line">            &lt;&lt; <span class="stringliteral">&quot;, Threshold: &quot;</span> &lt;&lt; threshold &lt;&lt; std::endl;</div>
<div class="line">    ext::shared_ptr&lt;ParticleSwarmOptimization::Topology&gt; topology =</div>
<div class="line">        ext::make_shared&lt;KNeighbors&gt;(kneighbor);</div>
<div class="line">    ext::shared_ptr&lt;ParticleSwarmOptimization::Inertia&gt; inertia =</div>
<div class="line">        ext::make_shared&lt;LevyFlightInertia&gt;(1.5, threshold, seed);</div>
<div class="line">    TestFunction f(rosenbrock);</div>
<div class="line">    <a name="_a21"></a><a class="code" href="class_quant_lib_1_1_particle_swarm_optimization.html">ParticleSwarmOptimization</a> pso(agents, topology, inertia, 2.05, 2.05, seed);</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_end_criteria.html" title="Criteria to end optimization process:">EndCriteria</a> ec(10000, 1000, 1.0e-8, 1.0e-8, 1.0e-8);</div>
<div class="line">    test(pso, f, ec, x, constraint, optimum);</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;================================================================&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> testDifferentialEvolution(<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> n, <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> agents){</div>
<div class="line">    <span class="comment">/*The Rosenbrock function has a global minima at (1.0, ...) and a local minima at (-1.0, 1.0, ...)</span></div>
<div class="line"><span class="comment">    The difficulty lies in the weird shape of the function*/</span></div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_nonhomogeneous_boundary_constraint.html" title="Constraint imposing i-th argument to be in [low_i,high_i] for all i">NonhomogeneousBoundaryConstraint</a> constraint(<a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>(n, -4.0), <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a>(n, 4.0));</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a> x(n, 0.0);</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_array.html" title="1-D array used in linear algebra.">Array</a> optimum(n, 1.0);</div>
<div class="line"> </div>
<div class="line">    TestFunction f(rosenbrock);</div>
<div class="line"> </div>
<div class="line">    <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> probability = 0.3;</div>
<div class="line">    <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> stepsizeWeight = 0.6;</div>
<div class="line">    DifferentialEvolution::Strategy strategy = DifferentialEvolution::BestMemberWithJitter;</div>
<div class="line"> </div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;Function: rosenbrock, Dimensions: &quot;</span> &lt;&lt; n &lt;&lt; <span class="stringliteral">&quot;, Agents: &quot;</span> &lt;&lt; agents</div>
<div class="line">              &lt;&lt; <span class="stringliteral">&quot;, Probability: &quot;</span> &lt;&lt; probability</div>
<div class="line">              &lt;&lt; <span class="stringliteral">&quot;, StepsizeWeight: &quot;</span> &lt;&lt; stepsizeWeight</div>
<div class="line">              &lt;&lt; <span class="stringliteral">&quot;, Strategy: BestMemberWithJitter&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">    DifferentialEvolution::Configuration config;</div>
<div class="line">    config.withBounds(<span class="keyword">true</span>)</div>
<div class="line">          .withCrossoverProbability(probability)</div>
<div class="line">          .withPopulationMembers(agents)</div>
<div class="line">          .withStepsizeWeight(stepsizeWeight)</div>
<div class="line">          .withStrategy(strategy)</div>
<div class="line">          .withSeed(seed);</div>
<div class="line"> </div>
<div class="line">    <a name="_a22"></a><a class="code" href="class_quant_lib_1_1_differential_evolution.html" title="Differential Evolution configuration object.">DifferentialEvolution</a> de(config);</div>
<div class="line">    <a class="code" href="class_quant_lib_1_1_end_criteria.html" title="Criteria to end optimization process:">EndCriteria</a> ec(5000, 1000, 1.0e-8, 1.0e-8, 1.0e-8);</div>
<div class="line">    test(de, f, ec, x, constraint, optimum);</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;================================================================&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span>, <span class="keywordtype">char</span>* []) {</div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">try</span> {</div>
<div class="line">        std::cout &lt;&lt; std::endl;</div>
<div class="line"> </div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;Firefly Algorithm Test&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;----------------------------------------------------------------&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">        testFirefly();</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;Hybrid Simulated Annealing Test&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;----------------------------------------------------------------&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">        testGaussianSA(3, 500, 200, 100.0, 0.1, GaussianSimulatedAnnealing::ResetToBestPoint, 150, GaussianSimulatedAnnealing::EveryNewPoint);</div>
<div class="line">        testGaussianSA(10, 500, 200, 100.0, 0.1, GaussianSimulatedAnnealing::ResetToBestPoint, 150, GaussianSimulatedAnnealing::EveryNewPoint);</div>
<div class="line">        testGaussianSA(30, 500, 200, 100.0, 0.1, GaussianSimulatedAnnealing::ResetToBestPoint, 150, GaussianSimulatedAnnealing::EveryNewPoint);</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;Particle Swarm Optimization Test&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;----------------------------------------------------------------&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">        testPSO(3);</div>
<div class="line">        testPSO(10);</div>
<div class="line">        testPSO(30);</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;Simulated Annealing Test&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;----------------------------------------------------------------&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">        testSimulatedAnnealing(3, 10000, 4000);</div>
<div class="line">        testSimulatedAnnealing(10, 10000, 4000);</div>
<div class="line">        testSimulatedAnnealing(30, 10000, 4000);</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;Differential Evolution Test&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;----------------------------------------------------------------&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">        testDifferentialEvolution(3, 50);</div>
<div class="line">        testDifferentialEvolution(10, 150);</div>
<div class="line">        testDifferentialEvolution(30, 450);</div>
<div class="line"> </div>
<div class="line">        <span class="keywordflow">return</span> 0;</div>
<div class="line">    } <span class="keywordflow">catch</span> (std::exception&amp; e) {</div>
<div class="line">        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;</div>
<div class="line">        <span class="keywordflow">return</span> 1;</div>
<div class="line">    } <span class="keywordflow">catch</span> (...) {</div>
<div class="line">        std::cerr &lt;&lt; <span class="stringliteral">&quot;unknown error&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">        <span class="keywordflow">return</span> 1;</div>
<div class="line">    }</div>
<div class="line">}</div>
</div><!-- fragment --> </div><!-- contents -->
<!-- HTML footer for doxygen 1.8.9.1-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by <a href="http://www.doxygen.org/index.html">Doxygen</a>
1.8.20
</small></address>
</body>
</html>