diff --git a/main.py b/main.py index 8bbd1e8817f49667311fdf0086e7df45b676f350..c80478728ef2fc2526f66245a95e30e326cae74c 100644 --- a/main.py +++ b/main.py @@ -19,7 +19,32 @@ from resources.simplex.base import SimplexBase logger = init_logging.get_logger(__name__) -def test_simplex(): +def solve_from_file(file_location): + """ + Solves a simplex problem read in from file. + """ + simplex = SimplexBase() + read_in_result = simplex.read_data_from_json(file_location) + + if read_in_result: + # Read in was successful. Continue to solve. + logger.info('Read in simplex from "{0}".'.format(file_location)) + simplex.display_all_printouts() + logger.info('Attempting to solve problem.') + solution = simplex.solve() + logger.info('Problem has been solved.') + simplex.display_all_printouts() + simplex.print_solution() + logger.info('') + else: + # Read in failed. + logger.info('Failed to read in file at "{0}". Make sure the location is correct and the JSON file follows ' + 'format provided by files in "<project_root>/resources/json_files/".'.format(file_location)) + +def run_simplex_examples(): + """ + Examples to run for proof of working. + """ simplex = SimplexBase() # Read and display ex_1.json. @@ -45,12 +70,27 @@ def test_simplex(): simplex.solve() logger.info('Problem has been solved.') simplex.display_all_printouts() + simplex.print_solution() logger.info('') if __name__ == '__main__': logger.info('Starting program.') - test_simplex() + # Prompt user for input. + logger.info('Choose an option:') + logger.info(' 1) Choose an JSON file to read in from.') + logger.info(' 2) Run with example JSON files.') + logger.info(' Any other input to exit.') + user_input = str(input()).strip() + + # Handle based on user input. + if user_input == '1': + logger.info('Enter JSON file location. File path can be either relative from project root or absolute.') + user_input = str(input()).strip() + logger.info('Attempting to read in and solve from "{0}".'.format(user_input)) + solve_from_file(user_input) + elif user_input == '2': + run_simplex_examples() logger.info('Terminating program.') diff --git a/readme.md b/readme.md index 1fa8ae765f5cef259cb234ad04a841cfbfbf6702..a5693c5efc1f41e7900fad2a75bbb47a9089b10d 100644 --- a/readme.md +++ b/readme.md @@ -33,10 +33,13 @@ Once created, load this environment with `source .venv/bin/activate` on Linux or Run the program via `python ./main.py` while at the `python` directory. ### Expected Output -Most of the logic is tested through UnitTesting. - -However, main has 2 examples of reading in from JSON files, then 1 example of reading in and solving. +Main has two possible outputs. + +First output is reading in and solving a file, determined by user input. Should work with both relative paths from the +project root, or absolute paths. +On both read in and solve, all three output formats should be displayed for the matrix. +Second output is has 2 examples of reading in from JSON files, then 1 example of reading in and solving. For each read in, the problem should be displayed to console in "Tableau" (aka matrix) form. After the one is solved, then it should print the solution in all three formats. diff --git a/resources/simplex/base.py b/resources/simplex/base.py index c68301c30e75c586de757e9c888db84cb9d6796a..b74a291b1ce766bff88d6aad4e039742a86f494c 100644 --- a/resources/simplex/base.py +++ b/resources/simplex/base.py @@ -105,12 +105,35 @@ class SimplexBase(): # endregion Child Class Functions + def print_solution(self): + """ + Prints solution to problem. + If "solve" function has not been run, then prints initial problem data. + """ + # Check that data is present to print. + if self._vector_b is not None and self._vector_c is not None and self._b_array is not None\ + and self._obj_constant_index is not None: + print_string = 'Solution has a max of {0} at coordinates ('.format(-self._vector_c[self._obj_constant_index]) + + # Loop through all basic variables. + for row_index in range(len(self._b_array)): + print_string += ' x{0}={1} '.format((self._b_array[row_index] + 1), self._vector_b[row_index]) + + print_string += ')' + + logger.info(print_string) + else: + logger.info('Please enter a problem, then run "solve" to get a solution.') + + + #region Simplex Read in and Setup def read_data_from_json(self, json_file_location): """ Attempts to read in data from provided file location. :param json_file_location: Location to attempt to read from. + :return: Bool indicating if file read in was successful. """ logger.info('') logger.info('Attempting to read in simplex from file.') @@ -125,11 +148,14 @@ class SimplexBase(): try: self._parse_json_data(json_data) + return True except (KeyError, TypeError, ValueError) as err: logger.error('Invalid JSON data read in. {0}'.format(err)) + return False except FileNotFoundError: logger.info('Could not locate file at location "{0}".'.format(json_file_location)) + return False def _parse_json_data(self, json_data): """