Mercurial > octave
annotate scripts/optimization/fminsearch.m @ 27584:2ab2e289cf84
fminsearch.m: Use only real part of objective function for Matlab compatibility (bug #57136).
* fminsearch.m (nmsmax): Replace input "fun" with either guarded_eval function
or "real (fun (x))". Include any additional arguments in varargin in the
anonymous function definition. Eliminate calls to feval and use handle calling
convention "fun (x)".
author | Rik <rik@octave.org> |
---|---|
date | Tue, 29 Oct 2019 16:33:40 -0700 |
parents | d4655f32dde7 |
children | b442ec6dda5c |
rev | line source |
---|---|
26376
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
26280
diff
changeset
|
1 ## Copyright (C) 2003-2019 Andy Adler |
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17281
diff
changeset
|
2 ## Copyright (C) 2002, 2013 N.J.Higham |
14895 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23567
diff
changeset
|
6 ## Octave is free software: you can redistribute it and/or modify it |
14895 | 7 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23567
diff
changeset
|
8 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
9 ## (at your option) any later version. |
14895 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
14 ## GNU General Public License for more details. |
14895 | 15 ## |
16 ## You should have received a copy of the GNU General Public License | |
17 ## along with Octave; see the file COPYING. If not, see | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23567
diff
changeset
|
18 ## <https://www.gnu.org/licenses/>. |
14895 | 19 |
20 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
21 ## @deftypefn {} {@var{x} =} fminsearch (@var{fun}, @var{x0}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
22 ## @deftypefnx {} {@var{x} =} fminsearch (@var{fun}, @var{x0}, @var{options}) |
25930
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
23 ## @deftypefnx {} {@var{x} =} fminsearch (@var{problem}) |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
24 ## @deftypefnx {} {[@var{x}, @var{fval}, @var{exitflag}, @var{output}] =} fminsearch (@dots{}) |
14895 | 25 ## |
25739
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
26 ## Find a value of @var{x} which minimizes the multi-variable function |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
27 ## @var{fun}. |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19596
diff
changeset
|
28 ## |
27389
d4655f32dde7
fminsearch.m: Accept string for FCN argument (bug #56885)
Rik <rik@octave.org>
parents:
26753
diff
changeset
|
29 ## @var{fun} is a function handle, inline function, or string containing the |
d4655f32dde7
fminsearch.m: Accept string for FCN argument (bug #56885)
Rik <rik@octave.org>
parents:
26753
diff
changeset
|
30 ## name of the function to evaluate. |
d4655f32dde7
fminsearch.m: Accept string for FCN argument (bug #56885)
Rik <rik@octave.org>
parents:
26753
diff
changeset
|
31 ## |
14895 | 32 ## The search begins at the point @var{x0} and iterates using the |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
33 ## @nospell{Nelder & Mead} Simplex algorithm (a derivative-free method). This |
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
34 ## algorithm is better-suited to functions which have discontinuities or for |
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
35 ## which a gradient-based search such as @code{fminunc} fails. |
14895 | 36 ## |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19596
diff
changeset
|
37 ## Options for the search are provided in the parameter @var{options} using the |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19596
diff
changeset
|
38 ## function @code{optimset}. Currently, @code{fminsearch} accepts the options: |
25739
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
39 ## @qcode{"Display"}, @qcode{"FunValCheck"},@qcode{"MaxFunEvals"}, |
25828
8b548f2f8086
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
25739
diff
changeset
|
40 ## @qcode{"MaxIter"}, @qcode{"OutputFcn"}, @qcode{"TolFun"}, @qcode{"TolX"}. |
25739
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
41 ## |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
42 ## @qcode{"MaxFunEvals"} proscribes the maximum number of function evaluations |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
43 ## before optimization is halted. The default value is |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
44 ## @code{200 * number_of_variables}, i.e., @code{200 * length (@var{x0})}. |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
45 ## The value must be a positive integer. |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
46 ## |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
47 ## @qcode{"MaxIter"} proscribes the maximum number of algorithm iterations |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
48 ## before optimization is halted. The default value is |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
49 ## @code{200 * number_of_variables}, i.e., @code{200 * length (@var{x0})}. |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
50 ## The value must be a positive integer. |
25828
8b548f2f8086
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
25739
diff
changeset
|
51 ## |
25739
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
52 ## For a description of the other options, see @code{optimset}. To initialize |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
53 ## an options structure with default values for @code{fminsearch} use |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
54 ## @code{options = optimset ("fminsearch")}. |
14895 | 55 ## |
25930
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
56 ## @code{fminsearch} may also be called with a single structure argument |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
57 ## with the following fields: |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
58 ## |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
59 ## @table @code |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
60 ## @item objective |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
61 ## The objective function. |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
62 ## |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
63 ## @item x0 |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
64 ## The initial point. |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
65 ## |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
66 ## @item solver |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
67 ## Must be set to @qcode{"fminsearch"}. |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
68 ## |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
69 ## @item options |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
70 ## A structure returned from @code{optimset} or an empty matrix to |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
71 ## indicate that defaults should be used. |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
72 ## @end table |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
73 ## |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
74 ## @noindent |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
75 ## The field @code{options} is optional. All others are required. |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
76 ## |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19596
diff
changeset
|
77 ## On exit, the function returns @var{x}, the minimum point, and @var{fval}, |
22967
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
78 ## the function value at the minimum. |
14895 | 79 ## |
25739
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
80 ## The third output @var{exitflag} reports whether the algorithm succeeded and |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
81 ## may take one of the following values: |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
82 ## |
23114
19e958974410
fminsearch.m: fix texinfo header (bug #50178)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
23113
diff
changeset
|
83 ## @table @asis |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
84 ## @item 1 |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
85 ## if the algorithm converged |
25739
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
86 ## (size of the simplex is smaller than @code{TolX} @strong{AND} the step in |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
87 ## function value between iterations is smaller than @code{TolFun}). |
14895 | 88 ## |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
89 ## @item 0 |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
90 ## if the maximum number of iterations or the maximum number of function |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
91 ## evaluations are exceeded. |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
92 ## |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
93 ## @item -1 |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
94 ## if the iteration is stopped by the @qcode{"OutputFcn"}. |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
95 ## @end table |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
96 ## |
25739
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
97 ## The fourth output is a structure @var{output} containing runtime |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
98 ## about the algorithm. Fields in the structure are @code{funcCount} |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
99 ## containing the number of function calls to @var{fun}, @code{iterations} |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
100 ## containing the number of iteration steps, @code{algorithm} with the name of |
25828
8b548f2f8086
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
25739
diff
changeset
|
101 ## the search algorithm (always: |
23567
71bfd507663c
doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
102 ## @nospell{@qcode{"Nelder-Mead simplex direct search"}}), and @code{message} |
71bfd507663c
doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
103 ## with the exit message. |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
104 ## |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
105 ## Example: |
14895 | 106 ## |
107 ## @example | |
108 ## fminsearch (@@(x) (x(1)-5).^2+(x(2)-8).^4, [0;0]) | |
109 ## @end example | |
25739
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
110 ## |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
111 ## Note: If you need to find the minimum of a single variable function it is |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
112 ## probably better to use @code{fminbnd}. |
14895 | 113 ## @seealso{fminbnd, fminunc, optimset} |
114 ## @end deftypefn | |
115 | |
116 ## PKG_ADD: ## Discard result to avoid polluting workspace with ans at startup. | |
117 ## PKG_ADD: [~] = __all_opts__ ("fminsearch"); | |
118 | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
119 ## FIXME: Add support for output function with "state" set to "interrupt". |
14895 | 120 |
25930
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
121 function [x, fval, exitflag, output] = fminsearch (varargin) |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
122 |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
123 if (nargin < 1) |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
124 print_usage (); |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
125 endif |
14895 | 126 |
127 ## Get default options if requested. | |
25930
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
128 if (nargin == 1 && ischar (varargin{1}) && strcmp (varargin{1}, "defaults")) |
26138
804e18e3e320
Reenable query of optimization options (bugs #54952 and #55089).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
25936
diff
changeset
|
129 x = struct ("Display", "notify", "FunValCheck", "off", |
804e18e3e320
Reenable query of optimization options (bugs #54952 and #55089).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
25936
diff
changeset
|
130 "MaxFunEvals", [], "MaxIter", [], |
804e18e3e320
Reenable query of optimization options (bugs #54952 and #55089).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
25936
diff
changeset
|
131 "OutputFcn", [], |
804e18e3e320
Reenable query of optimization options (bugs #54952 and #55089).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
25936
diff
changeset
|
132 "TolFun", 1e-4, "TolX", 1e-4); |
14895 | 133 return; |
134 endif | |
135 | |
25930
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
136 if (nargin == 1) |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
137 problem = varargin{1}; |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
138 varargin = {}; |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
139 if (! isstruct (problem)) |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
140 error ("fminsearch: PROBLEM must be a structure"); |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
141 endif |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
142 fun = problem.objective; |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
143 x0 = problem.x0; |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
144 if (! strcmp (problem.solver, "fminsearch")) |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
145 error ('fminsearch: problem.solver must be set to "fminsearch"'); |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
146 endif |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
147 if (isfield (problem, "options")) |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
148 options = problem.options; |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
149 else |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
150 options = []; |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
151 endif |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
152 elseif (nargin > 1) |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
153 fun = varargin{1}; |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
154 x0 = varargin{2}; |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
155 if (nargin > 2) |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
156 options = varargin{3}; |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
157 varargin(1:3) = []; |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
158 else |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
159 options = []; |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
160 varargin = {}; |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
161 endif |
14895 | 162 endif |
163 | |
27389
d4655f32dde7
fminsearch.m: Accept string for FCN argument (bug #56885)
Rik <rik@octave.org>
parents:
26753
diff
changeset
|
164 if (ischar (fun)) |
d4655f32dde7
fminsearch.m: Accept string for FCN argument (bug #56885)
Rik <rik@octave.org>
parents:
26753
diff
changeset
|
165 fun = str2func (fun); |
d4655f32dde7
fminsearch.m: Accept string for FCN argument (bug #56885)
Rik <rik@octave.org>
parents:
26753
diff
changeset
|
166 endif |
d4655f32dde7
fminsearch.m: Accept string for FCN argument (bug #56885)
Rik <rik@octave.org>
parents:
26753
diff
changeset
|
167 |
25930
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
168 if (isempty (options)) |
22967
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
169 options = struct (); |
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
170 endif |
14895 | 171 |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
172 [x, exitflag, output] = nmsmax (fun, x0, options, varargin{:}); |
14895 | 173 |
174 if (isargout (2)) | |
175 fval = feval (fun, x); | |
176 endif | |
177 | |
178 endfunction | |
179 | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
180 ## NMSMAX Nelder-Mead simplex method for direct search optimization. |
14895 | 181 ## [x, fmax, nf] = NMSMAX(FUN, x0, STOPIT, SAVIT) attempts to |
182 ## maximize the function FUN, using the starting vector x0. | |
183 ## The Nelder-Mead direct search method is used. | |
184 ## Output arguments: | |
185 ## x = vector yielding largest function value found, | |
186 ## fmax = function value at x, | |
187 ## nf = number of function evaluations. | |
188 ## The iteration is terminated when either | |
189 ## - the relative size of the simplex is <= STOPIT(1) | |
190 ## (default 1e-3), | |
191 ## - STOPIT(2) function evaluations have been performed | |
192 ## (default inf, i.e., no limit), or | |
193 ## - a function value equals or exceeds STOPIT(3) | |
194 ## (default inf, i.e., no test on function values). | |
195 ## The form of the initial simplex is determined by STOPIT(4): | |
196 ## STOPIT(4) = 0: regular simplex (sides of equal length, the default) | |
197 ## STOPIT(4) = 1: right-angled simplex. | |
198 ## Progress of the iteration is not shown if STOPIT(5) = 0 (default 1). | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21099
diff
changeset
|
199 ## STOPIT(6) indicates the direction (i.e., minimization or |
14895 | 200 ## maximization.) Default is 1, maximization. |
201 ## set STOPIT(6)=-1 for minimization | |
202 ## If a non-empty fourth parameter string SAVIT is present, then | |
15468
6437fa7263dd
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14895
diff
changeset
|
203 ## 'SAVE SAVIT x fmax nf' is executed after each inner iteration. |
14895 | 204 ## NB: x0 can be a matrix. In the output argument, in SAVIT saves, |
205 ## and in function calls, x has the same shape as x0. | |
206 ## NMSMAX(fun, x0, STOPIT, SAVIT, P1, P2,...) allows additional | |
207 ## arguments to be passed to fun, via feval(fun,x,P1,P2,...). | |
208 ## References: | |
209 ## N. J. Higham, Optimization by direct search in matrix computations, | |
210 ## SIAM J. Matrix Anal. Appl, 14(2): 317-333, 1993. | |
211 ## C. T. Kelley, Iterative Methods for Optimization, Society for Industrial | |
212 ## and Applied Mathematics, Philadelphia, PA, 1999. | |
213 | |
214 ## From Matrix Toolbox | |
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17281
diff
changeset
|
215 ## Copyright (C) 2002, 2013 N.J.Higham |
14895 | 216 ## www.maths.man.ac.uk/~higham/mctoolbox |
217 ## | |
218 ## Modifications for Octave by A.Adler 2003 | |
219 | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
220 function [stopit, savit, dirn, trace, tol, maxiter, tol_f, outfcn] = ... |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
221 parse_options (options, x) |
14895 | 222 |
223 ## Tolerance for cgce test based on relative size of simplex. | |
224 stopit(1) = tol = optimget (options, "TolX", 1e-4); | |
225 | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
226 ## Tolerance for cgce test based on step in function value. |
25739
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
227 tol_f = optimget (options, "TolFun", 1e-4); |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
228 |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
229 ## Max number of function evaluations. |
25739
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
230 stopit(2) = optimget (options, "MaxFunEvals", 200 * length (x)); |
14895 | 231 |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
232 ## Max number of iterations |
25739
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
233 maxiter = optimget (options, "MaxIter", 200 * length (x)); |
14895 | 234 |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
235 ## Default target for function values. |
14895 | 236 stopit(3) = Inf; # FIXME: expose this parameter to the outside |
237 | |
238 ## Default initial simplex. | |
239 stopit(4) = 0; # FIXME: expose this parameter to the outside | |
240 | |
241 ## Default: show progress. | |
242 display = optimget (options, "Display", "notify"); | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
243 switch (display) |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
244 case "iter" |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
245 stopit(5) = 1; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
246 case "final" |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
247 stopit(5) = 2; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
248 case "notify" |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
249 stopit(5) = 3; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
250 otherwise # "none" |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
251 stopit(5) = 0; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
252 endswitch |
14895 | 253 trace = stopit(5); |
254 | |
255 ## Use function to minimize, not maximize | |
256 stopit(6) = dirn = -1; | |
257 | |
20713
2469d78a1d8b
Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents:
20231
diff
changeset
|
258 ## Filename for snapshots. |
14895 | 259 savit = []; # FIXME: expose this parameter to the outside |
260 | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
261 ## OutputFcn |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
262 outfcn = optimget (options, "OutputFcn"); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
263 |
14895 | 264 endfunction |
265 | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
266 function [x, exitflag, output] = nmsmax (fun, x, options, varargin) |
14895 | 267 |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
268 [stopit, savit, dirn, trace, tol, maxiter, tol_f, outfcn] = ... |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
269 parse_options (options, x); |
14895 | 270 |
271 if (strcmpi (optimget (options, "FunValCheck", "off"), "on")) | |
272 ## Replace fcn with a guarded version. | |
27584
2ab2e289cf84
fminsearch.m: Use only real part of objective function for Matlab compatibility (bug #57136).
Rik <rik@octave.org>
parents:
27389
diff
changeset
|
273 fun = @(x) guarded_eval (fun, x, varargin{:}); |
2ab2e289cf84
fminsearch.m: Use only real part of objective function for Matlab compatibility (bug #57136).
Rik <rik@octave.org>
parents:
27389
diff
changeset
|
274 else |
2ab2e289cf84
fminsearch.m: Use only real part of objective function for Matlab compatibility (bug #57136).
Rik <rik@octave.org>
parents:
27389
diff
changeset
|
275 fun = @(x) real (fun (x, varargin{:})); |
14895 | 276 endif |
277 | |
278 x0 = x(:); # Work with column vector internally. | |
279 n = length (x0); | |
280 | |
281 V = [zeros(n,1) eye(n)]; | |
282 f = zeros (n+1,1); | |
283 V(:,1) = x0; | |
27584
2ab2e289cf84
fminsearch.m: Use only real part of objective function for Matlab compatibility (bug #57136).
Rik <rik@octave.org>
parents:
27389
diff
changeset
|
284 f(1) = dirn * fun (x); |
14895 | 285 fmax_old = f(1); |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
286 nf = 1; |
14895 | 287 |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
288 if (trace == 1) |
26280
da54f14258e1
fminsearch.m: Fix sign of results for intermediate reporting (bug #55261).
Rik <rik@octave.org>
parents:
26138
diff
changeset
|
289 printf ("f(x0) = %9.4e\n", dirn * f(1)); |
14895 | 290 endif |
291 | |
292 k = 0; m = 0; | |
293 | |
294 ## Set up initial simplex. | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
295 scale = max (norm (x0, Inf), 1); |
14895 | 296 if (stopit(4) == 0) |
297 ## Regular simplex - all edges have same length. | |
298 ## Generated from construction given in reference [18, pp. 80-81] of [1]. | |
299 alpha = scale / (n*sqrt (2)) * [sqrt(n+1)-1+n, sqrt(n+1)-1]; | |
300 V(:,2:n+1) = (x0 + alpha(2)*ones (n,1)) * ones (1,n); | |
301 for j = 2:n+1 | |
302 V(j-1,j) = x0(j-1) + alpha(1); | |
303 x(:) = V(:,j); | |
27584
2ab2e289cf84
fminsearch.m: Use only real part of objective function for Matlab compatibility (bug #57136).
Rik <rik@octave.org>
parents:
27389
diff
changeset
|
304 f(j) = dirn * fun (x); |
14895 | 305 endfor |
306 else | |
307 ## Right-angled simplex based on co-ordinate axes. | |
308 alpha = scale * ones(n+1,1); | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
309 for j = 2:n+1 |
14895 | 310 V(:,j) = x0 + alpha(j)*V(:,j); |
311 x(:) = V(:,j); | |
27584
2ab2e289cf84
fminsearch.m: Use only real part of objective function for Matlab compatibility (bug #57136).
Rik <rik@octave.org>
parents:
27389
diff
changeset
|
312 f(j) = dirn * fun (x); |
14895 | 313 endfor |
314 endif | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
315 nf += n; |
14895 | 316 how = "initial "; |
317 | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
318 [~, j] = sort (f); |
14895 | 319 j = j(n+1:-1:1); |
320 f = f(j); | |
321 V = V(:,j); | |
322 | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
323 exitflag = 0; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
324 |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
325 if (! isempty (outfcn)) |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
326 optimvalues.iteration = 0; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
327 optimvalues.funccount = nf; |
26280
da54f14258e1
fminsearch.m: Fix sign of results for intermediate reporting (bug #55261).
Rik <rik@octave.org>
parents:
26138
diff
changeset
|
328 optimvalues.fval = dirn * f(1); |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
329 optimvalues.procedure = how; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
330 state = "init"; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
331 stop = outfcn (x, optimvalues, state); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
332 if (stop) |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
333 msg = "Stopped by OutputFcn\n"; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
334 exitflag = -1; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
335 endif |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
336 endif |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
337 |
14895 | 338 alpha = 1; beta = 1/2; gamma = 2; |
339 | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
340 while (exitflag != -1) # Outer (and only) loop. |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
341 k += 1; |
14895 | 342 |
343 if (k > maxiter) | |
25739
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
344 msg = "Exceeded maximum iterations\n"; |
14895 | 345 break; |
346 endif | |
347 | |
348 fmax = f(1); | |
349 if (fmax > fmax_old) | |
350 if (! isempty (savit)) | |
351 x(:) = V(:,1); | |
352 eval (["save " savit " x fmax nf"]); | |
353 endif | |
354 endif | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
355 if (trace == 1) |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
356 printf ("Iter. %2.0f,", k); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
357 printf (" how = %-11s", [how ","]); |
26280
da54f14258e1
fminsearch.m: Fix sign of results for intermediate reporting (bug #55261).
Rik <rik@octave.org>
parents:
26138
diff
changeset
|
358 printf ("nf = %3.0f, f = %9.4e (%2.1f%%)\n", nf, dirn * fmax, ... |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
359 100*(fmax-fmax_old)/(abs(fmax_old)+eps)); |
14895 | 360 endif |
361 fmax_old = fmax; | |
362 | |
363 ## Three stopping tests from MDSMAX.M | |
364 | |
365 ## Stopping Test 1 - f reached target value? | |
366 if (fmax >= stopit(3)) | |
367 msg = "Exceeded target...quitting\n"; | |
25739
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
368 ## FIXME: Add documentation when stopit(3) gets exposed to the outside |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
369 exitflag = -1; |
14895 | 370 break; |
371 endif | |
372 | |
373 ## Stopping Test 2 - too many f-evals? | |
374 if (nf >= stopit(2)) | |
25739
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
375 msg = "Exceeded maximum number of function evaluations\n"; |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
376 exitflag = 0; |
14895 | 377 break; |
378 endif | |
379 | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
380 ## Stopping Test 3 - converged? The first part is test (4.3) in [1]. |
14895 | 381 v1 = V(:,1); |
382 size_simplex = norm (V(:,2:n+1)-v1(:,ones (1,n)),1) / max (1, norm (v1,1)); | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
383 step_f = max (abs (f(1) - f(2:n+1))); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
384 if (size_simplex <= tol && step_f <= tol_f ) |
25739
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
385 msg = sprintf (["Algorithm converged. Simplex size %9.4e <= %9.4e ", ... |
8eec0403a370
fminsearch.m: Update default TolFun to 1e-4 for Matlab compatibility.
Rik <rik@octave.org>
parents:
25231
diff
changeset
|
386 "and step in function value %9.4e <= %9.4e\n"], ... |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
387 size_simplex, tol, step_f, tol_f); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
388 exitflag = 1; |
14895 | 389 break; |
390 endif | |
391 | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
392 ## Call OutputFcn |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
393 if (! isempty (outfcn)) |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
394 optimvalues.funccount = nf; |
26280
da54f14258e1
fminsearch.m: Fix sign of results for intermediate reporting (bug #55261).
Rik <rik@octave.org>
parents:
26138
diff
changeset
|
395 optimvalues.fval = dirn * f(1); |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
396 optimvalues.iteration = k; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
397 optimvalues.procedure = how; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
398 state = "iter"; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
399 stop = outfcn (x, optimvalues, state); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
400 if (stop) |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
401 msg = "Stopped by OutputFcn\n"; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
402 exitflag = -1; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
403 break; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
404 endif |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
405 endif |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
406 |
14895 | 407 ## One step of the Nelder-Mead simplex algorithm |
408 ## NJH: Altered function calls and changed CNT to NF. | |
15468
6437fa7263dd
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14895
diff
changeset
|
409 ## Changed each 'fr < f(1)' type test to '>' for maximization |
14895 | 410 ## and re-ordered function values after sort. |
411 | |
412 vbar = (sum (V(:,1:n)')/n)'; # Mean value | |
413 vr = (1 + alpha)*vbar - alpha*V(:,n+1); | |
414 x(:) = vr; | |
27584
2ab2e289cf84
fminsearch.m: Use only real part of objective function for Matlab compatibility (bug #57136).
Rik <rik@octave.org>
parents:
27389
diff
changeset
|
415 fr = dirn * fun (x); |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
416 nf += 1; |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
417 vk = vr; fk = fr; how = "reflect"; |
14895 | 418 if (fr > f(n)) |
419 if (fr > f(1)) | |
420 ve = gamma*vr + (1-gamma)*vbar; | |
421 x(:) = ve; | |
27584
2ab2e289cf84
fminsearch.m: Use only real part of objective function for Matlab compatibility (bug #57136).
Rik <rik@octave.org>
parents:
27389
diff
changeset
|
422 fe = dirn * fun (x); |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
423 nf += 1; |
14895 | 424 if (fe > f(1)) |
425 vk = ve; | |
426 fk = fe; | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
427 how = "expand"; |
14895 | 428 endif |
429 endif | |
430 else | |
431 vt = V(:,n+1); | |
432 ft = f(n+1); | |
433 if (fr > ft) | |
434 vt = vr; | |
435 ft = fr; | |
436 endif | |
437 vc = beta*vt + (1-beta)*vbar; | |
438 x(:) = vc; | |
27584
2ab2e289cf84
fminsearch.m: Use only real part of objective function for Matlab compatibility (bug #57136).
Rik <rik@octave.org>
parents:
27389
diff
changeset
|
439 fc = dirn * fun (x); |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
440 nf += 1; |
14895 | 441 if (fc > f(n)) |
442 vk = vc; fk = fc; | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
443 how = "contract"; |
14895 | 444 else |
445 for j = 2:n | |
446 V(:,j) = (V(:,1) + V(:,j))/2; | |
447 x(:) = V(:,j); | |
27584
2ab2e289cf84
fminsearch.m: Use only real part of objective function for Matlab compatibility (bug #57136).
Rik <rik@octave.org>
parents:
27389
diff
changeset
|
448 f(j) = dirn * fun (x); |
14895 | 449 endfor |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
450 nf += n-1; |
14895 | 451 vk = (V(:,1) + V(:,n+1))/2; |
452 x(:) = vk; | |
27584
2ab2e289cf84
fminsearch.m: Use only real part of objective function for Matlab compatibility (bug #57136).
Rik <rik@octave.org>
parents:
27389
diff
changeset
|
453 fk = dirn * fun (x); |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
454 nf += 1; |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
455 how = "shrink"; |
14895 | 456 endif |
457 endif | |
458 V(:,n+1) = vk; | |
459 f(n+1) = fk; | |
460 [~,j] = sort(f); | |
461 j = j(n+1:-1:1); | |
462 f = f(j); | |
463 V = V(:,j); | |
464 | |
465 endwhile # End of outer (and only) loop. | |
466 | |
467 ## Finished. | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
468 if ( (trace == 1) || (trace == 2) || (trace == 3 && exitflag != 1) ) |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
469 printf (msg); |
14895 | 470 endif |
471 x(:) = V(:,1); | |
472 | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
473 ## FIXME: Should outputfcn be called only if exitflag != 0, |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
474 ## i.e., only when we have successfully converged? |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
475 if (! isempty (outfcn)) |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
476 optimvalues.funccount = nf; |
26280
da54f14258e1
fminsearch.m: Fix sign of results for intermediate reporting (bug #55261).
Rik <rik@octave.org>
parents:
26138
diff
changeset
|
477 optimvalues.fval = dirn * f(1); |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
478 optimvalues.iteration = k; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
479 optimvalues.procedure = how; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
480 state = "done"; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
481 outfcn (x, optimvalues, state); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
482 endif |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
483 |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
484 ## output |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
485 output.iterations = k; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
486 output.funcCount = nf; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
487 output.algorithm = "Nelder-Mead simplex direct search"; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
488 output.message = msg; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
489 |
14895 | 490 endfunction |
491 | |
492 ## A helper function that evaluates a function and checks for bad results. | |
27584
2ab2e289cf84
fminsearch.m: Use only real part of objective function for Matlab compatibility (bug #57136).
Rik <rik@octave.org>
parents:
27389
diff
changeset
|
493 function y = guarded_eval (fun, x, varargin) |
14895 | 494 |
27584
2ab2e289cf84
fminsearch.m: Use only real part of objective function for Matlab compatibility (bug #57136).
Rik <rik@octave.org>
parents:
27389
diff
changeset
|
495 y = fun (x, varargin{:}); |
14895 | 496 |
22962
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
497 if (! (isreal (y))) |
14895 | 498 error ("fminsearch:notreal", "fminsearch: non-real value encountered"); |
22962
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
499 elseif (any (isnan (y(:)))) |
14895 | 500 error ("fminsearch:isnan", "fminsearch: NaN value encountered"); |
22962
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
501 elseif (any (isinf (y(:)))) |
14895 | 502 error ("fminsearch:isinf", "fminsearch: Inf value encountered"); |
503 endif | |
504 | |
505 endfunction | |
506 | |
507 | |
508 %!demo | |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
509 %! clf; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
510 %! hold on; |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
511 %! draw_fcn = @(x) (plot (x(1), x(2)) && false); |
22962
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
512 %! fcn = @(x) (x(1)-5).^2 + (x(2)-8).^4; |
14895 | 513 %! x0 = [0;0]; |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
514 %! [xmin, fval] = fminsearch (fcn, x0, optimset ("OutputFcn", draw_fcn)) |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
515 %! hold off; |
14895 | 516 |
22962
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
517 %!assert (fminsearch (@sin, 3, optimset ("MaxIter", 30)), 3*pi/2, 1e-4) |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
518 |
25231
e1024058833c
test: change remaining %!xtests into %!tests
Mike Miller <mtmiller@octave.org>
parents:
25054
diff
changeset
|
519 ## FIXME: The following test is for checking that fminsearch stops earlier |
e1024058833c
test: change remaining %!xtests into %!tests
Mike Miller <mtmiller@octave.org>
parents:
25054
diff
changeset
|
520 ## with these settings. If the optimizer algorithm is changed, it |
e1024058833c
test: change remaining %!xtests into %!tests
Mike Miller <mtmiller@octave.org>
parents:
25054
diff
changeset
|
521 ## may fail. Just adapt the values to make it pass again. |
e1024058833c
test: change remaining %!xtests into %!tests
Mike Miller <mtmiller@octave.org>
parents:
25054
diff
changeset
|
522 %!test |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
523 %! x = fminsearch (@sin, 3, optimset ("MaxIter", 3, "Display", "none")); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
524 %! assert (x, 4.8750, 1e-4); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
525 %! x = fminsearch (@sin, 3, optimset ("MaxFunEvals", 18, "Display", "none")); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
526 %! assert (x, 4.7109, 1e-4); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
527 |
22962
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
528 %!test |
25930
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
529 %! problem.objective = @sin; |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
530 %! problem.x0 = 3; |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
531 %! problem.solver = "fminsearch"; |
25936
7fa5ee67094a
* fminsearch.m: Suppress output in test.
John W. Eaton <jwe@octave.org>
parents:
25930
diff
changeset
|
532 %! problem.options = optimset ("MaxIter", 3, "Display", "none"); |
25930
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
533 %! x = fminsearch (problem); |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
534 %! assert (x, 4.8750, 1e-4); |
25936
7fa5ee67094a
* fminsearch.m: Suppress output in test.
John W. Eaton <jwe@octave.org>
parents:
25930
diff
changeset
|
535 %! problem.options = optimset ("MaxFunEvals", 18, "Display", "none"); |
25930
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
536 %! x = fminsearch (problem); |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
537 %! assert (x, 4.7109, 1e-4); |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
538 |
a00379f6f8c4
* fminsearch.m: Accept problem structure for Matlab compatibility.
John W. Eaton <jwe@octave.org>
parents:
25828
diff
changeset
|
539 %!test |
14895 | 540 %! c = 1.5; |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
541 %! assert (fminsearch (@(x) x(1).^2 + c*x(2).^2, [1;1]), [0;0], 1e-4); |
22962
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
542 |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
543 ## additional input argument |
22967
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
544 %!test |
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
545 %! x1 = fminsearch (@(x, c) x(1).^2 + c*x(2).^2, [1;1], [], 1.5); |
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
546 %! assert (x1, [0;0], 1e-4); |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
547 %! x1 = fminsearch (@(x, c) c(1)*x(1).^2 + c(2)*x(2).^2, [1;1], ... |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
548 %! optimset ("Display", "none"), [1 1.5]); |
22967
3c896180df16
Pass additional arguments for fminsearch to called function (bug #41888).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22963
diff
changeset
|
549 %! assert (x1, [0;0], 1e-4); |
22962
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
550 |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
551 ## all output arguments |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
552 %!test |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
553 %! options = optimset ("Display", "none", "TolX", 1e-4, "TolFun", 1e-7); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
554 %! [x, fval, exitflag, output] = fminsearch (@sin, 3, options); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
555 %! assert (x, 3*pi/2, options.TolX); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
556 %! assert (fval, -1, options.TolFun); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
557 %! assert (exitflag, 1); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
558 %! assert (isstruct (output)); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
559 %! assert (isfield (output, "iterations") && isnumeric (output.iterations) |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
560 %! && isscalar (output.iterations) && output.iterations > 0); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
561 %! assert (isfield (output, "funcCount") && isnumeric (output.funcCount) |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
562 %! && isscalar (output.funcCount) && output.funcCount > 0); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
563 %! assert (isfield (output, "algorithm") && ischar (output.algorithm)); |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
564 %! assert (isfield (output, "message") && ischar (output.message)); |
14895 | 565 |
22962
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
566 ## Tests for guarded_eval |
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
567 %!error <non-real value encountered> |
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
568 %! fminsearch (@(x) ([0 2i]), 0, optimset ("FunValCheck", "on")); |
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
569 %!error <NaN value encountered> |
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
570 %! fminsearch (@(x) (NaN), 0, optimset ("FunValCheck", "on")); |
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
571 %!error <Inf value encountered> |
fba8a6c742c6
fminsearch.m: Fix error in "guarded_eval".
Markus Muetzel <markus.muetzel@gmx.de>
parents:
22323
diff
changeset
|
572 %! fminsearch (@(x) (Inf), 0, optimset ("FunValCheck", "on")); |
23113
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
573 |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
574 ## Test input validation |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
575 %!error fminsearch () |
9241a0fa7873
Additional output arguments for fminsearch (bug #44220).
Markus Muetzel <markus.muetzel@gmx.de>
parents:
23085
diff
changeset
|
576 %!error fminsearch (1) |