?? 1.m.txt
字號(hào):
%本程序是利用改進(jìn)后的單純形算法!
function [x,fval,exitflag,output] = N_M(funfcn,x,options,varargin)
defaultopt = struct('Display','notify','MaxIter','200*numberOfVariables',...
'MaxFunEvals','200*numberOfVariables','TolX',1e-6,'TolFun',1e-6);
if nargin==1 & nargout <= 1 & isequal(funfcn,'defaults')
x = defaultopt;
return
end
if nargin < 2,
error('FMINSEARCH requires at least two input arguments');
end
if nargin<3, options = []; end
n = prod(size(x));
numberOfVariables = n;
printtype = optimget(options,'Display',defaultopt,'fast');
tolx = optimget(options,'TolX',defaultopt,'fast');
tolf = optimget(options,'TolFun',defaultopt,'fast');
maxfun = optimget(options,'MaxFunEvals',defaultopt,'fast');
maxiter = optimget(options,'MaxIter',defaultopt,'fast');
% In case the defaults were gathered from calling: optimset('fminsearch'):
if ischar(maxfun)
if isequal(lower(maxfun),'200*numberofvariables')
maxfun = 200*numberOfVariables;
else
error('Option ''MaxFunEvals'' must be an integer value if not the default.')
end
end
if ischar(maxiter)
if isequal(lower(maxiter),'200*numberofvariables')
maxiter = 200*numberOfVariables;
else
error('Option ''MaxIter'' must be an integer value if not the default.')
end
end
switch printtype
case 'notify'
prnt = 1;
case {'none','off'}
prnt = 0;
case 'iter'
prnt = 3;
case 'final'
prnt = 2;
case 'simplex'
prnt = 4;
otherwise
prnt = 1;
end
header = ' Iteration Func-count min f(x) Procedure';
% Convert to inline function as needed.
funfcn = fcnchk(funfcn,length(varargin));
n = prod(size(x));
% Initialize parameters
rho = 1; chi = 2.65; psi = 0.5; sigma = 0.5;
onesn = ones(1,n);
two2np1 = 2:n+1;
one2n = 1:n;
% Set up a simplex near the initial guess.
xin = x(:); % Force xin to be a column vector
v = zeros(n,n+1); fv = zeros(1,n+1);
v(:,1) = xin; % Place input guess in the simplex! (credit L.Pfeffer at Stanford)
x(:) = xin; % Change x to the form expected by funfcn
fv(:,1) = feval(funfcn,x,varargin{:});
% Following improvement suggested by L.Pfeffer at Stanford
usual_delta = 0.05; % 5 percent deltas for non-zero terms
zero_term_delta = 0.00025; % Even smaller delta for zero elements of x
for j = 1:n
y = xin;
if y(j) ~= 0
y(j) = (1 + usual_delta)*y(j);
else
y(j) = zero_term_delta;
end
v(:,j+1) = y;
x(:) = y; f = feval(funfcn,x,varargin{:});
fv(1,j+1) = f;
end
% sort so v(1,:) has the lowest function value
[fv,j] = sort(fv);
v = v(:,j);
how = 'initial';
itercount = 1;
func_evals = n+1;
if prnt == 3
disp(' ')
disp(header)
disp([sprintf(' %5.0f %5.0f %12.6g ', itercount, func_evals, fv(1)), how])
elseif prnt == 4
clc
formatsave = get(0,{'format','formatspacing'});
format compact
format short e
disp(' ')
disp(how)
v
fv
func_evals
end
exitflag = 1;
while func_evals < maxfun & itercount < maxiter
if max(max(abs(v(:,two2np1)-v(:,onesn)))) <= tolx & ...
max(abs(fv(1)-fv(two2np1))) <= tolf
break
end
how = '';
% 計(jì)算反射點(diǎn)
xbar = sum(v(:,one2n), 2)/n;
xr = (1 + rho)*xbar - rho*v(:,end);
x(:) = xr; fxr = feval(funfcn,x,varargin{:});
func_evals = func_evals+1;
if fxr < fv(:,1)
% 計(jì)算擴(kuò)展點(diǎn)
xe = (1 + rho*chi)*xbar - rho*chi*v(:,end);
x(:) = xe; fxe = feval(funfcn,x,varargin{:});
func_evals = func_evals+1;
if fxe < fxr
v(:,end) = xe;
fv(:,end) = fxe;
how = 'expand';
else
v(:,end) = xr;
fv(:,end) = fxr;
how = 'reflect';
end
else % fv(:,1) <= fxr
if fxr < fv(:,n)
v(:,end) = xr;
fv(:,end) = fxr;
how = 'reflect';
else % fxr >= fv(:,n)
% Perform contraction
if fxr < fv(:,end)
% Perform an outside contraction
xc = (1 + psi*rho)*xbar - psi*rho*v(:,end);
x(:) = xc; fxc = feval(funfcn,x,varargin{:});
func_evals = func_evals+1;
if fxc <= fxr
v(:,end) = xc;
fv(:,end) = fxc;
how = 'contract outside';
else
% perform a shrink
how = 'shrink';
end
else
% Perform an inside contraction
xcc = (1-psi)*xbar + psi*v(:,end);
x(:) = xcc; fxcc = feval(funfcn,x,varargin{:});
func_evals = func_evals+1;
if fxcc < fv(:,end)
v(:,end) = xcc;
fv(:,end) = fxcc;
how = 'contract inside';
else
% perform a shrink
how = 'shrink';
end
end
if strcmp(how,'shrink')
for j=two2np1
v(:,j)=v(:,1)+sigma*(v(:,j) - v(:,1));
x(:) = v(:,j); fv(:,j) = feval(funfcn,x,varargin{:});
end
func_evals = func_evals + n;
end
end
end
[fv,j] = sort(fv);
v = v(:,j);
itercount = itercount + 1;
if prnt == 3
disp([sprintf(' %5.0f %5.0f %12.6g ', itercount, func_evals, fv(1)), how])
elseif prnt == 4
disp(' ')
disp(how)
v
fv
func_evals
end
end % while
x(:) = v(:,1);
if prnt == 4,
set(0,{'format','formatspacing'},formatsave);
end
output.iterations = itercount;
output.funcCount = func_evals;
output.algorithm = 'Nelder-Mead simplex direct search';
fval = min(fv);
if func_evals >= maxfun
if prnt > 0
disp(' ')
disp('Exiting: Maximum number of function evaluations has been exceeded')
disp(' - increase MaxFunEvals option.')
msg = sprintf(' Current function value: %f \n', fval);
disp(msg)
end
exitflag = 0;
elseif itercount >= maxiter
if prnt > 0
disp(' ')
disp('Exiting: Maximum number of iterations has been exceeded')
disp(' - increase MaxIter option.')
msg = sprintf(' Current function value: %f \n', fval);
disp(msg)
end
exitflag = 0;
else
if prnt > 1
convmsg1 = sprintf([ ...
'\nOptimization terminated successfully:\n',...
' the current x satisfies the termination criteria using OPTIONS.TolX of %e \n',...
' and F(X) satisfies the convergence criteria using OPTIONS.TolFun of %e \n'
],tolx, tolf);
disp(convmsg1)
end
exitflag = 1;
end
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -