matlab、lingo程序代码3-背包问题

上传人:大米 文档编号:498870959 上传时间:2023-01-24 格式:DOCX 页数:3 大小:17.51KB
返回 下载 相关 举报
matlab、lingo程序代码3-背包问题_第1页
第1页 / 共3页
matlab、lingo程序代码3-背包问题_第2页
第2页 / 共3页
matlab、lingo程序代码3-背包问题_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

《matlab、lingo程序代码3-背包问题》由会员分享,可在线阅读,更多相关《matlab、lingo程序代码3-背包问题(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;forj=1:nif(

2、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)=1form=1:kfort=p(2*m-1)+1:l

3、s=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)%使用惩罚法计算适应度值%*为染色体%p为背包问题中每个被选物体的价值%wM背包问题中背包总容积%W为背包问题中每个被选物品的容积l=length(x);fori=1:la(i)=p(i).*x(i);endf=sum(a);b=min(w0,abs(sum(w)-w0

4、);fori=1:lwx(i)=w(i).*x(i);endifabs(sum(wx)-w0)b*p=;elsep=abs(sum(wx)-w0)/b;endfitnessvalue=f*(1-p)*(1-p)*(1-p);functionPopulation=GA_Initial(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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 营销创新

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号