%maxpop给定群体规模%pop群体%newpop种群%t0初始温度function[codmin,finmin]=fc0(cc,v0,t0)N=length(cc(1,:));%定群体规模ifN50maxpop=2*N-20;endifN=40maxpop=2*N;end%产生初始群体pop=zeros(maxpop,N);pop(:,1)=v0;finmin=inf;codmin=0;fori=1:maxpopRa=randperm(N);Ra(find(Ra==v0))=Ra(1);Ra(1)=v0;pop(i,:)=Ra;endt=t0;whilet0%用模拟退火产生新的群体pop=fc1(maxpop,pop,N,cc,v0,t);%转轮赌选择种群f=zeros(1,maxpop);fori=1:maxpopforj=1:N-1x=pop(i,j);y=pop(i,j+1);fo1=cc(pop(i,j),pop(i,j+1));f(i)=f(i)+fo1;endf(i)=f(i)+cc(pop(i,1),pop(i,N));endfmin=min(f);fori=1:maxpopiffmin==inf&f(i)==infdd=inf;endiffmin~=inf|f(i)~=infdd=fmin-f(i);endftk(i)=exp(dd/t);end[fin1,cod]=sort(-ftk);fin=abs(fin1);%f(cod(1))iff(cod(1))finmin%记录当代最优解finmin=f(cod(1));codmin=pop(cod(1),:);endfori=1:maxpopRR=rand(1);cod2=find(fin=RR);%codnewpop(i,:)=pop(cod(cod2(end)),:);end%单亲繁殖ifN32jmax=round(N/9);endifN=32jmax=2;endifmod(jmax,2)jmax=jmax-1;endfori=1:maxpopforj=1:2:jmaxnn=randperm(N);x=nn(j);y=nn(j+1);ifnewpop(i,x)==v0|newpop(i,y)==v0continue;endbox1=newpop(i,x);newpop(i,x)=newpop(i,y);newpop(i,y)=box1;endend%变异PcPc=0.02;fori=1:maxpopR1=rand(1);ifPcR1forj=1:2:jmax+2nn=randperm(N);x=nn(j);y=nn(j+1);ifnewpop(i,x)==v0|newpop(i,y)==v0pop(i,:)=newpop(i,:);continue;endbox1=newpop(i,x);newpop(i,x)=newpop(i,y);newpop(i,y)=box1;pop(i,:)=newpop(i,:);endendend%温度下降t=t-0.1;endfunctionpop=fc1(maxpop,pop,N,cc,v0,t)ff(N-1)=0;f=0;pop1=zeros(maxpop,N);fori=1:maxpopforj=1:N-1x=pop(i,j);y=pop(i,j+1);ff(j)=cc(pop(i,j),pop(i,j+1));pop1(i,:)=pop(i,:);nn=randperm(N);x=nn(1);y=nn(2);pop1=pop;ifpop(i,x)==v0|pop(i,x)==v0continuebox1=pop(i,x);pop1(i,x)=pop1(i,y);pop1(i,y)=box1;endff1(j)=cc(pop1(i,j),pop1(i,j+1));endf=sum(ff);f1=sum(ff1);iff==inf&f1==infdd=inf;endiff~=inf|f1~=infdd=f-f1;endAij=min(1,exp(dd/t));Pacept=rand(1);ifAijPaceptpop(i,:)=pop1(i,:);endend