资源描述:
《大连海事大学作业汇编-遗传算法源代码》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、・遗传算法源代码1.主程序:clcclearall;closeall;popsize=60;%种群规模chromlength=24;%染色体长度J=4;%备选配送屮心数P=2;%预计建设数量1=6;%顾客数量B=1000000;%投资预算a=[400300500300];cl=[1008090120];c2=[120110130140150140150160120110150130160120110170120180130130130130130130];d=[20023018015030026020023018015030026020023018015
2、0300260200230180150300260];e=[10000110001200011000];pm=0•3;%变异概率pc=0•5;%交叉概率[popO,initpop]=init(popsize,chromlength,J,I);%随机产生初始种群fori=l:300%300为遗传代数[fit,cumsump]=fitrwss(initpop,popO,J,P,I,B,a,cl,c2,d,e);%计算种群中个体适应度[bestpop,bestrwwpop,bestfit]=best(popO,initpop,fit);%记录最佳适应度[pop
3、l,newpop]=selection(popO,initpop,cumsump);%选择newpopl=crossover(popl,newpop,pc);%交叉[pop2,newpop2]=mutation(popl,newpopl,pm);%变异pop0=pop2;initpop=newpop2;enddisp(*最小成本为:,)bestfitdispC配送屮心选择方案:,)bestpopdisp(*顾客配送方案:Jbestnewpop1.生成初代种群子函数function[popO,initpop]=init(popsize,chromlengt
4、h,J,I);initpop=zeros(popsize,chromlength);pop=zeros(popsize,chromlength);%%%%%%%%%%生成配送中心选择方案的初代解%%%%%%%%popO=round(rand(popsize,J));fori=l:popsizeifpopO(iz:)==[0000]pop0(iz:)=[1111];endend%%%%%%%%%%生成顾客配送方案的初代解%%%%%%%%fori=l:popsizeforj=l:Jifpop0(izj)==l;pop(iz((j-1)*1+1):((j-1)
5、*1+6))=round(rand(lzI));endendendinitpop=pop;2.种群中个体适应度子函数function[fit,cumsump]=fitrwss(initpop,popO,J,P,I,B,a,cl,c2,d,e);[px,py]=size(initpop);fit=zeros(px,1);dudu=zeros(px,I);fori=l:pxp=sum(popO(iz:));b=sum(popO(iz:).*e);a=0;%%%%%%%%%适应度函数%%%%%%%%%forj=l:Idudu(i,j)=dudu(i,j)+in
6、itpop(i,j)+initpop(i,6+j)+initpop(i,12+j)+initpop(i,18+j);ifdudu(i,j)==0a=l;endendif(b>B
7、p8、p>P
9、a==l)f让(i)=500000;elsefit(i)=sum(a.*cl.*popO(i,:)+e.*popO(i,:))+sum(initpop(i,:).*d.*c2);endend%%%%%%%%%种群选择准备工作%%%%%%fitl=600000-fit;fsum=sum(fitl);pp=fitl/fsum;cumsump=cumsum(pp);1.
10、记录最优解子函数function[bestpop,bestnewpop,bestfit]=best(popO,initpop,fit);[px,py]=size(popO);[bestfit,num]=min(fit);bestpop=popO(num,:);bestnewpop=initpop(num,:);1.染色体选择子函数function[popl,newpop]=selection(popO,initpop,cumsump);[px,py]=size(popO);ms=sort(rand(px,1));fitin=l;newin=l;while
11、newin<=pxif(ms(newin)