|
| 1 | +import numpy as np |
| 2 | + |
| 3 | + |
| 4 | +class Tableau: |
| 5 | + def __init__(self, tableau): |
| 6 | + self.tableau = tableau |
| 7 | + self.num_rows, self.num_cols = tableau.shape |
| 8 | + |
| 9 | + def pivot(self, row, col): |
| 10 | + # Divide the pivot row by the pivot element |
| 11 | + self.tableau[row] /= self.tableau[row, col] |
| 12 | + |
| 13 | + # Subtract multiples of the pivot row from all other rows |
| 14 | + for i in range(self.num_rows): |
| 15 | + if i != row: |
| 16 | + self.tableau[i] -= (self.tableau[i, col] * self.tableau[row]) |
| 17 | + |
| 18 | + def find_pivot_column(self): |
| 19 | + # The pivot column is the most negative value in the objective row |
| 20 | + obj_row = self.tableau[0, :-1] |
| 21 | + pivot_col = np.argmin(obj_row) |
| 22 | + if obj_row[pivot_col] >= 0: |
| 23 | + return -1 # No negative value, we are done |
| 24 | + return pivot_col |
| 25 | + |
| 26 | + def find_pivot_row(self, pivot_col): |
| 27 | + # Calculate the ratio of the righthand side to the pivotcolumnentries |
| 28 | + rhs = self.tableau[1:, -1] |
| 29 | + col = self.tableau[1:, pivot_col] |
| 30 | + ratios = [] |
| 31 | + for i in range(len(rhs)): |
| 32 | + if col[i] > 0: |
| 33 | + ratios.append(rhs[i] / col[i]) |
| 34 | + else: |
| 35 | + ratios.append(np.inf) # Ignore non-positive entries |
| 36 | + |
| 37 | + pivot_row = np.argmin(ratios) + 1 # Add 1 because we ignored row 0 |
| 38 | + if np.isinf(ratios[pivot_row - 1]): |
| 39 | + return -1 # No valid pivot row (unbounded) |
| 40 | + return pivot_row |
| 41 | + |
| 42 | + def run_simplex(self): |
| 43 | + while True: |
| 44 | + pivot_col = self.find_pivot_column() |
| 45 | + if pivot_col == -1: |
| 46 | + break # Optimal solution found |
| 47 | + |
| 48 | + pivot_row = self.find_pivot_row(pivot_col) |
| 49 | + if pivot_row == -1: |
| 50 | + raise ValueError("Linear program is unbounded.") |
| 51 | + |
| 52 | + self.pivot(pivot_row, pivot_col) |
| 53 | + |
| 54 | + return self.extract_solution() |
| 55 | + |
| 56 | + def extract_solution(self): |
| 57 | + solution = np.zeros(self.num_cols - 1) |
| 58 | + for i in range(1, self.num_rows): |
| 59 | + col = self.tableau[i, :-1] |
| 60 | + if np.count_nonzero(col) == 1: |
| 61 | + solution[np.argmax(col)] = self.tableau[i, -1] |
| 62 | + return solution, -self.tableau[0, -1] # Returnsolutionandoptimalvalue |
| 63 | + |
| 64 | + |
| 65 | +def construct_tableau(objective, constraints, rhs): |
| 66 | + """ |
| 67 | + Constructs the initial tableau for the simplex algorithm. |
| 68 | +
|
| 69 | + Parameters: |
| 70 | + - objective: List of coefficients of the objective function (maximize). |
| 71 | + - constraints: List of lists representing coefficients of the constraints. |
| 72 | + - rhs: List of right-hand-side values of constraints. |
| 73 | +
|
| 74 | + Returns: |
| 75 | + - tableau: A numpy array representing the initial simplex tableau. |
| 76 | + """ |
| 77 | + n_constraints = len(constraints) |
| 78 | + n_vars = len(objective) |
| 79 | + |
| 80 | + # Creating the tableau matrix |
| 81 | + tableau = np.zeros((n_constraints + 1, n_vars + n_constraints + 1)) |
| 82 | + |
| 83 | + # Fill the objective function (row 0, cols 0 to n_vars) |
| 84 | + tableau[0, :n_vars] = -np.array(objective) # Maximization -> negate |
| 85 | + |
| 86 | + # Fill the constraints |
| 87 | + for i in range(n_constraints): |
| 88 | + tableau[i + 1, :n_vars] = constraints[i] |
| 89 | + tableau[i + 1, n_vars + i] = 1 # Slack variable |
| 90 | + tableau[i + 1, -1] = rhs[i] # RHS of the constraints |
| 91 | + |
| 92 | + return tableau |
| 93 | + |
| 94 | + |
| 95 | +def solve_linear_program(objective, constraints, rhs): |
| 96 | + # Constructing the tableau |
| 97 | + tableau = construct_tableau(objective, constraints, rhs) |
| 98 | + |
| 99 | + # Instantiate the Tableau class |
| 100 | + simplex_tableau = Tableau(tableau) |
| 101 | + |
| 102 | + # Run the simplex algorithm |
| 103 | + solution, optimal_value = simplex_tableau.run_simplex() |
| 104 | + |
| 105 | + # Output the solution |
| 106 | + print("Optimal Solution:", solution) |
| 107 | + print("Optimal Value:", optimal_value) |
| 108 | + |
| 109 | + |
| 110 | +# Example usage |
| 111 | +if __name__ == "__main__": |
| 112 | + # Coefficients of the objective function: maximize 3x1 + 2x2 |
| 113 | + objective = [3, 2] |
| 114 | + |
| 115 | + # Coefficients of the constraints: |
| 116 | + # 2x1 + x2 <= 18 |
| 117 | + # x1 + 2x2 <= 20 |
| 118 | + constraints = [[2, 1], [1, 2]] |
| 119 | + |
| 120 | + # Right-hand side of the constraints |
| 121 | + rhs = [18, 20] |
| 122 | + |
| 123 | + # Solve the linear program |
| 124 | + solve_linear_program(objective, constraints, rhs) |
0 commit comments