AI_Project2

.pdf

School

University of North Texas *

*We aren’t endorsed by this school

Course

5300

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

8

Uploaded by amulyam232 on coursehero.com

Link - AI Project2.ipynb - Colaboratory (google.com) R1 import networkx as nx #import networkx, download if not available G = nx.DiGraph() #code for the network graph given in tutorial 4 G.add_edges_from([ ( 0 , 1 , { 'flow' : 2 , 'capacity' : 6 }), ( 0 , 2 , { 'flow' : 3 , 'capacity' : 3 }), ( 0 , 3 , { 'flow' : 4 , 'capacity' : 5 }), ( 1 , 2 , { 'flow' : 1 , 'capacity' : 5 }), ( 1 , 5 , { 'flow' : 1 , 'capacity' : 3 }), ( 2 , 4 , { 'flow' : 2 , 'capacity' : 9 }), ( 2 , 5 , { 'flow' : 4 , 'capacity' : 8 }), ( 3 , 2 , { 'flow' : 2 , 'capacity' : 2 }), ( 3 , 4 , { 'flow' : 2 , 'capacity' : 3 }), ( 4 , 5 , { 'flow' : 4 , 'capacity' : 5 }) ]) def heuristic (G, path): # calculate heuristic value based on the tutorial3 min_capacity_diff = float ( 'inf' ) for u, v in path: edge_data = G[u][v] flow = edge_data.get( 'flow' , 0 ) capacity = edge_data.get( 'capacity' , float ( 'inf' )) #to know how much flow can be increased capacity_diff = (capacity - flow) min_capacity_diff = min (min_capacity_diff, capacity_diff) return (min_capacity_diff) def optimal_path (G, source, target): #function to find optimal path from source to sink paths = list (nx.all_simple_edge_paths(G, source =source, target =target, cutoff = 9 )) if not paths: return None max_heuristic_value = - 1 #condition when there exists no optimal flow best_path = None for path in paths: #looping to check each path based on heuristic and return the best path heuristic_value = (heuristic(G, path)) if heuristic_value > max_heuristic_value: max_heuristic_value = heuristic_value
best_path = path return best_path def update_flow (G, path): # update the flow for each path in the network after increasing the flow min_capacity = ( min (G[u][v][ 'capacity' ] - G[u][v][ 'flow' ] for u, v in path)) for u, v in path: G[u][v][ 'flow' ] += min_capacity if G.has_edge(v, u): G[v][u][ 'capacity' ] = G[v][u].get( 'capacity' , 0 ) + min_capacity def successor_function (G, source, target): #a sucessor function to call heuristic function while True : best_path = optimal_path(G, source, target) if best_path is None or heuristic(G, best_path) == 0 : #calling the heuristic funtion if there exists break update_flow(G, best_path) return G updated_G = successor_function(G, source = 0 , target = 5 ) for path in nx.all_simple_edge_paths(updated_G, source = 0 , target = 5 , cutoff = 9 ): updated_path = [(u, v, { 'flow' : updated_G[u][v][ 'flow' ], 'capacity' : updated_G[u][v][ 'capacity' ]}) for u, v in path] print (updated_path) # updated graph/ output R2 import networkx as nx import random G = nx.gnp_random_graph( 5 , 0.6 , directed = True ) # Generate a directed graph with 30 nodes nx.is_weakly_connected(G) if 'S' not in G: # Add 'S' and 'T' nodes
G.add_node( 'S' ) if 'T' not in G: G.add_node( 'T' ) if not G.has_edge( 'S' , 'T' ): # Ensure an edge between 'S' and 'T' with capacities and flows capacity = random.randint( 1 , 10 ) flow = random.randint( 1 , capacity) G.add_edge( 'S' , 'T' , capacity =capacity, flow =flow) for u, v in G.edges(): # Set capacities and flows for other edges in the graph if u != 'S' and v != 'T' and not ( 'capacity' in G[u][v] and 'flow' in G[u][v]): capacity = random.randint( 1 , 10 ) flow = random.randint( 1 , capacity) G[u][v][ 'capacity' ] = capacity G[u][v][ 'flow' ] = flow # print("Generated Graph with Capacities and Flows:") # display the generated graph with capacities and flows # for u, v in G.edges(): # print(f"Edge: {u} -> {v} | Capacity: {G[u][v]['capacity']} | Flow: {G[u][v]['flow']}") def heuristic (G, path): # heuristic value calculation function min_capacity_diff = float ( 'inf' ) for u, v in path: edge_data = G[u][v] flow = edge_data.get( 'flow' , 0 ) capacity = edge_data.get( 'capacity' , float ( 'inf' )) capacity_diff = capacity - flow min_capacity_diff = min (min_capacity_diff, capacity_diff) return min_capacity_diff def find_best_path (G, source, target): # function to find the best path based on the heuristic value paths = list (nx.all_simple_edge_paths(G, source =source, target =target, cutoff = 6 )) # Limit the cutoff value if not paths: return None max_heuristic_value = - 1 best_path = None for path in paths: heuristic_value = heuristic(G, path) if heuristic_value > max_heuristic_value: max_heuristic_value = heuristic_value best_path = path
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help