diff --git a/resources/simplex/initialize.py b/resources/simplex/initialize.py index 85c4f8bd168e89e7fa548cf438e88bdf61b9ccba..d79fc8edfe2e83fa2624f9f1c8fc694e8132396a 100644 --- a/resources/simplex/initialize.py +++ b/resources/simplex/initialize.py @@ -8,7 +8,6 @@ Author: Brandon Rodriguez Implementation of the "Initialize" function for Linear Programming Simplex problems. """ - # System Imports. import copy diff --git a/resources/simplex/pivot.py b/resources/simplex/pivot.py index 37479f51cbcc1df291895edd51ab9fb61c920de6..1db73da4f89f6817f54d26c6796e46ac45c034d2 100644 --- a/resources/simplex/pivot.py +++ b/resources/simplex/pivot.py @@ -204,8 +204,9 @@ class Pivot(): # Remove new basic variable from set of nonbasics. n_array.remove(new_basic[1]) - # Add new nonbasic variable to set of nonbasics, making sure we put it in the proper equation index. - n_array.insert(old_basic[0], old_basic[1]) + # Add new nonbasic variable to set of nonbasics. Order doesn't matter so we sort for human readability. + n_array.append(old_basic[1]) + n_array.sort() # Remove new nonbasic variable from set of basics. b_array.remove(old_basic[1]) diff --git a/resources/simplex/run_simplex.py b/resources/simplex/run_simplex.py index 06750c3394b0edaccf3f0cc9bffb1ea418a90381..7378f43c9cd4a90a720d26dbc5da2443f2ed85d5 100644 --- a/resources/simplex/run_simplex.py +++ b/resources/simplex/run_simplex.py @@ -55,7 +55,7 @@ class RunSimplex(): nonbasic_var_indexes = self._parent._n_array # Check that we actually can still optimize values. - for col_index in range(len(nonbasic_var_indexes)): + for col_index in range(len(vector_b)): # Check if value is greater than 0. logger.info('vector_c: {0}'.format(vector_c)) logger.info('vector_c[{0}]: {1}'.format(col_index, vector_c[col_index])) @@ -79,7 +79,7 @@ class RunSimplex(): # Get solution. solution = [] - for col_index in range(len(vector_b)): + for col_index in range(len(vector_c) - 1): # Check if col is basic or not. if col_index in basic_var_indexes: # Col is basic. Find associated row where basic value exists. @@ -90,6 +90,7 @@ class RunSimplex(): else: # Col is not basic. Return 0. solution.append(0) + logger.info('solution: {0}'.format(solution)) return (-vector_c[obj_constant_index], solution) @@ -105,13 +106,29 @@ class RunSimplex(): # Loop through all rows, finding best one to pivot on. delta = [] + logger.info('') + logger.info('matrix_a: {0}'.format(matrix_a)) + logger.info('vector_b: {0}'.format(vector_b)) for row_index in range(len(matrix_a)): # Check that we can divide. + logger.info('vector_b[{0}]: {1} matrix_a[{0}][{2}]: {3}'.format( + row_index, + vector_b[row_index], + col_index, + matrix_a[row_index][col_index], + )) + try: + logger.info('b/a: {0}'.format(vector_b[row_index] / matrix_a[row_index][col_index])) + except: + logger.info('b/a: -1') if matrix_a[row_index][col_index] > 0: - delta.append(vector_b[col_index] / matrix_a[row_index][col_index]) + delta.append(vector_b[row_index] / matrix_a[row_index][col_index]) else: # Less than 1. Can't divide. delta.append(-1) + logger.info('delta: {0}'.format(delta)) + + logger.info('delta: {0}'.format(delta)) # Now check to see which row we actually use. smallest_valid_index = -1 diff --git a/tests/resources/simplex/run_simplex.py b/tests/resources/simplex/run_simplex.py index 990e451890029875b1fca296ac997e891f62e9b9..d54b67b3e6ef7c0b8457900203fc1a2d0be89551 100644 --- a/tests/resources/simplex/run_simplex.py +++ b/tests/resources/simplex/run_simplex.py @@ -19,14 +19,14 @@ class TestPivot(unittest.TestCase): def setUp(self): self.simplex = SimplexBase() - def test__run_simplex(self): + def test__run_simplex__ex1(self): # Setup initial simplex, using JSON data. self.simplex.read_data_from_json('./resources/json_files/ex_1.json') # Test initial values after setup. self.assertEqual(self.simplex._matrix_a, [ [1, 1, 1, 0], - [1, 1, 0, 1] + [1, 1, 0, 1], ]) self.assertEqual(self.simplex._vector_b, [1, 1]) self.assertEqual(self.simplex._vector_c, [1, 1, 0, 0, 0]) @@ -35,9 +35,55 @@ class TestPivot(unittest.TestCase): self.assertEqual(self.simplex._n_array, [0, 1]) # Attempt to fully solve simplex. - self.simplex.display_tableau() results = self.simplex.solve() # Test values after solving. + self.assertEqual(self.simplex._matrix_a, [ + [1, 1, 1, 0], + [0, 0, -1, 1], + ]) + self.assertEqual(self.simplex._vector_b, [1, 0]) + self.assertEqual(self.simplex._vector_c, [0, 0, -1, 0, -1]) + self.assertEqual(self.simplex._obj_constant_index, 4) + self.assertEqual(self.simplex._b_array, [0, 3]) + self.assertEqual(self.simplex._n_array, [1, 2]) + + # Test solution results. self.assertEqual(results[0], 1) - self.assertEqual(results[1], (1, 0)) + self.assertEqual(results[1], [1, 0, 0, 0]) + + def test__run_simplex__ex2(self): + # Setup initial simplex, using JSON data. + self.simplex.read_data_from_json('./resources/json_files/ex_2.json') + + # Test initial values after setup. + self.assertEqual(self.simplex._matrix_a, [ + [1, 1, 1, 1, 0, 0], + [2, -1, 2, 0, 1, 0], + [2, 1, 0, 0, 0, 1], + ]) + self.assertEqual(self.simplex._vector_b, [5, 3, 4]) + self.assertEqual(self.simplex._vector_c, [2, 4, 1, 0, 0, 0, 0]) + self.assertEqual(self.simplex._obj_constant_index, 6) + self.assertEqual(self.simplex._b_array, [3, 4, 5]) + self.assertEqual(self.simplex._n_array, [0, 1, 2]) + + # Attempt to fully solve simplex. + self.simplex.display_tableau() + results = self.simplex.solve() + + # Test values after solving. + self.assertEqual(self.simplex._matrix_a, [ + [-1, 0, 1, 1, 0, -1], + [6, 0, 0, -2, 1, 3], + [2, 1, 0, 0, 0, 1], + ]) + self.assertEqual(self.simplex._vector_b, [1, 5, 4]) + self.assertEqual(self.simplex._vector_c, [-5, 0, 0, -1, 0, -3, -17]) + self.assertEqual(self.simplex._obj_constant_index, 6) + self.assertEqual(self.simplex._b_array, [2, 4, 1]) + self.assertEqual(self.simplex._n_array, [0, 3, 5]) + + # Test solution results. + self.assertEqual(results[0], 17) + self.assertEqual(results[1], [0, 4, 1, 0, 5, 0])