一、输入形式要求
二、例题 & 效果
三、待优化
1、虽解出最值,但给出的解的形式有待调整
2、对于无解/无穷解未做细致的处理
四、代码
1、mian
tableau=[0 0 2 3 0 0 0 0
0 8 1 2 1 0 0 0
0 16 4 0 0 1 0 0
0 12 0 4 0 0 1 0
0 0 2 3 0 0 0 0];
dim_x=5;
num_equ=3;
[Max_Z,B] = simplex_method_0(dim_x,num_equ,tableau);
disp("解为:");
disp(B);
disp("最大值为:");
disp(Max_Z);
2、simplex_method_0
function [Max_Z,B] = simplex_method_0(dim_x,num_equ,tableau)
%tableau矩阵:(num_equ+2)*(dim_x+3)
%行:z系数 基变量 检验数
%列:C(B) X(B) b x1->x(dim_x) Ct
%Z计算
Max_Z=tableau(2:num_equ+1,1)'*tableau(2:num_equ+1,2);
%最大检验数获取
[Max_c_z,Index]=max(tableau(num_equ+2,3:dim_x+2));
%Ct计算
for i=1:num_equ
if(tableau(i+1,Index+2)~=0)
tableau(i+1,dim_x+3)=tableau(i+1,2)/tableau(i+1,Index+2);
else
tableau(i+1,dim_x+3)=-1;
end
end
%最小的正Ct下标获取
Index_Ct=Find(tableau(2:num_equ+1,dim_x+3));
while(Max_c_z>0 && Index_Ct~=-1)
%交换基Index=[Index_Ct+1,Index+2];
%C(B)价值系数更新
tableau(Index_Ct+1,1)=tableau(1,Index+2);
%开始交换
tableau(Index_Ct+1,2:dim_x+2)=tableau(Index_Ct+1,2:dim_x+2)./tableau(Index_Ct+1,Index+2);
for i=2:num_equ+1
if(i~=Index_Ct+1)
tableau(i,2:dim_x+2)=tableau(i,2:dim_x+2)-tableau(i,Index+2).*tableau(Index_Ct+1,2:dim_x+2);
end
end
%检验数更新
tableau(num_equ+2,3:dim_x+2)=tableau(num_equ+2,3:dim_x+2)-tableau(num_equ+2,Index+2).*tableau(Index_Ct+1,3:dim_x+2);
%Z更新
Max_Z=tableau(2:num_equ+1,1)'*tableau(2:num_equ+1,2);
%最大检验数获取
[Max_c_z,Index]=max(tableau(num_equ+2,3:dim_x+2));
%Ct计算
for i=1:num_equ
if(tableau(i+1,Index+2)~=0)
tableau(i+1,dim_x+3)=tableau(i+1,2)/tableau(i+1,Index+2);
else
tableau(i+1,dim_x+3)=-1;
end
end
%最小的正Ct下标获取
Index_Ct=Find(tableau(2:num_equ+1,dim_x+3));
end
%解返回
B=tableau(2:num_equ+1,2);
end
3、Find
function [Index_Ct] = Find(Ct)
size_Ct=size(Ct);
Index_Ct=-1;
if(~isempty(find(Ct>0)))
Min_Ct=max(Ct);
for i=1:size_Ct(1)
if(Ct(i,1)>0 && Ct(i,1)<Min_Ct)
Min_Ct=Ct(i,1);
Index_Ct=i;
end
end
end
end