diff --git a/readme.md b/readme.md index ffd0d522cb403877054decba3c2745b7a6913637..7e1c03fdbf94b6190476c96dc93437e8d664340f 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 6b79d55b58de1735f9c6917b5df2bd816d3a52ed..4fc9be60bfd9523c84e00bb714c9ebbf3fd7433c 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):