From fb1ff235bc367d2fbfa02d265b44417dc6d495d5 Mon Sep 17 00:00:00 2001
From: Brandon Rodriguez <brodriguez8774@gmail.com>
Date: Mon, 25 Nov 2019 03:37:55 -0500
Subject: [PATCH] Update pivot to more resemble book algorithm and thus be more
 efficient

Changed it for debugging/ease of understanding, while writing it
initially.
---
 readme.md                  |  9 +++++++++
 resources/simplex/pivot.py | 17 ++++++++++++-----
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/readme.md b/readme.md
index ffd0d52..7e1c03f 100644
--- a/readme.md
+++ b/readme.md
@@ -3,6 +3,15 @@
 
 ## Description
 Python implementation of a "Simplex" for Linear Programming problems.
+Algorithm is acquired from pages (869, , ) in "Algorithms Third Edition by Thomas H Cormen" book.
+
+
+## Changes to Algorithm
+The Pivot algorithm (pg 869) seemed to have an error on line 8.
+Essentially, it was saying to loop through "all columns that are a Basic Variable", but then on the next line, using
+this values as a "row_index". This was causing of "index out of range" errors.
+
+So instead, I changed that to properly loop through all rows in the matrix.
 
 
 ## Python Environment
diff --git a/resources/simplex/pivot.py b/resources/simplex/pivot.py
index 6b79d55..4fc9be6 100644
--- a/resources/simplex/pivot.py
+++ b/resources/simplex/pivot.py
@@ -154,18 +154,22 @@ class Pivot():
         :return:
         """
         # Loop through all basic indexes (minus the one we're removing). Adjust coefficients in matrix.
-        # logger.info('b_array: {0}'.format(b_array))
         for row_index in range(len(vector_b)):
+
             # Make sure to skip the one we're removing.
             if row_index != old_basic[0]:
                 # Adjust constraint constants.
                 vector_b[row_index] = self._vector_b[row_index] - (self._matrix_a[row_index][new_basic[1]] * self._vector_b[old_basic[0]])
 
-                # Loop through all nonbasic indexes (minus the one we're removing).
-                for col_index in range(len(self._vector_c) - 1):
+                # Loop through all nonbasic indexes.
+                for col_index in n_array:
                     # Adjust coefficient.
                     matrix_a[row_index][col_index] = self._matrix_a[row_index][col_index] - (self._matrix_a[new_basic[0]][col_index] * self._matrix_a[row_index][new_basic[1]])
 
+            # Separately adjust the index of the newly nonbasic variable. Make sure to skip the new basic variable.
+            if row_index != new_basic[0]:
+                matrix_a[row_index][old_basic[1]] = self._matrix_a[row_index][old_basic[1]] - (self._matrix_a[new_basic[0]][old_basic[1]] * self._matrix_a[row_index][new_basic[1]])
+
         return (matrix_a, vector_b)
 
     def _update_objective(self, n_array, matrix_a, vector_b, vector_c, obj_const_index, old_basic, new_basic):
@@ -183,11 +187,14 @@ class Pivot():
         # Update objective constant.
         vector_c[self._obj_constant_index] = self._vector_c[self._obj_constant_index] - (self._vector_b[new_basic[0]] * self._vector_c[new_basic[1]])
 
-        # Loop through all nonbasic indexes (minus the one we're adding).
-        for col_index in range(len(self._vector_c) - 1):
+        # Loop through all nonbasic indexes.
+        for col_index in n_array:
             # Adjust objective function value.
             vector_c[col_index] = self._vector_c[col_index] - (self._matrix_a[new_basic[0]][col_index] * self._vector_c[new_basic[1]])
 
+        # Separately adjust the index of the newly nonbasic variable.
+        vector_c[old_basic[1]] = self._vector_c[old_basic[1]] - (self._matrix_a[new_basic[0]][old_basic[1]] * self._vector_c[new_basic[1]])
+
         return (vector_c, obj_const_index)
 
     def _compute_basic_sets(self, n_array, b_array, old_basic, new_basic):
-- 
GitLab