
simplexmethod[c_,A_,B_,b_,xnb_,xb_,cp_,Ap_,Bp_,bp_]:= Block[{cB,nA,nB,mysymbol,pivotCol,AB,leaveVarpos,Bnew,xbnew, xnbnew,Anew,cnew,znew,z}, (*初始化變數*) nA=Length[xnb]; nB=Length[xb]; (*建立符號變數*) mysymbol=ToExpression[Flatten[{"x"<>ToString[#]&/@Range[nA], "s"<>ToString[#]&/@Range[nB]}]]; (*找出進入變數(最負的檢驗數)*) pivotCol=PositionSmallest[-c][[1]]; (*合併A和B矩陣*) AB=Flatten/@Transpose[{A,B}]; (*找出離開變數(最小比率測試)*) leaveVarpos=PositionSmallest[ b/(If[#<=0,10^-6,#]&/@ Map[Flatten,Transpose[{A,B}]][[All,pivotCol]])/.x_/;x<=0->10^6][[1]]; (*更新基變數和非基變數*) xbnew=xb/.xb[[leaveVarpos]]->xnb[[pivotCol]]; xnbnew=xnb/.xnb[[pivotCol]]->xb[[leaveVarpos]]; (*建立新的基矩陣*) Bnew=Map[Flatten,Transpose[{Ap,Bp}]][[All, Flatten[Position[mysymbol,#]&/@xbnew]]]; (*計算基變數的成本係數*) cB=cp[[Flatten[Position[mysymbol,#]&/@xbnew]]]; (*計算新的檢驗數和目標函數值*) znew= Flatten[{cB.Inverse[Bnew].Ap-cp[[1;;nA]], cB.Inverse[Bnew],cB.Inverse[Bnew].bp}]; (*顯示單形法表格*) Print[Grid[ Insert[Flatten/@Transpose[{Flatten[{z,xbnew}], Insert[Map[Flatten,Transpose[{Inverse[Bnew].Ap,Inverse[Bnew], Inverse[Bnew].bp}]],znew,1]}],Flatten[{Item["BasicVariable", Background->Lighter[Yellow,0.9]],mysymbol,"Solution"}], 1],Frame->All, Background->{{Lighter[Gray,0.8]},{Lighter[Yellow,0.9], Lighter[Pink,0.9]}},ItemStyle->Directive[FontSize->16], Spacings->{Table[5,{Length[mysymbol]+3}],2}]]; (*返回更新後的參數*) {-Flatten@{cB.Inverse[Bnew].Ap-cp[[1;;nA]], cB.Inverse[Bnew]},Inverse[Bnew].Ap,Inverse[Bnew], Inverse[Bnew].bp,xnbnew,xbnew,cp,Ap,Bp,bp}]
Nest[simplexmethod@@#&,{ {3,5,0,0,0}, {{1,0},{0,2},{3,2}}, {{1,0,0},{0,1,0},{0,0,1}},{4,12,18}, {x1,x2},{s1,s2,s3}, {3,5,0,0,0}, {{1,0},{0,2},{3,2}}, {{1,0,0},{0,1,0},{0,0,1}},{4,12,18}}, 2]
讀者回應 ( 0 意見 )
訂閱發佈留言 (Atom)
發佈留言
Please leave your name and tell me what you thought about this site. Comments, suggestions and views are welcomed.
如果這篇文章對你有幫助,那請留個訊息給我~