Simplex Method Algorithm

1. Find pivot column
2. Find pivot row
3. Do the pivot
4. Repeat 1,2, 3 until done

Let’s create the “doPivot” function first. It needs the matrix, the row, and the column to pivot. Row and column will be input variables, but the matrix to manipulate is assumed to be defined in a global variable called “tableau”.

doPivot[row_, col_] := 
(tableau[[row]] = tableau[[row]]/tableau[[row, col]];
Do[If[row ≠ i,tableau[[i]] = tableau[[i]] - tableau[[row]]* tableau[[i,col]]],
{i,1,Length[A]}];)

Now defining the funtion “getPivotCol”. It takes a matrix as inut and returns the column of the loation of the most negative value in the last row of the matrix A.

getPivotCol[A_] :=
Position[A[[Length[A]]],Min[A[[Length[A]]]]][[1,1]]

Now defining the function “getPivotRow”. It takes a matrix and a column number as input and returns the row of the smallest ratio of the last column divided by the column indicated as input. Note that this function defines three “local” variables, i.e. variables that are defined inside the given function and are valued only inside that function: Module[{list_of_local_vars}, code]

getPivotRow[A_, col_] :=
Module[{numer, denom, values},
numer =A[[All,-1]];
denom = A[[All,col]];
values = numer / denom;
Position[Drop[values,-1],Min[Drop[values,-1]]][[1,1]]
]

A={{10,5,1,0,0,0,50},
        {12,1,0,1,0,0,48},
        {3,2,0,0,1,0,18},
          {-4,-2,0,0,0,1,0}};
A // MatrixForm

tableau = A

While[Min[tableau[[-1]]]<0,doPivot[getPivotRow[tableau,getPivotCol[tableau]],getPivotCol[tableau]]];
tableau // MatrixForm

tableau // MatrixForm