def LA_od_costs(factors, output, verbose=0):
    '''
    compute the OD costs for UE, SO, and UE-K
    where the cognitive cost is K=3000
    and with different demand: alpha * demand for demand in factors
    save OD costs into csv array with columns
    demand, X1_so, X1_ue_k, X1_ue, X2_so, X2_ue_k, X2_ue, ...
    '''
    net, demand, node, geom = load_LA_3()
    demand[:, 2] = demand[:, 2] / 4000.
    fs_so = np.loadtxt('data/LA/so_single_class.csv',
                       delimiter=',',
                       skiprows=1)
    fs_ue_k = np.loadtxt('data/LA/ue_k_single_class.csv',
                         delimiter=',',
                         skiprows=1)
    fs_ue = np.loadtxt('data/LA/ue_single_class.csv',
                       delimiter=',',
                       skiprows=1)
    costs = []
    for i in range(len(factors)):
        costs.append(cost(fs_so[:, i], net))
        costs.append(cost(fs_ue_k[:, i], net))
        costs.append(cost(fs_ue[:, i], net))
    free_flow_OD_costs(net, costs, demand, output, verbose)
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])
예제 #4
0
def frank_wolfe_on_I210():
    '''
    Frank-Wolfe on I210
    '''
    graph = np.loadtxt('data/I210_attack_net.csv', delimiter=',', skiprows=1)
    demand = np.loadtxt('data/I210_od.csv', delimiter=',', skiprows=1)
    demand[:, 2] = 1. * demand[:, 2] / 4000
    # run solver
    f = solver_3(graph, demand, max_iter=1000, q=50, display=1, stop=1e-2)
    # display cost
    for a, b in zip(cost(f, graph), f * 4000):
        print a, b
    # visualization
    node = np.loadtxt('data/I210_node.csv', delimiter=',', skiprows=1)
    # extract features: 'capacity', 'length', 'fftt'
    feat = extract_features('data/I210_attack_Sketch_net.csv')
    ratio = cost_ratio(f, graph)
    # merge features with the cost ratios
    features = np.zeros((feat.shape[0], 4))
    features[:, :3] = feat
    features[:, 3] = ratio
    # join features with (lat1,lon1,lat2,lon2)
    links = process_links(graph, node, features)
    color = features[:, 3]  # we choose the costs
    names = ['capacity', 'length', 'fftt', 'tt_over_fftt']
    geojson_link(links, names, color)
def frank_wolfe_on_I210():
    '''
    Frank-Wolfe on I210
    '''    
    graph = np.loadtxt('data/I210_attack_net.csv', delimiter=',', skiprows=1)
    demand = np.loadtxt('data/I210_od.csv', delimiter=',', skiprows=1)
    demand[:,2] = 1. * demand[:,2] / 4000 
    # run solver
    f = solver_3(graph, demand, max_iter=1000, q=50, display=1, stop=1e-2)
    # display cost
    for a,b in zip(cost(f, graph), f*4000): print a,b
    # visualization
    node = np.loadtxt('data/I210_node.csv', delimiter=',', skiprows=1)
    # extract features: 'capacity', 'length', 'fftt'
    feat = extract_features('data/I210_attack_Sketch_net.csv')
    ratio = cost_ratio(f, graph)
    # merge features with the cost ratios
    features = np.zeros((feat.shape[0],4))
    features[:,:3] = feat
    features[:,3] = ratio
    # join features with (lat1,lon1,lat2,lon2)
    links = process_links(graph, node, features)
    color = features[:,3] # we choose the costs
    names = ['capacity', 'length', 'fftt', 'tt_over_fftt']
    geojson_link(links, names, color)
def LA_od_costs(factors, output, verbose=0):
    '''
    compute the OD costs for UE, SO, and UE-K 
    where the cognitive cost is K=3000
    and with different demand: alpha * demand for demand in factors
    save OD costs into csv array with columns
    demand, X1_so, X1_ue_k, X1_ue, X2_so, X2_ue_k, X2_ue, ...
    '''
    net, demand, node, geom = load_LA_3()
    demand[:,2] = demand[:,2] / 4000.
    fs_so = np.loadtxt('data/LA/so_single_class.csv', delimiter=',', skiprows=1)
    fs_ue_k = np.loadtxt('data/LA/ue_k_single_class.csv', delimiter=',', skiprows=1)
    fs_ue = np.loadtxt('data/LA/ue_single_class.csv', delimiter=',', skiprows=1)
    costs = []
    for i in range(len(factors)):
        costs.append(cost(fs_so[:,i],net))
        costs.append(cost(fs_ue_k[:,i],net))
        costs.append(cost(fs_ue[:,i],net))
    free_flow_OD_costs(net, costs, demand, output, verbose)
def check_LA_result():
    net, demand, node, features = load_LA_2()
    demand[:, 2] = demand[:, 2] / 4000.
    f = np.loadtxt('data/LA/LA_output_4.csv', delimiter=',', skiprows=0)
    costs = cost(f, net)
    cr = cost_ratio(f, net)
    print np.sort(cr)[-20:]
    for row in range(net.shape[0]):
        if cr[row] >= 10.:
            print cr[row]
            print net[row, :3], features[row, :]
    L = all_or_nothing_assignment(costs, net, demand)
    print costs.dot(L) / np.sum(demand[:, 2])
def check_LA_result():
    net, demand, node, features = load_LA_2()
    demand[:,2] = demand[:,2] / 4000.
    f = np.loadtxt('data/LA/LA_output_4.csv', delimiter=',', skiprows=0)
    costs = cost(f, net)
    cr = cost_ratio(f, net)
    print np.sort(cr)[-20:]
    for row in range(net.shape[0]):
        if cr[row] >= 10.: 
            print cr[row]
            print net[row,:3], features[row,:]
    L = all_or_nothing_assignment(costs, net, demand)
    print costs.dot(L) / np.sum(demand[:,2])
 def test_cost(self):
     net = np.loadtxt('data/braess_net.csv', delimiter=',', skiprows=1)
     net[:,5] = np.array([2.]*5)
     flow = np.array([0., 1., 2., 3., 4.])
     result = np.array([0., 3., 8., 19., 36.])
     self.assertTrue(np.linalg.norm(result - cost(flow, net)) < 1e-8)
예제 #10
0
 def test_cost(self):
     net = np.loadtxt('data/braess_net.csv', delimiter=',', skiprows=1)
     net[:, 5] = np.array([2.] * 5)
     flow = np.array([0., 1., 2., 3., 4.])
     result = np.array([0., 3., 8., 19., 36.])
     self.assertTrue(np.linalg.norm(result - cost(flow, net)) < 1e-8)