def test_all_or_nothing(self):
        # load the data
        graph = np.loadtxt("data/braess_graph.csv", delimiter=",", skiprows=1)
        od = np.loadtxt("data/braess_od.csv", delimiter=",", skiprows=1)
        od = np.reshape(od, (1, 3))
        # all or nothing assignment
        L = all_or_nothing(graph, od)
        self.assertTrue(np.linalg.norm(L - np.array([2.0, 0.0, 2.0, 0.0, 2.0])) < eps)

        # modify graph
        graph[2, 3] = 1000.0
        graph[0, 3] = 1.0
        L = all_or_nothing(graph, od)
        self.assertTrue(np.linalg.norm(L - np.array([0.0, 2.0, 0.0, 0.0, 2.0])) < eps)
 def test_all_or_nothing_2(self):
     # load the data
     graph = np.loadtxt("data/graph_test.csv", delimiter=",")
     # print graph
     od = np.loadtxt("data/od_test.csv", delimiter=",")
     od = np.reshape(od, (1, 3))
     L = all_or_nothing(graph, od)
def search_direction(f, graph, g, demand):
    # computes the Frank-Wolfe step
    # g is just a canvas containing the link information and to be updated with 
    # the most recent edge costs
    x = np.power(f.reshape((f.shape[0],1)), np.array([0,1,2,3,4]))
    grad = np.einsum('ij,ij->i', x, graph[:,3:])
    g[:,3] = grad
    return all_or_nothing(g, demand), grad
def residual(graphs, demands, fs):
    links = int(np.max(graphs[0][:,0])+1)
    f = np.sum(fs, axis=1)
    x = np.power(f.reshape((links,1)), np.array([0,1,2,3,4]))
    r = 0.
    for i, (graph, demand) in enumerate(zip(graphs, demands)):
        g = np.copy(graph[:,:4])
        grad = np.einsum('ij,ij->i', x, graph[:,3:])
        g[:,3] = grad
        L = all_or_nothing(g, demand)
        r = r + grad.dot(fs[:,i] - L)
    return r
def total_free_flow_cost(g, demand):
    # computes the total cost under free flow travel times
    L = all_or_nothing(g, demand)
    return g[:,3].dot(L)
def total_ff_costs_heterogeneous(graphs, demands):
    return np.sum([g[:,3].dot(all_or_nothing(g[:,:4], d)) for g,d in zip(graphs, demands)])