(*Input: A=Cost matrix, B=Position of basic cells, IA=Shipping amount corresponding to the postion in B*) (*Output:matrix of M1=z_{ij}-c_{ij},S=Matrix of Shipping plan*) TObj[A_,B_,IA_]:=Module[{M1,m,n,M,b,i,j,S}, m=Dimensions[A][[1]];n=Dimensions[A][[2]]; M1=Table[0,m,n]; S=M1; M=Table[0,m+n-1,m+n]; b=Table[0,m+n-1]; For[i=1,i<=m+n-1,i++,M[[i,B[[i,1]]]]=1;M[[i,m+B[[i,2]]]]=1;b[[i]]=Extract[A,B[[i]]]; (*A[[B[[i]][[1]],B[[i]][[2]]]]*)]; V=LinearSolve[M,b]; For[i=1,i<=m,i++, For[j=1,j<=n,j++, If[MemberQ[B,{i,j}],M1[[i,j]]=bp;S[[i,j]]=IA[[Flatten[Position[B,{i,j}]][[1]]]], M1[[i,j]]= V[[i]]+V[[m+j]]-A[[i,j]]; ] ] ]; {M1//MatrixForm, S//MatrixForm}] (*Lp=cells along loops, Bp=Basic cells, Ia=Shipping amount, Mv=Cost matrix*) (*Output:Bp2=basic position after pivoting, Ia2=new shipping amount, Tc=Shipping cost*) TPivot[Lp_,Bp_,Ia_,Mv_]:=Module[{ll,lpp,elia,pma,ma,Bp2,Ia2,Tc}, ll=Length[Lp]; lpp=Flatten[Table[Position[Bp,Lp[[i]]],{i,2,Length[Lp]}]]; (*Position in of LP(i) in BP*) elia=Table[Ia[[ lpp[[2i-1]] ]],{i,1,ll/2}]; (*removing the entering var, extract the odd position amount*) pma=PositionSmallest[elia][[1]]; (*find the position of smallest amount which is the departing var*) ma=Min[elia]; (*the smallest amount*) Bp2=ReplaceAll[Bp,Lp[[2*pma]]->Lp[[1]]] ;(*replacing the even departing position by the entering position*) lpp=Join[lpp,Position[Bp,Lp[[2*pma]]][[1]]]; Ia2=Ia; For[i=1,i<=ll/2,i++, Ia2[[lpp[[2i-1]]]]=Ia[[lpp[[2i-1]]]]-ma; Ia2[[lpp[[2i]]]]=Ia2[[lpp[[2i]]]]+ma; ]; Tc=Sum[Extract[Mv,Bp2[[i]]]*Ia2[[i]],{i,1,Length[Ia2]}]; {Bp2,Ia2,Tc} ]