Trust region solver: recompute Jacobian only when necessary
Previously, the solver would recompute the Jacobian at every iteration. But, if an iteration fails, the current point is not moved (only the radius of the trust region changes), hence it is not necessary to recompute the Jacobian in that case. This commit implements this optimization.time-shift
parent
49a17e75df
commit
caf0c8e1f8
|
@ -25,7 +25,7 @@ function [x,check,info] = trust_region(fcn,x0,j1,j2,jacobian_flag,gstep,tolf,tol
|
||||||
% none
|
% none
|
||||||
|
|
||||||
% Copyright (C) 2008-2012 VZLU Prague, a.s.
|
% Copyright (C) 2008-2012 VZLU Prague, a.s.
|
||||||
% Copyright (C) 2014-2017 Dynare Team
|
% Copyright (C) 2014-2019 Dynare Team
|
||||||
%
|
%
|
||||||
% This file is part of Dynare.
|
% This file is part of Dynare.
|
||||||
%
|
%
|
||||||
|
@ -65,24 +65,27 @@ info = 0;
|
||||||
fvec = fcn (x, varargin{:});
|
fvec = fcn (x, varargin{:});
|
||||||
fvec = fvec(j1);
|
fvec = fvec(j1);
|
||||||
fn = norm (fvec);
|
fn = norm (fvec);
|
||||||
|
recompute_jacobian = true;
|
||||||
|
|
||||||
% Outer loop.
|
% Outer loop.
|
||||||
while (niter < maxiter && ~info)
|
while (niter < maxiter && ~info)
|
||||||
|
|
||||||
% Calculate function value and Jacobian (possibly via FD).
|
% Calculate Jacobian (possibly via FD).
|
||||||
if jacobian_flag
|
if recompute_jacobian
|
||||||
[fvec, fjac] = fcn (x, varargin{:});
|
if jacobian_flag
|
||||||
fvec = fvec(j1);
|
[~, fjac] = fcn (x, varargin{:});
|
||||||
fjac = fjac(j1,j2);
|
fjac = fjac(j1,j2);
|
||||||
else
|
else
|
||||||
dh = max(abs(x(j2)),gstep(1)*ones(n,1))*eps^(1/3);
|
dh = max(abs(x(j2)),gstep(1)*ones(n,1))*eps^(1/3);
|
||||||
|
|
||||||
for j = 1:n
|
for j = 1:n
|
||||||
xdh = x ;
|
xdh = x ;
|
||||||
xdh(j2(j)) = xdh(j2(j))+dh(j) ;
|
xdh(j2(j)) = xdh(j2(j))+dh(j) ;
|
||||||
t = fcn(xdh,varargin{:});
|
t = fcn(xdh,varargin{:});
|
||||||
fjac(:,j) = (t(j1) - fvec)./dh(j) ;
|
fjac(:,j) = (t(j1) - fvec)./dh(j) ;
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
recompute_jacobian = false;
|
||||||
end
|
end
|
||||||
|
|
||||||
% Get column norms, use them as scaling factors.
|
% Get column norms, use them as scaling factors.
|
||||||
|
@ -164,20 +167,13 @@ while (niter < maxiter && ~info)
|
||||||
xn = norm (dg .* x(j2));
|
xn = norm (dg .* x(j2));
|
||||||
fvec = fvec1;
|
fvec = fvec1;
|
||||||
fn = fn1;
|
fn = fn1;
|
||||||
|
recompute_jacobian = true;
|
||||||
end
|
end
|
||||||
|
|
||||||
niter = niter + 1;
|
niter = niter + 1;
|
||||||
|
|
||||||
|
|
||||||
% Tests for termination conditions. A mysterious place, anything
|
% Tests for termination condition
|
||||||
% can happen if you change something here...
|
|
||||||
|
|
||||||
% The rule of thumb (which I'm not sure M*b is quite following)
|
|
||||||
% is that for a tolerance that depends on scaling, only 0 makes
|
|
||||||
% sense as a default value. But 0 usually means uselessly long
|
|
||||||
% iterations, so we need scaling-independent tolerances wherever
|
|
||||||
% possible.
|
|
||||||
|
|
||||||
if (fn <= tolf)
|
if (fn <= tolf)
|
||||||
info = 1;
|
info = 1;
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue