diff --git a/main.py b/main.py index 87f71803d27644c47b0a2194ba00a0b938bbb500..60ef14724e0944db27a753f2889f3654496d9755 100644 --- a/main.py +++ b/main.py @@ -9,22 +9,102 @@ Author: Brandon Rodriguez """ # System Imports. +import json # User Class Imports. from resources import logging as init_logging +from resources.graph_library import NetworkFlowGraph # Initialize Logger. logger = init_logging.get_logger(__name__) -def test(): - logger.info('Test function.') +def create_network_graph(): + """ + Creates new instance of network problem, using custom "Graph Library" Network Flow class. + :return: A "Network Flow Graph" class instance of the problem to solve. + """ + # Create new graph. + graph = NetworkFlowGraph() + + # Read in JSON data. We assume data is valid and correct, so we don't validate. + # Open file. + with open('./resources/json_files/network_flow_values.json') as json_file: + # Parse JSON data into Python format. + json_data = json.load(json_file) + + logger.info('JSON Data: {0}'.format(json_data)) + + # Create nodes. + for node_name in json_data['connections'].keys(): + graph.nodes.create(node_name) + + # Node "t" has no outgoing connections so it wouldn't be detected. Create separately. + graph.nodes.create('t') + + # Manually set y coordinates so graph is consistent. + top_row = ['A', 'D'] + mid_row = ['s', 'B', 'E', 't'] + bot_row = ['C', 'F'] + for node in graph.nodes.all().values(): + # Set coord based on row. + if node.get_name() in top_row: + node.set_y_coord(90) + elif node.get_name() in mid_row: + node.set_y_coord(50) + elif node.get_name() in bot_row: + node.set_y_coord(10) + + # Manually set x coordinates to give nodes more breathing room. + mid_left = ['A', 'B', 'C'] + mid_right = ['D', 'E', 'F'] + for node in graph.nodes.all().values(): + # Set coord based on col. + if node.get_name() in mid_left: + node.set_x_coord(35) + elif node.get_name() in mid_right: + node.set_x_coord(65) + + # Create edge connections. + for tail_node_name, connections in json_data['connections'].items(): + for head_node_name, capacity in connections.items(): + + # Get nodes to connect. + tail_node = graph.nodes.get(tail_node_name) + head_node = graph.nodes.get(head_node_name) + + # Create connection. + new_edge = graph.nodes.connect(tail_node, head_node) + + # Set connection capacity. + new_edge.set_capacity(capacity) + + # Display newly created graph. + graph.display.draw_graph_map(title='Network Flow Initial Problem') + + # Return newly created graph object. + return graph + + +def ford_fulkerson(): + logger.info('Solving problem with "Ford Fulkerson" algorithm method.') + + # Create new instance of problem graph. + graph = create_network_graph() + + +def simplex(): + logger.info('Solving problem with "Simplex" algorithm method.') + + # Create new instance of problem graph. + graph = create_network_graph() if __name__ == '__main__': logger.info('Starting program.') - test() + ford_fulkerson() + simplex() logger.info('Terminating program.')