Code matlab giải bài toán vận tải (transportation)

16 560 15
Code matlab giải bài toán vận tải (transportation)

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Soure code matlab giải bài toán vận tải. Báo cáo Thí Nghiệm Kỹ Thuật Ra Quyết Định: Giải Bài Toán Vận Tải (Transpotation)Trên Excel Và Matlab.Trình bày đầy đủ các bước giải bài toán vận tải bằng thuật toán thế vị, các bước thực hiện trên Excel và Matlab.Giải các ví dụ thực tế trên công cụ Excel và Matlab, sau đó kiểm chứng và nhận xét kết quả của 2 cách giải này Link down file báo cáo: https://123doc.org/document/5058814-giai-bai-toan-van-tai-transportation-tren-excel-va-matlab.htm

SOURCE CODE GIẢI BÀI TOÁN VẬN TẢI BẰNG MATLAB CostMin.m clc; c = input('Nhap ma tran chi phi:\n'); s = input('Nhap ma tran cung cap (dang cot):\n'); d = input('Nhap ma tran nhu cau (dang dong):\n'); [m,n] = size(c); r = 0.01; x = zeros(m+1,n+1); s1 = zeros(m,1); d1 = zeros(1,n); sumd = 0; sumd1 = 0; for j = : n sumd = sumd + d(j); d1(j) = d(j); sumd1 = sumd1 + d1(j); end sums = 0; sums1 = 0; for i = : m sums = sums + s(i); s1(i) = s(i); sums1 = sums1 + s1(i); end if sums ~= sumd disp('Bai toan cung khac cau!'); return end for j = : n while d1(j) > for i = : m if s1(i) > && d1(j) > t = i; k = j; break end end if d1(k) > s1(t) d1(k) = d1(k) - s1(t); x(t,k) = s1(t); s1(t) = 0; elseif d1(k) < s1(t) s1(t) = s1(t) - d1(k); x(t,k) = d1(k); d1(k) = 0; end elseif d1(k) == s1(t) x(t,k) = d1(k); d1(k) = 0; s1(t) = 0; end end disp('Loi giai ban dau:'); disp(x); cost = 0; for i = : m for j = : n if x(i,j) > cost = cost + x(i,j)*c(i,j); end end end NonBasic = 0; for i = : m for j = : n if x(i,j) > NonBasic = NonBasic + 1; end end end Check = m + n - 1; if NonBasic >= Check disp('Tong Chi Phi:'); disp(cost); degen = 0; else disp('Tong Chi Phi:'); disp(cost); degen = 1; end if degen == NumDegen = Check - NonBasic; CountDegen = 0; for A = : NumDegen CountDegen = CountDegen + 1; for j = : n CountCol = 0; for i = : m if x(i,j) > CountCol = CountCol + 1; end end x(m+1,j) = CountCol; end for i = : m CountRow = 0; for j = : n if x(i,j) > CountRow = CountRow + 1; end end x(i,n+1) = CountRow; end for j = : n - if x(m+1,j) == jEnter = j; for i = : m - if x(i,n+1) == iEnter = i; break end end end end if x(iEnter,jEnter) == x(iEnter,jEnter) = r; break end end end for j = : n for i = : m if x(i,j) == r d(j) = d(j) + r; end end end for i = : m for j = : n if x(i,j) == r s(i) = s(i) + r; end end end countxdegen=0; for i = : m for j = : n if x(i,j) > countxdegen = countxdegen + 1; end end end if countxdegen >= Check; else disp('Do not correct Degeneracy VAM'); end CountStep = 0; for A = : m*n CountStep = CountStep + 1; nonx = zeros(m,n); for j = : n for i = : m if x(i,j) == nonx(i,j) = 1; end end end CostLoop = zeros(m,n); for j = : n for i =1 : m if x(i,j) > CostLoop(i,j) = inf; end end end for k = : (m*n) countnon=0; for j=1:n for i=1:m if nonx(i,j)==1 ibas=i; jbas=j; countnon=1; end if countnon==1 nonx(ibas,jbas)=inf; break end end if countnon==1 end break end % Construct the equivalent basic cell matrix x11=zeros(m+1,n+1); x22=zeros(m+1,n+1); for j=1:n for i=1:m if x(i,j)>0 x11(i,j)=x(i,j); x22(i,j)=x(i,j); end end end %% Construct stepping stone path for searching the improvement index for j=1:n for i=1:m x11(ibas,jbas)=inf; x22(ibas,jbas)=inf; end end % Count the number of the basic cell on each row and column for j=1:n countcol=0; for i=1:m if x11(i,j)>0 countcol=countcol+1; end end; x11(m+1,j)=countcol; x22(m+1,j)=countcol; end for i=1:m countrow=0; for j=1:n if x11(i,j)>0 countrow=countrow+1; end end x11(i,n+1)=countrow; x22(i,n+1)=countrow; end %% Eliminate the basic variables that has only one on each row iterationloop=0; for i=1:m iterationloop=iterationloop+1; for i=1:m if x22(i,n+1)==1 ieliminate=i; for j=1:n if x22(ieliminate,j)0 jeliminate=j; x22(ieliminate,jeliminate)=0;% Eliminate the basic variable on row x22(ieliminate,n+1)=x22(ieliminate,n+1)-1; % decrease the number of the basic variable on row one unit x22(m+1,jeliminate)=x22(m+1,jeliminate)-1; % decrease the number of the basic variable on column one unit end end end end %% Eliminate the basic variables that has only one on each column for j=1:n if x22(m+1,j)==1 jeliminate1=j; for i=1:m if x22(i,jeliminate1)0 ieliminate1=i; x22(ieliminate1,jeliminate1)=0;% Eliminate the basic variable on row x22(m+1,jeliminate1)=x22(m+1,jeliminate1)-1; % decrease the number of the basic variable on column one unit x22(ieliminate1,n+1)=x22(ieliminate1,n+1)-1;% decrease the number of the basic variable on row one unit end end end end %% Control the constructing loop path for j=1:n for i=1:m if (x22(i,n+1)==0 || x22(i,n+1)==2) && (x22(m+1,j)==0 || x22(m+1,j)==2) break else end end end end %% Make +/-sign on basic variables in the loop path (x2) %1 Add - sign on basic variable on row(imax) and on basic variable on %column (jmax) for j=1:n if (x22(ibas,j)~=0 && x22(ibas,j)0 && x22(m+1,jneg)==1) ineg=i; end end end end for p=1:n for j=1:n if (j~=jneg && x22(ineg,j)>0 && x22(ineg,n+1)==2) jneg1=j; x22(ineg,jneg1)=(-1)*x22(ineg,jneg1); x22(ineg,n+1)=1; x22(m+1,jneg1)=1; for i=1:m if (x22(i,jneg1)>0 && x22(m+1,jneg1)==1) ineg1=i; ineg=ineg1; jneg=jneg1; end end end end % Control loop if jneg1==jbas %break end end %% Compute the improvement index (based on the unit cost of each basic cell) sumloop=0; for i=1:m for j=1:n if x22(i,j)~=0 icost=i; jcost=j; if x22(icost,jcost)>0 sumloop=sumloop+c(icost,jcost); elseif x22(icost,jcost)=0 countcontrol=countcontrol+1; end end end if countcontrol==m*n return end %% Searching the absolute smallest improvement index minindex=0; for j=1:n for i=1:m if CostLoop(i,j)0 countcol1=countcol1+1; end end x33(m+1,j)=countcol1; x44(m+1,j)=countcol1; end for i=1:m countrow1=0; for j=1:n if x33(i,j)>0 countrow1=countrow1+1; end end x33(i,n+1)=countrow1; x44(i,n+1)=countrow1; end % Eliminate the basic variables that has only one on each row iterationloop1=0; for i=1:m iterationloop1=iterationloop1+1; for i=1:m if x44(i,n+1)==1 ieliminate3=i; for j=1:n if x44(ieliminate3,j)0 jeliminate3=j; x44(ieliminate3,jeliminate3)=0;% Eliminate the basic variable on row x44(ieliminate3,n+1)=x44(ieliminate3,n+1)-1; % decrease the number of the basic variable on row one unit x44(m+1,jeliminate3)=x44(m+1,jeliminate3)-1; % decrease the number of the basic variable on column one unit end end end end %% Eliminate the basic variables that has only one on each column for j=1:n if x44(m+1,j)==1 jeliminate4=j; for i=1:m if x44(i,jeliminate4)0 ieliminate4=i; x44(ieliminate4,jeliminate4)=0;% Eliminate the basic variable on row x44(m+1,jeliminate4)=x44(m+1,jeliminate4)-1; % decrease the number of the basic variable on column one unit x44(ieliminate4,n+1)=x44(ieliminate4,n+1)-1;% decrease the number of the basic variable on row one unit end end end end %% Control the constructing loop path for j=1:n for i=1:m if (x44(i,n+1)==0 || x44(i,n+1)==2) && (x44(m+1,j)==0 || x44(m+1,j)==2) break end end end end %% Make +/-sign on basic variables in the loop path (x2) %1 Add - sign on basic variable on row(imax) and on basic variable on %column (jmax) for j=1:n if (x44(ismall,j)~=0 && x44(ismall,j)0 && x44(m+1,jneg)==1) ineg=i; end end end end for p=1:n for j=1:n if (j~=jneg && x44(ineg,j)>0 && x44(ineg,n+1)==2) jneg1=j; x44(ineg,jneg1)=(-1)*x44(ineg,jneg1); x44(ineg,n+1)=1; x44(m+1,jneg1)=1; for i=1:m if (x44(i,jneg1)>0 && x44(m+1,jneg1)==1) ineg1=i; ineg=ineg1; jneg=jneg1; end end end end % Control loop if jneg1==jsmall % return end end % Eliminate column j that has the number of basic variables =2 for j=1:n if x44(m+1,j)==2 for i=1:m if x44(i,j)>0 x44(i,j)=0; end end x44(m+1,j)=0; end end %Eliminate row i that has the number of basic variables =2 for i=1:m if x44(i,n+1)>=2 for j=1:n if x44(i,j)>0 x44(i,j)=0; end end x44(i,n+1)=0; end end %% Searching the absolute smallest path and add this path to (ismall,jsmall) cell minpath=inf; for j=1:n for i=1:m if x44(i,j)0 && x(i,j)~=inf countstepdegen=countstepdegen+1; end end end sumdemand=zeros(1,n); for j=1:n demandsum=0; for i=1:m if round(x(i,j))>0 demandsum=demandsum+round(x(i,j)); end end sumdemand(j)=demandsum; end for j=1:n if sumdemand(j)~=round(d(j)) disp('Unbalanced demand'); break end end % Check supply sumsupply=zeros(m,1); for i=1:m supplysum=0; for j=1:n if round(x(i,j))>0 supplysum=supplysum+round(x(i,j)); end end sumsupply(i)=supplysum; end for i=1:m if sumsupply(i)~=round(s(i)) disp('Unbalanced supply'); break end end if countstepdegen >= Check else % How to correct the degeneracy problem %% How to correct degeneracy matrix numstepdegen=reducetant-countstepdegen; iterationstepDegen=0; for A=1:numstepdegen iterationstepDegen=iterationstepDegen+1; %% Construct the u-v variables %% Construct the u-v variables udual=zeros(m,1); vdual=zeros(1,n); for i=1:m udual(i)=inf; end for j=1:n vdual(j)=inf; end udual(1)=0; for i=1:1 for j=1:n if x(i,j)>0 vdual(j)=c(i,j)-udual(i); end end end for j=1:1 for i=1:m if x(i,j)>0 iu=i; if udual(iu)0 ienter1=i; for j=1:n if j~=jmax if x1(ienter1,j)>0 jenter1=j; end end end end end end x1(imax,jenter1)=r; x(imax,jenter1)=r; % Add demand and supply for j=1:n d(jenter1)=d(jenter1)+r; end for i=1:m s(imax)=s(imax)+r; end end end %The number of basic variable countopt=0; for j=1:n for i=1:m if x(i,j)>0 countopt=countopt+1; end end end % Convert x matrix xpath=zeros(m,n); for j=1:n for i=1:m if x(i,j)>0 xpath(i,j)=round(x(i,j)); end end end % Total cost Zopt=0; for j=1:n for i=1:m if xpath(i,j)>0 end Zopt=Zopt+(xpath(i,j)*c(i,j)); end end disp('Ma tran ket qua:'); disp(xpath); disp('Tong chi phi:'); disp(Zopt); end

Ngày đăng: 15/12/2019, 19:36

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan