-
Notifications
You must be signed in to change notification settings - Fork 0
/
poop2.py
75 lines (59 loc) · 2.31 KB
/
poop2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import pandas as pd
import networkx as nx
from sklearn.neighbors import NearestNeighbors
import numpy as np
from tqdm import tqdm
import pickle
def load_graph(pickle_file):
with open(pickle_file, 'rb') as f:
return pickle.load(f)
def bfs_violations(G, start_node, max_distance=500):
visited = set()
queue = [(start_node, 0)]
violations = 0
while queue:
node, distance = queue.pop(0)
if node not in visited and distance <= max_distance:
visited.add(node)
violations += 1
for neighbor in G.neighbors(node):
if neighbor not in visited:
new_distance = distance + G[node][neighbor]['weight']
queue.append((neighbor, new_distance))
return violations
def dijkstra_violations(G, start_node, max_distance=500):
distances = {node: float('infinity') for node in G.nodes()}
distances[start_node] = 0
violations = 0
pq = [(0, start_node)]
while pq:
current_distance, current_node = min(pq)
pq.remove((current_distance, current_node))
if current_distance > max_distance:
break
violations += 1
for neighbor in G.neighbors(current_node):
distance = current_distance + G[current_node][neighbor]['weight']
if distance < distances[neighbor]:
distances[neighbor] = distance
pq.append((distance, neighbor))
return violations
def find_nearest_node(G, lat, lon):
coords = np.array([data['pos'] for _, data in G.nodes(data=True)])
nbrs = NearestNeighbors(n_neighbors=1, algorithm='ball_tree').fit(coords)
_, indices = nbrs.kneighbors([[lat, lon]])
nearest_node = list(G.nodes())[indices[0][0]]
return nearest_node
# Usage example
if __name__ == "__main__":
pickle_file = 'parking_violations_graph.pickle'
# Load the graph from pickle file
G = load_graph(pickle_file)
# Example coordinates
lat, lon = 38.910, -77.002
# Find the nearest node
start_node = find_nearest_node(G, lat, lon)
print(f"Nearest node to ({lat}, {lon}): {start_node}")
# Use the nearest node for BFS and Dijkstra's
print(f"Violations within 500m (BFS): {bfs_violations(G, start_node)}")
print(f"Violations within 500m (Dijkstra's): {dijkstra_violations(G, start_node)}")