Debian Codegraph @

inst/doc/nloptr.html

raw
1 <!DOCTYPE html>
2
3 <html>
4
5 <head>
6
7 <meta charset="utf-8" />
8 <meta name="generator" content="pandoc" />
9 <meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
10
11 <meta name="viewport" content="width=device-width, initial-scale=1" />
12
13 <meta name="author" content="Jelmer Ypma, Aymeric Stamm, and Avraham Adler" />
14
15 <meta name="date" content="2025-03-16" />
16
17 <title>nloptr</title>
18
19 <script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
20 // be compatible with the behavior of Pandoc < 2.8).
21 document.addEventListener('DOMContentLoaded', function(e) {
22 var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
23 var i, h, a;
24 for (i = 0; i < hs.length; i++) {
25 h = hs[i];
26 if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
27 a = h.attributes;
28 while (a.length > 0) h.removeAttribute(a[0].name);
29 }
30 });
31 </script>
32
33 <style type="text/css">
34 code{white-space: pre-wrap;}
35 span.smallcaps{font-variant: small-caps;}
36 span.underline{text-decoration: underline;}
37 div.column{display: inline-block; vertical-align: top; width: 50%;}
38 div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
39 ul.task-list{list-style: none;}
40 </style>
41
42
43
44 <style type="text/css">
45 code {
46 white-space: pre;
47 }
48 .sourceCode {
49 overflow: visible;
50 }
51 </style>
52 <style type="text/css" data-origin="pandoc">
53 pre > code.sourceCode { white-space: pre; position: relative; }
54 pre > code.sourceCode > span { line-height: 1.25; }
55 pre > code.sourceCode > span:empty { height: 1.2em; }
56 .sourceCode { overflow: visible; }
57 code.sourceCode > span { color: inherit; text-decoration: inherit; }
58 div.sourceCode { margin: 1em 0; }
59 pre.sourceCode { margin: 0; }
60 @media screen {
61 div.sourceCode { overflow: auto; }
62 }
63 @media print {
64 pre > code.sourceCode { white-space: pre-wrap; }
65 pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; }
66 }
67 pre.numberSource code
68 { counter-reset: source-line 0; }
69 pre.numberSource code > span
70 { position: relative; left: -4em; counter-increment: source-line; }
71 pre.numberSource code > span > a:first-child::before
72 { content: counter(source-line);
73 position: relative; left: -1em; text-align: right; vertical-align: baseline;
74 border: none; display: inline-block;
75 -webkit-touch-callout: none; -webkit-user-select: none;
76 -khtml-user-select: none; -moz-user-select: none;
77 -ms-user-select: none; user-select: none;
78 padding: 0 4px; width: 4em;
79 color: #aaaaaa;
80 }
81 pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
82 div.sourceCode
83 { }
84 @media screen {
85 pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
86 }
87 code span.al { color: #ff0000; font-weight: bold; }
88 code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
89 code span.at { color: #7d9029; }
90 code span.bn { color: #40a070; }
91 code span.bu { color: #008000; }
92 code span.cf { color: #007020; font-weight: bold; }
93 code span.ch { color: #4070a0; }
94 code span.cn { color: #880000; }
95 code span.co { color: #60a0b0; font-style: italic; }
96 code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
97 code span.do { color: #ba2121; font-style: italic; }
98 code span.dt { color: #902000; }
99 code span.dv { color: #40a070; }
100 code span.er { color: #ff0000; font-weight: bold; }
101 code span.ex { }
102 code span.fl { color: #40a070; }
103 code span.fu { color: #06287e; }
104 code span.im { color: #008000; font-weight: bold; }
105 code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
106 code span.kw { color: #007020; font-weight: bold; }
107 code span.op { color: #666666; }
108 code span.ot { color: #007020; }
109 code span.pp { color: #bc7a00; }
110 code span.sc { color: #4070a0; }
111 code span.ss { color: #bb6688; }
112 code span.st { color: #4070a0; }
113 code span.va { color: #19177c; }
114 code span.vs { color: #4070a0; }
115 code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
116 </style>
117 <script>
118 // apply pandoc div.sourceCode style to pre.sourceCode instead
119 (function() {
120 var sheets = document.styleSheets;
121 for (var i = 0; i < sheets.length; i++) {
122 if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
123 try { var rules = sheets[i].cssRules; } catch (e) { continue; }
124 var j = 0;
125 while (j < rules.length) {
126 var rule = rules[j];
127 // check if there is a div.sourceCode rule
128 if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
129 j++;
130 continue;
131 }
132 var style = rule.style.cssText;
133 // check if color or background-color is set
134 if (rule.style.color === '' && rule.style.backgroundColor === '') {
135 j++;
136 continue;
137 }
138 // replace div.sourceCode by a pre.sourceCode rule
139 sheets[i].deleteRule(j);
140 sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
141 }
142 }
143 })();
144 </script>
145
146
147
148 <style type="text/css">
149
150 div.csl-bib-body { }
151 div.csl-entry {
152 clear: both;
153 margin-bottom: 0em;
154 }
155 .hanging div.csl-entry {
156 margin-left:2em;
157 text-indent:-2em;
158 }
159 div.csl-left-margin {
160 min-width:2em;
161 float:left;
162 }
163 div.csl-right-inline {
164 margin-left:2em;
165 padding-left:1em;
166 }
167 div.csl-indent {
168 margin-left: 2em;
169 }
170 </style>
171
172 <style type="text/css">body {
173 background-color: #fff;
174 margin: 1em auto;
175 max-width: 700px;
176 overflow: visible;
177 padding-left: 2em;
178 padding-right: 2em;
179 font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
180 font-size: 14px;
181 line-height: 1.35;
182 }
183 #TOC {
184 clear: both;
185 margin: 0 0 10px 10px;
186 padding: 4px;
187 width: 400px;
188 border: 1px solid #CCCCCC;
189 border-radius: 5px;
190 background-color: #f6f6f6;
191 font-size: 13px;
192 line-height: 1.3;
193 }
194 #TOC .toctitle {
195 font-weight: bold;
196 font-size: 15px;
197 margin-left: 5px;
198 }
199 #TOC ul {
200 padding-left: 40px;
201 margin-left: -1.5em;
202 margin-top: 5px;
203 margin-bottom: 5px;
204 }
205 #TOC ul ul {
206 margin-left: -2em;
207 }
208 #TOC li {
209 line-height: 16px;
210 }
211 table {
212 margin: 1em auto;
213 border-width: 1px;
214 border-color: #DDDDDD;
215 border-style: outset;
216 border-collapse: collapse;
217 }
218 table th {
219 border-width: 2px;
220 padding: 5px;
221 border-style: inset;
222 }
223 table td {
224 border-width: 1px;
225 border-style: inset;
226 line-height: 18px;
227 padding: 5px 5px;
228 }
229 table, table th, table td {
230 border-left-style: none;
231 border-right-style: none;
232 }
233 table thead, table tr.even {
234 background-color: #f7f7f7;
235 }
236 p {
237 margin: 0.5em 0;
238 }
239 blockquote {
240 background-color: #f6f6f6;
241 padding: 0.25em 0.75em;
242 }
243 hr {
244 border-style: solid;
245 border: none;
246 border-top: 1px solid #777;
247 margin: 28px 0;
248 }
249 dl {
250 margin-left: 0;
251 }
252 dl dd {
253 margin-bottom: 13px;
254 margin-left: 13px;
255 }
256 dl dt {
257 font-weight: bold;
258 }
259 ul {
260 margin-top: 0;
261 }
262 ul li {
263 list-style: circle outside;
264 }
265 ul ul {
266 margin-bottom: 0;
267 }
268 pre, code {
269 background-color: #f7f7f7;
270 border-radius: 3px;
271 color: #333;
272 white-space: pre-wrap;
273 }
274 pre {
275 border-radius: 3px;
276 margin: 5px 0px 10px 0px;
277 padding: 10px;
278 }
279 pre:not([class]) {
280 background-color: #f7f7f7;
281 }
282 code {
283 font-family: Consolas, Monaco, 'Courier New', monospace;
284 font-size: 85%;
285 }
286 p > code, li > code {
287 padding: 2px 0px;
288 }
289 div.figure {
290 text-align: center;
291 }
292 img {
293 background-color: #FFFFFF;
294 padding: 2px;
295 border: 1px solid #DDDDDD;
296 border-radius: 3px;
297 border: 1px solid #CCCCCC;
298 margin: 0 5px;
299 }
300 h1 {
301 margin-top: 0;
302 font-size: 35px;
303 line-height: 40px;
304 }
305 h2 {
306 border-bottom: 4px solid #f7f7f7;
307 padding-top: 10px;
308 padding-bottom: 2px;
309 font-size: 145%;
310 }
311 h3 {
312 border-bottom: 2px solid #f7f7f7;
313 padding-top: 10px;
314 font-size: 120%;
315 }
316 h4 {
317 border-bottom: 1px solid #f7f7f7;
318 margin-left: 8px;
319 font-size: 105%;
320 }
321 h5, h6 {
322 border-bottom: 1px solid #ccc;
323 font-size: 105%;
324 }
325 a {
326 color: #0033dd;
327 text-decoration: none;
328 }
329 a:hover {
330 color: #6666ff; }
331 a:visited {
332 color: #800080; }
333 a:visited:hover {
334 color: #BB00BB; }
335 a[href^="http:"] {
336 text-decoration: underline; }
337 a[href^="https:"] {
338 text-decoration: underline; }
339
340 code > span.kw { color: #555; font-weight: bold; }
341 code > span.dt { color: #902000; }
342 code > span.dv { color: #40a070; }
343 code > span.bn { color: #d14; }
344 code > span.fl { color: #d14; }
345 code > span.ch { color: #d14; }
346 code > span.st { color: #d14; }
347 code > span.co { color: #888888; font-style: italic; }
348 code > span.ot { color: #007020; }
349 code > span.al { color: #ff0000; font-weight: bold; }
350 code > span.fu { color: #900; font-weight: bold; }
351 code > span.er { color: #a61717; background-color: #e3d2d2; }
352 </style>
353
354
355
356
357 </head>
358
359 <body>
360
361
362
363
364 <h1 class="title toc-ignore">nloptr</h1>
365 <h4 class="author">Jelmer Ypma, Aymeric Stamm, and Avraham Adler</h4>
366 <h4 class="date">2025-03-16</h4>
367
368
369
370 <p>This document is an introduction to <code>nloptr</code>: an R
371 interface to NLopt. <a href="https://nlopt.readthedocs.io/en/latest/">NLopt</a> is a
372 free/open-source library for nonlinear optimization, started by Steven
373 G. Johnson, providing a common interface for a number of different free
374 optimization routines available online as well as original
375 implementations of various other algorithms. The NLopt library is
376 available under the GNU Lesser General Public License (LGPL), and the
377 copyrights are owned by a variety of authors. This package should be
378 considered in beta and comments about any aspect of the package are
379 welcome. This document is an R vignette prepared with the aid of
380 <code>knitr</code> <span class="citation">(Xie 2014, 2015, 2016)</span>.
381 Financial support of the UK Economic and Social Research Council through
382 a grant (RES-589-28-0001) to the ESRC Centre for Microdata Methods and
383 Practice (CeMMAP) is gratefully acknowledged.</p>
384 <div id="introduction" class="section level2">
385 <h2>Introduction</h2>
386 <p>NLopt addresses general nonlinear optimization problems of the form:
387 <span class="math display">\[
388 \begin{aligned}
389 &amp;\min_{x \in R^n} f(x) \\
390 s.t.&amp; g(x) \leq 0 \\
391 &amp; h(x) = 0 \\
392 &amp; x_L \leq x \leq x_U
393 \end{aligned}
394 \]</span></p>
395 <p>where <span class="math inline">\(f(\cdot)\)</span> is the objective
396 function and <span class="math inline">\(x\)</span> represents the <span class="math inline">\(n\)</span> optimization parameters. This problem
397 may optionally be subject to the bound constraints (also called box
398 constraints), <span class="math inline">\(x_L\)</span> and <span class="math inline">\(x_U\)</span>. For partially or totally
399 unconstrained problems the bounds can take values <span class="math inline">\(-\infty\)</span> or <span class="math inline">\(\infty\)</span>. One may also optionally have
400 <span class="math inline">\(m\)</span> nonlinear inequality
401 constraints—sometimes called a nonlinear programming problem—which may
402 be specified in <span class="math inline">\(g(\cdot)\)</span>, and
403 equality constraints which may be specified in <span class="math inline">\(h(\cdot)\)</span>. Note that not all of the
404 algorithms in NLopt can handle constraints.</p>
405 <p>This vignette describes how to formulate minimization problems to be
406 solved with the R interface to NLopt. If you want to use the C interface
407 directly or are interested in the Matlab interface, there are other
408 sources of documentation available. Some of the information here has
409 been taken from the NLopt website, where more details are available. All
410 credit for implementing the C code for the different algorithms
411 available in NLopt should go to the respective authors. Also, please see
412 the <a href="https://nlopt.readthedocs.io/en/latest/Citing_NLopt/">website</a>
413 for information on how to cite NLopt and the algorithms you use.</p>
414 </div>
415 <div id="installation" class="section level2">
416 <h2>Installation</h2>
417 <p>This package is on CRAN and can be installed from within R using</p>
418 <div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">&quot;nloptr&quot;</span>)</span></code></pre></div>
419 <p>or</p>
420 <div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">&quot;nloptr&quot;</span>, <span class="at">type =</span> <span class="st">&quot;source&quot;</span>)</span></code></pre></div>
421 <p>to install the package from source. You should now be able to load
422 the R interface to NLopt and read the help.</p>
423 <div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a><span class="fu">library</span>(<span class="st">&quot;nloptr&quot;</span>)</span>
424 <span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a>?nloptr</span></code></pre></div>
425 <p>The most recent experimental <em>source</em> version of
426 <code>nloptr</code> can be installed from Github using the
427 <code>remotes</code> package:</p>
428 <div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a><span class="co"># install.packages(&quot;remotes&quot;)</span></span>
429 <span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a>remotes<span class="sc">::</span><span class="fu">install_github</span>(<span class="st">&quot;astamm/nloptr&quot;</span>)</span></code></pre></div>
430 </div>
431 <div id="minimizing-the-rosenbrock-banana-function" class="section level2">
432 <h2>Minimizing the Rosenbrock Banana function</h2>
433 <p>As a first example we will solve an unconstrained minimization
434 problem. The function we look at is the Rosenbrock Banana function:
435 <span class="math display">\[
436 f(x) = 100 \left(x_2-x_1^2\right)^2 + \left(1-x_1\right)^2,
437 \]</span></p>
438 <p>which is also used as an example in the documentation for the
439 standard R optimizer <code>optim</code>. The gradient of the objective
440 function is given by: <span class="math display">\[
441 \nabla f(x) =
442 \left(\begin{array}[1]{c}
443 -400 \cdot x_1 \cdot (x_2 - x_1^2) - 2 \cdot (1 - x_1) \\
444 200 \cdot (x_2 - x_1^2)
445 \end{array} \right).
446 \]</span></p>
447 <p>Not all of the algorithms in NLopt need gradients to be supplied by
448 the user. We will show examples with and without supplying the gradient.
449 After loading the library.</p>
450 <div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a><span class="fu">library</span>(nloptr)</span></code></pre></div>
451 <p>We start by specifying the objective function and its gradient:</p>
452 <div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a><span class="do">## Rosenbrock Banana function</span></span>
453 <span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a>eval_f <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {</span>
454 <span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a> <span class="dv">100</span> <span class="sc">*</span> (x[<span class="dv">2</span>] <span class="sc">-</span> x[<span class="dv">1</span>] <span class="sc">*</span> x[<span class="dv">1</span>]) <span class="sc">^</span> <span class="dv">2</span> <span class="sc">+</span> (<span class="dv">1</span> <span class="sc">-</span> x[<span class="dv">1</span>]) <span class="sc">^</span> <span class="dv">2</span></span>
455 <span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a>}</span>
456 <span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a></span>
457 <span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a><span class="do">## Gradient of Rosenbrock Banana function</span></span>
458 <span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a>eval_grad_f <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {</span>
459 <span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">400</span> <span class="sc">*</span> x[<span class="dv">1</span>] <span class="sc">*</span> (x[<span class="dv">2</span>] <span class="sc">-</span> x[<span class="dv">1</span>] <span class="sc">*</span> x[<span class="dv">1</span>]) <span class="sc">-</span> <span class="dv">2</span> <span class="sc">*</span> (<span class="dv">1</span> <span class="sc">-</span> x[<span class="dv">1</span>]),</span>
460 <span id="cb6-9"><a href="#cb6-9" tabindex="-1"></a> <span class="dv">200</span> <span class="sc">*</span> (x[<span class="dv">2</span>] <span class="sc">-</span> x[<span class="dv">1</span>] <span class="sc">*</span> x[<span class="dv">1</span>]))</span>
461 <span id="cb6-10"><a href="#cb6-10" tabindex="-1"></a>}</span></code></pre></div>
462 <p>We define initial values</p>
463 <div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a><span class="co"># initial values</span></span>
464 <span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a>x0 <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="sc">-</span><span class="fl">1.2</span>, <span class="dv">1</span>)</span></code></pre></div>
465 <p>and then minimize the function using the <code>nloptr</code> command.
466 This command runs some checks on the supplied inputs and returns an
467 object with the exit code of the solver, the optimal value of the
468 objective function and the solution. Before we can minimize the function
469 we need to specify which algorithm we want to use</p>
470 <div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a>opts <span class="ot">&lt;-</span> <span class="fu">list</span>(<span class="st">&quot;algorithm&quot;</span> <span class="ot">=</span> <span class="st">&quot;NLOPT_LD_LBFGS&quot;</span>,</span>
471 <span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a> <span class="st">&quot;xtol_rel&quot;</span> <span class="ot">=</span> <span class="fl">1.0e-8</span>)</span></code></pre></div>
472 <p>Here we use the L-BFGS algorithm <span class="citation">(Nocedal
473 1980; Liu and Nocedal 1989)</span>. The characters <code>LD</code> in
474 the algorithm show that this algorithm looks for local minima
475 (<code>L</code>) using a derivative-based (<code>D</code>) algorithm.
476 Other algorithms look for global (<code>G</code>) minima, or they don’t
477 need derivatives (<code>N</code>). We also specified the termination
478 criterium in terms of the relative x-tolerance. Other termination
479 criteria are available (see Appendix <code>\ref{sec:descoptions}</code>
480 for a full list of options). We then solve the minimization problem
481 using</p>
482 <div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a><span class="co"># solve Rosenbrock Banana function</span></span>
483 <span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a>res <span class="ot">&lt;-</span> <span class="fu">nloptr</span>(<span class="at">x0 =</span> x0,</span>
484 <span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a> <span class="at">eval_f =</span> eval_f,</span>
485 <span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a> <span class="at">eval_grad_f =</span> eval_grad_f,</span>
486 <span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a> <span class="at">opts =</span> opts)</span></code></pre></div>
487 <p>We can see the results by printing the resulting object.</p>
488 <div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a><span class="fu">print</span>(res)</span></code></pre></div>
489 <pre><code>##
490 ## Call:
491 ##
492 ## nloptr(x0 = x0, eval_f = eval_f, eval_grad_f = eval_grad_f, opts = opts)
493 ##
494 ##
495 ##
496 ## Minimization using NLopt version 2.10.0
497 ##
498 ## NLopt solver status: 1 ( NLOPT_SUCCESS: Generic success
499 ## return value. )
500 ##
501 ## Number of Iterations....: 56
502 ## Termination conditions: xtol_rel: 1e-08
503 ## Number of inequality constraints: 0
504 ## Number of equality constraints: 0
505 ## Optimal value of objective function: 7.35727226897802e-23
506 ## Optimal value of controls: 1 1</code></pre>
507 <p>Sometimes the objective function and its gradient contain common
508 terms. To economize on calculations, we can return the objective and its
509 gradient in a list. For the Rosenbrock Banana function we have for
510 instance:</p>
511 <div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a><span class="do">## Rosenbrock Banana function and gradient in one function</span></span>
512 <span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a>eval_f_list <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {</span>
513 <span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a> common_term <span class="ot">&lt;-</span> x[<span class="dv">2</span>] <span class="sc">-</span> x[<span class="dv">1</span>] <span class="sc">*</span> x[<span class="dv">1</span>]</span>
514 <span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a> <span class="fu">list</span>(<span class="st">&quot;objective&quot;</span> <span class="ot">=</span> <span class="dv">100</span> <span class="sc">*</span> common_term <span class="sc">^</span> <span class="dv">2</span> <span class="sc">+</span> (<span class="dv">1</span> <span class="sc">-</span> x[<span class="dv">1</span>]) <span class="sc">^</span> <span class="dv">2</span>,</span>
515 <span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a> <span class="st">&quot;gradient&quot;</span> <span class="ot">=</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">400</span> <span class="sc">*</span> x[<span class="dv">1</span>] <span class="sc">*</span> common_term <span class="sc">-</span> <span class="dv">2</span> <span class="sc">*</span> (<span class="dv">1</span> <span class="sc">-</span> x[<span class="dv">1</span>]),</span>
516 <span id="cb12-6"><a href="#cb12-6" tabindex="-1"></a> <span class="dv">200</span> <span class="sc">*</span> common_term))</span>
517 <span id="cb12-7"><a href="#cb12-7" tabindex="-1"></a>}</span></code></pre></div>
518 <p>which we minimize using</p>
519 <div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a>res <span class="ot">&lt;-</span> <span class="fu">nloptr</span>(<span class="at">x0 =</span> x0,</span>
520 <span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a> <span class="at">eval_f =</span> eval_f_list,</span>
521 <span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a> <span class="at">opts =</span> opts)</span>
522 <span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a><span class="fu">print</span>(res)</span></code></pre></div>
523 <pre><code>##
524 ## Call:
525 ## nloptr(x0 = x0, eval_f = eval_f_list, opts = opts)
526 ##
527 ##
528 ## Minimization using NLopt version 2.10.0
529 ##
530 ## NLopt solver status: 1 ( NLOPT_SUCCESS: Generic success
531 ## return value. )
532 ##
533 ## Number of Iterations....: 56
534 ## Termination conditions: xtol_rel: 1e-08
535 ## Number of inequality constraints: 0
536 ## Number of equality constraints: 0
537 ## Optimal value of objective function: 7.35727226897802e-23
538 ## Optimal value of controls: 1 1</code></pre>
539 <p>This gives the same results as before.</p>
540 </div>
541 <div id="minimization-with-inequality-constraints" class="section level2">
542 <h2>Minimization with inequality constraints</h2>
543 <p>This section shows how to minimize a function subject to inequality
544 constraints. This example is the same as the one used in the tutorial on
545 the NLopt website. The problem we want to solve is:</p>
546 <p><span class="math display">\[
547 \begin{aligned}
548 &amp;\min_{x \in R^n} \sqrt{x_2} \\
549 s.t.&amp; x_2 \geq 0 \\
550 &amp; x_2 \geq (a_1 x_1 + b_1)^3 \\
551 &amp; x_2 \geq (a_2 x_1 + b_2)^3,
552 \end{aligned}
553 \]</span></p>
554 <p>where <span class="math inline">\(a_1 = 2\)</span>, <span class="math inline">\(b_1 = 0\)</span>, <span class="math inline">\(a_2
555 = -1\)</span>, and <span class="math inline">\(b_2 = 1\)</span>. In
556 order to solve this problem, we first have to re-formulate the
557 constraints to be of the form <span class="math inline">\(g(x)
558 \leq 0\)</span>. Note that the first constraint is a bound on <span class="math inline">\(x_2\)</span>, which we will add later. The other
559 two constraints can be re-written as:</p>
560 <p><span class="math display">\[
561 \begin{aligned}
562 (a_1 x_1 + b_1)^3 - x_2 &amp;\leq 0 \\
563 (a_2 x_1 + b_2)^3 - x_2 &amp;\leq 0
564 \end{aligned}
565 \]</span></p>
566 <p>First, define R functions to calculate the objective function and its
567 gradient:</p>
568 <div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a><span class="co"># objective function</span></span>
569 <span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a>eval_f0 <span class="ot">&lt;-</span> <span class="cf">function</span>(x, a, b) {</span>
570 <span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a> <span class="fu">sqrt</span>(x[<span class="dv">2</span>])</span>
571 <span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a>}</span>
572 <span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a></span>
573 <span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a><span class="co"># gradient of objective function</span></span>
574 <span id="cb15-7"><a href="#cb15-7" tabindex="-1"></a>eval_grad_f0 <span class="ot">&lt;-</span> <span class="cf">function</span>(x, a, b) {</span>
575 <span id="cb15-8"><a href="#cb15-8" tabindex="-1"></a> <span class="fu">c</span>(<span class="dv">0</span>, <span class="fl">0.5</span> <span class="sc">/</span> <span class="fu">sqrt</span>(x[<span class="dv">2</span>]))</span>
576 <span id="cb15-9"><a href="#cb15-9" tabindex="-1"></a>}</span></code></pre></div>
577 <p>If needed, these can of course be calculated in the same function as
578 before. Then define the two constraints and the Jacobian of the
579 constraints:</p>
580 <div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a><span class="co"># constraint function</span></span>
581 <span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a>eval_g0 <span class="ot">&lt;-</span> <span class="cf">function</span>(x, a, b) {</span>
582 <span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a> (a <span class="sc">*</span> x[<span class="dv">1</span>] <span class="sc">+</span> b) <span class="sc">^</span> <span class="dv">3</span> <span class="sc">-</span> x[<span class="dv">2</span>]</span>
583 <span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a>}</span>
584 <span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a></span>
585 <span id="cb16-6"><a href="#cb16-6" tabindex="-1"></a><span class="co"># Jacobian of constraint</span></span>
586 <span id="cb16-7"><a href="#cb16-7" tabindex="-1"></a>eval_jac_g0 <span class="ot">&lt;-</span> <span class="cf">function</span>(x, a, b) {</span>
587 <span id="cb16-8"><a href="#cb16-8" tabindex="-1"></a> <span class="fu">rbind</span>(<span class="fu">c</span>(<span class="dv">3</span> <span class="sc">*</span> a[<span class="dv">1</span>] <span class="sc">*</span> (a[<span class="dv">1</span>] <span class="sc">*</span> x[<span class="dv">1</span>] <span class="sc">+</span> b[<span class="dv">1</span>]) <span class="sc">^</span> <span class="dv">2</span>, <span class="sc">-</span><span class="fl">1.0</span>),</span>
588 <span id="cb16-9"><a href="#cb16-9" tabindex="-1"></a> <span class="fu">c</span>(<span class="dv">3</span> <span class="sc">*</span> a[<span class="dv">2</span>] <span class="sc">*</span> (a[<span class="dv">2</span>] <span class="sc">*</span> x[<span class="dv">1</span>] <span class="sc">+</span> b[<span class="dv">2</span>]) <span class="sc">^</span> <span class="dv">2</span>, <span class="sc">-</span><span class="fl">1.0</span>))</span>
589 <span id="cb16-10"><a href="#cb16-10" tabindex="-1"></a>}</span></code></pre></div>
590 <p>Note that all of the functions above depend on additional parameters,
591 <code>a</code> and <code>b</code>. We have to supply specific values for
592 these when we invoke the optimization command. The constraint function
593 <code>eval_g0</code> returns a vector with in this case the same length
594 as the vectors <code>a</code> and <code>b</code>. The function
595 calculating the Jacobian of the constraint should return a matrix where
596 the number of rows equal the number of constraints (in this case two).
597 The number of columns should equal the number of control variables (two
598 in this case as well).</p>
599 <p>After defining values for the parameters</p>
600 <div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a><span class="co"># define parameters</span></span>
601 <span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a>a <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="dv">2</span>, <span class="sc">-</span><span class="dv">1</span>)</span>
602 <span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a>b <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">1</span>)</span></code></pre></div>
603 <p>we can minimize the function subject to the constraints with the
604 following command:</p>
605 <div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" tabindex="-1"></a><span class="co"># Solve using NLOPT_LD_MMA with gradient information supplied in separate</span></span>
606 <span id="cb18-2"><a href="#cb18-2" tabindex="-1"></a><span class="co"># function</span></span>
607 <span id="cb18-3"><a href="#cb18-3" tabindex="-1"></a>res0 <span class="ot">&lt;-</span> <span class="fu">nloptr</span>(<span class="at">x0 =</span> <span class="fu">c</span>(<span class="fl">1.234</span>, <span class="fl">5.678</span>),</span>
608 <span id="cb18-4"><a href="#cb18-4" tabindex="-1"></a> <span class="at">eval_f =</span> eval_f0,</span>
609 <span id="cb18-5"><a href="#cb18-5" tabindex="-1"></a> <span class="at">eval_grad_f =</span> eval_grad_f0,</span>
610 <span id="cb18-6"><a href="#cb18-6" tabindex="-1"></a> <span class="at">lb =</span> <span class="fu">c</span>(<span class="sc">-</span><span class="cn">Inf</span>, <span class="dv">0</span>),</span>
611 <span id="cb18-7"><a href="#cb18-7" tabindex="-1"></a> <span class="at">ub =</span> <span class="fu">c</span>(<span class="cn">Inf</span>, <span class="cn">Inf</span>),</span>
612 <span id="cb18-8"><a href="#cb18-8" tabindex="-1"></a> <span class="at">eval_g_ineq =</span> eval_g0,</span>
613 <span id="cb18-9"><a href="#cb18-9" tabindex="-1"></a> <span class="at">eval_jac_g_ineq =</span> eval_jac_g0,</span>
614 <span id="cb18-10"><a href="#cb18-10" tabindex="-1"></a> <span class="at">opts =</span> <span class="fu">list</span>(<span class="st">&quot;algorithm&quot;</span> <span class="ot">=</span> <span class="st">&quot;NLOPT_LD_MMA&quot;</span>,</span>
615 <span id="cb18-11"><a href="#cb18-11" tabindex="-1"></a> <span class="st">&quot;xtol_rel&quot;</span> <span class="ot">=</span> <span class="fl">1.0e-8</span>,</span>
616 <span id="cb18-12"><a href="#cb18-12" tabindex="-1"></a> <span class="st">&quot;print_level&quot;</span> <span class="ot">=</span> <span class="dv">2</span>,</span>
617 <span id="cb18-13"><a href="#cb18-13" tabindex="-1"></a> <span class="st">&quot;check_derivatives&quot;</span> <span class="ot">=</span> <span class="cn">TRUE</span>,</span>
618 <span id="cb18-14"><a href="#cb18-14" tabindex="-1"></a> <span class="st">&quot;check_derivatives_print&quot;</span> <span class="ot">=</span> <span class="st">&quot;all&quot;</span>),</span>
619 <span id="cb18-15"><a href="#cb18-15" tabindex="-1"></a> <span class="at">a =</span> a,</span>
620 <span id="cb18-16"><a href="#cb18-16" tabindex="-1"></a> <span class="at">b =</span> b)</span></code></pre></div>
621 <pre><code>## Checking gradients of objective function.</code></pre>
622 <pre><code>## Derivative checker results: 0 error(s) detected.</code></pre>
623 <pre><code>##
624 ## eval_grad_f[1] = 0.000000e+00 ~ 0.000000e+00 [0.000000e+00]
625 ## eval_grad_f[2] = 2.098323e-01 ~ 2.098323e-01 [1.422937e-09]</code></pre>
626 <pre><code>## Checking gradients of inequality constraints.</code></pre>
627 <pre><code>## Derivative checker results: 0 error(s) detected.</code></pre>
628 <pre><code>##
629 ## eval_jac_g_ineq[1, 1] = 3.654614e+01 ~ 3.654614e+01 [1.667794e-08]
630 ## eval_jac_g_ineq[2, 1] = -1.642680e-01 ~ -1.642680e-01 [2.103453e-07]
631 ## eval_jac_g_ineq[1, 2] = -1.000000e+00 ~ -1.000000e+00 [0.000000e+00]
632 ## eval_jac_g_ineq[2, 2] = -1.000000e+00 ~ -1.000000e+00 [0.000000e+00]</code></pre>
633 <pre><code>## iteration: 1
634 ## f(x) = 2.382855
635 ## g(x) = (9.354647, -5.690813)
636 ## iteration: 2
637 ## f(x) = 2.356135
638 ## g(x) = (-0.122988, -5.549587)
639 ## iteration: 3
640 ## f(x) = 2.245864
641 ## g(x) = (-0.531886, -5.038655)
642 ## iteration: 4
643 ## f(x) = 2.019102
644 ## g(x) = (-3.225103, -3.931195)
645 ## iteration: 5
646 ## f(x) = 1.740934
647 ## g(x) = (-2.676263, -2.761136)
648 ## iteration: 6
649 ## f(x) = 1.404206
650 ## g(x) = (-1.674055, -1.676216)
651 ## iteration: 7
652 ## f(x) = 1.022295
653 ## g(x) = (-0.748790, -0.748792)
654 ## iteration: 8
655 ## f(x) = 0.685203
656 ## g(x) = (-0.173206, -0.173207)
657 ## iteration: 9
658 ## f(x) = 0.552985
659 ## g(x) = (-0.009496, -0.009496)
660 ## iteration: 10
661 ## f(x) = 0.544354
662 ## g(x) = (-0.000025, -0.000025)
663 ## iteration: 11
664 ## f(x) = 0.544331
665 ## g(x) = (0.000000, 0.000000)
666 ## iteration: 12
667 ## f(x) = 0.544331
668 ## g(x) = (-0.000000, 0.000000)
669 ## iteration: 13
670 ## f(x) = 0.544331
671 ## g(x) = (-0.000000, 0.000000)
672 ## iteration: 14
673 ## f(x) = 0.544331
674 ## g(x) = (-0.000000, 0.000000)
675 ## iteration: 15
676 ## f(x) = 0.544331
677 ## g(x) = (-0.000000, 0.000000)
678 ## iteration: 16
679 ## f(x) = 0.544331
680 ## g(x) = (-0.000000, 0.000000)
681 ## iteration: 17
682 ## f(x) = 0.544331
683 ## g(x) = (-0.000000, 0.000000)
684 ## iteration: 18
685 ## f(x) = 0.544331
686 ## g(x) = (-0.000000, 0.000000)
687 ## iteration: 19
688 ## f(x) = 0.544331
689 ## g(x) = (0.000000, 0.000000)
690 ## iteration: 20
691 ## f(x) = 0.544331
692 ## g(x) = (-0.000000, -0.000000)
693 ## iteration: 21
694 ## f(x) = 0.544331
695 ## g(x) = (0.000000, 0.000000)</code></pre>
696 <div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" tabindex="-1"></a><span class="fu">print</span>(res0)</span></code></pre></div>
697 <pre><code>##
698 ## Call:
699 ##
700 ## nloptr(x0 = c(1.234, 5.678), eval_f = eval_f0, eval_grad_f = eval_grad_f0,
701 ## lb = c(-Inf, 0), ub = c(Inf, Inf), eval_g_ineq = eval_g0,
702 ## eval_jac_g_ineq = eval_jac_g0, opts = list(algorithm = &quot;NLOPT_LD_MMA&quot;,
703 ## xtol_rel = 1e-08, print_level = 2, check_derivatives = TRUE,
704 ## check_derivatives_print = &quot;all&quot;), a = a, b = b)
705 ##
706 ##
707 ## Minimization using NLopt version 2.10.0
708 ##
709 ## NLopt solver status: 4 ( NLOPT_XTOL_REACHED: Optimization
710 ## stopped because xtol_rel or xtol_abs (above) was reached.
711 ## )
712 ##
713 ## Number of Iterations....: 21
714 ## Termination conditions: xtol_rel: 1e-08
715 ## Number of inequality constraints: 2
716 ## Number of equality constraints: 0
717 ## Optimal value of objective function: 0.54433104762009
718 ## Optimal value of controls: 0.3333333 0.2962963</code></pre>
719 <p>Here we supplied lower bounds for <span class="math inline">\(x_2\)</span> in <code>lb</code>. There are no
720 upper bounds for both control variables, so we supply <code>Inf</code>
721 values. If we don’t supply lower or upper bounds, plus or minus infinity
722 is chosen by default. The inequality constraints and its Jacobian are
723 defined using <code>eval_g_ineq</code> and <code>eval_jac_g_ineq</code>.
724 Not all algorithms can handle inequality constraints, so we have to
725 specify one that does, <code>NLOPT_LD_MMA</code> <span class="citation">(Svanberg 2002)</span>.</p>
726 <p>We also specify the option <code>print_level</code> to obtain output
727 during the optimization process. For the available
728 <code>print_level</code> values, see <code>?nloptr</code>. Setting the
729 <code>check_derivatives</code> option to <code>TRUE</code>, compares the
730 gradients supplied by the user with a finite difference approximation in
731 the initial point (<code>x0</code>). When this check is run, the option
732 <code>check_derivatives_print</code> can be used to print all values of
733 the derivative checker (<code>all</code> (default)), only those values
734 that result in an error (<code>errors</code>) or no output
735 (<code>none</code>), in which case only the number of errors is shown.
736 The tolerance that determines if a difference between the analytic
737 gradient and the finite difference approximation results in an error can
738 be set using the option <code>check_derivatives_tol</code> (default =
739 1e-04). The first column shows the value of the analytic gradient, the
740 second column shows the value of the finite difference approximation,
741 and the third column shows the relative error. Stars are added at the
742 front of a line if the relative error is larger than the specified
743 tolerance.</p>
744 <p>Finally, we add all the parameters that have to be passed on to the
745 objective and constraint functions, <code>a</code> and
746 <code>b</code>.</p>
747 <p>We can also use a different algorithm to solve the same minimization
748 problem. The only thing we have to change is the algorithm that we want
749 to use, in this case <code>NLOPT_LN_COBYLA</code>, which is an algorithm
750 that doesn’t need gradient information <span class="citation">(Powell
751 1994, 1998)</span>.</p>
752 <div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" tabindex="-1"></a><span class="co"># Solve using NLOPT_LN_COBYLA without gradient information</span></span>
753 <span id="cb28-2"><a href="#cb28-2" tabindex="-1"></a>res1 <span class="ot">&lt;-</span> <span class="fu">nloptr</span>(<span class="at">x0 =</span> <span class="fu">c</span>(<span class="fl">1.234</span>, <span class="fl">5.678</span>),</span>
754 <span id="cb28-3"><a href="#cb28-3" tabindex="-1"></a> <span class="at">eval_f =</span> eval_f0,</span>
755 <span id="cb28-4"><a href="#cb28-4" tabindex="-1"></a> <span class="at">lb =</span> <span class="fu">c</span>(<span class="sc">-</span><span class="cn">Inf</span>, <span class="dv">0</span>),</span>
756 <span id="cb28-5"><a href="#cb28-5" tabindex="-1"></a> <span class="at">ub =</span> <span class="fu">c</span>(<span class="cn">Inf</span>, <span class="cn">Inf</span>),</span>
757 <span id="cb28-6"><a href="#cb28-6" tabindex="-1"></a> <span class="at">eval_g_ineq =</span> eval_g0,</span>
758 <span id="cb28-7"><a href="#cb28-7" tabindex="-1"></a> <span class="at">opts =</span> <span class="fu">list</span>(<span class="st">&quot;algorithm&quot;</span> <span class="ot">=</span> <span class="st">&quot;NLOPT_LN_COBYLA&quot;</span>,</span>
759 <span id="cb28-8"><a href="#cb28-8" tabindex="-1"></a> <span class="st">&quot;xtol_rel&quot;</span> <span class="ot">=</span> <span class="fl">1.0e-8</span>),</span>
760 <span id="cb28-9"><a href="#cb28-9" tabindex="-1"></a> <span class="at">a =</span> a,</span>
761 <span id="cb28-10"><a href="#cb28-10" tabindex="-1"></a> <span class="at">b =</span> b)</span>
762 <span id="cb28-11"><a href="#cb28-11" tabindex="-1"></a><span class="fu">print</span>(res1)</span></code></pre></div>
763 <pre><code>##
764 ## Call:
765 ##
766 ## nloptr(x0 = c(1.234, 5.678), eval_f = eval_f0, lb = c(-Inf, 0),
767 ## ub = c(Inf, Inf), eval_g_ineq = eval_g0, opts = list(algorithm = &quot;NLOPT_LN_COBYLA&quot;,
768 ## xtol_rel = 1e-08), a = a, b = b)
769 ##
770 ##
771 ## Minimization using NLopt version 2.10.0
772 ##
773 ## NLopt solver status: 4 ( NLOPT_XTOL_REACHED: Optimization
774 ## stopped because xtol_rel or xtol_abs (above) was reached.
775 ## )
776 ##
777 ## Number of Iterations....: 50
778 ## Termination conditions: xtol_rel: 1e-08
779 ## Number of inequality constraints: 2
780 ## Number of equality constraints: 0
781 ## Optimal value of objective function: 0.544331053951819
782 ## Optimal value of controls: 0.3333333 0.2962963</code></pre>
783 </div>
784 <div id="derivative-checker" class="section level2">
785 <h2>Derivative checker</h2>
786 <p>The derivative checker can be called when supplying a minimization
787 problem to <code>nloptr</code>, using the options
788 <code>check_derivatives</code>, <code>check_derivatives_tol</code> and
789 <code>check_derivatives_print</code>, but it can also be used
790 separately. For example, define the function <code>g</code>, with vector
791 outcome, and its gradient <code>g_grad</code>:</p>
792 <div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" tabindex="-1"></a>g <span class="ot">&lt;-</span> <span class="cf">function</span>(x, a) {</span>
793 <span id="cb30-2"><a href="#cb30-2" tabindex="-1"></a> <span class="fu">c</span>(x[<span class="dv">1</span>] <span class="sc">-</span> a[<span class="dv">1</span>],</span>
794 <span id="cb30-3"><a href="#cb30-3" tabindex="-1"></a> x[<span class="dv">2</span>] <span class="sc">-</span> a[<span class="dv">2</span>],</span>
795 <span id="cb30-4"><a href="#cb30-4" tabindex="-1"></a> (x[<span class="dv">1</span>] <span class="sc">-</span> a[<span class="dv">1</span>]) <span class="sc">^</span> <span class="dv">2</span>,</span>
796 <span id="cb30-5"><a href="#cb30-5" tabindex="-1"></a> (x[<span class="dv">2</span>] <span class="sc">-</span> a[<span class="dv">2</span>]) <span class="sc">^</span> <span class="dv">2</span>,</span>
797 <span id="cb30-6"><a href="#cb30-6" tabindex="-1"></a> (x[<span class="dv">1</span>] <span class="sc">-</span> a[<span class="dv">1</span>]) <span class="sc">^</span> <span class="dv">3</span>,</span>
798 <span id="cb30-7"><a href="#cb30-7" tabindex="-1"></a> (x[<span class="dv">2</span>] <span class="sc">-</span> a[<span class="dv">2</span>]) <span class="sc">^</span> <span class="dv">3</span>)</span>
799 <span id="cb30-8"><a href="#cb30-8" tabindex="-1"></a>}</span>
800 <span id="cb30-9"><a href="#cb30-9" tabindex="-1"></a></span>
801 <span id="cb30-10"><a href="#cb30-10" tabindex="-1"></a>g_grad <span class="ot">&lt;-</span> <span class="cf">function</span>(x, a) {</span>
802 <span id="cb30-11"><a href="#cb30-11" tabindex="-1"></a> <span class="fu">rbind</span>(</span>
803 <span id="cb30-12"><a href="#cb30-12" tabindex="-1"></a> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">0</span>),</span>
804 <span id="cb30-13"><a href="#cb30-13" tabindex="-1"></a> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">1</span>),</span>
805 <span id="cb30-14"><a href="#cb30-14" tabindex="-1"></a> <span class="fu">c</span>(<span class="dv">2</span> <span class="sc">*</span> (x[<span class="dv">1</span>] <span class="sc">-</span> a[<span class="dv">1</span>]), <span class="dv">0</span>),</span>
806 <span id="cb30-15"><a href="#cb30-15" tabindex="-1"></a> <span class="fu">c</span>(<span class="dv">2</span> <span class="sc">*</span> (x[<span class="dv">1</span>] <span class="sc">-</span> a[<span class="dv">1</span>]), <span class="dv">2</span> <span class="sc">*</span> (x[<span class="dv">2</span>] <span class="sc">-</span> a[<span class="dv">2</span>])),</span>
807 <span id="cb30-16"><a href="#cb30-16" tabindex="-1"></a> <span class="fu">c</span>(<span class="dv">3</span> <span class="sc">*</span> (x[<span class="dv">1</span>] <span class="sc">-</span> a[<span class="dv">2</span>]) <span class="sc">^</span> <span class="dv">2</span>, <span class="dv">0</span>),</span>
808 <span id="cb30-17"><a href="#cb30-17" tabindex="-1"></a> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">3</span> <span class="sc">*</span> (x[<span class="dv">2</span>] <span class="sc">-</span> a[<span class="dv">2</span>]) <span class="sc">^</span> <span class="dv">2</span>)</span>
809 <span id="cb30-18"><a href="#cb30-18" tabindex="-1"></a> )</span>
810 <span id="cb30-19"><a href="#cb30-19" tabindex="-1"></a>}</span></code></pre></div>
811 <p><code>a</code> is some vector containing data. The gradient contains
812 some errors in this case. By calling the function
813 <code>check.derivatives</code> we can check the user-supplied analytic
814 gradients with a finite difference approximation at a point
815 <code>.x</code>.</p>
816 <div class="sourceCode" id="cb31"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" tabindex="-1"></a>res <span class="ot">&lt;-</span> <span class="fu">check.derivatives</span>(</span>
817 <span id="cb31-2"><a href="#cb31-2" tabindex="-1"></a> <span class="at">.x =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>),</span>
818 <span id="cb31-3"><a href="#cb31-3" tabindex="-1"></a> <span class="at">func =</span> g,</span>
819 <span id="cb31-4"><a href="#cb31-4" tabindex="-1"></a> <span class="at">func_grad =</span> g_grad,</span>
820 <span id="cb31-5"><a href="#cb31-5" tabindex="-1"></a> <span class="at">check_derivatives_print =</span> <span class="st">&quot;all&quot;</span>,</span>
821 <span id="cb31-6"><a href="#cb31-6" tabindex="-1"></a> <span class="at">a =</span> <span class="fu">c</span>(<span class="fl">0.3</span>, <span class="fl">0.8</span>)</span>
822 <span id="cb31-7"><a href="#cb31-7" tabindex="-1"></a>)</span></code></pre></div>
823 <pre><code>## Derivative checker results: 2 error(s) detected.</code></pre>
824 <pre><code>##
825 ## grad_f[1, 1] = 1.00e+00 ~ 1.00e+00 [0.000000e+00]
826 ## grad_f[2, 1] = 0.00e+00 ~ 0.00e+00 [0.000000e+00]
827 ## grad_f[3, 1] = 1.40e+00 ~ 1.40e+00 [9.579318e-09]
828 ## * grad_f[4, 1] = 1.40e+00 ~ 0.00e+00 [1.400000e+00]
829 ## * grad_f[5, 1] = 1.20e-01 ~ 1.47e+00 [9.183673e-01]
830 ## grad_f[6, 1] = 0.00e+00 ~ 0.00e+00 [0.000000e+00]
831 ## grad_f[1, 2] = 0.00e+00 ~ 0.00e+00 [0.000000e+00]
832 ## grad_f[2, 2] = 1.00e+00 ~ 1.00e+00 [0.000000e+00]
833 ## grad_f[3, 2] = 0.00e+00 ~ 0.00e+00 [0.000000e+00]
834 ## grad_f[4, 2] = 2.40e+00 ~ 2.40e+00 [1.179675e-08]
835 ## grad_f[5, 2] = 0.00e+00 ~ 0.00e+00 [0.000000e+00]
836 ## grad_f[6, 2] = 4.32e+00 ~ 4.32e+00 [2.593906e-08]</code></pre>
837 <p>The errors are shown on screen, where the option
838 <code>check_derivatives_print</code> determines the amount of output you
839 see. The value of the analytic gradient and the value of the finite
840 difference approximation at the supplied point is returned in a
841 list.</p>
842 <div class="sourceCode" id="cb34"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" tabindex="-1"></a>res</span></code></pre></div>
843 <pre><code>## $analytic
844 ## [,1] [,2]
845 ## [1,] 1.00 0.00
846 ## [2,] 0.00 1.00
847 ## [3,] 1.40 0.00
848 ## [4,] 1.40 2.40
849 ## [5,] 0.12 0.00
850 ## [6,] 0.00 4.32
851 ##
852 ## $finite_difference
853 ## [,1] [,2]
854 ## [1,] 1.00 0.00
855 ## [2,] 0.00 1.00
856 ## [3,] 1.40 0.00
857 ## [4,] 0.00 2.40
858 ## [5,] 1.47 0.00
859 ## [6,] 0.00 4.32
860 ##
861 ## $relative_error
862 ## [,1] [,2]
863 ## [1,] 0.000000e+00 0.000000e+00
864 ## [2,] 0.000000e+00 0.000000e+00
865 ## [3,] 9.579318e-09 0.000000e+00
866 ## [4,] 1.400000e+00 1.179675e-08
867 ## [5,] 9.183673e-01 0.000000e+00
868 ## [6,] 0.000000e+00 2.593906e-08
869 ##
870 ## $flag_derivative_warning
871 ## [,1] [,2]
872 ## [1,] FALSE FALSE
873 ## [2,] FALSE FALSE
874 ## [3,] FALSE FALSE
875 ## [4,] TRUE FALSE
876 ## [5,] TRUE FALSE
877 ## [6,] FALSE FALSE</code></pre>
878 <p>Note that not all errors will be picked up by the derivative checker.
879 For instance, if we run the check with <code>a = c(.5, .5)</code>, one
880 of the errors is not flagged as an error.</p>
881 </div>
882 <div id="notes" class="section level2">
883 <h2>Notes</h2>
884 <p>The <code>.R</code> scripts in the <code>tests</code> directory
885 contain more examples. For instance, <code>hs071.R</code> and
886 <code>systemofeq.R</code> show how to solve problems with equality
887 constraints. See the <a href="https://nlopt.readthedocs.io/en/latest/NLopt_Algorithms/#augmented-lagrangian-algorithm">NLopt
888 website</a> for more details. Please let us know if there are any of
889 features implemented in NLopt which should be implemented in
890 <code>nloptr</code>.</p>
891 <p>Sometimes the optimization procedure terminates with a message
892 <code>maxtime was reached</code> without evaluating the objective
893 function. Submitting the same problem again usually solves this
894 problem.</p>
895 </div>
896 <div id="description-of-options" class="section level2">
897 <h2>Description of options</h2>
898 <div class="sourceCode" id="cb36"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" tabindex="-1"></a>nloptr<span class="sc">::</span><span class="fu">nloptr.print.options</span>()</span></code></pre></div>
899 <pre><code>## algorithm
900 ## possible values: NLOPT_GN_DIRECT, NLOPT_GN_DIRECT_L,
901 ## NLOPT_GN_DIRECT_L_RAND, NLOPT_GN_DIRECT_NOSCAL,
902 ## NLOPT_GN_DIRECT_L_NOSCAL,
903 ## NLOPT_GN_DIRECT_L_RAND_NOSCAL,
904 ## NLOPT_GN_ORIG_DIRECT, NLOPT_GN_ORIG_DIRECT_L,
905 ## NLOPT_GD_STOGO, NLOPT_GD_STOGO_RAND,
906 ## NLOPT_LD_SLSQP, NLOPT_LD_LBFGS, NLOPT_LN_PRAXIS,
907 ## NLOPT_LD_VAR1, NLOPT_LD_VAR2, NLOPT_LD_TNEWTON,
908 ## NLOPT_LD_TNEWTON_RESTART,
909 ## NLOPT_LD_TNEWTON_PRECOND,
910 ## NLOPT_LD_TNEWTON_PRECOND_RESTART,
911 ## NLOPT_GN_CRS2_LM, NLOPT_GN_MLSL, NLOPT_GD_MLSL,
912 ## NLOPT_GN_MLSL_LDS, NLOPT_GD_MLSL_LDS,
913 ## NLOPT_LD_MMA, NLOPT_LD_CCSAQ, NLOPT_LN_COBYLA,
914 ## NLOPT_LN_NEWUOA, NLOPT_LN_NEWUOA_BOUND,
915 ## NLOPT_LN_NELDERMEAD, NLOPT_LN_SBPLX,
916 ## NLOPT_LN_AUGLAG, NLOPT_LD_AUGLAG,
917 ## NLOPT_LN_AUGLAG_EQ, NLOPT_LD_AUGLAG_EQ,
918 ## NLOPT_LN_BOBYQA, NLOPT_GN_ISRES
919 ## default value: none
920 ##
921 ## This option is required. Check the NLopt website for a description of
922 ## the algorithms.
923 ##
924 ## stopval
925 ## possible values: -Inf &lt;= stopval &lt;= Inf
926 ## default value: -Inf
927 ##
928 ## Stop minimization when an objective value &lt;= stopval is found.
929 ## Setting stopval to -Inf disables this stopping criterion (default).
930 ##
931 ## ftol_rel
932 ## possible values: ftol_rel &gt; 0
933 ## default value: 0.0
934 ##
935 ## Stop when an optimization step (or an estimate of the optimum)
936 ## changes the objective function value by less than ftol_rel multiplied
937 ## by the absolute value of the function value. If there is any chance
938 ## that your optimum function value is close to zero, you might want to
939 ## set an absolute tolerance with ftol_abs as well. Criterion is
940 ## disabled if ftol_rel is non-positive (default).
941 ##
942 ## ftol_abs
943 ## possible values: ftol_abs &gt; 0
944 ## default value: 0.0
945 ##
946 ## Stop when an optimization step (or an estimate of the optimum)
947 ## changes the function value by less than ftol_abs. Criterion is
948 ## disabled if ftol_abs is non-positive (default).
949 ##
950 ## xtol_rel
951 ## possible values: xtol_rel &gt; 0
952 ## default value: 1.0e-04
953 ##
954 ## Stop when an optimization step (or an estimate of the optimum)
955 ## changes every parameter by less than xtol_rel multiplied by the
956 ## absolute value of the parameter. If there is any chance that an
957 ## optimal parameter is close to zero, you might want to set an absolute
958 ## tolerance with xtol_abs as well. Criterion is disabled if xtol_rel is
959 ## non-positive.
960 ##
961 ## xtol_abs
962 ## possible values: xtol_abs &gt; 0
963 ## default value: rep(0.0, length(x0))
964 ##
965 ## xtol_abs is a vector of length n (the number of elements in x) giving
966 ## the tolerances: stop when an optimization step (or an estimate of the
967 ## optimum) changes every parameter x[i] by less than xtol_abs[i].
968 ## Criterion is disabled if all elements of xtol_abs are non-positive
969 ## (default).
970 ##
971 ## maxeval
972 ## possible values: maxeval is a positive integer
973 ## default value: 100
974 ##
975 ## Stop when the number of function evaluations exceeds maxeval. This is
976 ## not a strict maximum: the number of function evaluations may exceed
977 ## maxeval slightly, depending upon the algorithm. Criterion is disabled
978 ## if maxeval is non-positive.
979 ##
980 ## maxtime
981 ## possible values: maxtime &gt; 0
982 ## default value: -1.0
983 ##
984 ## Stop when the optimization time (in seconds) exceeds maxtime. This is
985 ## not a strict maximum: the time may exceed maxtime slightly, depending
986 ## upon the algorithm and on how slow your function evaluation is.
987 ## Criterion is disabled if maxtime is non-positive (default).
988 ##
989 ## tol_constraints_ineq
990 ## possible values: tol_constraints_ineq &gt; 0.0
991 ## default value: rep(1e-8, num_constraints_ineq)
992 ##
993 ## The parameter tol_constraints_ineq is a vector of tolerances. Each
994 ## tolerance corresponds to one of the inequality constraints. The
995 ## tolerance is used for the purpose of stopping criteria only: a point
996 ## x is considered feasible for judging whether to stop the optimization
997 ## if eval_g_ineq(x) &lt;= tol. A tolerance of zero means that NLopt will
998 ## try not to consider any x to be converged unless eval_g_ineq(x) is
999 ## strictly non-positive; generally, at least a small positive tolerance
1000 ## is advisable to reduce sensitivity to rounding errors. By default the
1001 ## tolerances for all inequality constraints are set to 1e-8.
1002 ##
1003 ## tol_constraints_eq
1004 ## possible values: tol_constraints_eq &gt; 0.0
1005 ## default value: rep(1e-8, num_constraints_eq)
1006 ##
1007 ## The parameter tol_constraints_eq is a vector of tolerances. Each
1008 ## tolerance corresponds to one of the equality constraints. The
1009 ## tolerance is used for the purpose of stopping criteria only: a point
1010 ## x is considered feasible for judging whether to stop the optimization
1011 ## if abs(eval_g_ineq(x)) &lt;= tol. For equality constraints, a small
1012 ## positive tolerance is strongly advised in order to allow NLopt to
1013 ## converge even if the equality constraint is slightly nonzero. By
1014 ## default the tolerances for all quality constraints are set to 1e-8.
1015 ##
1016 ## print_level
1017 ## possible values: 0, 1, 2, or 3
1018 ## default value: 0
1019 ##
1020 ## The option print_level controls how much output is shown during the
1021 ## optimization process. Possible values: 0 (default): no output; 1:
1022 ## show iteration number and value of objective function; 2: 1 + show
1023 ## value of (in)equalities; 3: 2 + show value of controls.
1024 ##
1025 ## check_derivatives
1026 ## possible values: TRUE or FALSE
1027 ## default value: FALSE
1028 ##
1029 ## The option check_derivatives can be activated to compare the
1030 ## user-supplied analytic gradients with finite difference
1031 ## approximations.
1032 ##
1033 ## check_derivatives_tol
1034 ## possible values: check_derivatives_tol &gt; 0.0
1035 ## default value: 1e-04
1036 ##
1037 ## The option check_derivatives_tol determines when a difference between
1038 ## an analytic gradient and its finite difference approximation is
1039 ## flagged as an error.
1040 ##
1041 ## check_derivatives_print
1042 ## possible values: &#39;none&#39;, &#39;all&#39;, &#39;errors&#39;,
1043 ## default value: all
1044 ##
1045 ## The option check_derivatives_print controls the output of the
1046 ## derivative checker (if check_derivatives == TRUE). All comparisons
1047 ## are shown (&#39;all&#39;), only those comparisions that resulted in an error
1048 ## (&#39;error&#39;), or only the number of errors is shown (&#39;none&#39;).
1049 ##
1050 ## print_options_doc
1051 ## possible values: TRUE or FALSE
1052 ## default value: FALSE
1053 ##
1054 ## If TRUE, a description of all options and their current and default
1055 ## values is printed to the screen.
1056 ##
1057 ## population
1058 ## possible values: population is a positive integer
1059 ## default value: 0
1060 ##
1061 ## Several of the stochastic search algorithms (e.g., CRS, MLSL, and
1062 ## ISRES) start by generating some initial population of random points
1063 ## x. By default, this initial population size is chosen heuristically
1064 ## in some algorithm-specific way, but the initial population can by
1065 ## changed by setting a positive integer value for population. A
1066 ## population of zero implies that the heuristic default will be used.
1067 ##
1068 ## vector_storage
1069 ## possible values: vector_storage is a positive integer
1070 ## default value: 20
1071 ##
1072 ## Number of gradients to remember from previous optimization steps.
1073 ##
1074 ## ranseed
1075 ## possible values: ranseed is a positive integer
1076 ## default value: 0
1077 ##
1078 ## For stochastic optimization algorithms, pseudorandom numbers are
1079 ## generated. Set the random seed using ranseed if you want to use a
1080 ## &#39;deterministic&#39; sequence of pseudorandom numbers, i.e. the same
1081 ## sequence from run to run. If ranseed is 0 (default), the seed for the
1082 ## random numbers is generated from the system time, so that you will
1083 ## get a different sequence of pseudorandom numbers each time you run
1084 ## your program.</code></pre>
1085 </div>
1086 <div id="references" class="section level2 unnumbered">
1087 <h2 class="unnumbered">References</h2>
1088 <div id="refs" class="references csl-bib-body hanging-indent" entry-spacing="0">
1089 <div id="ref-NLopt:website" class="csl-entry">
1090 Johnson, Steven G. n.d. <span>“The <span>N</span><span>L</span>opt
1091 Nonlinear-Optimization Package.”</span> <a href="https://nlopt.readthedocs.io/en/latest/">https://nlopt.readthedocs.io/en/latest/</a>.
1092 </div>
1093 <div id="ref-LiuNocedal:1989" class="csl-entry">
1094 Liu, D. C., and J. Nocedal. 1989. <span>“On the Limited Memory
1095 <span>B</span><span>F</span><span>G</span><span>S</span> Method for
1096 Large Scale Optimization.”</span> <em>Math. Programming</em> 45: 503–28.
1097 </div>
1098 <div id="ref-Nocedal:1980" class="csl-entry">
1099 Nocedal, J. 1980. <span>“Updating Quasi-<span>N</span>ewton Matrices
1100 with Limited Storage.”</span> <em>Math. Comput.</em> 35: 773–82.
1101 </div>
1102 <div id="ref-Powell:1994" class="csl-entry">
1103 Powell, M. J. D. 1994. <span>“A Direct Search Optimization Method That
1104 Models the Objective and Constraint Functions by Linear
1105 Interpolation.”</span> In <em>Advances in Optimization and Numerical
1106 Analysis</em>, edited by S. Gomez and J.-P. Hennart, 51–67. Kluwer
1107 Academic, Dordrecht.
1108 </div>
1109 <div id="ref-Powell:1998" class="csl-entry">
1110 ———. 1998. <span>“Direct Search Algorithms for Optimization
1111 Calculations.”</span> <em>Acta Numerica</em> 7: 287–336.
1112 </div>
1113 <div id="ref-Svanberg:2002" class="csl-entry">
1114 Svanberg, Krister. 2002. <span>“A Class of Globally Convergent
1115 Optimization Methods Based on Conservative Convex Separable
1116 Approximations.”</span> <em>SIAM J. Optim.</em> 12 (2): 555–73.
1117 </div>
1118 <div id="ref-Xie:2014" class="csl-entry">
1119 Xie, Yihui. 2014. <span>“Knitr: A Comprehensive Tool for Reproducible
1120 Research in <span>R</span>.”</span> In <em>Implementing Reproducible
1121 Computational Research</em>, edited by Victoria Stodden, Friedrich
1122 Leisch, and Roger D. Peng. Chapman; Hall/CRC. <a href="https://www.routledge.com/Implementing-Reproducible-Research/Stodden-Leisch-Peng/p/book/9781466561595">https://www.routledge.com/Implementing-Reproducible-Research/Stodden-Leisch-Peng/p/book/9781466561595</a>.
1123 </div>
1124 <div id="ref-Xie:2015" class="csl-entry">
1125 ———. 2015. <em>Dynamic Documents with <span>R</span> and Knitr</em>. 2nd
1126 ed. Boca Raton, Florida: Chapman; Hall/CRC. <a href="https://yihui.org/knitr/">https://yihui.org/knitr/</a>.
1127 </div>
1128 <div id="ref-Xie:2016" class="csl-entry">
1129 ———. 2016. <em>Knitr: A General-Purpose Package for Dynamic Report
1130 Generation in r</em>. <a href="https://yihui.org/knitr/">https://yihui.org/knitr/</a>.
1131 </div>
1132 </div>
1133 </div>
1134
1135
1136
1137 <!-- code folding -->
1138
1139
1140 <!-- dynamically load mathjax for compatibility with self-contained -->
1141 <script>
1142 (function () {
1143 var script = document.createElement("script");
1144 script.type = "text/javascript";
1145 script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
1146 document.getElementsByTagName("head")[0].appendChild(script);
1147 })();
1148 </script>
1149
1150 </body>
1151 </html>