coordinatedecent.docx
- 文档编号:11001629
- 上传时间:2023-02-24
- 格式:DOCX
- 页数:13
- 大小:18.22KB
coordinatedecent.docx
《coordinatedecent.docx》由会员分享,可在线阅读,更多相关《coordinatedecent.docx(13页珍藏版)》请在冰豆网上搜索。
coordinatedecent
ACD(strfitnessfct,N,X_a,X_b,MAX_EVAL,stopfitness,howOftenUpdateRotation)
%---------------------------------------------------------------
%AdaptiveCoordinateDescent.TobeusedunderthetermsoftheBSDlicense
%Author:
IlyaLoshchilov,MarcSchoenauer,MicheleSebag,2012.
%e-mail:
ilya.loshchilov@marc.schoenauer@inria.frmichele.sebag@lri.fr
%URL:
http:
//www.lri.fr/~ilya
%REFERENCE:
Loshchilov,I.,Schoenauer,M.,Sebag,M.(2011).AdaptiveCoordinateDescent.
%N.Krasnogoretal.(eds.)
%GeneticandEvolutionaryComputationConference(GECCO)2012,
%Proceedings,ACM.http:
//hal.inria.fr/docs/00/58/75/34/PDF/AdaptiveCoordinateDescent.pdf
%ThissourcecodeincludestheAdaptiveEncodingprocedurebyNikolausHansen,2008
%---------------------------------------------------------------
function[xmean,bestFit,neval]=ACD(strfitnessfct,N,X_a,X_b,MAX_EVAL,stopfitness,howOftenUpdateRotation)
closeall;
%%%parameters
k_succ=2.0;
k_unsucc=0.5;
c1=0.5/N;
cmu=0.5/N;
howOftenUpdateRotation=floor(howOftenUpdateRotation);%integer>=1
%%%Stoppingcriteria
TolHistFun=1e-25;
%nIterHist=10+floor(20*N^1.5);
arrFstop=1e+30;
posArrF=0;
%%%Initializemeanandsigma
fori=1:
N
XminPrime(i,1)=X_a;
XmaxPrime(i,1)=X_b;
xmean(i,1)=XminPrime(i,1)+(rand())*(XmaxPrime(i,1)-XminPrime(i,1));
sigma(i,1)=(XmaxPrime(i,1)-XminPrime(i,1))/4;
end;
bestFit=1e+30;
neval=0;
B=eye(N,N);
if1<2%%generateifdesiredanarbitraryorthogonalinitialtransformation.N.Hansen
fori=1:
N
v=randn(N,1);
forj=1:
i-1
v=v-(v'*B(:
j))*B(:
j);
end
B(:
i)=v/norm(v);%mightfailwithaverysmallprobability
end
end
iter=0;
firstAE=1;
xperm=xpermutation(N);
xperm=1:
N;
qix=0;
somebetter=0;
allx=zeros(N,2*N);
allf=zeros(1,2*N);
xmeanold=xmean;
%arrFstop=1e+10*ones(nIterHist);
firstsav=1;
%--------------------GenerationLoop--------------------------------
while((neval
iter=iter+1;
qix=qix+1;
if(qix>N)qix=1;end;
ix=xperm(qix);%willperformthesearchalongthisix'thprincipalcomponent
%%%Sampletwocandidatesolutions
dx=sigma(ix,1)*B(:
ix);%shiftalongix'thprincipalcomponent,thecomputationalcomplexityislinear
x1=xmean-dx;%firstpointtotestalongix'thprincipalcomponent
x2=xmean+dx;%secondpointtotestissymmetrictothefirstoneonthesameix'principalcomponent
%%%ComputeFitness
Fit1=feval(strfitnessfct,x1);
neval=neval+1;
Fit2=feval(strfitnessfct,x2);
neval=neval+1;
%%%Checkstoppingcriteria
if(0)
posArrF=posArrF+1;
if(posArrF==nIterHist)posArrF=1;end;
arrFstop(posArrF)=Fit1;
posArrF=posArrF+1;
if(posArrF==nIterHist)posArrF=1;end;
arrFstop(posArrF)=Fit2;
if(neval>nIterHist)
if((max(arrFstop)-min(arrFstop)) disp('restart'); return; end; end; end; %%%Whoisthenextmeanpoint? lsucc=0; if(Fit1 bestFit=Fit1; xmean=x1; lsucc=1; end; if(Fit2 bestFit=Fit2; xmean=x2; lsucc=1; end; %%%Adaptstep-sizesigmadependingonthesuccess/unsuccessoftheprevioussearch if(lsucc==1)%increasethestep-size sigma(ix,1)=sigma(ix,1)*k_succ;%defaultk_succ=2.0 somebetter=1; else%decreasethestep-size sigma(ix,1)=sigma(ix,1)*k_unsucc;%defaultk_unsucc=0.5 end; %%%Updatearchive posx1=ix*2-1; posx2=ix*2; allx(: posx1)=x1(: 1); allf(1,posx1)=Fit1; allx(: posx2)=x2(: 1); allf(1,posx2)=Fit2; %%%Updatetheencoding update=0; step=floor(N/1); if(qix==step)update=1;end;%%weupdateourrotationmatrixBeveryN=dimensioniterations if(update&&(somebetter==1))&&1&&(rand()<1.1) somebetter=0; [allfsort,arindex]=sort(allf,2,'ascend'); popsz=step; allxbest=allx(: arindex(1: popsz)); if(firstAE==1) ae=ACD_AEupdateFAST([],allxbest,c1,cmu,howOftenUpdateRotation);%initializeencoding ae.B=B; ae.Bo=ae.B;%assumingtheinitialBisorthogonal ae.invB=ae.B';%assumingtheinitialBisorthogonal firstAE=0; else ae=ACD_AEupdateFAST(ae,allxbest,c1,cmu,howOftenUpdateRotation);%adaptencoding end; B=ae.B; end; if(rem(neval,1000)==0) %disp([num2str(neval)''num2str(bestFit-fgeneric('ftarget'))]); disp([num2str(neval)''num2str(bestFit)]); end; end %outplot(out); %outplot(inc_out); %disp([num2str(neval)''num2str(bestFit)]); end %permutationof[1,...,N] functionarr=xpermutation(N) arr1=1: N; arr=arr1; sz=N; fori=1: N pos=ceil(rand()*sz); arr1(i)=arr(pos); arr(pos)=arr(sz); arr(sz)=-1; sz=sz-1; end; arr=arr1; end functionae=ACD_AEupdateFAST(ae,pop,c1,cmu,howOftenUpdateRotation) %ThiscodeisbasedontheoriginalAdaptiveEncodingprocedure(andcode) %proposedbyN.Hansen,seePPSNXpaperfordetails. %--------------------------------------------------------------- %Adaptiveencoding.TobeusedunderthetermsoftheBSDlicense %Author: NikolausHansen,2008. %e-mail: nikolaus.hansenATinria.fr %URL: http: //www.lri.fr/~hansen %REFERENCE: Hansen,N.(2008).AdaptiveEncoding: HowtoRender %SearchCoordinateSystemInvariant.InRudolphetal.(eds.) %ParallelProblemSolvingfromNature,PPSNX, %Proceedings,Springer.http: //hal.inria.fr/inria-00287351/en/ %--------------------------------------------------------------- ifnargin<2||isempty(pop) error('needtwoarguments,firstcanbeempty'); end N=size(pop,1); %initialize"object" ifisempty(ae) %parametersetting ae.N=N; ae.mu=size(pop,2); ae.weights=ones(ae.mu,1)/ae.mu; ae.mucov=ae.mu;%forcomputingc1andcmu if11<3%non-uniformweights,assumesacorrectorderingof %inputarguments ae.weights=log(ae.mu+1)-log(1: ae.mu)'; ae.weights=ae.weights/sum(ae.weights); end ae.alpha_p=1; ae.c1=c1; ae.cmu=cmu; ae.cc=1/sqrt(N); %initialization ae.pc=zeros(N,1); ae.pcmu=zeros(N,1); ae.xmean=pop*ae.weights; ae.C=eye(N); ae.Cold=ae.C; ae.diagD=ones(N,1); ae.ps=0; ae.iter=1; return end%initializeobject %beginAdaptiveEncodingprocedure ae.iter=ae.iter+1; ae.xold=ae.xmean; ae.xmean=pop*ae.weights; %ae.xmean=pop(: 1); updatePath=1; if(updatePath==1) %adapttheencoding dpath=ae.xmean-ae.xold; if(sum((ae.invB*dpath).^2)==0) z=0; else alpha0=sqrt(N)/sqrt(sum((ae.invB*dpath).^2)); z=alpha0*dpath; end; ae.pc=(1-ae.cc)*ae.pc+sqrt(ae.cc*(2-ae.cc))*z; S=ae.pc*ae.pc'; % ae.C=(1-ae.c1)*ae.C+ae.c1*S; end; if((rem(ae.iter,howOftenUpdateRotation)==0)||(ae.iter<=2))&&1 if(sum(isnan(ae.C(: ))))||sum(isinf(ae.C(: ))) ae.C=ae.Cold; end; ae.C=(triu(ae.C)+triu(ae.C,1)'); %tic [ae.Bo,EV]=eig(ae.C); EV=diag(EV); %toc/(2*N*N) if (1)%limitconditionofCto1e14+1 cond=1e14; ifmin(EV)<=0 EV(EV<0)=0; tmp=max(EV)/cond; ae.C=ae.C+tmp*eye(N,N);EV=EV+tmp*ones(N,1); end ifmax(EV)>cond*min(EV) tmp=max(EV)/cond-min(EV); ae.C=ae.C+tmp*eye(N,N);EV=EV+tmp*ones(N,1); end end; ae.diagD=sqrt(EV); if(min(EV)<=0) return end; ae.B=ae.Bo*diag(ae.diagD); ae.invB=diag(1./ae.diagD)*ae.Bo'; ae.Cold=ae.C; end end functionf=cigar(x,ar) ifnargin<2||isempty(ar) ar=1e3; end f=x(1,: ).^2+ar^2*sum(x(2: end,: ).^2,1); functionf=fcigtab(x) f=x(1,: ).^2+1e8*x(end,: ).^2+1e4*sum(x(2: (end-1),: ).^2,1); functionf=fdiffpow(x) [Npopsi]=size(x);ifN<2error('dimensionmustbegreaterone');end f=sum(abs(x).^repmat(2+10*(0: N-1)'/(N-1),1,popsi),1); f=sqrt(f); functionf=felli(x) %f=rand(); %return; N=size(x,1);ifN<2error('dimensionmustbegreaterone');end f=1e6.^((0: N-1)/(N-1))*x.^2; end functionf=ftablet(x) f=1e6*x(1,: ).^2+sum(x(2: end,: ).^2,1); functionf=frosenbrock(x) ifsize(x,1)<2error('dimensionmustbegreaterone');end f=100*sum((x(1: end-1).^2-x(2: end)).^2)+sum((x(1: end-1)-1).^2); end functiony=fsphere(X) %y=sphere(X) %Input %-X: d-by-nmatrix,disthedimensionandnisthenumberofinputs. %Output %-y: 1-by-nvector. %y=rand();return y=sum(X.^2); functiony=schwefel(X) %y=schwefel(X) %Input %-X: d-by-nmatrix,disthedimensionandnisthenumberofinputs. %Output %-y: 1-by-nvector. [d,n]=size(X); y=zeros(1,n); fori=1: n x=X(: i); forj=1: d y(i)=y(i)+sum(x(1: j))^2; end end ACD_test.m dim=2;%try2,40,100,1000 ffunc='frosenbrock'; %ffunc='fsphere'; %ffunc='felli'; %ffunc='ftablet'; %ffunc='fdiffpow'; %ffunc='fschwefel'; %ffunc='fcigar'; MAX_EVAL=1000000*dim; x_a=-5.0;x_b=5.0; ftarget=1e-10; fcurrent=1e+30; prof=0; howOftenUpdateRotation=1;%ateachiteration->quadratictimecomplexityofthealgorithm,butneedlessfunctionevaluationstoreachtheoptimum if(0)%trytouse (1),iftheproblemdimension>>100 howOftenUpdateRotation=floor(dim/10);%everyN/10iterations->lineartimecomplexityofthealgorithm,butneedmorefunctionevaluationstoreachtheoptimum %or %howOftenUpdateRotation=dim; end; nevaltotal=0; tic if(prof) profileon-history end; while(nevaltotal maxeval_available=MAX_EVAL-nevaltotal; [xmean,fcurrent,neval]=ACD(ffunc,dim,x_a,x_b,maxeval_available,ftarget,howOftenUpdateRotation); nevaltota
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- coordinatedecent
![提示](https://static.bdocx.com/images/bang_tan.gif)