运筹实验报告.docx
- 文档编号:27999188
- 上传时间:2023-07-07
- 格式:DOCX
- 页数:14
- 大小:18.57KB
运筹实验报告.docx
《运筹实验报告.docx》由会员分享,可在线阅读,更多相关《运筹实验报告.docx(14页珍藏版)》请在冰豆网上搜索。
运筹实验报告
运筹学实验报告
一、实验目的
运用MATLAB程序设计语言完成单纯形算法求解线性规划问题。
二、实验内容
编写一个MATLAB的函数文件:
linp.m,用于求解标准形的线性规划问题:
minf=c'*xsubjectto:
1.函数调用基本形式:
[x,minf,optmatrx,flag]=linp(c,A,b)
2.参数介绍:
C:
线性规划问题的目标函数f=c'*x中各变量的系数向量,是一个n维的行向量.
A:
线性规划问题的约束
中各变量的系数组成的矩阵,是一个m×n的矩阵.
b:
线性规划问题的约束
中的常数向量,是一个mn维的列向量.
X:
输出线性规划问题的最优解,当线性规划问题没有可行解或者有可行解但没有最优解时X=[].
minf:
输出线性规划问题的最优值,当线性规划问题没有可行解时minf=[],当线性规划问题有可行解但没有最优解时minf=-inf.
optmatrx:
输出最优解对应的单纯形表,当线性规划问题没有可行解或者有可行解但没有最优解时optmatrx=[].
flag:
线性规划问题的求解结果标志值,当线性规划问题有最优解时flag=1,当线性规划问题有可行解但没有最优解时flag=0,当线性规划问题没有可行解时flag=-1.
三、linp函数
%此函数是使用两阶段算法求解线性规划问题
function[x,minf,optmatrx,flag]=linp(c,A,b)
%最优解对应的x,最优解,单纯形表,标志;参数A,c,b
fprintf('\n请输入约束条件的系数矩阵');
A=input('A=');
fprintf('\n请输入最右端的列向量');
b=input('b=');
fprintf('\n请输入目标函数的系数矩阵');
c=input('c=');
1、%检验b≥0?
否则变换成b≥0.
[m,n]=size(A);
num=length(b);
%将b转为正数
fori=1:
num
if(b(i)<0)
b(i)=-b(i);
A(i,:
)=-A(i,:
);
end
end
2、%形成辅助问题的第一个单纯形表.
[m,n]=size(A);
optmatrx1=zeros(m+2,m+n+1);%初始化表
c=-c;
b=[00b];
z=zeros(1,n);%添加函数
fujia=zeros(m+2,m);%辅助
fujia(2,:
)=-1;
fori=3:
m+2
fujia(i,i-2)=-1;
end
optmatrx1=[c;z;A];
optmatrx1=[optmatrx1fujiab'];%获得第一张单纯形表
base=[n+1,m+n];%基变量为辅助变量
fori=3:
m+2
optmatrx1(2,:
)=optmatrx1(2,:
)+optmatrx1(i,:
);
end
optmatrx1
3、换基迭代
judge=find(optmatrx1(2,1:
m+n)>0);%judge向量中记录检验向量中大于零的数的位置
while~isempty(judge)
volume=judge
(1);
temp1=optmatrx1(3:
m+2,volume);
temp2=optmatrx1(3:
m+2,m+n+1);
temp=find(temp1>0);
ifisempty(temp)
optmatrx1(2,volume)=0;
flag=0;
else
min=inf;
fori=3:
m+2
iftemp1(i-2)>0&temp2(i-2)/temp1(i-2) min=temp2(i-2)/temp1(i-2); row=i; end end optmatrx1(row,: )=optmatrx1(row,: )/optmatrx1(row,volume); fori=1: m+2 ifi~=row optmatrx1(i,: )=optmatrx1(i,: )+(-1*optmatrx1(i,volume)*optmatrx1(row,: )); end end base(row-2)=volume;%出基和进基 optmatrx1 end judge=find(optmatrx1(2,[1: m+n])>0);%重新找检验向量中大于零的数的位置 end 4、如果辅助问题的最优解大于零,则原问题无可行解 ifoptmatrx1(2,m+n+1)>0 fprintf('\n此问题不存在可行解! \n'); flag=-1; return; end judge=find(base>n); 5、辅助问题的最优解等于零,且基变量中无辅助变量,删除表中第二行和辅助变量 ifoptmatrx1(2,m+n+1)==0%&isempty(judge) if~isempty(judge) while~isempty(judge) row=judge (1)+2; temp=find(optmatrx1(row,[1: n])~=0); ifisempty(temp) optmatrx1(row,: )=[];%直接删除全为零的行 judge(1,: )=[];%删除人工变量 base(row-2)=[];%记录下进基变量 else volume=temp (1);%记录下第一个不为零的列数 optmatrx1(row,: )=optmatrx1(row,: )/optmatrx1(row,volume); base(row-2)=volume;%记录下进基变量 fori=1: m+2 ifi~=row optmatrx1(i,: )=optmatrx1(i,: )+(-1*optmatrx1(i,volume)*optmatrx1(row,: )); end end judge (1)=[]; end end end end optmatrx1(2,: )=[]; optmatrx1(: [n+1: n+m])=[]; optmatrx2=optmatrx1; judge=find(optmatrx2(1,[1: n])>0);%judge数列记下判断向量中大于零的位置 [p,q]=size(optmatrx2); m=p-1; n=q-1; y=-inf; judge=find(optmatrx2(1,[1: n])>0);%judge数列记下判断向量中大于零的位置 6、若judge不为空进行迭代 while~isempty(judge) volume=judge (1);%把判断向量的第一个大于零的数的列记为volume temp1=optmatrx2([2: m+1],volume);%得到临时向量(A的第volume) temp2=optmatrx2([2: m+1],n+1);%得到临时向量(即为单纯形表的最后一列,即是b) temp=find(temp1>0);%temp向量记下temp1中大于零的数的位置 %如果temp为空,即temp1向量都是小于零的数,则不存在则原问题不存在最优解 ifisempty(temp) fprintf('此问题有可行解,但没有最优解\n'); x=zeros(n,1); [size1,size2]=size(base); fori=1: size2 x(base(i))=optmatrx2(i+1,n+1); end; flag=0; fprintf('\n可行解为: \n');x minf=-inf return; end 7、若temp不为空,即volume列存在大于零的数则找到bi/ai的最小值,记下所在的行数row min=inf; fori=1: m iftemp1(i)>0&temp2(i)/temp1(i) min=temp2(i)/temp1(i); row=i+1; end end 8、把转轴chart1(row,volume)置为1 optmatrx2(row,: )=optmatrx2(row,: )/optmatrx2(row,volume); %把chart1的volume列的其他数都置为零 fori=1: m+1 ifi~=row optmatrx2(i,: )=optmatrx2(i,: )+(-1*optmatrx2(i,volume)*optmatrx2(row,: )); end end %base中的基变量改为volume base(row-1)=volume; %继续找判断向量是否有大于零的数,如果有继续迭代 judge=find(optmatrx2(1,[1: n])>0); end 9、如果judge为空了,循环结束,走到最优解即为chart的右上角的值 y=optmatrx2(1,n+1); x=zeros(n,1); [size1,size2]=size(base); fprintf('已经找到最优解! ! ! \n\n'); fori=1: size2 x(base(i))=optmatrx2(i+1,n+1); end; flag=1; fprintf('最优可行解为: ');x fprintf('最优值为: ');y optmatrx=optmatrx2; fprintf('最优解对应的单纯形表为: ');optmatrx return; 四、例题求解 例题1(有最优解) c=[4030] A=[1/211/2-2/3;3/203/40] b=[23] 运行结果: optmatrx1= Columns1through6 -40-3000 215/4-2/3-2-2 1/211/2-2/3-10 3/203/400-1 Column7 0 5 2 3 optmatrx1= Columns1through6 00-100-8/3 011/4-2/3-2-2/3 011/4-2/3-11/3 101/200-2/3 Column7 8 1 1 2 optmatrx1= Columns1through6 00-100-8/3 0000-1-1 011/4-2/3-11/3 101/200-2/3 Column7 8 0 1 2 已经找到最优解! ! ! 最优可行解为: x= 2 1 0 0 最优值为: y= 8 最优解对应的单纯形表为: optmatrx= 00-108 011/4-2/31 101/202 例题2(有可行解但无最优解) c=[-2-5000] A=[-10100;01010;-12001] b=[438] 运行结果: optmatrx1= Columns1through5 25000 -23111 -10100 01010 -12001 Columns6through9 0000 -2-2-215 -1004 0-103 00-18 optmatrx1= Columns1through5 200-50 -201-21 -10100 01010 -100-21 Columns6through9 050-15 -21-26 -1004 0-103 02-12 optmatrx1= Columns1through5 200-50 -100-21 -10100 01010 -100-21 Columns6through9 050-15 -11-22 -1004 0-103 02-12 optmatrx1= Columns1through5 200-50 00000 -10100 01010 -100-21 Columns6through9 050-15 -1-1-10 -1004 0-103 02-12 此问题有可行解,但没有最优解 可行解为: x= 0 3 4 0 2 minf= -1/0 例题3(没有可行解) c=[2200] A=[-11-10;-1-10-1] b=[12] 运行结果: optmatrx1= Columns1through5 -2-2000 -20-1-1-2 -11-10-1 -1-10-10 Columns6through7 00 -23 01 -12 此问题不存在可行解!
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 运筹 实验 报告