Advertisement
_who___

max?

Mar 1st, 2024
562
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. import numpy as np
  2. from scipy.optimize import linprog
  3. import matplotlib.pyplot as plt
  4. import networkx as nx
  5.  
  6. # Define the directed graph representing the network flow
  7. G = nx.DiGraph()
  8. G.add_nodes_from(["S", "A", "B", "C", "D", "T"])
  9. edges = [("S", "A", 3), ("S", "B", 2), ("A", "C", 3), ("A", "D", 2),
  10.          ("B", "C", 1), ("B", "D", 1), ("C", "T", 3), ("D", "T", 2)]
  11. for u, v, w in edges:
  12.     G.add_edge(u, v, capacity=w)
  13.  
  14. # Visualize the original graph
  15. plt.figure()
  16. pos = nx.circular_layout(G)
  17. nx.draw(G, pos, with_labels=True, node_size=1000, node_color='skyblue')
  18. nx.draw_networkx_edge_labels(G, pos, edge_labels={(u, v): str(d['capacity']) for u, v, d in G.edges(data=True)})
  19. plt.title("Original Network Flow Graph")
  20. plt.show()
  21.  
  22. # Linear programming problem setup
  23. num_edges = len(G.edges)
  24. num_nodes = len(G.nodes)
  25. c = np.zeros(num_edges)  # Initialize cost function
  26. bounds = [(0, G[u][v]['capacity']) for u, v in G.edges]  # Flow bounds are from 0 to edge capacity
  27.  
  28. node_indices = {node: i for i, node in enumerate(G.nodes)}
  29. A_ub = np.zeros((num_nodes - 2, num_edges))  # One row for each node except source and sink
  30. b_ub = np.zeros(num_nodes - 2)
  31.  
  32. # Set up the optimization problem
  33. for i, ((u, v), w) in enumerate(G.edges.items()):
  34.     if u == 'S':
  35.         c[i] = -1  # Maximize flow from the source
  36.     elif v == 'T':
  37.         c[i] = 1  # Minimize flow to the sink
  38.  
  39. # Flow conservation constraints for nodes except source and sink
  40. for i, node in enumerate(node for node in G.nodes if node not in ('S', 'T')):
  41.     for j, (u, v) in enumerate(G.edges()):
  42.         if v == node:
  43.             A_ub[i, j] = -1  # Outgoing flow decreases
  44.         if u == node:
  45.             A_ub[i, j] = 1   # Incoming flow increases
  46.     b_ub[i] = 0  # Flow conservation for nodes except source and sink
  47.  
  48. # Solve the linear programming problem
  49. result = linprog(c, A_ub=A_ub, b_ub=b_ub, bounds=bounds, method='highs')
  50. max_flow = -result.fun  # Negate because we minimized the negative flow
  51.  
  52. # Update graph with flow values
  53. flow_values = result.x
  54. for i, (u, v) in enumerate(G.edges()):
  55.     G[u][v]['flow'] = flow_values[i]
  56.  
  57. # Visualize the solution graph with flow values
  58. plt.figure()
  59. plt.title("Optimal Flow in the Network")
  60. nx.draw(G, pos, with_labels=True, node_size=1000, node_color='lightgreen')
  61. nx.draw_networkx_edge_labels(G, pos, edge_labels={(u, v): f"{G[u][v]['flow']}/{G[u][v]['capacity']}" for u, v in G.edges()})
  62. plt.show()
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement