-
Notifications
You must be signed in to change notification settings - Fork 0
/
nelder_mead.m
350 lines (311 loc) · 7.74 KB
/
nelder_mead.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
function [ x_opt, n_feval ] = nelder_mead ( x, function_handle, flag )
%*****************************************************************************80
%
%% nelder_mead() performs the Nelder-Mead optimization search.
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
% Modified:
%
% 19 January 2009
%
% Author:
%
% Jeff Borggaard
%
% Reference:
%
% John Nelder, Roger Mead,
% A simplex method for function minimization,
% Computer Journal,
% Volume 7, Number 4, January 1965, pages 308-313.
%
% Input:
%
% real X(M+1,M), contains a list of distinct points that serve as
% initial guesses for the solution. If the dimension of the space is M,
% then the matrix must contain exactly M+1 points. For instance,
% for a 2D space, you supply 3 points. Each row of the matrix contains
% one point; for a 2D space, this means that X would be a
% 3x2 matrix.
%
% handle FUNCTION_HANDLE, a quoted expression for the function,
% or the name of an M-file that defines the function, preceded by an
% "@" sign;
%
% logical FLAG, an optional argument; if present, and set to 1,
% it will cause the program to display a graphical image of the contours
% and solution procedure. Note that this option only makes sense for
% problems in 2D, that is, with N=2.
%
% Output:
%
% real X_OPT, the optimal value of X found by the algorithm.
%
% integer N_FEVAL, the number of function evaluations.
%
%
% Define algorithm constants
%
rho = 1; % rho > 0
xi = 2; % xi > max(rho, 1)
gam = 0.5; % 0 < gam < 1
sig = 0.5; % 0 < sig < 1
tolerance = 1.0E-06;
max_feval = 250;
%
% Initialization
%
[ temp, n_dim ] = size ( x );
if ( temp ~= n_dim + 1 )
fprintf ( 1, '\n' );
fprintf ( 1, 'NELDER_MEAD - Fatal error!\n' );
error(' Number of points must be = number of design variables + 1\n');
end
if ( nargin == 2 )
flag = 0;
end
if ( flag )
xp = linspace(-5,5,101);
yp = xp;
for i=1:101
for j=1:101
fp(j,i) = feval(function_handle,[xp(i),yp(j)]);
end
end
figure ( 27 )
hold on
contour(xp,yp,fp,linspace(-5,10,25))
if ( flag )
plot(x(1:2,1),x(1:2,2),'r')
plot(x(2:3,1),x(2:3,2),'r')
plot(x([1 3],1),x([1 3],2),'r')
pause
plot(x(1:2,1),x(1:2,2),'b')
plot(x(2:3,1),x(2:3,2),'b')
plot(x([1 3],1),x([1 3],2),'b')
end
end
index = 1 : n_dim + 1;
[f ] = evaluate ( x, function_handle );
n_feval = n_dim + 1;
[ f, index ] = sort ( f );
x = x(index,:);
%
% Begin the Nelder Mead iteration.
%
converged = 0;
diverged = 0;
while ( ~converged && ~diverged )
disp(x)
%
% Compute the midpoint of the simplex opposite the worst point.
%
x_bar = sum ( x(1:n_dim,:) ) / n_dim;
%
% Compute the reflection point.
%
x_r = ( 1 + rho ) * x_bar ...
- rho * x(n_dim+1,:);
f_r = feval(function_handle,x_r);
n_feval = n_feval + 1;
%
% Accept the point:
%
if ( f(1) <= f_r && f_r <= f(n_dim) )
x(n_dim+1,:) = x_r;
f(n_dim+1 ) = f_r;
if (flag)
title('reflection')
end
%
% Test for possible expansion.
%
elseif ( f_r < f(1) )
x_e = ( 1 + rho * xi ) * x_bar ...
- rho * xi * x(n_dim+1,:);
f_e = feval(function_handle,x_e);
n_feval = n_feval+1;
%
% Can we accept the expanded point?
%
if ( f_e < f_r )
x(n_dim+1,:) = x_e;
f(n_dim+1 ) = f_e;
if (flag), title('expansion'), end
else
x(n_dim+1,:) = x_r;
f(n_dim+1 ) = f_r;
if (flag), title('eventual reflection'), end
end
%
% Outside contraction.
%
elseif ( f(n_dim) <= f_r && f_r < f(n_dim+1) )
x_c = (1+rho*gam)*x_bar - rho*gam*x(n_dim+1,:);
f_c = feval(function_handle,x_c); n_feval = n_feval+1;
if (f_c <= f_r) % accept the contracted point
x(n_dim+1,:) = x_c;
f(n_dim+1 ) = f_c;
if (flag), title('outside contraction'), end
else
[x,f] = shrink(x,function_handle,sig); n_feval = n_feval+n_dim;
if (flag), title('shrink'), end
end
%
% F_R must be >= F(N_DIM+1).
% Try an inside contraction.
%
else
x_c = ( 1 - gam ) * x_bar ...
+ gam * x(n_dim+1,:);
f_c = feval(function_handle,x_c);
n_feval = n_feval+1;
%
% Can we accept the contracted point?
%
if (f_c < f(n_dim+1))
x(n_dim+1,:) = x_c;
f(n_dim+1 ) = f_c;
if (flag), title('inside contraction'), end
else
[x,f] = shrink(x,function_handle,sig); n_feval = n_feval+n_dim;
if (flag), title('shrink'), end
end
end
%
% Resort the points. Note that we are not implementing the usual
% Nelder-Mead tie-breaking rules (when f(1) = f(2) or f(n_dim) =
% f(n_dim+1)...
%
[ f, index ] = sort ( f );
x = x(index,:);
%
% Test for convergence
%
converged = f(n_dim+1)-f(1) < tolerance;
%
% Test for divergence
%
diverged = ( max_feval < n_feval );
if ( flag )
plot(x(1:2,1),x(1:2,2),'r')
plot(x(2:3,1),x(2:3,2),'r')
plot(x([1 3],1),x([1 3],2),'r')
pause
plot(x(1:2,1),x(1:2,2),'b')
plot(x(2:3,1),x(2:3,2),'b')
plot(x([1 3],1),x([1 3],2),'b')
end
end
if ( false )
fprintf('The best point x^* was: %d %d\n',x(1,:));
fprintf('f(x^*) = %d\n',f(1));
end
x_opt = x(1,:);
if ( diverged )
fprintf ( 1, '\n' );
fprintf ( 1, 'NELDER_MEAD - Warning!\n' );
fprintf ( 1, ' The maximum number of function evaluations was exceeded\n')
fprintf ( 1, ' without convergence being achieved.\n' );
end
return
end
function f = evaluate ( x, function_handle )
%*****************************************************************************80
%
%% evaluate() handles the evaluation of the function at each point.
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
% Modified:
%
% 19 January 2009
%
% Author:
%
% Jeff Borggaard
%
% Reference:
%
% John Nelder, Roger Mead,
% A simplex method for function minimization,
% Computer Journal,
% Volume 7, Number 4, January 1965, pages 308-313.
%
% Input:
%
% real X(N_DIM+1,N_DIM), the points.
%
% real FUNCTION_HANDLE ( X ), the handle of a MATLAB procedure
% to evaluate the function.
%
% Output:
%
% real F(1,NDIM+1), the value of the function at each point.
%
n_dim = size ( x, 2 );
f = zeros ( 1, n_dim + 1 );
for i = 1 : n_dim + 1
f(i) = feval(function_handle,x(i,:));
end
return
end
function [ x, f ] = shrink ( x, function_handle, sig )
%*****************************************************************************80
%
%% shrink() shrinks the simplex towards the best point.
%
% Discussion:
%
% In the worst case, we need to shrink the simplex along each edge towards
% the current "best" point. This is quite expensive, requiring n_dim new
% function evaluations.
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
% Modified:
%
% 19 January 2009
%
% Author:
%
% Jeff Borggaard
%
% Reference:
%
% John Nelder, Roger Mead,
% A simplex method for function minimization,
% Computer Journal,
% Volume 7, Number 4, January 1965, pages 308-313.
%
% Input:
%
% real X(N_DIM+1,N_DIM), the points.
%
% real FUNCTION_HANDLE ( X ), the handle of a MATLAB procedure
% to evaluate the function.
%
% real SIG, ?
%
% Output:
%
% real X(N_DIM+1,N_DIM), the points after shrinking was applied.
%
% real F(1,NDIM+1), the value of the function at each point.
%
n_dim = size ( x, 2 );
x1 = x(1,:);
f(1) = feval ( function_handle, x1 );
for i = 2 : n_dim + 1
x(i,:) = sig * x(i,:) + ( 1.0 - sig ) * x(1,:);
f(i) = feval ( function_handle, x(i,:) );
end
return
end