annotate main/optim/inst/nelder_mead_min.m @ 9930:d30cfca46e8a octave-forge

optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
author carandraug
date Fri, 30 Mar 2012 15:14:48 +0000
parents 557017e3e86e
children 28b882879c6e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9930
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 7723
diff changeset
1 ## Copyright (C) 2002-2008 Etienne Grossmann <etienne@cs.uky.edu>
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
2 ##
9930
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 7723
diff changeset
3 ## This program is free software; you can redistribute it and/or modify it under
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 7723
diff changeset
4 ## the terms of the GNU General Public License as published by the Free Software
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 7723
diff changeset
5 ## Foundation; either version 3 of the License, or (at your option) any later
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 7723
diff changeset
6 ## version.
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
7 ##
9930
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 7723
diff changeset
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9930
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 7723
diff changeset
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 7723
diff changeset
11 ## details.
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
12 ##
9930
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 7723
diff changeset
13 ## You should have received a copy of the GNU General Public License along with
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 7723
diff changeset
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
15
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
16 ## [x0,v,nev] = nelder_mead_min (f,args,ctl) - Nelder-Mead minimization
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
17 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
18 ## Minimize 'f' using the Nelder-Mead algorithm. This function is inspired
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
19 ## from the that found in the book "Numerical Recipes".
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
20 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
21 ## ARGUMENTS
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
22 ## ---------
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
23 ## f : string : Name of function. Must return a real value
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
24 ## args : list : Arguments passed to f.
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
25 ## or matrix : f's only argument
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
26 ## ctl : vector : (Optional) Control variables, described below
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
27 ## or struct
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
28 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
29 ## RETURNED VALUES
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
30 ## ---------------
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
31 ## x0 : matrix : Local minimum of f
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
32 ## v : real : Value of f in x0
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
33 ## nev : number : Number of function evaluations
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
34 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
35 ## CONTROL VARIABLE : (optional) may be named arguments (i.e. "name",value
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
36 ## ------------------ pairs), a struct, or a vector of length <= 6, where
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
37 ## NaN's are ignored. Default values are written <value>.
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
38 ## OPT. VECTOR
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
39 ## NAME POS
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
40 ## ftol,f N/A : Stopping criterion : stop search when values at simplex
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
41 ## vertices are all alike, as tested by
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
42 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
43 ## f > (max_i (f_i) - min_i (f_i)) /max(max(|f_i|),1)
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
44 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
45 ## where f_i are the values of f at the vertices. <10*eps>
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
46 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
47 ## rtol,r N/A : Stop search when biggest radius of simplex, using
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
48 ## infinity-norm, is small, as tested by :
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
49 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
50 ## ctl(2) > Radius <10*eps>
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
51 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
52 ## vtol,v N/A : Stop search when volume of simplex is small, tested by
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
53 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
54 ## ctl(2) > Vol
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
55 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
56 ## crit,c ctl(1) : Set one stopping criterion, 'ftol' (c=1), 'rtol' (c=2)
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
57 ## or 'vtol' (c=3) to the value of the 'tol' option. <1>
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
58 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
59 ## tol, t ctl(2) : Threshold in termination test chosen by 'crit' <10*eps>
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
60 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
61 ## narg ctl(3) : Position of the minimized argument in args <1>
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
62 ## maxev ctl(4) : Maximum number of function evaluations. This number <inf>
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
63 ## may be slightly exceeded.
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
64 ## isz ctl(5) : Size of initial simplex, which is : <1>
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
65 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
66 ## { x + e_i | i in 0..N }
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
67 ##
7140
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
68 ## Where x == args{narg} is the initial value
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
69 ## e_0 == zeros (size (x)),
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
70 ## e_i(j) == 0 if j != i and e_i(i) == ctl(5)
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
71 ## e_i has same size as x
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
72 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
73 ## Set ctl(5) to the distance you expect between the starting
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
74 ## point and the minimum.
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
75 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
76 ## rst ctl(6) : When a minimum is found the algorithm restarts next to
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
77 ## it until the minimum does not improve anymore. ctl(6) is
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
78 ## the maximum number of restarts. Set ctl(6) to zero if
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
79 ## you know the function is well-behaved or if you don't
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
80 ## mind not getting a true minimum. <0>
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
81 ##
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
82 ## verbose, v Be more or less verbose (quiet=0) <0>
9930
d30cfca46e8a optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents: 7723
diff changeset
83
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
84 function [x,v,nev] = nelder_mead_min (f, args, varargin)
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
85
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
86 verbose = 0;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
87
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
88 # Default control variables
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
89 ftol = rtol = 10*eps; # Stop either by likeness of values or
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
90 vtol = nan; # radius, but don't care about volume.
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
91 crit = 0; # Stopping criterion ctl(1)
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
92 tol = 10*eps; # Stopping test's threshold ctl(2)
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
93 narg = 1; # Position of minimized arg ctl(3)
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
94 maxev = inf; # Max num of func evaluations ctl(4)
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
95 isz = 1; # Initial size ctl(5)
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
96 rst = 0; # Max # of restarts
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
97
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
98
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
99 if nargin >= 3, # Read control arguments
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
100 va_arg_cnt = 1;
7140
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
101 if nargin > 3,
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
102 ctl = struct (varargin{:});
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
103 else
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
104 ctl = varargin{va_arg_cnt++};
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
105 end
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
106 if isnumeric (ctl)
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
107 if length (ctl)>=1 && !isnan (ctl(1)), crit = ctl(1); end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
108 if length (ctl)>=2 && !isnan (ctl(2)), tol = ctl(2); end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
109 if length (ctl)>=3 && !isnan (ctl(3)), narg = ctl(3); end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
110 if length (ctl)>=4 && !isnan (ctl(4)), maxev = ctl(4); end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
111 if length (ctl)>=5 && !isnan (ctl(5)), isz = ctl(5); end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
112 if length (ctl)>=6 && !isnan (ctl(6)), rst = ctl(6); end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
113 else
7140
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
114 if isfield (ctl, "crit") && ! isnan (ctl.crit ), crit = ctl.crit ; end
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
115 if isfield (ctl, "tol") && ! isnan (ctl.tol ), tol = ctl.tol ; end
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
116 if isfield (ctl, "ftol") && ! isnan (ctl.ftol ), ftol = ctl.ftol ; end
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
117 if isfield (ctl, "rtol") && ! isnan (ctl.rtol ), rtol = ctl.rtol ; end
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
118 if isfield (ctl, "vtol") && ! isnan (ctl.vtol ), vtol = ctl.vtol ; end
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
119 if isfield (ctl, "narg") && ! isnan (ctl.narg ), narg = ctl.narg ; end
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
120 if isfield (ctl,"maxev") && ! isnan (ctl.maxev), maxev = ctl.maxev; end
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
121 if isfield (ctl, "isz") && ! isnan (ctl.isz ), isz = ctl.isz ; end
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
122 if isfield (ctl, "rst") && ! isnan (ctl.rst ), rst = ctl.rst ; end
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
123 if isfield(ctl,"verbose")&& !isnan(ctl.verbose),verbose=ctl.verbose;end
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
124 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
125 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
126
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
127
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
128 if crit == 1, ftol = tol;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
129 elseif crit == 2, rtol = tol;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
130 elseif crit == 3, vtol = tol;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
131 elseif crit, error ("crit is %i. Should be 1,2 or 3.\n");
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
132 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
133
5857
4e699246fb48 corrected bug fix for nelder_mead_min
jjstickel
parents: 5854
diff changeset
134 if iscell (args)
4e699246fb48 corrected bug fix for nelder_mead_min
jjstickel
parents: 5854
diff changeset
135 x = args{1};
4e699246fb48 corrected bug fix for nelder_mead_min
jjstickel
parents: 5854
diff changeset
136 else # Single argument
4e699246fb48 corrected bug fix for nelder_mead_min
jjstickel
parents: 5854
diff changeset
137 x = args;
4e699246fb48 corrected bug fix for nelder_mead_min
jjstickel
parents: 5854
diff changeset
138 args = {args};
4e699246fb48 corrected bug fix for nelder_mead_min
jjstickel
parents: 5854
diff changeset
139 endif
5088
469d7cbdfcf1 Go from lists to cells
etienne
parents: 2370
diff changeset
140
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
141 if narg > length (args) # Check
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
142 error ("nelder_mead_min : narg==%i, length (args)==%i\n",
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
143 narg, length (args));
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
144 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
145
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
146 [R,C] = size (x);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
147 N = R*C; # Size of argument
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
148 x = x(:);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
149 # Initial simplex
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
150 u = isz * eye (N+1,N) + ones(N+1,1)*x';
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
151
5088
469d7cbdfcf1 Go from lists to cells
etienne
parents: 2370
diff changeset
152 y = zeros (N+1,1);
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
153 for i = 1:N+1,
7158
6b42db28f880 fix for test_fminunc_, test_minimize_1, test_nelder_mead_min_1; remove debugging information
schloegl
parents: 7156
diff changeset
154 y(i) = feval (f, args{1:narg-1},reshape(u(i,:),R,C),args{narg+1:end});
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
155 end ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
156 nev = N+1;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
157
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
158 [ymin,imin] = min(y);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
159 ymin0 = ymin;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
160 ## y
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
161 nextprint = 0 ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
162 v = nan;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
163 while nev <= maxev,
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
164
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
165 ## ymin, ymax, ymx2 : lowest, highest and 2nd highest function values
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
166 ## imin, imax, imx2 : indices of vertices with these values
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
167 [ymin,imin] = min(y); [ymax,imax] = max(y) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
168 y(imax) = ymin ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
169 [ymx2,imx2] = max(y) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
170 y(imax) = ymax ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
171
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
172 ## ymin may be > ymin0 after restarting
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
173 ## if ymin > ymin0 ,
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
174 ## "nelder-mead : Whoa 'downsimplex' Should be renamed 'upsimplex'"
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
175 ## keyboard
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
176 ## end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
177
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
178 # Compute stopping criterion
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
179 done = 0;
7140
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
180 if ! isnan (ftol),
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
181 done |= ((max(y)-min(y)) / max(1,max(abs(y))) < ftol);
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
182 end
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
183 if ! isnan (rtol),
7723
557017e3e86e For Muhali <muhali@shaw.ca>: small fix for stoppin criteria.
i7tiol
parents: 7158
diff changeset
184 done |= (2*max (max (u) - min (u)) < rtol);
7140
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
185 end
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
186 if ! isnan (vtol)
7140
4010976d6f86 replace obsolete functions (mostly related to lists)
schloegl
parents: 5857
diff changeset
187 done |= (abs (det (u(1:N,:)-ones(N,1)*u(N+1,:)))/factorial(N) < vtol);
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
188 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
189 ## [ 2*max (max (u) - min (u)), abs (det (u(1:N,:)-ones(N,1)*u(N+1,:)))/factorial(N);\
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
190 ## rtol, vtol]
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
191
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
192 # Eventually print some info
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
193 if verbose && nev > nextprint && ! done
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
194
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
195 printf("nev=%-5d imin=%-3d ymin=%-8.3g done=%i\n",\
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
196 nev,imin,ymin,done) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
197
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
198 nextprint = nextprint + 100 ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
199 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
200
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
201 if done # Termination test
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
202 if (rst > 0) && (isnan (v) || v > ymin)
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
203 rst--;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
204 if verbose
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
205 if isnan (v),
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
206 printf ("Restarting next to minimum %10.3e\n",ymin);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
207 else
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
208 printf ("Restarting next to minimum %10.3e\n",ymin-v);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
209 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
210 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
211 # Keep best minimum
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
212 x = reshape (u(imin,:), R, C) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
213 v = ymin ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
214
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
215 jumplen = 10 * max (max (u) - min (u));
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
216
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
217 u += jumplen * randn (size (u));
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
218 for i = 1:N+1, y(i) = \
7156
16130d52716a simplify arg-list, remove obsolete leval.oct
schloegl
parents: 7140
diff changeset
219 feval (f, args{1:narg-1},reshape(u(i,:),R,C),args{narg+1:length(args)});
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
220 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
221 nev += N+1;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
222 [ymin,imin] = min(y); [ymax,imax] = max(y);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
223 y(imax) = ymin;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
224 [ymx2,imx2] = max(y);
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
225 y(imax) = ymax ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
226 else
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
227 if isnan (v),
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
228 x = reshape (u(imin,:), R, C) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
229 v = ymin ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
230 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
231 if verbose,
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
232 printf("nev=%-5d imin=%-3d ymin=%-8.3g done=%i. Done\n",\
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
233 nev,imin,ymin,done) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
234 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
235 return
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
236 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
237
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
238 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
239 ## [ y' u ]
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
240
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
241 tra = 0 ; # 'trace' debug var contains flags
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
242 if verbose > 1, str = sprintf (" %i : %10.3e --",done,ymin); end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
243
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
244 # Look for a new point
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
245 xsum = sum(u) ; # Consider reflection of worst vertice
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
246 # around centroid.
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
247 ## f1 = (1-(-1))/N = 2/N;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
248 ## f2 = f1 - (-1) = 2/N + 1 = (N+2)/N
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
249 xnew = (2*xsum - (N+2)*u(imax,:)) / N;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
250 ## xnew = (2*xsum - N*u(imax,:)) / N;
7156
16130d52716a simplify arg-list, remove obsolete leval.oct
schloegl
parents: 7140
diff changeset
251 ynew = feval (f, args{1:narg-1},reshape(xnew,R,C),args{narg+1:length(args)});
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
252 nev++;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
253
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
254 if ynew <= ymin , # Reflection is good
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
255
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
256 tra += 1 ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
257 if verbose > 1
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
258 str = [str,sprintf(" %3i : %10.3e good refl >>",nev,ynew-ymin)];
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
259 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
260 y(imax) = ynew; u(imax,:) = xnew ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
261 ## ymin = ynew;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
262 ## imin = imax;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
263 xsum = sum(u) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
264
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
265 ## f1 = (1-2)/N = -1/N
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
266 ## f2 = f1 - 2 = -1/N - 2 = -(2*N+1)/N
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
267 xnew = ( -xsum + (2*N+1)*u(imax,:) ) / N;
7156
16130d52716a simplify arg-list, remove obsolete leval.oct
schloegl
parents: 7140
diff changeset
268 ynew = feval (f, args{1:narg-1},reshape(xnew,R,C),args{narg+1:length(args)});
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
269 nev++;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
270
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
271 if ynew <= ymin , # expansion improves
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
272 tra += 2 ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
273 ## 'expanded reflection'
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
274 y(imax) = ynew ; u(imax,:) = xnew ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
275 xsum = sum(u) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
276 if verbose > 1
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
277 str = [str,sprintf(" %3i : %10.3e expd refl",nev,ynew-ymin)];
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
278 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
279 else
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
280 tra += 4 ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
281 ## 'plain reflection'
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
282 ## Updating of y and u has already been done
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
283 if verbose > 1
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
284 str = [str,sprintf(" %3i : %10.3e plain ref",nev,ynew-ymin)];
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
285 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
286 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
287 # Reflexion is really bad
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
288 elseif ynew >= ymax ,
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
289
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
290 tra += 8 ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
291 if verbose > 1
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
292 str = [str,sprintf(" %3i : %10.3e intermedt >>",nev,ynew-ymin)];
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
293 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
294 ## look for intermediate point
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
295 # Bring worst point closer to centroid
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
296 ## f1 = (1-0.5)/N = 0.5/N
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
297 ## f2 = f1 - 0.5 = 0.5*(1 - N)/N
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
298 xnew = 0.5*(xsum + (N-1)*u(imax,:)) / N;
7156
16130d52716a simplify arg-list, remove obsolete leval.oct
schloegl
parents: 7140
diff changeset
299 ynew = feval (f, args{1:narg-1},reshape(xnew,R,C),args{narg+1:length(args)});
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
300 nev++;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
301
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
302 if ynew >= ymax , # New point is even worse. Contract whole
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
303 # simplex
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
304
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
305 nev += N + 1 ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
306 ## u0 = u;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
307 u = (u + ones(N+1,1)*u(imin,:)) / 2;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
308 ## keyboard
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
309
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
310 ## Code that doesn't care about value of empty_list_elements_ok
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
311 if imin == 1 , ii = 2:N+1;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
312 elseif imin == N+1, ii = 1:N;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
313 else ii = [1:imin-1,imin+1:N+1]; end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
314 for i = ii
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
315 y(i) = \
7156
16130d52716a simplify arg-list, remove obsolete leval.oct
schloegl
parents: 7140
diff changeset
316 ynew = feval (f, args{1:narg-1},reshape(u(i,:),R,C),args{narg+1:length(args)});
2370
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
317 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
318 ## 'contraction'
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
319 tra += 16 ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
320 if verbose > 1
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
321 str = [str,sprintf(" %3i contractn",nev)];
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
322 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
323 else # Replace highest point
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
324 y(imax) = ynew ; u(imax,:) = xnew ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
325 xsum = sum(u) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
326 ## 'intermediate'
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
327 tra += 32 ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
328 if verbose > 1
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
329 str = [str,sprintf(" %3i : %10.3e intermedt",nev,ynew-ymin)];
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
330 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
331 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
332
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
333 else # Reflexion is neither good nor bad
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
334 y(imax) = ynew ; u(imax,:) = xnew ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
335 xsum = sum(u) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
336 ## 'plain reflection (2)'
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
337 tra += 64 ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
338 if verbose > 1
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
339 str = [str,sprintf(" %3i : %10.3e keep refl",nev,ynew-ymin)];
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
340 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
341 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
342 if verbose > 1, printf ("%s\n",str); end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
343 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
344
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
345 if verbose >= 0
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
346 printf ("nelder_mead : Too many iterations. Returning\n");
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
347 end
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
348
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
349 if isnan (v) || v > ymin,
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
350 x = reshape (u(imin,:), R, C) ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
351 v = ymin ;
24d6a5cdedfe Changed the directory structure to match the package system
hauberg
parents:
diff changeset
352 end