用MATLAB软件解线性规划问题!??

2024-11-29 13:37:23
推荐回答(2个)
回答(1):

%整数规划分支定界纯凯算法matlab通用源程序
%各参数的意义同matlab优化工具顷裤滚箱的线性规划雀余函数linprog
%调用前,输入参数要化成matlab的标准形式
[x,val]=kfz-f-3(n,f,a,b,aeq,beq,lb,ub)
x=zeros(n,1);
x1=zeros(n,1);
m1=2;
m2=1;
[x1,val1]=linprog(f,a,b,aeq,beq,lb,ub);
if (x1==0)
x=x1;
val=val1;
elseif (round(x1)==x1)
x=x1;
val=val1;
else
e1={0,a,b,aeq,beq,lb,ub,x1,val1};
e(1,1)={e1};
zl=0;
zu=-val1;
while (zu~=zl)
for c=1:1:m2
if (m1~=2)
if (cell2mat(e{m1-1,c}(1))==1)
e1={1,[],[],[],[],[],[],[],0};
e(m1,c*2-1)={e1};
e(m1,c*2)={e1};
continue;
end;
end;
x1=cell2mat(e{m1-1,c}(8));
x2=zeros(n,1);
s=0;
s1=1;
s2=1;
lb1=cell2mat(e{m1-1,c}(6));
ub1=cell2mat(e{m1-1,c}(7));
lb2=cell2mat(e{m1-1,c}(6));
ub2=cell2mat(e{m1-1,c}(7));
for d=1:1:n
if (abs((round(x1(d))-x1(d)))>0.0001)&(s==0)
s=1;
lb1(d)=fix(x1(d))+1;
if (a*lb1<=b)
s1=0;
end;
ub2(d)=fix(x1(d));
if (a*lb2<=b)
s2=0;
end;
end;
end;
e1={s1,a,b,aeq,beq,lb1,ub1,[],0};
e2={s2,a,b,aeq,beq,lb2,ub2,[],0};
e(m1,c*2-1)={e1};
e(m1,c*2)={e2};
end;
m1=m1+1;
m2=m2*2;
for c=1:1:m2
if (cell2mat(e{m1-1,c}(1))==0)
[x1,val1]=linprog(f,cell2mat(e{m1-1,c}( 2)),cell2mat(e{m1-1,c}(3)),cell2mat(e{m1-1,c}(4)),cell2mat(e{m1-1,c}(5)),cell2mat(e{m1-1,c}(6)),cell2mat(e{m1-1,c}(7)));
e1={cell2mat(e{m1-1,c}(1)),cell2mat(e{m1-1,c}(2)),cell2mat(e{m1-1,c}(3)),cell2mat(e{m1-1,c}(4)),cell2mat(e{m1-1,c}(5)),cell2mat(e{m1-1,c}(6)),cell2mat(e{m1-1,c}(7)),x1,val1};
e(m1-1,c)={e1};
end;
z=val1;
if ((-z)<(-zl))
e1={1,[],[],[],[],[],[],[],0};
e(m1-1,c)={e1};
elseif (abs(round(x1)-x1)<=0.0001)
zl=z;
end;
end;
for c=1:1:m2
if (cell2mat(e{m1-1,c}(1))==0)
zu=cell2mat(e{m1-1,c}(9));
end;
end;
for c=1:1:m2
if (-cell2mat(e{m1-1,c}(9))>(-zu))
zu=cell2mat(e{m1-1,c}(9));
end;
end;
end;
for c=1:1:m2
if (cell2mat(e{m1-1,c}(1))==0)&(cell2mat(e{m1-1,c}(9))==zu)
x=cell2mat(e{m1-1,c}(8));
end;
end;
val=zu;
end;

回答(2):

MATLAB求解线性的整数规划可以用分支定型轿界法,但实现起来还是比较困难。可以去下载一个叫YALMIP的工具箱,用他可以解决线性规划,非线性规划,整数规划,混合规划,强烈推荐把这个工具整合到matlab中去,这个工具是私人的,不过可以免费下载使用。不过卜腔肆最好的方法是用LINGO求解。
有了YALMIP工具箱,输入也变的相对简单,代码如下:
x=intvar(2,7);
f=[0.487,0.520,0.613,0.720,0.487,0.520,0.640;
0.487,0.520,0.613,0.720,0.487,0.520,0.640]*x';
F=set(x>=0);
F=F+set(x(1,1)+x(2,1)<=8)+set(x(1,2)+x(2,2)<=7)...
+set(x(1,3)+x(2,3)<=9)+set(x(1,4)+x(2,4)]<=6)...
+set(x(1,5)+x(2,5)<=6)+set(x(1,6)+x(2,6)<=4);
F=F+set([2,3,1,0.5,4,2,7;2,3,1,0.5,4,2,7]*.x'<=[40;40])...
+set([0.487,0.52,0.613,0.72,0.487,0.52,0.64;
0.487,0.52,0.613,0.72,0.487,0.52,0.64].*x'<=[10.2;10.2])...
+ set([0,0,0,0,0.487,0.52,0.64;
0,0,0,0,0.487,0.52,0.64].*x'<=[3.027;3.027]);
solvesdp(F,-f)

下面用lingo求解:
model:
max=0.487*x11+0.52*x12+0.613*x13+0.72*x14+0.487*x15+0.52*x16+0.64*x17+0.487*x21+0.52*x22+0.613*x23+0.72*x24+0.487*x25+0.52*x26+0.64*x27;
x11+x21<=8;
x12+x22<=7;
x13+x23<=9;
x14+x24<=6;
x15+x25<圆山=6;
x16+x26<=4;
2*x11+3*x12+x13+0.5*x14+4*x15+2*x16+x17<=40;
2*x21+3*x22+x23+0.5*x24+4*x25+2*x26+x27<=40;
0.487*x11+0.52*x12+0.613*x13+0.72*x14+0.487*x15+0.52*x16+0.64*x27<=10.2;
0.487*x21+0.52*x22+0.613*x23+0.72*x24+0.487*x25+0.52*x26+0.64*x17<=10.2;
0.487*x15+0.52*x16+0.64*x17<=3.027;
0.487*x25+0.52*x26+0.64*x27<=3.027;

@gin(x11);@gin(x12);@gin(x13);@gin(x14);@gin(x15);@gin(x16);@gin(x17);
@gin(x21);@gin(x22);@gin(x23);@gin(x24);@gin(x25);@gin(x26);@gin(x27);
end

运行结果:
(由于字数超限,运行结果已删除)