diff --git a/resources/simplex/base.py b/resources/simplex/base.py index 130c06d9c9ab8fcb135bd9e42d786114fbd00354..7c101f104127d71fbe2bd12a0e98911140f5f7bd 100644 --- a/resources/simplex/base.py +++ b/resources/simplex/base.py @@ -5,7 +5,7 @@ Assignment: Linear Programming Simplex Algorithm Author: Brandon Rodriguez -Implementation of the "Simplex" for Linear Programming problems. +Implementation of base logic for "Simplex" to solve Linear Programming problems. General Terminology to Remember: diff --git a/resources/simplex/run_simplex.py b/resources/simplex/run_simplex.py new file mode 100644 index 0000000000000000000000000000000000000000..145d0e792311c3875be1e1d5250d6a11bf354f87 --- /dev/null +++ b/resources/simplex/run_simplex.py @@ -0,0 +1,97 @@ +""" +Date: 11-22-19 +Class: CS5310 +Assignment: Linear Programming Simplex Algorithm +Author: Brandon Rodriguez + + +Implementation of the "Simplex" function for Linear Programming Simplex problems. +""" + +# System Imports. +import copy + +# User Class Imports. +from resources import logging as init_logging + + +# Initialize Logger. +logger = init_logging.get_logger(__name__) + + +class RunSimplex(): + def __init__(self, parent, *args, **kwargs): + # Get calling parent. We use this to pull parent data on __call__. + self._parent = parent + + # Parent data values to set later. + self._matrix_a = None + self._vector_b = None + self._vector_c = None + self._obj_constant_index = None + self._basic_var_indexes = None + self._nonbasic_var_indexes = None + + def __call__(self, old_basic_col, new_basic_col, *args, **kwargs): + # Run pivot function. + return self._simplex() + + def _simplex(self): + # Run "Initialize-Simplex" function before we do any work. + self._parent.initialize() + + # Get parent data, after initialized. + matrix_a = self._parent._matrix_a + vector_b = self._parent._vector_b + vector_c = self._parent._vector_c + obj_constant_index = self._parent._obj_constant_index + basic_var_indexes = self._parent._b_array + nonbasic_var_indexes = self._parent._n_array + + # Create loop to continually improve simplex until optimized. + still_optimizable = True + while still_optimizable: + still_optimizable = False + highest_obj_index = -1 + # Check that we actually can still optimize values. + for col_index in range(len(nonbasic_var_indexes)): + # Check if value is greater than 0. + if vector_c[col_index] > 0: + still_optimizable = True + # Check if current index is larger than previously found one. + if highest_obj_index == -1 or vector_c[col_index] > vector_c[highest_obj_index]: + highest_obj_index = col_index + + # Check that we found something to optimize. + if still_optimizable: + self._optimize_on_index(highest_obj_index) + + def _optimize_on_index(self, col_index): + + # Get parent data, after initialized. + matrix_a = self._parent._matrix_a + vector_b = self._parent._vector_b + vector_c = self._parent._vector_c + obj_constant_index = self._parent._obj_constant_index + basic_var_indexes = self._parent._b_array + nonbasic_var_indexes = self._parent._n_array + + # Loop through all rows, finding best one to pivot on. + delta = [] + for row_index in range(len(matrix_a)): + # Check that we can divide. + if matrix_a[row_index][col_index] > 0: + delta.append(vector_b[col_index] / matrix_a[row_index][col_index]) + else: + # Less than 1. Can't divide. + delta.append(-1) + + # Now check to see which row we actually use. + smallest_valid_index = -1 + for row_index in range(len(delta)): + if delta[row_index] > 0: + # Check if current index is smaller than previous one. + if smallest_valid_index == -1 or delta[row_index] < delta[smallest_valid_index]: + smallest_valid_index = row_index + + # Now pivot on our found values. diff --git a/tests/resources/simplex/base.py b/tests/resources/simplex/base.py index 9c238290d68820b260882a96f00ee27e464b1e7d..b8549d76ff28188782c1001f94479f7bad85f612 100644 --- a/tests/resources/simplex/base.py +++ b/tests/resources/simplex/base.py @@ -5,7 +5,7 @@ Assignment: Linear Programming Simplex Algorithm Author: Brandon Rodriguez -Tests for implementation of the "Simplex" algorithm. +Tests for base logic of the "Simplex" algorithm. """ # System Imports. diff --git a/tests/resources/simplex/run_simplex.py b/tests/resources/simplex/run_simplex.py new file mode 100644 index 0000000000000000000000000000000000000000..3b5f8a4f94814ccd13cdbfde885ca53d56f5047a --- /dev/null +++ b/tests/resources/simplex/run_simplex.py @@ -0,0 +1,24 @@ +""" +Date: 11-22-19 +Class: CS5310 +Assignment: Linear Programming Simplex Algorithm +Author: Brandon Rodriguez + + +Tests for "Simplex" function implementation of the Simplex algorithm. +""" + +# System Imports. +import unittest + +# User Class Imports. +from resources.simplex.base import SimplexBase + + +class TestPivot(unittest.TestCase): + def setUp(self): + self.simplex = SimplexBase() + + def test__run_simplex(self): + # Setup initial simplex. + pass