fixed persistent variable initialization

time-shift
Marco Ratto 2010-04-19 16:40:28 +02:00
parent 18c36c3dd5
commit 47037687d5
3 changed files with 29 additions and 23 deletions

View File

@ -1,5 +1,5 @@
function [f0, x, ig] = mr_gstep(func0,x,htol0,varargin) function [f0, x, ig] = mr_gstep(init,x,func0,htol0,varargin)
% function [f0, x] = mr_gstep(func0,x,htol0,varargin) % function [f0, x] = mr_gstep(init,x,func0,htol0,varargin)
% %
% Gibbs type step in optimisation % Gibbs type step in optimisation
@ -23,21 +23,22 @@ function [f0, x, ig] = mr_gstep(func0,x,htol0,varargin)
global bayestopt_ options_ global bayestopt_ options_
persistent h1 persistent h1
gstep_ = options_.gstep; n=size(x,1);
if nargin<3,
if init,
gstep_ = options_.gstep;
h1=max(abs(x),sqrt(gstep_)*ones(n,1))*eps^(1/4);
return
end
if nargin<4,
htol = 1.e-6; htol = 1.e-6;
else else
htol = htol0; htol = htol0;
end end
func = str2func(func0); func = str2func(func0);
f0=feval(func,x,varargin{:}); f0=feval(func,x,varargin{:});
n=size(x,1);
h2=bayestopt_.ub-bayestopt_.lb; h2=bayestopt_.ub-bayestopt_.lb;
if isempty(h1),
h1=max(abs(x),sqrt(gstep_)*ones(n,1))*eps^(1/4);
end
xh1=x; xh1=x;
f1=zeros(size(f0,1),n); f1=zeros(size(f0,1),n);
f_1=f1; f_1=f1;

View File

@ -1,5 +1,5 @@
function [hessian_mat, gg, htol1, ihh, hh_mat0] = mr_hessian(func,x,hflag,htol0,varargin) function [hessian_mat, gg, htol1, ihh, hh_mat0] = mr_hessian(init,x,func,hflag,htol0,varargin)
% [hessian_mat, gg, htol1, ihh, hh_mat0] = mr_hessian(func,x,hflag,htol0,varargin) % [hessian_mat, gg, htol1, ihh, hh_mat0] = mr_hessian(init,x,func,hflag,htol0,varargin)
% %
% numerical gradient and Hessian, with 'automatic' check of numerical % numerical gradient and Hessian, with 'automatic' check of numerical
% error % error
@ -44,19 +44,20 @@ function [hessian_mat, gg, htol1, ihh, hh_mat0] = mr_hessian(func,x,hflag,htol0,
global options_ bayestopt_ global options_ bayestopt_
persistent h1 htol persistent h1 htol
gstep_=options_.gstep; n=size(x,1);
if isempty(htol), htol = 1.e-4; end if init,
gstep_=options_.gstep;
htol = 1.e-4;
%h1=max(abs(x),gstep_*ones(n,1))*eps^(1/3);
%h1=max(abs(x),sqrt(gstep_)*ones(n,1))*eps^(1/6);
h1=max(abs(x),sqrt(gstep_)*ones(n,1))*eps^(1/4);
return,
end
func = str2func(func); func = str2func(func);
[f0, ff0]=feval(func,x,varargin{:}); [f0, ff0]=feval(func,x,varargin{:});
n=size(x,1);
h2=bayestopt_.ub-bayestopt_.lb; h2=bayestopt_.ub-bayestopt_.lb;
hmax=bayestopt_.ub-x; hmax=bayestopt_.ub-x;
hmax=min(hmax,x-bayestopt_.lb); hmax=min(hmax,x-bayestopt_.lb);
%h1=max(abs(x),gstep_*ones(n,1))*eps^(1/3);
%h1=max(abs(x),sqrt(gstep_)*ones(n,1))*eps^(1/6);
if isempty(h1),
h1=max(abs(x),sqrt(gstep_)*ones(n,1))*eps^(1/4);
end
h1 = min(h1,0.5.*hmax); h1 = min(h1,0.5.*hmax);

View File

@ -59,8 +59,12 @@ func_hh = [func0,'_hh'];
func = str2func(func0); func = str2func(func0);
fval0=feval(func,x,varargin{:}); fval0=feval(func,x,varargin{:});
fval=fval0; fval=fval0;
% initialize mr_gstep and mr_hessian
mr_gstep(1,x);
mr_hessian(1,x);
if isempty(hh) if isempty(hh)
[dum, gg, htol0, igg, hhg]=mr_hessian(func_hh,x,flagit,htol,varargin{:}); [dum, gg, htol0, igg, hhg]=mr_hessian(0,x,func_hh,flagit,htol,varargin{:});
hh0 = reshape(dum,nx,nx); hh0 = reshape(dum,nx,nx);
hh=hhg; hh=hhg;
if min(eig(hh0))<0, if min(eig(hh0))<0,
@ -121,7 +125,7 @@ while norm(gg)>gtol & check==0 & jit<nit,
iggx(find(ig),find(ig)) = inv( hhx(find(ig),find(ig)) ); iggx(find(ig),find(ig)) = inv( hhx(find(ig),find(ig)) );
[fvala x0 fc retcode] = csminit(func0,x0,fval,ggx,0,iggx,varargin{:}); [fvala x0 fc retcode] = csminit(func0,x0,fval,ggx,0,iggx,varargin{:});
end end
[fvala, x0, ig] = mr_gstep(func0,x0,htol,varargin{:}); [fvala, x0, ig] = mr_gstep(0,x0,func0,htol,varargin{:});
nig=[nig ig]; nig=[nig ig];
if (fval-fvala)<gibbstol*(fval0(icount)-fval), if (fval-fvala)<gibbstol*(fval0(icount)-fval),
igibbs=0; igibbs=0;
@ -162,7 +166,7 @@ while norm(gg)>gtol & check==0 & jit<nit,
if flagit==2, if flagit==2,
hh=hh0; hh=hh0;
elseif flagg>0, elseif flagg>0,
[dum, gg, htol0, igg, hhg]=mr_hessian(func_hh,xparam1,flagg,ftol0,varargin{:}); [dum, gg, htol0, igg, hhg]=mr_hessian(0,xparam1,func_hh,flagg,ftol0,varargin{:});
if flagg==2, if flagg==2,
hh = reshape(dum,nx,nx); hh = reshape(dum,nx,nx);
ee=eig(hh); ee=eig(hh);
@ -202,7 +206,7 @@ while norm(gg)>gtol & check==0 & jit<nit,
catch catch
save m1 x fval0 nig save m1 x fval0 nig
end end
[dum, gg, htol0, igg, hhg]=mr_hessian(func_hh,xparam1,flagit,htol,varargin{:}); [dum, gg, htol0, igg, hhg]=mr_hessian(0,xparam1,func_hh,flagit,htol,varargin{:});
if htol0>htol, %ftol, if htol0>htol, %ftol,
%ftol=htol0; %ftol=htol0;
htol=htol0; htol=htol0;