Skip to content

Commit cda2cf7

Browse files
author
MohdFuzailHaider
committed
Fixed formatting issues
1 parent 76aea53 commit cda2cf7

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

my_script.py

+24-20
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
import numpy as np
22

3+
34
class Tableau:
45
def __init__(self, tableau):
56
self.tableau = tableau
67
self.num_rows, self.num_cols = tableau.shape
7-
8+
89
def pivot(self, row, col):
910
# Divide the pivot row by the pivot element
1011
self.tableau[row] /= self.tableau[row, col]
11-
12+
1213
# Subtract multiples of the pivot row from all other rows
1314
for i in range(self.num_rows):
1415
if i != row:
15-
self.tableau[i] -= self.tableau[i, col] * self.tableau[row]
16+
self.tableau[i] -= (self.tableau[i, col] * self.tableau[row])
1617

1718
def find_pivot_column(self):
18-
# The pivot column is the most negative value in the objective row (row 0)
19+
# The pivot column is the most negative value in the objective row
1920
obj_row = self.tableau[0, :-1]
2021
pivot_col = np.argmin(obj_row)
2122
if obj_row[pivot_col] >= 0:
2223
return -1 # No negative value, we are done
2324
return pivot_col
2425

2526
def find_pivot_row(self, pivot_col):
26-
# Calculate the ratios of the right-hand side to the pivot column entries
27+
# Calculate the ratio of the righthand side to the pivotcolumnentries
2728
rhs = self.tableau[1:, -1]
2829
col = self.tableau[1:, pivot_col]
2930
ratios = []
@@ -32,7 +33,7 @@ def find_pivot_row(self, pivot_col):
3233
ratios.append(rhs[i] / col[i])
3334
else:
3435
ratios.append(np.inf) # Ignore non-positive entries
35-
36+
3637
pivot_row = np.argmin(ratios) + 1 # Add 1 because we ignored row 0
3738
if np.isinf(ratios[pivot_row - 1]):
3839
return -1 # No valid pivot row (unbounded)
@@ -47,7 +48,7 @@ def run_simplex(self):
4748
pivot_row = self.find_pivot_row(pivot_col)
4849
if pivot_row == -1:
4950
raise ValueError("Linear program is unbounded.")
50-
51+
5152
self.pivot(pivot_row, pivot_col)
5253

5354
return self.extract_solution()
@@ -58,63 +59,66 @@ def extract_solution(self):
5859
col = self.tableau[i, :-1]
5960
if np.count_nonzero(col) == 1:
6061
solution[np.argmax(col)] = self.tableau[i, -1]
61-
return solution, -self.tableau[0, -1] # Return solution and optimal value
62+
return solution, -self.tableau[0, -1] # Returnsolutionandoptimalvalue
63+
6264

6365
def construct_tableau(objective, constraints, rhs):
6466
"""
6567
Constructs the initial tableau for the simplex algorithm.
66-
68+
6769
Parameters:
6870
- objective: List of coefficients of the objective function (maximize).
6971
- constraints: List of lists representing coefficients of the constraints.
7072
- rhs: List of right-hand-side values of constraints.
71-
73+
7274
Returns:
7375
- tableau: A numpy array representing the initial simplex tableau.
7476
"""
7577
n_constraints = len(constraints)
7678
n_vars = len(objective)
77-
79+
7880
# Creating the tableau matrix
7981
tableau = np.zeros((n_constraints + 1, n_vars + n_constraints + 1))
80-
82+
8183
# Fill the objective function (row 0, cols 0 to n_vars)
8284
tableau[0, :n_vars] = -np.array(objective) # Maximization -> negate
83-
85+
8486
# Fill the constraints
8587
for i in range(n_constraints):
8688
tableau[i + 1, :n_vars] = constraints[i]
8789
tableau[i + 1, n_vars + i] = 1 # Slack variable
8890
tableau[i + 1, -1] = rhs[i] # RHS of the constraints
89-
91+
9092
return tableau
9193

94+
9295
def solve_linear_program(objective, constraints, rhs):
9396
# Constructing the tableau
9497
tableau = construct_tableau(objective, constraints, rhs)
95-
98+
9699
# Instantiate the Tableau class
97100
simplex_tableau = Tableau(tableau)
98-
101+
99102
# Run the simplex algorithm
100103
solution, optimal_value = simplex_tableau.run_simplex()
101-
104+
102105
# Output the solution
103106
print("Optimal Solution:", solution)
104107
print("Optimal Value:", optimal_value)
105108

109+
106110
# Example usage
107111
if __name__ == "__main__":
108112
# Coefficients of the objective function: maximize 3x1 + 2x2
109113
objective = [3, 2]
110-
114+
111115
# Coefficients of the constraints:
112116
# 2x1 + x2 <= 18
113117
# x1 + 2x2 <= 20
114118
constraints = [[2, 1], [1, 2]]
115-
119+
116120
# Right-hand side of the constraints
117121
rhs = [18, 20]
118-
122+
119123
# Solve the linear program
120124
solve_linear_program(objective, constraints, rhs)

0 commit comments

Comments
 (0)