@@ -287,22 +287,25 @@ def run_simplex(self) -> dict[Any, Any]:
287
287
{'P': 132.0, 'x1': 12.000... 'x2': 5.999...}
288
288
"""
289
289
# 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
+ """
290
296
for _ in range (Tableau .maxiter ):
291
- # Completion of each stage removes an objective. If both stages
292
- # are complete, then no objectives are left
293
297
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
296
299
297
- row_idx , col_idx = self .find_pivot ()
300
+ row_idx , col_idx = self .find_pivot () # Find the next pivot
298
301
299
- # If there are no more negative values in objective row
300
302
if self .stop_iter :
301
- # Delete artificial variable columns and rows. Update attributes
303
+ # Transition to the next stage of the two-stage method
302
304
self .tableau = self .change_stage ()
303
305
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
306
309
307
310
def interpret_tableau (self ) -> dict [str , float ]:
308
311
"""Given the final tableau, add the corresponding values of the basic
@@ -315,22 +318,18 @@ def interpret_tableau(self) -> dict[str, float]:
315
318
{'P': 5.0, 'x1': 1.0, 'x2': 1.0}
316
319
"""
317
320
# 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
319
322
320
323
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
334
333
335
334
336
335
if __name__ == "__main__" :
0 commit comments