From bb69ad4b6ef2e7077591b0a8bf22ac18b62e8ba1 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Tue, 26 Nov 2019 23:02:44 -0500 Subject: [PATCH] Partial implementation of "Simplex" algorithm function --- resources/simplex/base.py | 2 +- resources/simplex/run_simplex.py | 97 ++++++++++++++++++++++++++ tests/resources/simplex/base.py | 2 +- tests/resources/simplex/run_simplex.py | 24 +++++++ 4 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 resources/simplex/run_simplex.py create mode 100644 tests/resources/simplex/run_simplex.py diff --git a/resources/simplex/base.py b/resources/simplex/base.py index 130c06d..7c101f1 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 0000000..145d0e7 --- /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 9c23829..b8549d7 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 0000000..3b5f8a4 --- /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 -- GitLab