function y=beibao(product,weight,value)
fprintf('请输入数据product,weight,value:\\n'); product=input('product='); weight=input('weighe='); value=input('value='); Total_value=0; Total_weight=2000;
for i=1:length(product),
Total_value=Total_value+value(i); Total_weight=Total_weight-weight(i); if(Total_weight<0)
Total_value=Total_value-value(i); Total_weight=Total_weight+weight(i); else
chanpin_N(i)=product(i); chanpin_W(i)=weight(i); chanpin_V(i)=value(i); end end
disp('输出对应装入背包的产品号') chanpin_N
disp('输出装入产品后背包总重量') sum(chanpin_W)
disp('输出装入产品后背包总价值') sum(chanpin_V) 运行:
请输入数据product,weight,value: product=[1 2 3 4 5 6 7 8 9 10 11]
weighe=[800 800 500 500 300 300 300 300 200 200 200]
value=[22*800 22*800 12*500 12*500 7*300 7*300 7*300 7*300 3*200 3*200 3*200]
输出对应装入背包的产品号
chanpin_N =
1 2 0 0 5
输出装入产品后背包总重量
ans =
1900
输出装入产品后背包总价值
ans =
37300 chanpin_N =
1 2 0 0 0 0 0 0 9
输出装入产品后背包总重量
ans =
1800
输出装入产品后背包总价值
ans =
35800
6-4
min=240*x1+220*x2+275*x3-430+0*s1+0*s2+0*s3;
s1+x1<=5; x1<=3; s1+x1>=3; s2+x2<=6; x2<=2; s2+x2>=3; s3+x3<=4; x3<=3; s3+x3>=2; s3=1;
s2<=2; s1<=3;
s2=s3+x3-2; s1=s2+x2-3; @gin(x1); @gin(x2); @gin(x3); @gin(s1); @gin(s2); @gin(s3);
实验八:网络规划
7-3
在此题编程过程中,需要把其序号对应改变,将s节点改为1号节点,1至
4节点依次加1,t节点改为6节点。
则s到各点的最短距离的编程具体程序如下: clc,clear a=zeros(6);
a(1,2)=2;a(1,3)=5; a(2,4)=-3;
a(3,2)=4;a(3,5)=3;
a(4,3)=2;a(4,5)=2; a(4,6)=8; a(5,3)=-2; a(5,4)=4; a(5,6)=5; a=a+a';
a(find(a==0))=inf;
pb(1:length(a))=0;pb(1)=1;index1=1;index2=ones(1,length(a)); d(1:length(a))=inf;d(1)=0;temp=1; while sum(pb) d(tb)=min(d(tb),d(temp)+a(temp,tb)); tmpb=find(d(tb)==min(d(tb))); temp=tb(tmpb(1)); pb(temp)=1; index1=[index1,temp]; temp2=find(d(index1)==d(temp)-a(temp,index1)); index2(temp)=index1(temp2(1)); end d, index1, index2 有下面的表达式: d = 0 2 1 -1 2 7 index1 = 1 2 4 3 5 6 index2 = 1 1 4 2 3 4 7-4 简单变化序号利于编程, 列出线性规划表达式可得: minz??x1?x2?x1?x3?x4?x5?0?x?x?x?0236??s.t.?x4?x7?x9?0??x?x?x?0?567??x1,x2,x3,x4,x5x6,x7x8,x9?0 转化为MATLAB程序为: c=[-1;-1;0;0;0;0;0;0;0]; aeq=[1 0 -1 -1 1 0 0 0 0 0 1 1 0 0 -1 0 0 0 0 0 0 1 0 0 1 -1 0 0 0 0 0 -1 1 -1 0 0]; ub=[4;5;2;4;1;3;3;5;4]; beq=zeros(4,1); x=linprog(c,[],[],aeq,beq,zeros(9,1),ub) value=c'*x 运行得出结果: Optimization terminated. x = 2.6978 2.3022 0.2443 2.8876 0.4341 2.5465 2.1124 5.0000 0 value = -5.0000 7-5 这里所介绍的求最小费用流的方法叫做迭代法,其中调用了利用Floyd 算法求最短路的函数floydpath。其主要步骤如下: function mainexample19
相关推荐: