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