Skip to content
Snippets Groups Projects
Commit d746aacd authored by Brandon Rodriguez's avatar Brandon Rodriguez
Browse files

Implement partially working simplex

parent 3508e19f
Branches
No related merge requests found
......@@ -14,6 +14,7 @@ import json
# User Class Imports.
from resources import logging as init_logging
from resources.graph_library import NetworkFlowGraph
from resources.simplex import Simplex
# Initialize Logger.
......@@ -241,6 +242,97 @@ def simplex():
# Create new instance of problem graph.
graph = create_network_graph()
# Start by setting all edge weights to 1, to help with simplex initializing logic.
for edge in graph.edges.all().values():
edge.set_weight(1)
constraints = []
constants = []
objective = []
# Get basic data node and edge structures, so we know it doesn't possibly change at all during iterations.
all_graph_nodes = list(graph.nodes.all().values())
all_graph_edges = list(graph.edges.all().values())
# Set objective function.
for index in range(len(all_graph_edges)):
objective.append(0)
source_node = graph.nodes.get('s')
for edge_connection in source_node.get_edges().values():
edge_index = all_graph_edges.index(edge_connection)
objective[edge_index] = 1
# Set max flow capacity constraints for each edge.
for edge_index in range(len(all_graph_edges)):
# Populate constraint row.
constraint_row = []
for index in range(len(all_graph_edges)):
constraint_row.append(0)
# Set edge variable value to 1.
constraint_row[edge_index] = 1
constraints.append(constraint_row)
# Set constant values. This is the flow capacity itself.
edge = all_graph_edges[edge_index]
constants.append(edge.get_capacity())
# Update all nodes to state that "edge flow coming in must be less or equal to edge flow going out."
for node in all_graph_nodes:
# Check values of all incoming and outgoing edges for node.
incoming_edges = node.get_incoming_edges()
outgoing_edges = node.get_outgoing_edges()
# Populate constraint row.
constraint_row = []
for index in range(len(all_graph_edges)):
constraint_row.append(0)
# Set constraint row values.
for incoming_edge in incoming_edges:
edge_index = all_graph_edges.index(incoming_edge)
constraint_row[edge_index] = incoming_edge.get_weight()
constraints.append(constraint_row)
# Set constant values.
total_capacity_out = 0
for outgoing_edge in outgoing_edges:
total_capacity_out += outgoing_edge.get_capacity()
constants.append(total_capacity_out)
logger.info('obj: {0}'.format(objective))
logger.info('constraints: {0}'.format(constraints))
# # Loop through one last time and create basic variable columns.
# for edge_index in range(len(all_graph_edges)):
# for row_index in range(len(constraints)):
# # Check if edge and row index are the same.
# if edge_index == row_index:
# # Set as basic var value.
# constraints[row_index].append(1)
# else:
# # Set as not basic var value.
# constraints[row_index].append(0)
#
# objective.append(0)
# Pass values into simplex and attempt to solve.
simplex = Simplex()
simplex.set_simplex_values(constraints, constants, objective)
simplex.display_tableau()
simplex.solve()
simplex.display_tableau()
# Now update graph so we can more visually see the found solution.
constants = simplex._vector_b
for edge_index in range(len(all_graph_edges)):
edge = all_graph_edges[edge_index]
edge.set_weight(constants[edge_index])
# Display graph.
graph.display.draw_graph_map(title='Network Flow Solution<Br>Simplex Version')
if __name__ == '__main__':
logger.info('Starting program.')
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment