單純形法中大m法 單純形法(大M法)

2021-10-17 01:14:54 字數 3103 閱讀 6205

function [x,fval,flag,iteration]=original******m(c,a,b)

%原始單純形法(大m法,無需給出初始基變數)

%programmed by liyang(faruto's studio~!) bnu math

%last modified 2008.4.27

%求解標準型線性規劃:max c*x;s.t. a*x=b

(b>=0);x>=0;

%輸入:c是n維行向量,a是m*n的係數矩陣,b是m維列向量

%輸出:x最優解(如果有的話),fval最優值,flag解的狀態說明,interation求解時的迴圈次數

%flag 最終解的狀態說明:

%flag = 0 lp converged to a

solution x

%flag = 1 inf feasible

solutions

%flag = 2 lp is

unbounded

%flag = 3 no feasible point

was found

m = 1e5;

[m,n] = size(a);

a = [a,eye(m)];

for run = 1:1:m

c =[c,-m];

endxb = (n+1:n+m);%xb承裝初始基變數的下標

n = n+m;

iteration = 0;

while 1

iteration

= iteration +1;

flag =

0;sigma =

zeros(1,n);

for col =

1:ntemp = 0;

for row = 1:m

temp = temp + c(xb(row))*a(row,col);

endsigma(col) = c(col)-temp;

endif

sigma<=0

x = zeros(1,n);

for row = 1:m

x(xb(row)) = b(row);

endfval = c*x';

for row = 1:m

for temp = n-m+1:n

if xb(row)==temp && x(temp)~=0

flag = 3;

x = 0;

fval = 0;

break;

endend

if flag == 3

break;

endend

if flag == 3

break;

endfor col = 1:n

tflag = 0;

for row = 1:m

if col == xb(row)

tflag = 1;

break;

endend

if tflag == 0

if sigma(col) == 0

flag =

1;break;

endend

endif flag == 1

x = x(:,1:n-m);

break;

endif flag == 0;

x = x(:,1:n-m);

fval;

break;

endelse

for col = 1:n

if sigma(col)>0 &

a(:,col)<=0

flag = 2;

x = 0;

fval = 0;

break;

endend

if flag == 2

break;

endtemp = 0;

for col = 1:n

if sigma(col)>temp

temp = sigma(col);

intobase = col;%入基變數的下標

endend

theta = zeros(1,m);

for row = 1:m

if a(row,intobase)>0

theta(row) = b(row)/a(row,intobase);

endend

temp = inf;

for row = 1:m

if theta(row)>0 & theta(row)

temp = theta(row);

outbase = xb(row);%出基變數的下標

outrow

= row;%出基變數在基變數中的位置

endend

b(outrow) = b(outrow)/a(outrow,intobase);

a(outrow,:) = a(outrow,:)/a(outrow,intobase);

for row = 1:m

if row ~= outrow

b(row) = b(row) - b(outrow)*a(row,intobase);

a(row,:) = a(row,:) - a(outrow,:)*a(row,intobase);

endend

for row = 1:m

if xb(row) == outbase;

xb(row) = intobase;

endend

endend

if flag == 0

disp('lp

converged to a solution x!');

x;fval;

endif flag == 1

disp('inf

feasible solutions!');

disp('one

of the solutions is:');

endif flag == 2

disp('lp

is unbounded!');

endif flag == 3

disp('no

feasible point was found!');

end

單純形法簡介

考慮目標函式,ma xz 3 x1 4 x2增加x1 和x2 的值都將改進 z 的值,單純形法的設計要求每次都選擇使 z值有最大改善的那個變數。意味著在上述目標函式中,首先選擇增加x2 的值。通過對問題約束施加以下兩項要求來方便單純形法的計算 1.所有的約束都是等式,並且具有非負右端項 2.所有變數...

C語言實現單純形法與對偶單純形法

某次為了完成課程要求所做 單純形法 如下,使用方法修改二位陣列a ip jp include define ip 3 define jp 7 int i,j,m,n,flag 1 float max 1 min 1000 guiyi float c jp 1 float a ip jp float ...

單純形法python實現

coding utf 8 單純形法的實現,只支援最簡單的實現方法 且我們假設約束矩陣a的最後m列是可逆的 這樣就必須滿足a是行滿秩的 m n的矩陣 import numpy as np class x object def init self,c,a,b 形式 minf x c.tx s.t.ax ...