欢迎来到天天文库
浏览记录
ID:61484831
大小:26.50 KB
页数:5页
时间:2021-02-04
《蚁群聚类程序.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、%AntColonyOptimizationfortheClusteringclc;clear;N=44;%N=number_of_test_sample;n=3;%n=number_of_attribute_of_test_sample;%K=number_of_cluster;K=3;%R=number_of_ants;R=10;%t_max=MaxIterations;t_max=1000;%X=test_sample_matrix;xdata=xlsread('2011.xls');X=zscore(xdata);%INITIALIZATIO
2、Nc=10^-2;%pheromonetrailstau=ones(N,K)*c;%tau=[%0.0.0.;%0.0.0.;%0.0.0.;%0.0.0.;%0.0.0.;%0.0.0.;%0.0.0.;%0.0.0.;%];q=0.9;%probabilitythresholdqrho=0.1;%evaporationratebest_solution_function_value=inf;%fitnessfunctiont=1;while((t<=t_max))%SENDRANTSEACHWITHEMPTYSOLUTIONSTRING,S%==
3、===========================solution_string=zeros(R,N+1);%solutionstring:everyantconstructionsolutionandfitnessfunction%COMPUTERSOLUTIONfori=1:R%solution_string(1,1:N)=randint(1,N,[1,K]);r=rand(1,N);%constructsolutionSiusingpheromonetrailforg=1:Nifr(g)4、ster_number=find(tau(g,:)==tau_max);solution_string(i,g)=Cluster_number(1);%%%%%%%%%%%%%1--ielsesum_p=sum(tau(g,:));p=tau(g,:)/sum_p;foru=2:Kp(u)=p(u)+p(u-1);endrr=rand;%Cluster_number=K;fors=1:Kif(rr<=p(s))Cluster_number=s;solution_string(i,g)=Cluster_number;%1--ibreak;endende5、ndend%computeweights(weight)ofalltestsamplesandcluster%centers(cluster_center)weight=zeros(N,K);forh=1:NCluster_index=solution_string(i,h);%%%%%%%%%%%%%1--iweight(h,Cluster_index)=1;endcluster_center=zeros(K,n);forj=1:Kforv=1:nsum_wx=sum(weight(:,j).*X(:,v));sum_w=sum(weight(:,6、j));ifsum_w==0cluster_center(j,v)=0continue;elsecluster_center(j,v)=sum_wx/sum_wendendend%computerclusteringmetricand%assignitasobjectivefunctionvalueFiofsolution,SiF=0;forj=1:Kforii=1:NTemp=0;ifsolution_string(i,ii)==j;%%%%%%%%%%%%%1--iforv=1:nTemp=((abs(X(ii,v)-cluster_center7、(j,v))).^2)+Temp;endTemp=sqrt(Temp);endF=(Temp)+F;endendsolution_string(i,end)=F;%%%%%%%%%%%%%1--iend%fori=1:R%selectbestLsolutionsoutofRsolutionsusingobjectivefunctionvalues%LOCALSEARCHPROCEDURES====================================================%fitnesssortinascendingorder[f8、itness_ascend,solution_index]=sort(solution_string(:,e
4、ster_number=find(tau(g,:)==tau_max);solution_string(i,g)=Cluster_number(1);%%%%%%%%%%%%%1--ielsesum_p=sum(tau(g,:));p=tau(g,:)/sum_p;foru=2:Kp(u)=p(u)+p(u-1);endrr=rand;%Cluster_number=K;fors=1:Kif(rr<=p(s))Cluster_number=s;solution_string(i,g)=Cluster_number;%1--ibreak;endende
5、ndend%computeweights(weight)ofalltestsamplesandcluster%centers(cluster_center)weight=zeros(N,K);forh=1:NCluster_index=solution_string(i,h);%%%%%%%%%%%%%1--iweight(h,Cluster_index)=1;endcluster_center=zeros(K,n);forj=1:Kforv=1:nsum_wx=sum(weight(:,j).*X(:,v));sum_w=sum(weight(:,
6、j));ifsum_w==0cluster_center(j,v)=0continue;elsecluster_center(j,v)=sum_wx/sum_wendendend%computerclusteringmetricand%assignitasobjectivefunctionvalueFiofsolution,SiF=0;forj=1:Kforii=1:NTemp=0;ifsolution_string(i,ii)==j;%%%%%%%%%%%%%1--iforv=1:nTemp=((abs(X(ii,v)-cluster_center
7、(j,v))).^2)+Temp;endTemp=sqrt(Temp);endF=(Temp)+F;endendsolution_string(i,end)=F;%%%%%%%%%%%%%1--iend%fori=1:R%selectbestLsolutionsoutofRsolutionsusingobjectivefunctionvalues%LOCALSEARCHPROCEDURES====================================================%fitnesssortinascendingorder[f
8、itness_ascend,solution_index]=sort(solution_string(:,e
此文档下载收益归作者所有