Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f11d9c4

Browse files
authoredOct 29, 2024··
Update simplex.py
1 parent 52602ea commit f11d9c4

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed
 

‎linear_programming/simplex.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -287,22 +287,25 @@ def run_simplex(self) -> dict[Any, Any]:
287287
{'P': 132.0, 'x1': 12.000... 'x2': 5.999...}
288288
"""
289289
# Stop simplex algorithm from cycling.
290+
def run_simplex(self) -> Dict[str, Any]:
291+
"""Executes the simplex algorithm until an optimal solution is found.
292+
293+
Returns:
294+
Dict[str, Any]: A dictionary containing the optimal solution values.
295+
"""
290296
for _ in range(Tableau.maxiter):
291-
# Completion of each stage removes an objective. If both stages
292-
# are complete, then no objectives are left
293297
if not self.objectives:
294-
# Find the values of each variable at optimal solution
295-
return self.interpret_tableau()
298+
return self.interpret_tableau() # Final tableau interpretation
296299

297-
row_idx, col_idx = self.find_pivot()
300+
row_idx, col_idx = self.find_pivot() # Find the next pivot
298301

299-
# If there are no more negative values in objective row
300302
if self.stop_iter:
301-
# Delete artificial variable columns and rows. Update attributes
303+
# Transition to the next stage of the two-stage method
302304
self.tableau = self.change_stage()
303305
else:
304-
self.tableau = self.pivot(row_idx, col_idx)
305-
return {}
306+
self.tableau = self.pivot(row_idx, col_idx) # Perform pivot operation
307+
308+
return {} # Return an empty dictionary if max iterations are reached
306309

307310
def interpret_tableau(self) -> dict[str, float]:
308311
"""Given the final tableau, add the corresponding values of the basic
@@ -315,22 +318,18 @@ def interpret_tableau(self) -> dict[str, float]:
315318
{'P': 5.0, 'x1': 1.0, 'x2': 1.0}
316319
"""
317320
# P = RHS of final tableau
318-
output_dict = {"P": abs(self.tableau[0, -1])}
321+
output_dict = {"P": abs(self.tableau[0, -1])} # Objective value from RHS
319322

320323
for i in range(self.n_vars):
321-
# Gives indices of nonzero entries in the ith column
322-
nonzero = np.nonzero(self.tableau[:, i])
323-
n_nonzero = len(nonzero[0])
324-
325-
# First entry in the nonzero indices
326-
nonzero_rowidx = nonzero[0][0]
327-
nonzero_val = self.tableau[nonzero_rowidx, i]
328-
329-
# If there is only one nonzero value in column, which is one
330-
if n_nonzero == 1 and nonzero_val == 1:
331-
rhs_val = self.tableau[nonzero_rowidx, -1]
332-
output_dict[self.col_titles[i]] = rhs_val
333-
return output_dict
324+
nonzero = np.nonzero(self.tableau[:, i]) # Indices of nonzero entries
325+
n_nonzero = len(nonzero[0]) # Count of nonzero entries
326+
327+
if n_nonzero == 1 and self.tableau[nonzero[0][0], i] == 1:
328+
# If this column corresponds to a basic variable
329+
rhs_val = self.tableau[nonzero[0][0], -1] # Get corresponding RHS value
330+
output_dict[self.col_titles[i]] = rhs_val # Store in output dictionary
331+
332+
return output_dict # Return the final solution
334333

335334

336335
if __name__ == "__main__":

0 commit comments

Comments
 (0)
Please sign in to comment.