diff --git a/MaxFlowUtils.py b/MaxFlowUtils.py index 7b41c63..04a9a51 100644 --- a/MaxFlowUtils.py +++ b/MaxFlowUtils.py @@ -1,22 +1,25 @@ import math, collections from MaxFlowLib import FlowEdge -import matplotlib.pyplot as plt +# import matplotlib.pyplot as plt import numpy as np import copy _DISTANCE = .4 # km # vectorized implementation def distance(v, stations): - '''find distance from v to all other vertices''' - R = 6371 - dLat_dLong = np.radians(stations[:, [0, 1]] - stations[v, [0, 1]]) - dLat = dLat_dLong[:, 0] - dLong = dLat_dLong[:, 1] - lat = np.radians(stations[:, 0]) # col vector of lat values for all stations; num_stations x 1 - lat_v = np.radians(stations[v][0]) # scalar for this station - - dLatH = np.divide(dLat, 2) - dLongH = np.divide(dLong, 2) + ''' + find distance from v to all other vertices + input: stations array of [(lat, long), (lat, long) ...] + output: distance array of [dist, dist, ... ] + ''' + stations = np.radians(stations) + R = 6371 # ? + dLat_dLong = stations - stations[v] + dLatH = dLat_dLong[:, 0] / 2 + dLongH = dLat_dLong[:, 1] / 2 + lat = stations[:, 0] # col vector of lat values for all stations; num_stations x 1 + lat_v = stations[v][0] # scalar for this station + a = np.sin(dLatH) ** 2 + np.sin(dLongH) ** 2 * np.cos(lat) * np.cos(lat_v) c = np.arctan2(np.sqrt(a), np.sqrt(1-a)) * 2 d = c * R @@ -32,8 +35,9 @@ def findFlowEdges(stations, totalDocks, source, capacity_func): ''' flow_edges = collections.deque() num_stations = len(stations) - marked_v = np.ones((num_stations,), dtype=bool) # marks all from vertices - marked_w = np.ones((num_stations,), dtype=bool) # marks all to vertices + marked_v = np.ones(num_stations, dtype=bool) # marks all from vertices + marked_w = np.ones(num_stations, dtype=bool) # marks all to vertices + # this feels like there should be an object q = collections.deque() q.append(source) diff --git a/NetworkFlowAnalyzer.py b/NetworkFlowAnalyzer.py index e30a2e6..0beedd7 100644 --- a/NetworkFlowAnalyzer.py +++ b/NetworkFlowAnalyzer.py @@ -11,7 +11,7 @@ ''' import json import argparse -import matplotlib.pyplot as plt +# import matplotlib.pyplot as plt import numpy as np import random import MaxFlowUtils @@ -34,22 +34,20 @@ input = json.load(f) num_stations = len(input["stationBeanList"]) -stations = np.random.random((num_stations, 2)) # 2 cols for latitude, longitude +stations = np.zeros(shape=(num_stations, 2)) # 2 cols for latitude, longitude #totalDocks = np.zeros((num_stations,), dtype=np.uint8) totalDocks = np.zeros((num_stations,), dtype=np.int16) names = {} # maps vertex number to stationNam vertex = {} # maps stationName to vertex number # store data -row = 0 -for station in input["stationBeanList"]: +for row, station in enumerate(input["stationBeanList"]): # store data in numpy arrays totalDocks[row] = station["totalDocks"] stations[row][0] = station["latitude"] stations[row][1] = station["longitude"] vertex[station["stationName"]] = row names[row] = station["stationName"] - row += 1 # process optional args if args.stations: @@ -78,15 +76,15 @@ # calculate maxflow using Ford-Fulkerson algorithm maxflow = FF(flownet, source, target) # plot flow network - MaxFlowUtils.plotFlowNetwork(stations, source, target, flow_edges) - plt.show() + # MaxFlowUtils.plotFlowNetwork(stations, source, target, flow_edges) + # plt.show() # flow_edges.append(FlowEdge(329, 330, totalDocks[329] + totalDocks[330])) # plot flow flowpath = MaxFlowUtils.flowPath(stations, flownet, source) flowpath2 = copy.copy(flowpath) - MaxFlowUtils.plotFlow(stations, source, target, flowpath) - plt.show() + # MaxFlowUtils.plotFlow(stations, source, target, flowpath) + # plt.show() # convert vertices in flow path to station names station_path = MaxFlowUtils.toStationNames(flowpath2, names) @@ -105,8 +103,8 @@ print e # plot edges in st-cut - MaxFlowUtils.plotSTcut(stations, source, target, flowpath, stcut_v) - plt.show() + # MaxFlowUtils.plotSTcut(stations, source, target, flowpath, stcut_v) + # plt.show() else: print '{} and {} are not connected.'.format(start_station, end_station) diff --git a/README b/README index 96dc9f3..aa6ade4 100644 --- a/README +++ b/README @@ -10,7 +10,7 @@ Capacity on an edge is the max of the bike docks at each edge vertex. usage: run NetworkFlowAnalyzer.py "citybike.json" Example: -run NetworkFlowAnalyzer.py "citybike.json" "Broadway & Battery Pl" "E 47 St & 1 Ave" +run NetworkFlowAnalyzer.py --start_station "Broadway & Battery Pl" --end_station "E 47 St & 1 Ave" "citybike.json" run NetworkFlowAnalyzer.py [-h] [--stations] [--start_station START_STATION] [--end_station END_STATION] filename-json default start_station: "Broadway & Battery Pl"