This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfminsearch_nm.m
executable file
·333 lines (276 loc) · 10.6 KB
/
fminsearch_nm.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
function [x, fval, exitflag, output] = fminsearch_nm(fun, x0, options)
% -- [x, fval, exitflag, output] = fminsearch_nm(fun, x0, options)
%
% Find value of `x` which minimizes value of `fun` using the Nelder-Mead
% method. This function is a drop-in replacement for built-in `fminsearch`
% implementation.
%
% This implementation follows algorithm statement from [1], Section 8.1.
%
% Minimization parameters are passed through "options" argument. You can
% use `optimset` to set these options. If you want to use additional
% options, use `xoptimset`. Additional options are described in `xoptimset`
% manpage: `help xoptimset`.
%
% Structure `optimValues` passed to each 'OutputFcn' function call is
% extended with additional attributes:
%
% "fun": handle to the minimized function.
%
% "simplex_vertices": Matrix of current simplex vertices, in columns.
%
% References:
% [1] C. T. Kelley, Iterative Methods for Optimization, Society for
% Industrial and Applied Mathematics, Philadelphia, PA, 1999.
% Use a vector in computations
x0 = x0(:);
% Set options
verbosity = parse_display_option(options);
initial_simplex_strategy = xoptimget(options, 'InitialSimplexStrategy', 0); % initial simplex strategy
custom_initial_simplex = xoptimget(options, 'InitialSimplex', []); % custom initial simplex override
kmax = xoptimget(options, 'MaxFunEvals', 200 * length(x0)); % maximum function evaluations
max_iters = xoptimget(options, 'MaxIter', 200 * length(x0)); % maximum iterations
output_fun = xoptimget(options, 'OutputFcn', []);
tol_fun = xoptimget(options, 'TolFun', 1e-4); % maximum function value tolerance
tol_x = xoptimget(options, 'TolX', 1e-4); % maximum simplex oriented length
halting_criterion = xoptimget(options, 'HaltingTest', 0); % halting test number
max_restarts = xoptimget(options, 'MaxOrientedRestarts', 0); % enable oriented restarts
greedy_expansion = xoptimget(options, 'AcceptGreedyExpansion', false); % enable greedy expansion
% Prepare output function
output = @(iter, action, X, f, fcount, exitflag, output_msg) call_output_fun(output_fun, fun, 'iter', iter, action, X, f, fcount, exitflag, output_msg);
% Set transformation coefficients
mu_ic = -0.5; % inside contraction
mu_oc = 0.5; % outside contraction
mu_r = 1.0; % reflection
mu_e = 2.0; % expansion
% Define variables to establish naming
N = length(x0);
X = []; % matrix of vertices
X_prev = []; % matrix of vertices in previous iteration
f = []; % vector of values in vertices
f_prev = []; % vector of values in previous iteration
fcount = 0; % number of function evaluations
iter = 0; % number of iteration
% Define initial simplex
if ~isempty(custom_initial_simplex)
X = custom_initial_simplex;
else
X = create_simplex(initial_simplex_strategy, x0);
end
X_prev = X;
% Compute function values and sort vertices of S
for i = 1:N+1
x_i = X(:, i);
f(i) = fun(x_i);
end
f_prev = f;
fcount = N+1;
[X, X_prev, f, f_prev] = sort_by_values(X, X_prev, f, f_prev);
% Initialize oriented restart
restarts_enabled = (max_restarts > 0);
if restarts_enabled
alpha_0 = 0.0001; % oriented restart parameter
rcount = 0; % number of executed oriented restarts
V = X(:,2:end) - X(:,1);
sigma_max = max(vecnorm(V, 2));
sgrad = simplex_gradient(X, f);
alpha = alpha_0 * sigma_max / norm(sgrad);
end
% Call output function
iter = 0;
[exitflag, output_msg] = call_output_fun(output_fun, fun, 'init', iter, 'init', X, f, fcount, 0, '');
% Display log
if verbosity >= 3
iter_display_header();
iter_display_row(iter, fcount, f(1), 'initial simplex');
end
% Main loop
while exitflag ~= -1
% Skip halting test if using simplex movement and before first iteration
if (iter == 0 && halting_criterion == 3)
halt_now = false;
else
[halt_now, message] = should_halt(halting_criterion, N, X, X_prev, f, tol_x, tol_fun);
end
if halt_now
exitflag = 1;
output_msg = message;
break;
end
iter = iter + 1;
if iter > max_iters
exitflag = 0;
output_msg = 'Maximum number of iterations exceeded.\n';
break;
end
X_prev = X;
f_prev = f;
action = '';
shrink = false;
% (a) Compute centroid and reflection
x_bar = sum(X(:, 1:N), 2) ./ N;
x_N1 = X(:,N+1);
x_r = reflect_about(x_N1, x_bar, mu_r);
f_r = fun(x_r);
% Terminate on kmax evaluations
fcount = fcount + 1;
if fcount > kmax
exitflag = 0;
output_msg = 'Maximum number of function evaluations exceeded.\n';
break;
end
if f_r < f(N)
% (b) Reflect and sort
if f(1) <= f_r
X(:, N+1) = x_r;
f(N+1) = f_r;
action = 'reflect';
% (c) Expand and sort
else % f_r < f(1)
x_e = reflect_about(x_N1, x_bar, mu_e);
f_e = fun(x_e);
% Terminate on kmax evaluations
fcount = fcount + 1;
if fcount > kmax
exitflag = 0;
output_msg = 'Maximum number of function evaluations exceeded.\n';
break;
end
if greedy_expansion
expand = (f_e < f(1));
else
expand = (f_e < f_r);
end
if expand
X(:, N+1) = x_e;
f(N+1) = f_e;
action = 'expand';
else
X(:, N+1) = x_r;
f(N+1) = f_r;
action = 'reflect';
end
end
else % f(N) <= f_r
% (d) Outside contract
if f_r < f(N+1)
x_oc = reflect_about(x_N1, x_bar, mu_oc);
f_oc = fun(x_oc);
% Terminate on kmax evaluations
fcount = fcount + 1;
if fcount > kmax
exitflag = 0;
output_msg = 'Maximum number of function evaluations exceeded.\n';
break;
end
if f_oc <= f_r
X(:, N+1) = x_oc;
f(N+1) = f_oc;
action = 'outside contract';
else
shrink = true;
end
% (e) Inside contract
else % f(N+1) <= f_r
x_ic = reflect_about(x_N1, x_bar, mu_ic);
f_ic = fun(x_ic);
% Terminate on kmax evaluations
fcount = fcount + 1;
if fcount > kmax
exitflag = 0;
output_msg = 'Maximum number of function evaluations exceeded.\n';
break;
end
if f_ic < f(N+1)
X(:, N+1) = x_ic;
f(N+1) = f_ic;
action = 'inside contract';
else
shrink = true;
end
end
end
% (f) Shrink
if shrink
% Terminate if cannot do as many evaluations
if fcount > kmax - N
exitflag = 0;
output_msg = 'Maximum number of function evaluations exceeded.\n';
break;
end
x_1 = X(:, 1);
for i = 2:N+1
X(:, i) = x_1 - (X(:, i) - x_1) * 0.5;
f(i) = fun(X(:, i));
end
fcount = fcount + N;
action = 'shrink';
end
% (g) Sort vertices of S
[X, X_prev, f, f_prev] = sort_by_values(X, X_prev, f, f_prev);
% Display log
if verbosity >= 3
iter_display_row(iter, fcount, f(1), action);
end
% Call output function
[exitflag, output_msg] = output(iter, action, X, f, fcount, exitflag, output_msg);
if exitflag == -1
break;
end
% Oriented restart
simplex_found = (shrink == false);
if (simplex_found && restarts_enabled)
% New simplex was found before shrinking, so average over vertices
% dropped. We only need to test for sufficient decrease.
f_avg_diff = (sum(f) - sum(f_prev)) / (N+1);
X_prev_grad = simplex_gradient(X_prev, f_prev);
sufficient_decrease = (f_avg_diff < -alpha * norm(X_prev_grad)^2);
% Jump to next iteration if everything is OK
if sufficient_decrease == true
continue; % in main loop
end
% Terminate on max_restarts
rcount = rcount + 1;
if rcount > max_restarts
exitflag = 0;
output_msg = 'Maximum number of oriented restarts exceeded.\n';
break;
end
% Terminate if cannot do as many evaluations
if fcount > kmax - N
exitflag = 0;
output_msg = 'Maximum number of function evaluations exceeded.\n';
break;
end
fcount = fcount + N;
% Execute restart
[X, X_prev, f, f_prev] = restart_simplex(N, X_prev, f_prev, X_prev_grad, fun);
action = 'restart';
% Display log
if verbosity >= 3
iter_display_row(iter, fcount, f(1), action);
end
% Call output function
[exitflag, output_msg] = output(iter, action, X, f, fcount, exitflag, output_msg);
if exitflag == -1
break;
end
end
end % end of the main loop
% Print final message if verbosity set
if verbosity > 1 || (verbosity == 1 && exitflag ~= 1)
fprintf('\n');
fprintf(output_msg);
end
% Set return values
x = X(:, 1);
fval = f(1);
% Call output function
[exitflag, output_msg] = call_output_fun(output_fun, fun, 'done', iter, 'finish', X, f, fcount, exitflag, output_msg);
% Set output
output = struct;
output.iterations = iter;
output.funcCount = fcount;
output.algorithm = 'Nelder-Mead method';
output.message = output_msg;
end