|
| 1 | +
# 🐍Linear Programming Problem – Simplex Method using Python |
| 2 | + |
| 3 | +This example presents a complete, step-by-step solution to a **Linear Programming (LP)** problem using the **Simplex Method**, along with a basic **Python implementation**. |
| 4 | + |
| 5 | +## 🧮 Problem Statement |
| 6 | + |
| 7 | +**Maximise:** |
| 8 | + |
| 9 | +Z = 4x₁ + 3x₂ |
| 10 | + |
| 11 | +**Subject to:** |
| 12 | + |
| 13 | +x₁ + 3x₂ ≤ 7 |
| 14 | +2x₁ + 2x₂ ≤ 8 |
| 15 | +x₁ + x₂ ≤ 3 |
| 16 | +x₂ ≤ 2 |
| 17 | +x₁ ≥ 0, x₂ ≥ 0 |
| 18 | + |
| 19 | +## ✅ Standard Form Conversion |
| 20 | + |
| 21 | +Introduce slack variables: s₁, s₂, s₃, s₄ |
| 22 | + |
| 23 | +The system becomes: |
| 24 | + |
| 25 | + |
| 26 | +$x_1 + 3x_2 + s_1 = 7$ |
| 27 | +$2x_1 + 2x_2 + s_2 = 8$ |
| 28 | +$x_1 + x_2 + s_3 = 3$ |
| 29 | +$x_2 + s_4 = 2$ |
| 30 | +$x_1,\ x_2,\ s_1,\ s_2,\ s_3,\ s_4 \geq 0$ |
| 31 | + |
| 32 | +\ |
| 33 | +\begin{aligned} |
| 34 | +\text{Max.} \quad & Z = 4x_1 + 3x_2 \\ |
| 35 | +\text{S.a.} \quad & |
| 36 | +\begin{cases} |
| 37 | +x_1 + 3x_2 + s_1 = 7 \\ |
| 38 | +2x_1 + 2x_2 + s_2 = 8 \\ |
| 39 | +x_1 + x_2 + s_3 = 3 \\ |
| 40 | +x_2 + s_4 = 2 \\ |
| 41 | +x_1,\, x_2,\, s_1,\, s_2,\, s_3,\, s_4 \geq 0 |
| 42 | +\end{cases} |
| 43 | +\end{aligned} |
| 44 | +\ |
| 45 | + |
| 46 | +x₁ + 3x₂ + s₁ = 7 |
| 47 | +2x₁ + 2x₂ + s₂ = 8 |
| 48 | +x₁ + x₂ + s₃ = 3 |
| 49 | +x₂ + s₄ = 2 |
| 50 | +x₁, x₂, s₁, s₂, s₃, s₄ ≥ 0 |
| 51 | + |
| 52 | +## 📊 Initial Simplex Tableau |
| 53 | + |
| 54 | +| Base | x₁ | x₂ | s₁ | s₂ | s₃ | s₄ | RHS | |
| 55 | +|------|----|----|----|----|----|----|-----| |
| 56 | +| s₁ | 1 | 3 | 1 | 0 | 0 | 0 | 7 | |
| 57 | +| s₂ | 2 | 2 | 0 | 1 | 0 | 0 | 8 | |
| 58 | +| s₃ | 1 | 1 | 0 | 0 | 1 | 0 | 3 | |
| 59 | +| s₄ | 0 | 1 | 0 | 0 | 0 | 1 | 2 | |
| 60 | +| **Z**| -4 | -3 | 0 | 0 | 0 | 0 | 0 | |
| 61 | + |
| 62 | +## 🔄 Iterations Overview |
| 63 | + |
| 64 | +### Iteration 1 |
| 65 | + |
| 66 | +- **Entering variable**: x₁ (most negative in Z row) |
| 67 | +- **Leaving variable**: s₃ (minimum ratio = 3) |
| 68 | +- Pivot to bring x₁ into the basis. |
| 69 | + |
| 70 | +Updated tableau shows next candidate as: |
| 71 | +- **Entering variable**: x₂ |
| 72 | +- **Leaving variable**: s₁ or s₄ (tie – choose s₁) |
| 73 | + |
| 74 | +### Iteration 2 |
| 75 | + |
| 76 | +After pivoting x₂ into the basis, tableau is updated again. |
| 77 | +Now the most negative coefficient in the Z row is for s₃, but: |
| 78 | +- No valid pivot is possible (no positive coefficients in that column). |
| 79 | +- Hence, no further improvement is feasible. |
| 80 | + |
| 81 | +## 🏁 Final Optimal Solution |
| 82 | + |
| 83 | +The optimal solution was reached at the end of Iteration 1: |
| 84 | + |
| 85 | +x₁ = 3 |
| 86 | +x₂ = 0 |
| 87 | +Z(max) = 12 |
| 88 | + |
| 89 | +**All constraints are satisfied.** |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +## 🐍 Python Code – Simplex Solver (Basic Version) |
| 94 | + |
| 95 | +```python |
| 96 | +from scipy.optimize import linprog |
| 97 | + |
| 98 | +# Coefficients of the objective function (to maximise Z = 4x₁ + 3x₂) |
| 99 | +# Convert to minimisation: -Z |
| 100 | +c = [-4, -3] |
| 101 | + |
| 102 | +# Coefficients of the inequality constraints (Ax ≤ b) |
| 103 | +A = [ |
| 104 | + [1, 3], |
| 105 | + [2, 2], |
| 106 | + [1, 1], |
| 107 | + [0, 1] |
| 108 | +] |
| 109 | + |
| 110 | +b = [7, 8, 3, 2] |
| 111 | + |
| 112 | +# Bounds for x₁ and x₂: both ≥ 0 |
| 113 | +x_bounds = (0, None) |
| 114 | +bounds = [x_bounds, x_bounds] |
| 115 | + |
| 116 | +# Solve the problem |
| 117 | +res = linprog(c, A_ub=A, b_ub=b, bounds=bounds, method="simplex") |
| 118 | + |
| 119 | +# Output results |
| 120 | +if res.success: |
| 121 | + print("Optimal solution found:") |
| 122 | + print(f"x₁ = {res.x[0]:.2f}") |
| 123 | + print(f"x₂ = {res.x[1]:.2f}") |
| 124 | + print(f"Maximum Z = {(-res.fun):.2f}") |
| 125 | +else: |
| 126 | + print("No solution found:", res.message) |
| 127 | +``` |
0 commit comments