File: optim-mini-howto-2.lyx

package info (click to toggle)
octave-optim 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,064 kB
  • ctags: 374
  • sloc: cpp: 1,126; perl: 158; makefile: 79
file content (1014 lines) | stat: -rw-r--r-- 16,101 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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
#LyX 2.0 created this file. For more info see http://www.lyx.org/
\lyxformat 413
\begin_document
\begin_header
\textclass article
\begin_preamble
\usepackage{dsfont}

% No date please
\date{}
\fontfamily{cmss}
\selectfont
\end_preamble
\use_default_options false
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding auto
\fontencoding global
\font_roman default
\font_sans helvet
\font_typewriter default
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100

\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize default
\use_geometry false
\use_amsmath 1
\use_esint 0
\use_mhchem 0
\use_mathdots 1
\cite_engine basic
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\use_refstyle 0
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header

\begin_body

\begin_layout Standard
\begin_inset ERT
status collapsed

\begin_layout Plain Layout

% This is for 
\backslash
LyX
\end_layout

\end_inset


\end_layout

\begin_layout Standard
\begin_inset FormulaMacro
\newcommand{\bfrac}[2]{\frac{#1}{#2}}
\end_inset


\end_layout

\begin_layout Standard
\begin_inset FormulaMacro
\newcommand{\nth}{^{\textrm{th}}}
\end_inset


\end_layout

\begin_layout Standard
\begin_inset FormulaMacro
\newcommand{\R}{R}
\end_inset


\end_layout

\begin_layout Standard
\begin_inset FormulaMacro
\renewcommand{\N}{N}
\end_inset


\end_layout

\begin_layout Standard
\begin_inset FormulaMacro
\renewcommand{\Z}{Z}
\end_inset


\end_layout

\begin_layout Standard
\begin_inset FormulaMacro
\renewcommand{\tra}{^{T}}
\end_inset


\end_layout

\begin_layout Standard
\begin_inset FormulaMacro
\renewcommand{\xx}{\mathbf{x}}
\end_inset


\end_layout

\begin_layout Standard
\begin_inset ERT
status collapsed

\begin_layout Plain Layout

% This is for 
\backslash
LaTeX
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout


\backslash
fontfamily{cmss}
\backslash
selectfont
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout


\backslash
renewcommand{
\backslash
R}{{
\backslash
mathds{R}}} 
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout


\backslash
renewcommand{
\backslash
N}{{
\backslash
mathds{N}}}
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout


\backslash
renewcommand{
\backslash
Z}{{
\backslash
mathds{Z}}}
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout


\backslash
renewcommand{
\backslash
tra}{^{
\backslash
top}}
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout


\backslash
renewcommand{
\backslash
bfrac}[2]{
\backslash
frac{{
\backslash
textstyle #1 }}{{
\backslash
textstyle #2 }}}
\end_layout

\end_inset


\end_layout

\begin_layout Title
Mini-HOWTO on using Octave for Unconstrained Nonlinear Optimization
\begin_inset Foot
status collapsed

\begin_layout Plain Layout
Author : Etienne Grossmann 
\family typewriter
<etienne@egdn.net>
\family default
 (soon replaced by 
\begin_inset Quotes eld
\end_inset

Octave-Forge developers
\begin_inset Quotes erd
\end_inset

?).
 This document is free documentation; you can redistribute it and/or modify
 it under the terms of the GNU Free Documentation License as published by
 the Free Software Foundation.
\begin_inset Newline newline
\end_inset

.
\begin_inset space ~
\end_inset


\begin_inset space ~
\end_inset


\begin_inset space ~
\end_inset

This is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.
\end_layout

\end_inset


\end_layout

\begin_layout Standard
\begin_inset Note Comment
status collapsed

\begin_layout Plain Layout
Keywords: nonlinear optimization, octave, tutorial, Nelder-Mead, Conjugate
 Gradient, Levenberg-Marquardt
\end_layout

\end_inset


\end_layout

\begin_layout Standard

\emph on
This document refers to the frontend function 
\begin_inset Quotes eld
\end_inset

minimize
\begin_inset Quotes erd
\end_inset

, which is now deprecated and will be removed in a future version of the
 optim package.
 An alternative frontend function is 
\begin_inset Quotes eld
\end_inset

nonlin_min
\begin_inset Quotes erd
\end_inset

, not described here.
 The backends of 
\begin_inset Quotes eld
\end_inset

minimize
\begin_inset Quotes erd
\end_inset

 (
\begin_inset Quotes eld
\end_inset

nelder_mead_min
\begin_inset Quotes erd
\end_inset

, 
\begin_inset Quotes eld
\end_inset

bfgsmin
\begin_inset Quotes erd
\end_inset

, 
\begin_inset Quotes eld
\end_inset

d2_min
\begin_inset Quotes erd
\end_inset

) can also be called directly.
 This document will be removed in the future.
\end_layout

\begin_layout Standard
Nonlinear optimization problems are very common and when a solution cannot
 be found analytically, one usually tries to find it numerically.
 This document shows how to perform unconstrained nonlinear minimization
 using the Octave language for numerical computation.
 We assume to be so lucky as to have an initial guess from which to start
 an iterative method, and so impatient as to avoid as much as possible going
 into the details of the algorithm.
 In the following examples, we consider multivariable problems, but the
 single variable case is solved in exactly the same way.
\end_layout

\begin_layout Standard
All the algorithms used below return numerical approximations of 
\emph on
local minima
\emph default
 of the optimized function.
 In the following examples, we minimize a function with a single minimum
 (Figure
\begin_inset space ~
\end_inset


\begin_inset CommandInset ref
LatexCommand ref
reference "fig:function"

\end_inset

), which is relatively easily found.
 In practice, success of optimization algorithms greatly depend on the optimized
 function and on the starting point.
\end_layout

\begin_layout Section*

\end_layout

\begin_layout Standard
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
fontfamily{cmss} 
\backslash
selectfont A simple example
\end_layout

\end_inset


\end_layout

\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open

\begin_layout Plain Layout
\align center
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
raisebox{6mm}{
\end_layout

\end_inset


\begin_inset Graphics
	filename figures/2D_slice-3.eps2
	width 30text%

\end_inset


\begin_inset ERT
status collapsed

\begin_layout Plain Layout

}
\end_layout

\end_inset


\begin_inset space ~
\end_inset


\end_layout

\begin_layout Plain Layout
\begin_inset Caption

\begin_layout Plain Layout
\begin_inset CommandInset label
LatexCommand label
name "fig:function"

\end_inset

 2D slice of the function that is minimized throughout this tutorial.
 Although not obvious at first sight, it has a unique minimum.
\end_layout

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Standard
We will use a call of the type
\end_layout

\begin_layout LyX-Code
[x_best, best_value, niter] = minimize (func, x_init)
\end_layout

\begin_layout Standard
to find the minimum of 
\begin_inset Formula 
\[
\begin{array}{cccc}
f\,: & \left(x_{1},.x_{2},x_{3}\right)\in\R^{3} & \longrightarrow & \left(x_{1}-1\right)^{2}/9+\left(x_{3}-1\right)^{2}/9+\left(x_{3}-1\right)^{2}/9\\
 &  &  & -\cos\left(x_{1}-1\right)-\cos\left(x_{2}-1\right)-\cos\left(x_{3}-1\right).
\end{array}
\]

\end_inset


\end_layout

\begin_layout Standard
The following commands should find a local minimum of 
\begin_inset Formula \ensuremath{f()}

\end_inset

, using the Nelder-Mead (aka 
\begin_inset Quotes eld
\end_inset

downhill simplex
\begin_inset Quotes erd
\end_inset

) algorithm and starting from a randomly chosen point 
\family typewriter
x0
\family default

\begin_inset space ~
\end_inset

:
\end_layout

\begin_layout LyX-Code
function cost = foo (xx)
\end_layout

\begin_layout LyX-Code
  xx--;  
\end_layout

\begin_layout LyX-Code
  cost = sum (-cos(xx)+xx.^2/9);
\end_layout

\begin_layout LyX-Code
endfunction
\end_layout

\begin_layout LyX-Code
x0 = [-1, 3, -2];
\end_layout

\begin_layout LyX-Code
[x,v,n] = minimize ("foo", x0)
\end_layout

\begin_layout Standard
The output should look like
\begin_inset space ~
\end_inset

:
\end_layout

\begin_layout LyX-Code
x =
\end_layout

\begin_layout LyX-Code
  1.00000 1.00000 1.00000
\end_layout

\begin_layout LyX-Code

\end_layout

\begin_layout LyX-Code
v = -3.0000
\end_layout

\begin_layout LyX-Code
n = 248
\end_layout

\begin_layout Standard
This means that a minimum has been found in 
\begin_inset Formula \ensuremath{\left(1,1,1\right)}

\end_inset

 and that the value at that point is 
\begin_inset Formula \ensuremath{-3}

\end_inset

.
 This is correct, since all the points of the form 
\begin_inset Formula \ensuremath{x_{1}=1+2i\pi,\, x_{2}=1+2j\pi,\, x_{3}=1+2k\pi}

\end_inset

, for some 
\begin_inset Formula \ensuremath{i,j,k\in\N}

\end_inset

, minimize 
\begin_inset Formula \ensuremath{f()}

\end_inset

.
 The number of function evaluations, 248, is also returned.
 Note that this number depends on the starting point.
 You will most likely obtain different numbers if you change 
\family typewriter
x0
\family default
.
\end_layout

\begin_layout Standard
The Nelder-Mead algorithm is quite robust, but unfortunately it is not very
 efficient.
 For high-dimensional problems, its execution time may become prohibitive.
\end_layout

\begin_layout Section*

\end_layout

\begin_layout Standard
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
fontfamily{cmss} 
\backslash
selectfont Using the first differential
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Fortunately, when a function, like 
\begin_inset Formula \ensuremath{f()}

\end_inset

 above, is differentiable, more efficient optimization algorithms can be
 used.
 If 
\family typewriter
minimize()
\family default
 is given the differential of the optimized function, using the 
\family typewriter
"df"
\family default
 option, it will use a conjugate gradient method.
\end_layout

\begin_layout LyX-Code
## Function returning partial derivatives
\end_layout

\begin_layout LyX-Code
function dc = diffoo (x)
\end_layout

\begin_layout LyX-Code
    x = x(:)' - 1;
\end_layout

\begin_layout LyX-Code
    dc = sin (x) + 2*x/9;
\end_layout

\begin_layout LyX-Code
endfunction
\end_layout

\begin_layout LyX-Code
[x, v, n] = minimize ("foo", x0, "df", "diffoo")
\end_layout

\begin_layout Standard
This produces the output
\begin_inset space ~
\end_inset

:
\end_layout

\begin_layout LyX-Code
x =
\end_layout

\begin_layout LyX-Code
  1.00000 1.00000 1.00000
\end_layout

\begin_layout LyX-Code
v = -3 
\end_layout

\begin_layout LyX-Code
n =
\end_layout

\begin_layout LyX-Code
  108 6
\end_layout

\begin_layout Standard
The same minimum has been found, but only 108 function evaluations were
 needed, together with 6 evaluations of the differential.
 Here, 
\family typewriter
diffoo()
\family default
 takes the same argument as 
\family typewriter
foo()
\family default
 and returns the partial derivatives of 
\begin_inset Formula \ensuremath{f()}

\end_inset

 with respect to the corresponding variables.
 It doesn't matter if it returns a row or column vector or a matrix, as
 long as the 
\begin_inset Formula \ensuremath{i\nth}

\end_inset

 element of 
\family typewriter
diffoo(x)
\family default
 is the partial derivative of 
\begin_inset Formula \ensuremath{f()}

\end_inset

 with respect to 
\begin_inset Formula \ensuremath{x_{i}}

\end_inset

 .
\end_layout

\begin_layout Section*

\end_layout

\begin_layout Standard
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
fontfamily{cmss} 
\backslash
selectfont Using numerical approximations of the first differential
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Sometimes, the minimized function is differentiable, but actually writing
 down its differential is more work than one would like.
 Numerical differentiation offers a solution which is less efficient in
 terms of computation cost, but easy to implement.
 The 
\family typewriter
"ndiff"
\family default
 option of 
\family typewriter
minimize()
\family default
 uses numerical differentiation to execute exactly the same algorithm as
 in the previous example.
 However, because numerical approximation of the differentia is used, the
 outpud may differ slightly
\begin_inset space ~
\end_inset

:
\end_layout

\begin_layout LyX-Code
[x, v, n] = minimize ("foo", x0, "ndiff")
\end_layout

\begin_layout Standard
wich yields
\begin_inset space ~
\end_inset

:
\end_layout

\begin_layout LyX-Code
x =
\end_layout

\begin_layout LyX-Code
  1.00000 1.00000 1.00000
\end_layout

\begin_layout LyX-Code
v = -3 
\end_layout

\begin_layout LyX-Code
n =
\end_layout

\begin_layout LyX-Code
  78 6
\end_layout

\begin_layout Standard
Note that each time the differential is numerically approximated, 
\family typewriter
foo()
\family default
 is called 6 times (twice per input element), so that 
\family typewriter
foo()
\family default
 is evaluated a total of (78+6*6=) 114 times in this example.
\end_layout

\begin_layout Section*

\end_layout

\begin_layout Standard
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
fontfamily{cmss} 
\backslash
selectfont Using the first and second differentials
\end_layout

\end_inset


\end_layout

\begin_layout Standard
When the function is twice differentiable and one knows how to compute its
 first and second differentials, still more efficient algorithms can be
 used (in our case, a variant of Levenberg-Marquardt).
 The option 
\family typewriter
"d2f"
\family default
 allows to specify a function that returns the value of the function, the
 first and second differentials of the minimized function.
 Entering the commands
\begin_inset space ~
\end_inset

: 
\end_layout

\begin_layout LyX-Code
function [c, dc, d2c] = d2foo (x)
\end_layout

\begin_layout LyX-Code
    c = foo(x);
\end_layout

\begin_layout LyX-Code
    dc = diffoo(x);
\end_layout

\begin_layout LyX-Code
    d2c = diag (cos (x(:)-1) + 2/9);
\end_layout

\begin_layout LyX-Code
end
\end_layout

\begin_layout LyX-Code
[x,v,n] = minimize ("foo", x0, "d2f", "d2foo") 
\end_layout

\begin_layout Standard
produces the output
\begin_inset space ~
\end_inset

:
\end_layout

\begin_layout LyX-Code
x =
\end_layout

\begin_layout LyX-Code
  1.0000 1.0000 1.0000
\end_layout

\begin_layout LyX-Code
v = -3
\end_layout

\begin_layout LyX-Code
n =
\end_layout

\begin_layout LyX-Code
  34 5
\end_layout

\begin_layout Standard
This time, 34 function evaluations, and 5 evaluations of 
\family typewriter
d2foo()
\family default
 were needed.
\end_layout

\begin_layout Section*

\end_layout

\begin_layout Standard
\begin_inset ERT
status collapsed

\begin_layout Plain Layout


\backslash
fontfamily{cmss} 
\backslash
selectfont Summary
\end_layout

\end_inset


\end_layout

\begin_layout Standard
We have just seen the most basic ways of solving nonlinear unconstrained
 optimization problems.
 The online help system of Octave (try e.g.
 
\begin_inset Quotes eld
\end_inset


\family typewriter
help minimize
\family default

\begin_inset Quotes erd
\end_inset

) will yield information on other issues, such as 
\emph on
passing extra arguments
\emph default
 to the minimized function, 
\emph on
controling the termination
\emph default
 of the optimization process, choosing the algorithm etc.
\end_layout

\begin_layout LyX-Code

\end_layout

\end_body
\end_document