From f11d9c46a469b144d4e289312ae34bd162942526 Mon Sep 17 00:00:00 2001 From: Cgarg547 Date: Tue, 29 Oct 2024 02:19:37 -0400 Subject: [PATCH 1/2] Update simplex.py --- linear_programming/simplex.py | 45 +++++++++++++++++------------------ 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/linear_programming/simplex.py b/linear_programming/simplex.py index a8affe1b72d2..325259f991df 100644 --- a/linear_programming/simplex.py +++ b/linear_programming/simplex.py @@ -287,22 +287,25 @@ def run_simplex(self) -> dict[Any, Any]: {'P': 132.0, 'x1': 12.000... 'x2': 5.999...} """ # Stop simplex algorithm from cycling. + def run_simplex(self) -> Dict[str, Any]: + """Executes the simplex algorithm until an optimal solution is found. + + Returns: + Dict[str, Any]: A dictionary containing the optimal solution values. + """ for _ in range(Tableau.maxiter): - # Completion of each stage removes an objective. If both stages - # are complete, then no objectives are left if not self.objectives: - # Find the values of each variable at optimal solution - return self.interpret_tableau() + return self.interpret_tableau() # Final tableau interpretation - row_idx, col_idx = self.find_pivot() + row_idx, col_idx = self.find_pivot() # Find the next pivot - # If there are no more negative values in objective row if self.stop_iter: - # Delete artificial variable columns and rows. Update attributes + # Transition to the next stage of the two-stage method self.tableau = self.change_stage() else: - self.tableau = self.pivot(row_idx, col_idx) - return {} + self.tableau = self.pivot(row_idx, col_idx) # Perform pivot operation + + return {} # Return an empty dictionary if max iterations are reached def interpret_tableau(self) -> dict[str, float]: """Given the final tableau, add the corresponding values of the basic @@ -315,22 +318,18 @@ def interpret_tableau(self) -> dict[str, float]: {'P': 5.0, 'x1': 1.0, 'x2': 1.0} """ # P = RHS of final tableau - output_dict = {"P": abs(self.tableau[0, -1])} + output_dict = {"P": abs(self.tableau[0, -1])} # Objective value from RHS for i in range(self.n_vars): - # Gives indices of nonzero entries in the ith column - nonzero = np.nonzero(self.tableau[:, i]) - n_nonzero = len(nonzero[0]) - - # First entry in the nonzero indices - nonzero_rowidx = nonzero[0][0] - nonzero_val = self.tableau[nonzero_rowidx, i] - - # If there is only one nonzero value in column, which is one - if n_nonzero == 1 and nonzero_val == 1: - rhs_val = self.tableau[nonzero_rowidx, -1] - output_dict[self.col_titles[i]] = rhs_val - return output_dict + nonzero = np.nonzero(self.tableau[:, i]) # Indices of nonzero entries + n_nonzero = len(nonzero[0]) # Count of nonzero entries + + if n_nonzero == 1 and self.tableau[nonzero[0][0], i] == 1: + # If this column corresponds to a basic variable + rhs_val = self.tableau[nonzero[0][0], -1] # Get corresponding RHS value + output_dict[self.col_titles[i]] = rhs_val # Store in output dictionary + + return output_dict # Return the final solution if __name__ == "__main__": From e3c375e1f04936007b04ebfff14ca9dd48ed9fcc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 29 Oct 2024 06:20:06 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- linear_programming/simplex.py | 1 + 1 file changed, 1 insertion(+) diff --git a/linear_programming/simplex.py b/linear_programming/simplex.py index 325259f991df..08069a4a5f8f 100644 --- a/linear_programming/simplex.py +++ b/linear_programming/simplex.py @@ -287,6 +287,7 @@ def run_simplex(self) -> dict[Any, Any]: {'P': 132.0, 'x1': 12.000... 'x2': 5.999...} """ # Stop simplex algorithm from cycling. + def run_simplex(self) -> Dict[str, Any]: """Executes the simplex algorithm until an optimal solution is found.