Simplex Algorithm is a well-known optimization technique in Linear Programming.
The general form of an LPP (Linear Programming Problem) is
Example: Let’s consider the following maximization problem.
Initial construction steps :
- Build your matrix A. A will contain the coefficients of the constraints.
- Matrix b will contain the amount of resources.
- And matrix c will contain the coefficients of objective function or cost.
For the above problem –
Matrix A – At Iteration 0
Explanation of table-
B : Basis and contains the basic variables. Simplex algorithm starts with those variables which form an indentity matrix. In the above eg x4 and x3 forms a 2×2 identity matrix.
CB : Its the coefficients of the basic variables in the objective function. The objective functions doesn’t contain x4 and x3, so these are 0.
XB : The number of resources or we can say the RHS of the constraints.
yi : The complete Matrix A.
Simplex Algorithm 1. Start with the initial basis associated with identity matrix. 2. Calculate the relative profits. For MAX problem- If all the relative profits are less than or equal to 0, then the current basis is the optimal one. STOP. Else contniue to 3. For MIN problem If all the relative profits are greater than or equal to 0, then the current basis is the optimal one. STOP. Else contniue to 3. 3. Find the column corresponding to max relative profit. Say column k has the max Rel. profit. So xk will enter the basis. 4. Perform a min ratio test to determine which variable will leave the basis. Index of the min element i.e 'r' will determine the leaving variable. The basic variable at index r, will leave the basis. NOTE: Min ratio test is always performed on positive elements. 5. It's evident that the entered variable will not form an identity matrix, so we will have to perform row operations to make it identity again. Find the pivot element. The element at index (r, k) will be the pivot element and row r will be the pivot row. 6. Divide the rth row by pivot to make it 1. And subtract c*(rth row) from other rows to make them 0, where c is the coefficient required to make that row 0.
Table at Iteration 1
Calculation of relative profits – (Cj – Zj), where Cj is the coefficient in Z and Zj is yi*CB
C1 – Z1 = 1 – (1*0 + 2*0)
C2 – Z2 = 1 – (1*0 + 1*0)
C3 – Z3 = 0 – (0*0 + 1*0)
C4 – Z4 = 0 – (1*0 + 0*0)
So Relative profits are- 1, 1, 0, 0 (As shown in the table)
Clearly not all the relative profits are less or equal to 0. So will perform the next iteration.
Determination of entering variable and leaving variable.
Max relative profit 1 at index 1. So x1 will enter basis.
Min of (8, 5) is 5 which is at index 2. So x3 will leave the basis.
Since x1 entered perform required row operations to make idenity matrix.
Pivot index = [2, 4]
Pivot element = 2
Divide the 2nd row by pivot element i.e 2 to make it 1.
And subtract 1*R2 from R1 to make it 0
See next table.
Table At Iteration 2
Relative profits = 0, 1/2, -1/2, 0
Pivot index = [1, 5]
Pivot element = 1/2
Perform necessary row operations.
See next table
Relative profits = 0, 0, 0, -1
Since all relative profits are less than or equal to 0. So optimality is reached.
This will be the final simplex table and the optimal one.
Value of Z at optimality = 6*1 + 2*1 = 8
Following cases can occur while performing this algorithm.
- Case 1 – Unbounded Solution
If the column corresponding to the max relative profit contains only non-positive real numbers then we won’t be able to perform the min ratio test. Therefore it is reported as unbounded solution.
- Case 2 – Alternate Solution
If at any iteration any one of the non-basic variable’s relative profit comes out to be 0, then it contains alternate solutions. Many optimal solutions will exist.
The above example was an equality case where we were able to find the initial basis. Now we will perform simplex on an example where there is no identity forming.
Convert the above problem into standard form i.e
where x3, x4 and x5 are slack variables. These will form identity and hence the initial basis.
Table at Iteration 0
Now continuing as the previous example.
Table at iteration 1
Relative profits = 2, 5, 0, 0, 0
Pivot Index = [2, 5]
Pivot element = 1
Table at Iteration 2
Relative Profits = 2, 0, 0, -5, 0
Pivot Index = [1, 4]
Pivot Element = 1
Table at iteration 3
Relative profits = 0, 0, 0, -2, -3, 0
Since all relative profits are less than equal to 0. Optimality is reached.
This is the final simplex table and the optimal one.
Value of Z at optimality = 3*2 + 3*5 + 0*0 = 21
Code Implementation of Simplex Algorithm
For the above just plug in the required values and you will get a detailed step by step solution of your LPP by simplex algorithm.
- ML | ECLAT Algorithm
- Cristian's Algorithm
- Implementing Apriori algorithm in Python
- Page Rank Algorithm and Implementation
- Silhouette Algorithm to determine the optimal value of k
- Implementing DBSCAN algorithm using Sklearn
- Bisect Algorithm Functions in Python
- ML | Frequent Pattern Growth Algorithm
- k-nearest neighbor algorithm in Python
- Explanation of Fundamental Functions involved in A3C algorithm
- Asynchronous Advantage Actor Critic (A3C) algorithm
- ML | Mini Batch K-means clustering algorithm
- ML | T-distributed Stochastic Neighbor Embedding (t-SNE) Algorithm
- Python | Single Point Crossover in Genetic Algorithm
- Python | Foreground Extraction in an Image using Grabcut Algorithm