matlab、lingo程序代码3-背包问题(遗传算法)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

背包问题---遗传算法解决functionPopulation1=GA_copy(Population,p,w0,w)%复制算子%Population为种群n=length(Population(:,1));fvalue=zeros(1,n);fori=1:nfvalue(i)=GA_beibao_fitnessvalue(Population(i,:),p,w0,w);endfval=fvalue/sum(fvalue);F(1)=0;forj=1:nF(j+1)=0;fork=1:jF(j+1)=F(j+1)+fval(k);endendfori=1:ntest=rand;forj=1:nif((test=F(j))&&(testF(j+1)))Population1(i,:)=Population(j,:);endendendfunctionPopulation1=GA_exchange(Population,pc)%遗传算法交换算子%pc为交换概率Population1=Population;POP=[];n=length(Population(:,1));%k=floor(n*pc);%用于交换的染色体数目%采用单点交换算子j=1;l=length(Population(1,:));fori=1:ntest(i)=rand;iftest(i)pcforz=1:lPOP(j,z)=Population(i,z);endPOP(j,l+1)=i;p(j)=randint(1,1,[1l-1]);j=j+1;endendk0=j-1;k=floor(k0/2);ifk=1form=1:kfort=p(2*m-1)+1:ls=POP(2*m-1,t);POP(2*m-1,t)=POP(2*m,t);POP(2*m,t)=s;endendform=1:k0fori=1:lPopulation1(POP(m,l+1),i)=POP(m,i);endendendfunctionfitnessvalue=GA_fitnessvalue(x,p,w0,w)%使用惩罚法计算适应度值%x为染色体%p为背包问题中每个被选物体的价值%w0为背包问题中背包总容积%w为背包问题中每个被选物品的容积l=length(x);fori=1:la(i)=p(i).*x(i);endf=sum(a);b=min(w0,abs(sum(w)-w0));fori=1:lwx(i)=w(i).*x(i);endifabs(sum(wx)-w0)b*0.99p=0.99;elsep=abs(sum(wx)-w0)/b;endfitnessvalue=f*(1-p)*(1-p)*(1-p);functionPopulation=GA_Initial(n,P)%n为染色体长度,即所供选择的物品总数%P为初始种群大小fori=1:Pforj=1:ntemp=rands(1,1);iftemp0Population(i,j)=1;elsePopulation(i,j)=0;endendendfunctionPopulation1=GA_tubian(Population,pe_tubian)%遗传算法突变算子%pe为突变概率Population1=Population;n=length(Population(:,1));m=length(Population(1,:));fori=1:nforj=1:mtest=rand;iftestpe_tubianPopulation1(i,j)=1-Population1(i,j);endendend

1 / 3
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功