Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Codes For North West Corner Cell Method, Least Corner Cell Method and Vogel'S Approximation Method

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

CODES FOR NORTH WEST CORNER CELL

METHOD, LEAST CORNER CELL METHOD AND


VOGEL’S APPROXIMATION METHOD

NORTH WEST CORNER CELL METHOD:


CODE:
clc;
clear all;
close all;
x=input('enter the transportation matrix');
disp(x);
[m n]=size(x);
x1=zeros(m,n);
sumc=0;
sumr=0;
for i=1:m-1
sumc=sumc+x(i,n);
end
for j=1:n-1
sumr=sumr+x(m,j);
end
if(sumc == sumr)
for i=1:m
for j=1:n
x11=min(x(i,n),x(m,j));
x1(i,j)=x11;
x(i,n)=x(i,n)-x11;
x(m,j)=x(m,j)-x11;
end
end
else
disp('unbalanced transportation');
end
xre=0;
for i=1:m-1
for j=1:n-1
xre=xre+(x(i,j).*x1(i,j));
end
end
disp(['the transportation cost is ',num2str(xre)]);
OUTPUT:

LEAST COST CELL METHOD

CODE:
%% Transportation Problem using least cost cell method
close all
clear all
clc
A = input('Enter the cost matrix:')
supply = input('Enter vector of supplies')
demand = input('Enter vector of demands')
%Initialization of cost
y = 0;
while (~isempty(supply) && ~isempty(demand))
M = min(A);
N = min(M);
i = 0;
j = 0;
[i, j] =find(A==N);
X = min(supply(i(1)),demand(j(1)));
y = y+X*N;
if(X==supply(i(1)))
A(i(1),:) = [];
supply(i(1))=[];
demand(j(1))= demand(j(1))-X;
end
if(X==demand(j(1)))
A(:,j(1)) = [];
demand(j(1))=[];
supply(i(1))=supply(i(1))-X;
end
end
disp('THe least cost of given input matrix is:')
disp(y)
OUTPUT:
Enter the cost matrix:
[4 5 1;3 4 3;6 2 8]

A =

4 5 1
3 4 3
6 2 8

Enter vector of supplies


[40 60 70]

supply =

40 60 70

Enter vector of emands


[70 40 60]

demand =

70 40 60

THe least cost of given input matrix is:


520

 
 

VOGEL’S APPROXIMATION METHOD:


CODE:
function [ z,x ] = vogel(s,d,c,m,n );
s = input("Enter the supplies of column vector :");
d = input("Enter the demands of row vector");
c = input("Enter the cost matrix :");
m = input("Enter the value of m");
n = input("Enter the value of n");
x=zeros(m,n);
numbasic=n+m-1;
for j=1:n
for i=1:m
c1(i,j)=c(i,j);
end
end
for i=1:m
s1(i)=s(i);
end
for j=1:n
d1(j)=d(j);
end
iteration=0;
for k=1:n+m-1
iteration=iteration+1;
%% Row Difference
minrow1=zeros(m,1);
minrow2=zeros(m,1);
jmin=zeros(1,m);

for i=1:m
min1=inf;
for j=1:n
if c1(i,j)<min1
min1=c1(i,j);
jmin(i)=j;% position of min1 on each row
end
end
minrow1(i)=min1;
end;
for i=1:m
min2=inf;
for j=1:n
if j~=jmin(i)
if c1(i,j)<=min2
min2=c1(i,j);
end
end
end
minrow2(i)=min2;
end
%% Column Difference
mincol1=zeros(1,n);
mincol2=zeros(1,n);
imin=zeros(n,1);
for j=1:n
minR1=inf;
for i=1:m
if c1(i,j)<minR1
minR1=c1(i,j);
imin(j)=i;
end
end
mincol1(j)=minR1;
end
for j=1:n
minR2=inf;
for i=1:m
if i~=imin(j)
if c1(i,j)<=minR2
minR2=c1(i,j);
end
end
end
mincol2(j)=minR2;
end
%% Difference
diffrow=zeros(m,1);
diffcol=zeros(1,n);
for i=1:m
diffrow(i)=minrow2(i)-minrow1(i);
end
for j=1:n
diffcol(j)=mincol2(j)-mincol1(j);
end
%% The greatest difference
R=0;
Row=zeros(m,1);
for i=1:m
if diffrow(i)>=R
R=diffrow(i);
iminrow=i;
end
end
Row(iminrow)=R;
S=0;
Col=zeros(1,n);
for j=1:n
if diffcol(j)>=S
S=diffcol(j);
jmincol=j;
end
end
Col(jmincol)=S;
great=zeros(1,n);
for j=1:n
if S>=R
great(jmincol)=Col(jmincol);
Colline=1;
else
great(iminrow)=Row(iminrow);
Colline=0;
end
end
%% Search the entry cell
if Colline==1
j=jmincol;
R1=inf;
for i=1:m
if c1(i,jmincol)<=R1
R1=c1(i,jmincol);
igreat=i;
end
end
if s1(igreat)>d1(jmincol)
x(igreat,jmincol)=d1(jmincol);
s1(igreat)=s1(igreat)-d1(jmincol);
d1(jmincol)=0;
eliminaterow=0;
elseif s1(igreat)<d1(jmincol)
x(igreat,jmincol)=s1(igreat);
d1(jmincol)=d1(jmincol)-s1(igreat);
s1(igreat)=0;
eliminaterow=1;
elseif s1(igreat)==d1(jmincol)
x(igreat,jmincol)=s1(igreat);
d1(jmincol)=0;
s1(igreat)=0;
eliminaterow=2;
end
% Eliminate a column or a row
if eliminaterow==0
for i=1:m
c1(i,jmincol)=inf;
end
elseif eliminaterow==1
for j=1:n
c1(igreat,j)=inf;
end
elseif eliminaterow==2
for i=1:m
c1(i,jmincol)=inf;
end
for j=1:n
c1(igreat,j)=inf;
end
end
else % Colline=0;
i=iminrow;
R2=inf;
for j=1:n
if c1(iminrow,j)<R2
R2=c1(iminrow,j);
jgreat=j;
end
end
if s1(iminrow)>d1(jgreat)
x(iminrow,jgreat)=d1(jgreat);
s1(iminrow)=s1(iminrow)-d1(jgreat);
d1(jgreat)=0;
eliminaterow=0;
elseif s1(iminrow)<d1(jgreat)
x(iminrow,jgreat)=s1(iminrow);
d1(jgreat)=d1(jgreat)-s1(iminrow);
s1(iminrow)=0;
eliminaterow=1;
elseif s1(iminrow)==d1(jgreat)
x(iminrow,jgreat)=s1(iminrow);
d1(jgreat)=0;
s1(iminrow)=0;
eliminaterow=2;
end
% Eliminate a column or a row
if eliminaterow==0
for i=1:m
c1(i,jgreat)=inf;
end
elseif eliminaterow==1
for j=1:n
c1(iminrow,j)=inf ;
end
elseif eliminaterow==2
for i=1:m
c1(i,jgreat)=inf
end
for j=1:n
c1(iminrow,j)=inf;
end
end
end
%% Calculate the objective function
z=0;
for j=1:n
for i=1:m
if x(i,j)>0
z=z+c(i,j)*x(i,j);
end
end
end
sums=0;
for i=1:m
sums=sums+s1(i);
end
sumd=0;
for j=1:n
sumd=sumd+d1(j);
end
if (sums & sumd) ==0
return
end
end
%% The degeneracy
countx=0;
for i=1:m
for j=1:n
if x(i,j)>0
countx=countx+1;
x1(i,j)=x(i,j);
x2(i,j)=x(i,j);
end
end
end
if countx>=numbasic
disp('Total cost of non-degeneracy VAM')
disp(z)
disp('Occupied Matrix of VAM')
disp(x)
else
disp('Total cost of degeneracy VAM')
disp(z)
disp('Occupied Matrix of VAM')
disp(x)
end

end

OUTPUT:

You might also like