Hi everyone! I need to get a script for transportation problem (balanced).
1 次查看(过去 30 天)
显示 更早的评论
I need to write a program
function [x,cost]=transport(s,d,c);
% [x,cost]=transport(s,d,c)
% Input:
% s = supplies (m*1)
% d = demands (n*1)
% c = costs (m*n)
% Output
% x = optimal solution (m*n)
% cost = minimal transport cost
....
I already have this part:
function [x,b]=northwest(s,d)
% [x,b]=northwest(s,d)
% x: shipments using nw-rule (m*n)
% b: 1 for each basic variables 0 for nonbasic (m*n)
% s: supplies (m*1)
% d: demands (n*1)
if (sum(s)~=sum(d)),
disp('ERROR: The total supply is not equal to the total demand.');
return;
end
m=length(s);
n=length(d);
i=1;
j=1;
x=zeros(m,n);
b=zeros(m,n);
while ((i<=m) & (j<=n))
if s(i)<d(j)
x(i,j)=s(i);
b(i,j)=1;
d(j)=d(j)-s(i);
i=i+1;
else
x(i,j)=d(j);
b(i,j)=1;
s(i)=s(i)-d(j);
j=j+1;
end
end
function [u,v]=multipliers(x,c,b)
% [u,v]=multipliers(x,c,b)
% x: current solution (m*n)
% b: 1 for each basic variables 0 for nonbasic (m*n)
% c: costs (m*n)
% u: lagrange multipliers for rows (m*1)
% v: lagrange multipliers for columns (n*1)
[m,n]=size(x);
if sum(sum(b))< m+n-1
disp('Error in multipliers')
break
else
u=Inf*ones(m,1);
v=Inf*ones(n,1);
u(1)=0; % choose an arbitrary multiplier = 0
nr=1;
while nr<m+n % until all multipliers are assigned
for row=1:m
for col=1:n
if b(row,col)>0
if (u(row)~=Inf) & (v(col)==Inf)
v(col)=c(row,col)-u(row);
nr=nr+1;
elseif (u(row)==Inf) & (v(col)~=Inf)
u(row)=c(row,col)-v(col);
nr=nr+1;
end
end
end
end
end
end
function [y,bout]=cycle(x,row,col,b)
% [y,bout]=cycle(x,row,col)
% x: current solution (m*n)
% b: entering basic variables (m*n)
% row,col: index for element entering basis
% y: solution after cycle of change (m*n)
% bout: new basic variables after cycle of change (m*n)
bout=b;
y=x;
[m,n]=size(x);
loop=[row col]; % describes the cycle of change
x(row,col)=Inf; % do not include (row,col) in the search
b(row,col)=Inf;
rowsearch=1; % start searching in the same row
while (loop(1,1)~=row | loop(1,2)~=col | length(loop)==2),
if rowsearch, % search in row
j=1;
while rowsearch
if (b(loop(1,1),j)~=0) & (j~=loop(1,2))
loop=[loop(1,1) j ;loop]; % add indices of found element to loop
rowsearch=0; % start searching in columns
elseif j==n, % no interesting element in this row
b(loop(1,1),loop(1,2))=0;
loop=loop(2:length(loop),:); % backtrack
rowsearch=0;
else
j=j+1;
end
end
else % column search
i=1;
while ~rowsearch
if (b(i,loop(1,2))~=0) & (i~=loop(1,1))
loop=[i loop(1,2) ; loop];
rowsearch=1;
elseif i==m
b(loop(1,1),loop(1,2))=0;
loop=loop(2:length(loop),:);
rowsearch=1;
else
i=i+1;
end
end
end
end
% compute maximal loop shipment
l=length(loop);
theta=Inf;
minindex=Inf;
for i=2:2:l
if x(loop(i,1),loop(i,2))<theta,
theta=x(loop(i,1),loop(i,2));
minindex=i;
end;
end
% compute new transport matrix
y(row,col)=theta;
for i=2:l-1
y(loop(i,1),loop(i,2))=y(loop(i,1),loop(i,2))+(-1)^(i-1)*theta;
end
bout(row,col)=1;
bout(loop(minindex,1),loop(minindex,2))=0;
Thank you!
1 个评论
Walter Roberson
2016-11-6
It is not clear how your problem relates to the functions you have already created?
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!