def check__LA_connectivity():
    graph, demand, node = load_LA()
    print np.min(graph[:, 1:3])
    print np.max(graph[:, 1:3])
    print np.min(demand[:, :2])
    print np.max(demand[:, :2])
    # od = construct_od(demand)
    # g = construct_igraph(graph)
    f = np.zeros((graph.shape[0], ))
    print average_cost_all_or_nothing(f, graph, demand)
def check__LA_connectivity():
    graph, demand, node = load_LA()
    print np.min(graph[:,1:3])
    print np.max(graph[:,1:3])
    print np.min(demand[:,:2])
    print np.max(demand[:,:2])
    od = construct_od(demand)
    g = construct_igraph(graph)
    f = np.zeros((graph.shape[0],))
    print average_cost_all_or_nothing(f, graph, demand)
 def test_average_cost_all_or_nothing(self):
     net = np.loadtxt('data/braess_net.csv', delimiter=',', skiprows=1)
     flow = np.array([.5,.5,.0,.5,.5])
     demand = np.loadtxt('data/braess_od.csv', delimiter=',', skiprows=1)
     demand=np.reshape(demand, (1,3))
     c = average_cost_all_or_nothing(flow, net, demand)
     self.assertTrue(abs(c - 1.0) < 1e-8)
 def test_average_cost_all_or_nothing(self):
     net = np.loadtxt('data/braess_net.csv', delimiter=',', skiprows=1)
     flow = np.array([.5, .5, .0, .5, .5])
     demand = np.loadtxt('data/braess_od.csv', delimiter=',', skiprows=1)
     demand = np.reshape(demand, (1, 3))
     c = average_cost_all_or_nothing(flow, net, demand)
     self.assertTrue(abs(c - 1.0) < 1e-8)
def compute_metrics_beta(alpha, beta, f, net, d, feat, subset, out, row, fs=None, net2=None, \
    length_unit='Mile', time_unit='Minute'):
    '''
    Save in the numpy array 'out' at the specific 'row' the following metrics
    - average cost for non-routed
    - average cost for routed
    - average cost
    - average cost on a subset (e.g. local routes)
    - average cost outside of a subset (e.g. non-local routes)
    - total gas emissions
    - total gas emissions on a subset (e.g. local routes)
    - total gas emissions outside of a subset (e.g. non-local routes)
    - total flow in the network
    - total flow in the network on a subset (e.g. local routes)
    - total flow in the network outside of a subset (e.g. non-local routes)
    '''
    if length_unit == 'Meter':
        lengths = feat[:, 1] / 1609.34  # convert into miles
    elif length_unit == 'Mile':
        lengths = feat[:, 1]
    if time_unit == 'Minute':
        a = 60.0
    elif time_unit == 'Second':
        a = 3600.
    b = 60. / a
    speed = a * np.divide(lengths, np.maximum(cost(f, net), 10e-8))
    co2 = np.multiply(gas_emission(speed), lengths)
    out[row, 0] = alpha
    out[row, 1] = beta
    out[row, 4] = b * average_cost(f, net, d)
    out[row, 5] = b * average_cost_subset(f, net, d, subset)
    out[row, 6] = out[row, 3] - out[row, 4]
    out[row, 7] = co2.dot(f) / f.dot(lengths)
    out[row, 8] = np.multiply(co2, subset).dot(f) / f.dot(lengths)
    out[row, 9] = out[row, 6] - out[row, 7]
    out[row, 10] = np.sum(np.multiply(f, lengths)) * 4000.
    out[row, 11] = np.sum(np.multiply(np.multiply(f, lengths), subset)) * 4000.
    out[row, 12] = out[row, 9] - out[row, 10]
    if alpha == 0.0:
        out[row, 2] = b * average_cost(f, net, d)
        out[row, 3] = b * average_cost_all_or_nothing(f, net, d)
        return
    if alpha == 1.0:
        L = all_or_nothing_assignment(cost(f, net2), net, d)
        out[row, 2] = b * cost(f, net).dot(L) / np.sum(d[:, 2])
        out[row, 3] = b * average_cost(f, net, d)
        return
    out[row, 2] = b * cost(f, net).dot(fs[:, 0]) / np.sum(
        (1 - alpha) * d[:, 2])
    out[row, 3] = b * cost(f, net).dot(fs[:, 1]) / np.sum(alpha * d[:, 2])
def compute_metrics_beta(alpha, beta, f, net, d, feat, subset, out, row, fs=None, net2=None, \
    length_unit='Mile', time_unit='Minute'):
    '''
    Save in the numpy array 'out' at the specific 'row' the following metrics
    - average cost for non-routed
    - average cost for routed
    - average cost
    - average cost on a subset (e.g. local routes)
    - average cost outside of a subset (e.g. non-local routes)
    - total gas emissions
    - total gas emissions on a subset (e.g. local routes)
    - total gas emissions outside of a subset (e.g. non-local routes)
    - total flow in the network
    - total flow in the network on a subset (e.g. local routes)
    - total flow in the network outside of a subset (e.g. non-local routes)
    '''
    if length_unit == 'Meter':
        lengths = feat[:,1] / 1609.34 # convert into miles
    elif length_unit == 'Mile':
        lengths = feat[:,1]
    if time_unit == 'Minute':
        a = 60.0
    elif time_unit == 'Second':
        a = 3600.
    b = 60./a
    speed = a * np.divide(lengths, np.maximum(cost(f, net), 10e-8))
    co2 = np.multiply(gas_emission(speed), lengths)
    out[row,0] = alpha
    out[row,1] = beta
    out[row,4] = b * average_cost(f, net, d)
    out[row,5] = b * average_cost_subset(f, net, d, subset)
    out[row,6] = out[row,3] - out[row,4]
    out[row,7] = co2.dot(f) / f.dot(lengths)
    out[row,8] = np.multiply(co2, subset).dot(f) / f.dot(lengths)
    out[row,9] = out[row,6] - out[row,7]
    out[row,10] = np.sum(np.multiply(f, lengths)) * 4000.
    out[row,11] = np.sum(np.multiply(np.multiply(f, lengths), subset)) * 4000.
    out[row,12] = out[row,9] - out[row,10]
    if alpha == 0.0:
        out[row,2] = b * average_cost(f, net, d)
        out[row,3] = b * average_cost_all_or_nothing(f, net, d)
        return
    if alpha == 1.0:
        L = all_or_nothing_assignment(cost(f, net2), net, d)
        out[row,2] = b * cost(f, net).dot(L) / np.sum(d[:,2])
        out[row,3] = b * average_cost(f, net, d)
        return
    out[row,2] = b * cost(f, net).dot(fs[:,0]) / np.sum((1-alpha)*d[:,2])
    out[row,3] = b * cost(f, net).dot(fs[:,1]) / np.sum(alpha*d[:,2])