コード例 #1
0
def generate_augmented_matrix(graph, routes, sensors, flow_portions, flow_from_each_node=1.0):
    route_indices_by_origin = flows.get_route_indices_by_origin(routes)

    f = []
    where_did_f_come_from = []
    for node in graph.nodes():
        for (p, n) in graph.node[node]['2nd_flow']:
                f.append(graph.node[node]['2nd_flow'][(p, n)][0])
                where_did_f_come_from.append((graph.node[node]['2nd_flow'][(p, n)][1]));
    
    f = np.array(f);

    phis = []

    print ":begin"

    for node in graph.nodes():
        route_indices_from_node = route_indices_by_origin[node]
        edges_in_route = [set(zip(routes[i], routes[i][1:])) for i in route_indices_from_node]
        phi = np.zeros(shape=(len(f), len(route_indices_from_node)))
        for j in xrange(len(route_indices_from_node)):
            for i in xrange(len(f)):
                info = where_did_f_come_from[i]
                if  route_indices_from_node[j] in info:
                    phi[i, j] = flow_from_each_node
        phis.append(phi)

    print ":end"

    print where_did_f_come_from

    return np.hstack(phis), f
コード例 #2
0
def generate_random_matrix(graph, routes, sensors, flow_portions,
                  flow_from_each_node=1.0):
  # All route indices are with respect to _routes_.
  route_indices_by_origin = flows.get_route_indices_by_origin(routes)
    
#  f = np.array([graph[u][v]['flow'] for (u,v) in sensors])
  
  alphas = []
  mus = []
  num_routes = []
  for node in graph.nodes():
    route_indices_from_node = route_indices_by_origin[node]
    edges_in_route = [collections.Counter(zip(routes[i], routes[i][1:])) for i in \
            route_indices_from_node]
    
    alpha = np.zeros(shape=len(route_indices_from_node))
    mu = np.zeros(shape=len(route_indices_from_node))
    for j in xrange(len(route_indices_from_node)):
      alpha[j] = flow_portions[route_indices_from_node[j]]
      route = routes[route_indices_from_node[j]]
      mu[j] = sum(1./graph[de[0]][de[1]]['weight'] for de in zip(route,
              route[1:]))
    num_routes.append(len(route_indices_from_node))
    mus.append(mu)
    alphas.append(alpha)
  
  alpha = np.concatenate(alphas)
  phi = np.random.normal(size=(len(sensors), len(routes)))  
  f = np.dot(phi, alpha)

  return phi, np.concatenate(alphas), np.concatenate(mus), f, \
          np.array(num_routes)