欢迎来到天天文库
浏览记录
ID:41688198
大小:13.12 KB
页数:3页
时间:2019-08-30
《matlab、lingo程序代码3-背包问题遗传算法资料资料》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、背包问题---遗传算法解决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;for
2、j=1:nif((test>=F(j))&&(test3、forz=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(x4、,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_Initial5、(n,P)%n为染色体长度,即所供选择的物品总数%P为初始种群大小fori=1:Pforj=1:ntemp=rands(1,1);iftemp>0Population(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;ifte6、st
3、forz=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
4、,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
5、(n,P)%n为染色体长度,即所供选择的物品总数%P为初始种群大小fori=1:Pforj=1:ntemp=rands(1,1);iftemp>0Population(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;ifte
6、st
此文档下载收益归作者所有