NWC and LCM Code Block in Matlab
    3 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
I need North-West Corner and Least Cost Method. 
My inputs are:
c=[10 2 20 11
   12 7 9 20
   4 14 16 18];
s=[15 25 10];
d=[5 15 15 15];
..........................................................................................................
I want my output like this:

I found North-West Corner and Least Cost Method: https://it.mathworks.com/matlabcentral/fileexchange/67001-solving-transportation-problem-with-north-west-corner-rule-stepping-stone-method 
coded by Warut Boonphakdee
but there are 4 methods and 4 outputs with together. I could not parse and compile the codes.
I want just North-West Corner and Least Cost Method and output like above. 
I would be glad if you help me.
I could not add from Least Cost Methodto this code block.
My tried:
tic
c=[10 2 20 11
   12 7 9 20
   4 14 16 18];
[m,n]=size(c);
s=[15 25 10];
d=[5 15 15 15];
r=0.01;% Extra value for adding element in corrective degeneracy method
nvar = m+n-1;
x=zeros(m+1,n+1);
s1=zeros(m,1); 
d1=zeros(1,n); 
% Sum of demand and supply
  sumd=0;
sumd1=0;
  for j=1:n
      sumd=sumd+d(j);
      d1(j)=d(j);
      sumd1=sumd1+d1(j);
  end
sums=0;
sums1=0;
for i=1:m
    sums=sums+s(i);
    s1(i)=s(i);
    sums1=sums1+s1(i);
end
  % Balance supply and demand
  if sums~=sumd
      disp('Review amount of supply and demand');
      return
  end
     toc
     tic
for j=1:n
  while d1(j)>0
    for i=1:m
           if s1(i)>0 &&  d1(j)>0
               iall=i;
               jall=j;
               break
           end
    end
    if d1(jall)>s1(iall)
       d1(jall)=d1(jall)-s1(iall);
       x(iall,jall)=s1(iall);
       s1(iall)=0;
    elseif d1(jall)<s1(iall)
        s1(iall)=s1(iall)-d1(jall);
        x(iall,jall)=d1(jall);
        d1(jall)=0;
    elseif d1(jall)==s1(iall) 
         x(iall,jall)=d1(jall);
        d1(jall)=0;
        s1(iall)=0;
    end
  end
end
 disp('Occupied matrix of NWC');
 disp(x);
  % Calulate the objective function of the BF solution (NWC)
     ZNWC=0;
     for i=1:m
         for j=1:n
             if x(i,j)>0
                 ZNWC=ZNWC+x(i,j)*c(i,j);
             end
         end
     end
%% number of non-basic variables in astepstone
 countnwc=0;
 for i=1:m
   for j=1:n
        if x(i,j)>0
          countnwc=countnwc+1;
        end
   end
 end
   % Check degeneracy solution
reducetant=m+n-1;
if countnwc>=reducetant
    disp('The nondegeneracy NWC');
   disp('Z=');
   disp(ZNWC);
   degen=0;
else
    disp('The degeneracy NWC');
    disp('Z=');
    disp(ZNWC);
    degen=1;
end
if degen==1
 % How to correct degeneracy problem
   numdegen=reducetant-countnwc;
     iterationDegen=0;
 for A=1:numdegen
     iterationDegen=iterationDegen+1;
    % Count the number of the basic cell on each row and column
      for j=1:n
           countcol=0;
          for i=1:m
              if x(i,j)>0
              countcol=countcol+1;
              end
          end
         x(m+1,j)=countcol;
      end
      for i=1:m
            countrow=0;
          for j=1:n
              if x(i,j)>0
              countrow=countrow+1;
              end
          end
         x(i,n+1)=countrow;
      end
    % Assign adding one on the entering cell
       for j=1:n-1
           if x(m+1,j)==1
               jenter=j;
               for i=1:m-1
                   if x(i,n+1)==1
                       ienter=i;
                       break
                   end
               end
           end
       end
       if x(ienter,jenter)== 0 
          x(ienter,jenter)=r;
          break
       end
 end
 % Add demand and supply with r
      for j=1:n
          for i=1:m
              if x(i,j)==r
                  d(j)=d(j)+r;
              end
          end
      end
      for i=1:m
          for j=1:n
              if x(i,j)==r
                  s(i)=s(i)+r;
              end
          end
      end
 end   
0 Commenti
Risposte (1)
  Madhuri Rajpoot
 il 23 Feb 2024
        Elapsed time is 0.100513 seconds.
Occupied matrix of NWC
     5    10     0     0     0
     0     5    15     5     0
     0     0     0    10     0
     0     0     0     0     0
The nondegeneracy NWC
Z=
   520
0 Commenti
Vedere anche
Categorie
				Scopri di più su Transportation Engineering in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

