def test_all_or_nothing_assignment(self): net = np.loadtxt('data/braess_net.csv', delimiter=',', skiprows=1) cost = np.array([1.,5.,1.,5.,1.]) demand = np.loadtxt('data/braess_od.csv', delimiter=',', skiprows=1) demand=np.reshape(demand, (1,3)) L = all_or_nothing_assignment(cost, net, demand) self.assertTrue(np.linalg.norm(L - np.array([2.,0.,2.,0.,2.])) < 1e-8)
def test_all_or_nothing_assignment(self): net = np.loadtxt('data/braess_net.csv', delimiter=',', skiprows=1) cost = np.array([1., 5., 1., 5., 1.]) demand = np.loadtxt('data/braess_od.csv', delimiter=',', skiprows=1) demand = np.reshape(demand, (1, 3)) L = all_or_nothing_assignment(cost, net, demand) self.assertTrue( np.linalg.norm(L - np.array([2., 0., 2., 0., 2.])) < 1e-8)
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 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])