您好,欢迎访问三七文档
当前位置:首页 > 高等教育 > 理学 > matlab常用算法大全(数学建模)
本文总结了matlab常用的几个算法,希望对数学建模有帮助。利用matlab编程FFD算法完成装箱问题:设有6种物品,它们的体积分别为:60、45、35、20、20和20单位体积,箱子的容积为100个单位体积。建立box_main.mfunction[box_count,b]=box_main(v)vmax=100;sort(v,'descend');n=length(v);b=zeros(1,n);fori=1:nb(i)=vmax;endbox_count=1;fori=1:nforj=1:box_countifv(i)=b(j)%可以放入b(j)=b(j)-v(i);break;else%不可放入时continue;endendifj==box_countbox_count=box_count+1;endendbox_count=box_count-1;end主程序为:v=[604535202020];[box_count,b]=box_main(v)结果:box_count=3b=51580100100100所以,使用的箱子数为3,使用的箱子的剩余空间为5,15,80。“超市大赢家”提供了50种商品作为奖品供中奖顾客选择,车的容量为1000dm3,奖品i占用的空间为widm3,价值为vi元,具体的数据如下:vi={220,208,198,192,180,180,165,162,160,158,155,130,125,122,120,118,115,110,105,101,100,100,98,96,95,90,88,82,80,77,75,73,72,70,69,66,65,63,60,58,56,50,30,20,15,10,8,5,3,1}wi={80,82,85,70,72,70,66,50,55,25,50,55,40,48,50,32,22,60,30,32,40,38,35,32,25,28,30,22,50,30,45,30,60,50,20,65,20,25,30,10,20,25,15,10,10,10,4,4,2,1}。解:模型建立:用价值密度贪婪准则的方法设x=v/w,对x做正向排序,依次选取商品。建立chaoshi.mfunction[item_count,y]=chaoshi(v,w,car)n=length(v);x=zeros(n,3);x(:,1)=v';x(:,2)=w';x(:,3)=v'./v';x=sortrows(x,-3);item_count=0;fori=1:nifcar=x(i,2)car=car-x(i,2);item_count=item_count+1;elsebreak;endendy=zeros(item_count,2);fori=1:item_county(i,1)=x(i,1);y(i,2)=x(i,2);endend主程序为:v=[220,208,198,192,180,180,165,162,160,158,155,130,125,122,120,118,115,110,105,101,100,100,98,96,95,90,88,82,80,77,75,73,72,70,69,66,65,63,60,58,56,50,30,20,15,10,8,5,3,1];w=[80,82,85,70,72,70,66,50,55,25,50,55,40,48,50,32,22,60,30,32,40,38,35,32,25,28,30,22,50,30,45,30,60,50,20,65,20,25,30,10,20,25,15,10,10,10,4,4,2,1];car=1000;[item_count,y]=chaoshi(v,w,car);y’;结果为:ans=Columns1through111585811595821181056965162902510222522323020205028Columns12through22101125155968816098562201921003240503230553520807038Columns23through261807712220870304882最大总价值为3095元,可装入体积为996贪婪算法练习练习题1:考虑1、8、9、11这四种面值的硬币,要找出币值24的零钱,怎么找能使硬币数最少?利用matlab编程求解。解:设xj为二进制变量,如果硬币j被选中,则,xj=1,否则xj=0,则找硬币问题的数学模型如下:minnjjx1;mnjjjxv1;用贪婪算法求解,其MATLAB程序如下:function[n,x]=payback(v,y,m)[m,n]=size(y);fori=1:nforj=1:n练习题2:利用matlab编程FFD算法完成下题:设有6种物品,它们的体积分别为:60、45、35、20、20和20单位体积,箱子的容积为100个单位体积。function[nbox,p]=sjy(n,v,limitv)[m,n]=size(v);w=limitv*ones(m,n);p=zeros(n);nbox=0;fori=1:nforj=1:iifv(i)w(j)w(j)=w(j)-v(i);p(i,j)=1;break;elsecontinue;endw(j+1)=w(j+1)-v(i);p(i,j+1)=1;nbox=nbox+1;endend运行结果:p=100000010000100000010000010000001000练习题3:如果把选择策略从“选出一个下标最小的箱子并把物品ai放入该箱子中”(FF算法)改为选择最佳的箱子(已装载物品大小和最大的-这个称为bestfit-BF最佳适应算法),再计算一次上题。比较两次求解的结果。练习题4:背包问题:c=[10,5,15,7,6,18,3];w=[2,3,5,7,1,4,1];limitw=15;n=7;求最优解。练习题5:“超市大赢家”提供了50种商品作为奖品供中奖顾客选择,车的容量为1000dm3,奖品i占用的空间为widm3,价值为vi元,具体的数据如下:vi={220,208,198,192,180,180,165,162,160,158,155,130,125,122,120,118,115,110,105,101,100,100,98,96,95,90,88,82,80,77,75,73,72,70,69,66,65,63,60,58,56,50,30,20,15,10,8,5,3,1}wi={80,82,85,70,72,70,66,50,55,25,50,55,40,48,50,32,22,60,30,32,40,38,35,32,25,28,30,22,50,30,45,30,60,50,20,65,20,25,30,10,20,25,15,10,10,10,4,4,2,1}。模型的建立:设xj为二进制变量,如果物品j被选中,则xj=1,否则,xj=0,如此可将本题转化为如下优化模型:maxnjjjxv1;s.t.njWxxwjnjjj,,2,1},1,0{;1模型的解决:对此优化问题,我们可以选用价值密度贪婪准则,从剩下的物品中选择可装入购物车的单位价值wvjj,最大的物品,即按wvjj非递增的次序装入物品,只要正被考虑的物品装的进就装入小车。其MATLAB编程代码如下:function[a1,b1]=sort1(n,a,b)%按单位价值排序[m,n]=size(a);d=zeros(m,n);fork=1:nd(k)=a(k)/b(k);end%单位价值forh=1:n-1forj=1:n-h%向后排序ifd(j)d(j+1)t1=a(j);a(j)=a(j+1);a(j+1)=t1;t2=b(j);b(j)=b(j+1);b(j+1)=t2;t3=d(j);d(j)=d(j+1);d(j+1)=t3;%endendenda1=a;b1=b;function[p,c,w]=goodsinknapsack(n,limitw,v,w,x)%计算背包中物品数cl=limitw;%cl为背包剩余可装载重量p=0;[m,z]=size(c);x=zeros(m,z);[v,t]=sort1(n,c,w);%物品按单位价值排序c=v;w=t;fori=1:nifw(i)clbreak%待放入包的物品重量大于包的重量,跳出循环elsex(i)=1;%x(i)为1时,物品i在包中cl=cl-w(i);p=p+1;%p记录放入背包物品的个数endendfunctionknapsack(n,limitw,w,v)totalc=0;totalw=0;[m,n]=size(w);%m是w的行数n是w的列数x=zeros(m,n);t=w;%记录原数组k=c;y=x;[p,c,w]=goodsinknapsack(n,limitw,v,w,x);%排序及计算装箱物品数forj=1:p%装包的p件物品fori=1:n%原n件物品if(w(j)==t(i))&&(c(j)==k(i))%被选择的物品装箱y(i)=1;endendendyfori=1:ntotalc=totalc+k(i)*y(i);%背包的总价值ify(i)==1totalw=totalw+t(i);%背包所装载总体积endendtotalwtotalcv=[220,208,198,192,180,180,165,162,160,158,155,130,125,122,120,118,115,110,105,101,100,100,98,96,95,90,88,82,80,77,75,73,72,70,69,66,65,63,60,58,56,50,30,20,15,10,8,5,3,1];w=[80,82,85,70,72,70,66,50,55,25,50,55,40,48,50,32,22,60,30,32,40,38,35,32,25,28,30,22,50,30,45,30,60,50,20,65,20,25,30,10,20,25,15,10,10,10,4,4,2,1];limitw=1000;n=50;knapsack(n,limitw,w,v);运行结果为:y=Columns1through161101010111101101Columns17through321011011111110100Columns33through480010100110000000Columns49through5000层次分析法matlab源程序disp('请输入判断矩阵A(n阶)');A=input('A=');[n,n]=size(A);x=ones(n,100);y=ones(n,100);m=zeros(1,100);m(1)=max(x(:,1));y(:,1)=x(:,1);x(:,2)=A*y(:,1);m(2)=max(x(:,2));y(:,2)=x(:,2)/m(2);p=0.0001;i=2;k=abs(m(2)-m(1));whilekpi=i+1;x(:,i)=A*y(:,i-1);m(i)=max(x(:,i));y(:,i)=x(:,i)/m(i);k=abs(m(i)-m(i-1));enda=sum(y(:,i));w=y(:,i)/a;t=m(i);disp(w);disp(t);%以下是一致性检验CI=(t-n)/(n-1);RI=[000.520.891.121.261.361.411.461.491.521.541.561.581.59];CR=CI/RI(n);ifCR0.10disp('此矩阵的一致性可以接受!');disp('CI=');disp(CI);disp('CR=');disp(CR);endfunctionAHPInit1(x,y)%层次分析的初始化%默认只有两层x为准则数,y为方案数%CTo
本文标题:matlab常用算法大全(数学建模)
链接地址:https://www.777doc.com/doc-1338852 .html