|
| 1 | +# Linear Programming Problem – Simplex Method |
| 2 | + |
| 3 | +This repository 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 | +x₁ + 3x₂ + s₁ = 7 |
| 26 | +2x₁ + 2x₂ + s₂ = 8 |
| 27 | +x₁ + x₂ + s₃ = 3 |
| 28 | +x₂ + s₄ = 2 |
| 29 | +x₁, x₂, s₁, s₂, s₃, s₄ ≥ 0 |
| 30 | + |
| 31 | +## 📊 Initial Simplex Tableau |
| 32 | + |
| 33 | +| Base | x₁ | x₂ | s₁ | s₂ | s₃ | s₄ | RHS | |
| 34 | +|------|----|----|----|----|----|----|-----| |
| 35 | +| s₁ | 1 | 3 | 1 | 0 | 0 | 0 | 7 | |
| 36 | +| s₂ | 2 | 2 | 0 | 1 | 0 | 0 | 8 | |
| 37 | +| s₃ | 1 | 1 | 0 | 0 | 1 | 0 | 3 | |
| 38 | +| s₄ | 0 | 1 | 0 | 0 | 0 | 1 | 2 | |
| 39 | +| **Z**| -4 | -3 | 0 | 0 | 0 | 0 | 0 | |
| 40 | + |
| 41 | +## 🔄 Iterations Overview |
| 42 | + |
| 43 | +### 🔹 Iteration 1 |
| 44 | + |
| 45 | +- **Entering variable**: x₁ (most negative in Z row) |
| 46 | +- **Leaving variable**: s₃ (minimum ratio = 3) |
| 47 | +- Pivot to bring x₁ into the basis. |
| 48 | + |
| 49 | +Updated tableau shows next candidate as: |
| 50 | +- **Entering variable**: x₂ |
| 51 | +- **Leaving variable**: s₁ or s₄ (tie – choose s₁) |
| 52 | + |
| 53 | +### 🔹 Iteration 2 |
| 54 | + |
| 55 | +After pivoting x₂ into the basis, tableau is updated again. |
| 56 | +Now the most negative coefficient in the Z row is for s₃, but: |
| 57 | +- No valid pivot is possible (no positive coefficients in that column). |
| 58 | +- Hence, no further improvement is feasible. |
| 59 | + |
| 60 | +## 🏁 Final Optimal Solution |
| 61 | + |
| 62 | +The optimal solution was reached at the end of Iteration 1: |
| 63 | + |
| 64 | +x₁ = 3 |
| 65 | +x₂ = 0 |
| 66 | +Z(max) = 12 |
| 67 | + |
| 68 | +**All constraints are satisfied.** |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +## 🐍 Python Code – Simplex Solver (Basic Version) |
| 73 | + |
| 74 | +```python |
| 75 | +from scipy.optimize import linprog |
| 76 | + |
| 77 | +# Coefficients of the objective function (to maximise Z = 4x₁ + 3x₂) |
| 78 | +# Convert to minimisation: -Z |
| 79 | +c = [-4, -3] |
| 80 | + |
| 81 | +# Coefficients of the inequality constraints (Ax ≤ b) |
| 82 | +A = [ |
| 83 | + [1, 3], |
| 84 | + [2, 2], |
| 85 | + [1, 1], |
| 86 | + [0, 1] |
| 87 | +] |
| 88 | + |
| 89 | +b = [7, 8, 3, 2] |
| 90 | + |
| 91 | +# Bounds for x₁ and x₂: both ≥ 0 |
| 92 | +x_bounds = (0, None) |
| 93 | +bounds = [x_bounds, x_bounds] |
| 94 | + |
| 95 | +# Solve the problem |
| 96 | +res = linprog(c, A_ub=A, b_ub=b, bounds=bounds, method="simplex") |
| 97 | + |
| 98 | +# Output results |
| 99 | +if res.success: |
| 100 | + print("Optimal solution found:") |
| 101 | + print(f"x₁ = {res.x[0]:.2f}") |
| 102 | + print(f"x₂ = {res.x[1]:.2f}") |
| 103 | + print(f"Maximum Z = {(-res.fun):.2f}") |
| 104 | +else: |
| 105 | + print("No solution found:", res.message) |
0 commit comments