Skip to content

Update simplex.py #12314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions linear_programming/simplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,22 +287,26 @@
{'P': 132.0, 'x1': 12.000... 'x2': 5.999...}
"""
# Stop simplex algorithm from cycling.

def run_simplex(self) -> Dict[str, Any]:

Check failure on line 291 in linear_programming/simplex.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F811)

linear_programming/simplex.py:291:9: F811 Redefinition of unused `run_simplex` from line 210

Check failure on line 291 in linear_programming/simplex.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

linear_programming/simplex.py:291:30: F821 Undefined name `Dict`
"""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
Expand All @@ -315,22 +319,18 @@
{'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__":
Expand Down
Loading