Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Matlab by Somesh ( 6 years ago )
function tab = SSSSimplex(A,b,c)
validate_input(A,b,c);
tab = Simplex(A,b,c);
end
%By Somesh
function tab = Simplex(A,b,c)
[m,n] = size(A);
tab = zeros(m+1,n+m+1);
tab(1:m,1:n) = A;
tab(1:m,end) = b;
tab(end,1:n) = c;
keep_running = true;
while keep_running
if any(tab(end,1:n)>0)%check if there is negative cost coeff.
[~,J] = max(tab(end,1:n)); %yes, find the most +tive
% now check if corresponding column is unbounded
if all(tab(1:m,J)<=0)
error('problem unbounded. All entries <= 0 in column %d',J);
%do row operations to make all entries in the column 0
%except pivot
else
pivot_row = 0;
min_found = inf;
for i = 1:m
if tab(i,J)>0
tmp = tab(i,end)/tab(i,J);
if tmp < min_found
min_found = tmp;
pivot_row = i;
end
end
end
%normalize
tab(pivot_row,:) = tab(pivot_row,:)/tab(pivot_row,J);
%now make all entries in J column zero.
for i=1:m+1
if i ~= pivot_row
tab(i,:)=tab(i,:)-sign(tab(i,J))*...
abs(tab(i,J))*tab(pivot_row,:);
end
end
end
else
keep_running=false;
end
end
end
function validate_input(A,b,c)
if ~ismatrix(A)
error('A must be matrix');
end
if ~isvector(b)
error('b must be vector');
end
if ~isvector(c)
error('c must be vector');
end
[m,n]=size(A);
if rank(A) <m
error('Rank A must be equal to number of rows in A');
end
if length(b) ~= m
error('b must have same size as number of rows of A');
end
if length(c) ~= n
error('c must have same size as number of columns of A');
end
end
Revise this Paste
Children: 102932