Mercurial > forge
annotate main/optim/inst/nmsmax.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 | b11b5363d680 |
children |
rev | line source |
---|---|
9930
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
1 %% Copyright (C) 2002 N.J.Higham |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
2 %% Copyright (C) 2003 Andy Adler <adler@ncf.ca> |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
3 %% |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
4 %% 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:
7870
diff
changeset
|
5 %% 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:
7870
diff
changeset
|
6 %% 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:
7870
diff
changeset
|
7 %% version. |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
8 %% |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
9 %% This program is distributed in the hope that it will be useful, but WITHOUT |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
10 %% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
11 %% 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:
7870
diff
changeset
|
12 %% details. |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
13 %% |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
14 %% 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:
7870
diff
changeset
|
15 %% this program; if not, see <http://www.gnu.org/licenses/>. |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
16 |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
17 %%NMSMAX Nelder-Mead simplex method for direct search optimization. |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
18 %% [x, fmax, nf] = NMSMAX(FUN, x0, STOPIT, SAVIT) attempts to |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
19 %% maximize the function FUN, using the starting vector x0. |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
20 %% The Nelder-Mead direct search method is used. |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
21 %% Output arguments: |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
22 %% x = vector yielding largest function value found, |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
23 %% fmax = function value at x, |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
24 %% nf = number of function evaluations. |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
25 %% The iteration is terminated when either |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
26 %% - the relative size of the simplex is <= STOPIT(1) |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
27 %% (default 1e-3), |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
28 %% - STOPIT(2) function evaluations have been performed |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
29 %% (default inf, i.e., no limit), or |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
30 %% - a function value equals or exceeds STOPIT(3) |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
31 %% (default inf, i.e., no test on function values). |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
32 %% The form of the initial simplex is determined by STOPIT(4): |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
33 %% STOPIT(4) = 0: regular simplex (sides of equal length, the default) |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
34 %% STOPIT(4) = 1: right-angled simplex. |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
35 %% Progress of the iteration is not shown if STOPIT(5) = 0 (default 1). |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
36 %% STOPIT(6) indicates the direction (ie. minimization or |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
37 %% maximization.) Default is 1, maximization. |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
38 %% set STOPIT(6)=-1 for minimization |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
39 %% If a non-empty fourth parameter string SAVIT is present, then |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
40 %% `SAVE SAVIT x fmax nf' is executed after each inner iteration. |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
41 %% NB: x0 can be a matrix. In the output argument, in SAVIT saves, |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
42 %% and in function calls, x has the same shape as x0. |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
43 %% NMSMAX(fun, x0, STOPIT, SAVIT, P1, P2,...) allows additional |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
44 %% arguments to be passed to fun, via feval(fun,x,P1,P2,...). |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
45 %% References: |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
46 %% N. J. Higham, Optimization by direct search in matrix computations, |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
47 %% SIAM J. Matrix Anal. Appl, 14(2): 317-333, 1993. |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
48 %% C. T. Kelley, Iterative Methods for Optimization, Society for Industrial |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
49 %% and Applied Mathematics, Philadelphia, PA, 1999. |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
50 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
51 % From Matrix Toolbox |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
52 % Copyright (C) 2002 N.J.Higham |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
53 % www.maths.man.ac.uk/~higham/mctoolbox |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
54 % Modifications for octave by A.Adler 2003 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
55 |
9930
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7870
diff
changeset
|
56 function [x, fmax, nf] = nmsmax(fun, x, stopit, savit, varargin) |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
57 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
58 x0 = x(:); % Work with column vector internally. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
59 n = length(x0); |
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 % Set up convergence parameters etc. |
7870 | 62 if (nargin < 3 || isempty(stopit)) |
63 stopit(1) = 1e-3; | |
64 end | |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
65 tol = stopit(1); % Tolerance for cgce test based on relative size of simplex. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
66 if length(stopit) == 1, stopit(2) = inf; end % Max no. of f-evaluations. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
67 if length(stopit) == 2, stopit(3) = inf; end % Default target for f-values. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
68 if length(stopit) == 3, stopit(4) = 0; end % Default initial simplex. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
69 if length(stopit) == 4, stopit(5) = 1; end % Default: show progress. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
70 trace = stopit(5); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
71 if length(stopit) == 5, stopit(6) = 1; end % Default: maximize |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
72 dirn= stopit(6); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
73 if nargin < 4, savit = []; end % File name for snapshots. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
74 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
75 V = [zeros(n,1) eye(n)]; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
76 f = zeros(n+1,1); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
77 V(:,1) = x0; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
78 f(1) = dirn*feval(fun,x,varargin{:}); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
79 fmax_old = f(1); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
80 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
81 if trace, fprintf('f(x0) = %9.4e\n', f(1)), end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
82 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
83 k = 0; m = 0; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
84 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
85 % Set up initial simplex. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
86 scale = max(norm(x0,inf),1); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
87 if stopit(4) == 0 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
88 % Regular simplex - all edges have same length. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
89 % Generated from construction given in reference [18, pp. 80-81] of [1]. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
90 alpha = scale / (n*sqrt(2)) * [ sqrt(n+1)-1+n sqrt(n+1)-1 ]; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
91 V(:,2:n+1) = (x0 + alpha(2)*ones(n,1)) * ones(1,n); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
92 for j=2:n+1 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
93 V(j-1,j) = x0(j-1) + alpha(1); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
94 x(:) = V(:,j); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
95 f(j) = dirn*feval(fun,x,varargin{:}); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
96 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
97 else |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
98 % Right-angled simplex based on co-ordinate axes. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
99 alpha = scale*ones(n+1,1); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
100 for j=2:n+1 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
101 V(:,j) = x0 + alpha(j)*V(:,j); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
102 x(:) = V(:,j); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
103 f(j) = dirn*feval(fun,x,varargin{:}); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
104 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
105 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
106 nf = n+1; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
107 how = 'initial '; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
108 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
109 [temp,j] = sort(f); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
110 j = j(n+1:-1:1); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
111 f = f(j); V = V(:,j); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
112 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
113 alpha = 1; beta = 1/2; gamma = 2; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
114 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
115 while 1 %%%%%% Outer (and only) loop. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
116 k = k+1; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
117 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
118 fmax = f(1); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
119 if fmax > fmax_old |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
120 if ~isempty(savit) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
121 x(:) = V(:,1); eval(['save ' savit ' x fmax nf']) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
122 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
123 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
124 if trace |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
125 fprintf('Iter. %2.0f,', k) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
126 fprintf([' how = ' how ' ']); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
127 fprintf('nf = %3.0f, f = %9.4e (%2.1f%%)\n', nf, fmax, ... |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
128 100*(fmax-fmax_old)/(abs(fmax_old)+eps)) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
129 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
130 fmax_old = fmax; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
131 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
132 %%% Three stopping tests from MDSMAX.M |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
133 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
134 % Stopping Test 1 - f reached target value? |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
135 if fmax >= stopit(3) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
136 msg = ['Exceeded target...quitting\n']; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
137 break % Quit. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
138 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
139 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
140 % Stopping Test 2 - too many f-evals? |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
141 if nf >= stopit(2) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
142 msg = ['Max no. of function evaluations exceeded...quitting\n']; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
143 break % Quit. |
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 % Stopping Test 3 - converged? This is test (4.3) in [1]. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
147 v1 = V(:,1); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
148 size_simplex = norm(V(:,2:n+1)-v1(:,ones(1,n)),1) / max(1, norm(v1,1)); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
149 if size_simplex <= tol |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
150 msg = sprintf('Simplex size %9.4e <= %9.4e...quitting\n', ... |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
151 size_simplex, tol); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
152 break % Quit. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
153 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
154 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
155 % One step of the Nelder-Mead simplex algorithm |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
156 % NJH: Altered function calls and changed CNT to NF. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
157 % Changed each `fr < f(1)' type test to `>' for maximization |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
158 % and re-ordered function values after sort. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
159 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
160 vbar = (sum(V(:,1:n)')/n)'; % Mean value |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
161 vr = (1 + alpha)*vbar - alpha*V(:,n+1); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
162 x(:) = vr; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
163 fr = dirn*feval(fun,x,varargin{:}); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
164 nf = nf + 1; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
165 vk = vr; fk = fr; how = 'reflect, '; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
166 if fr > f(n) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
167 if fr > f(1) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
168 ve = gamma*vr + (1-gamma)*vbar; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
169 x(:) = ve; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
170 fe = dirn*feval(fun,x,varargin{:}); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
171 nf = nf + 1; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
172 if fe > f(1) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
173 vk = ve; fk = fe; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
174 how = 'expand, '; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
175 end |
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 else |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
178 vt = V(:,n+1); ft = f(n+1); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
179 if fr > ft |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
180 vt = vr; ft = fr; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
181 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
182 vc = beta*vt + (1-beta)*vbar; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
183 x(:) = vc; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
184 fc = dirn*feval(fun,x,varargin{:}); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
185 nf = nf + 1; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
186 if fc > f(n) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
187 vk = vc; fk = fc; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
188 how = 'contract,'; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
189 else |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
190 for j = 2:n |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
191 V(:,j) = (V(:,1) + V(:,j))/2; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
192 x(:) = V(:,j); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
193 f(j) = dirn*feval(fun,x,varargin{:}); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
194 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
195 nf = nf + n-1; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
196 vk = (V(:,1) + V(:,n+1))/2; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
197 x(:) = vk; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
198 fk = dirn*feval(fun,x,varargin{:}); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
199 nf = nf + 1; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
200 how = 'shrink, '; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
201 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
202 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
203 V(:,n+1) = vk; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
204 f(n+1) = fk; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
205 [temp,j] = sort(f); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
206 j = j(n+1:-1:1); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
207 f = f(j); V = V(:,j); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
208 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
209 end %%%%%% End of outer (and only) loop. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
210 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
211 % Finished. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
212 if trace, fprintf(msg), end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
213 x(:) = V(:,1); |