示例#1
0
def nodelink_incidence(graph, rm_redundant = False):
    """
    get node-link incidence matrix

    Parameters
    ----------
    graph: graph object
    rm_redundant: if True, remove redundant constraints for the ue solver

    Return value
    ------------
    C: matrix of incidence node-link
    ind: indices of a basis formed by the rows of C
    """
    m, n = graph.numnodes, graph.numlinks
    entries, I, J = [], [], []
    for id1,node in graph.nodes.items():
        for id2,link in node.inlinks.items(): entries.append(1.0); I.append(id1-1); J.append(graph.indlinks[id2])
        for id2,link in node.outlinks.items(): entries.append(-1.0); I.append(id1-1); J.append(graph.indlinks[id2])
    C = spmatrix(entries, I, J, (m,n))
    if rm_redundant:
        M = matrix(C)
        r = rn.rank(M)
        if r < m:
            print 'Remove {} redundant constraint(s)'.format(m-r)
            ind = find_basis(M.trans())
            return C[ind,:], ind
    return C, range(m)
示例#2
0
def nodelink_incidence(graph, rm_redundant = False):
    """
    get node-link incidence matrix
    
    Parameters
    ----------
    graph: graph object
    rm_redundant: if True, remove redundant constraints for the ue solver
    
    Return value
    ------------
    C: matrix of incidence node-link
    ind: indices of a basis formed by the rows of C
    """
    m, n = graph.numnodes, graph.numlinks
    entries, I, J = [], [], []
    for id1,node in graph.nodes.items():
        for id2,link in node.inlinks.items(): entries.append(1.0); I.append(id1-1); J.append(graph.indlinks[id2])
        for id2,link in node.outlinks.items(): entries.append(-1.0); I.append(id1-1); J.append(graph.indlinks[id2])
    C = spmatrix(entries, I, J, (m,n))
    if rm_redundant:
        M = matrix(C)
        r = rn.rank(M)
        if r < m:
            print 'Remove {} redundant constraint(s)'.format(m-r)
            ind = find_basis(M.trans())
            return C[ind,:], ind
    return C, range(m)
def feasible_pathflows(graph, l_obs, obs=None, update=False,
                       with_cell_paths=False, with_ODs=False, x_true=None, wp_trajs=None):
    """Attempts to find feasible pathflows given partial of full linkflows
    
    Parameters:
    ----------
    graph: Graph object
    l_obs: observations of link flows
    obs: indices of the observed links
    update: if True, update path flows in graph
    with_cell_paths: if True, include cell paths as constraints
    with_ODs: if True, include ODs in the constraints if no with_cell_paths or in the objective if with_cell_paths
    """
    assert with_cell_paths or with_ODs # we must have some measurements!
    n = graph.numpaths
    # route to links flow constraints
    A, b = linkpath_incidence(graph), l_obs
    if obs: A = A[obs,:] # trim matrix if we have partial observations
    Aineq, bineq = spmatrix(-1.0, range(n), range(n)), matrix(0.0, (n,1)) # positive constraints
    if not with_cell_paths: # if just with ODs flow measurements:
        Aeq, beq = path_to_OD_simplex(graph) # route to OD flow constraints
    else: # if we have cellpath flow measurements:
        assert wp_trajs is not None
        Aeq, beq = WP.simplex(graph, wp_trajs) # route to cellpath flow constraints
        if with_ODs: # if we have ODs + cellpaths measurements
          T, d = path_to_OD_simplex(graph) # route to OD flow constraints included in objective
          A, b = matrix([A, T]), matrix([b, d]) # add the constraints to the objective
        
    if x_true is not None:
        err1 =  np.linalg.norm(A * x_true - b, 1) / np.linalg.norm(b, 1)
        err2 = np.linalg.norm(Aeq * x_true - beq) / np.linalg.norm(beq, 1)
        assert err1 < TOL, 'Ax!=b'
        assert err2 < TOL, 'Aeq x!=beq'
    # construct objective for cvxopt.solvers.qp
    Q, c = A.trans()*A, -A.trans()*b
    #x = solvers.qp(Q + REG_EPS*spmatrix(1.0, range(n), range(n)), c, Aineq, bineq, Aeq, beq)['x']
    # try with cvxopt.solvers.cp
    def qp_objective(x=None, z=None):
      if x is None: return 0, matrix(1.0, (n, 1))
      f = 0.5 * x.trans()*Q*x + c.trans() * x
      Df = (Q*x + c).trans()
      if z is None: return f, Df
      return f, Df, z[0]*Q
  
    dims = {'l': n, 'q': [], 's': []}
    x = solvers.cp(qp_objective, G=Aineq, h=bineq, A=Aeq, b=beq, 
        kktsolver=get_kktsolver(Aineq, dims, Aeq, qp_objective))['x']
    
    if update:
        logging.info('Update link flows, delays in Graph.'); graph.update_linkflows_linkdelays(P*x)
        logging.info('Update path delays in Graph.'); graph.update_pathdelays()
        logging.info('Update path flows in Graph object.'); graph.update_pathflows(x)
    #import ipdb
    #rank = 5
    #if with_ODs == False:
    #    ipdb.set_trace()
    return x, rn.rank(matrix([A, Aeq])), n
    
示例#4
0
 def SetT(self):
     if self.H_hat.shape[0] != 0:
         NS = nullspace(self.H_hat)
         if rank(NS) >= self.Nj:
             self.PrecodingT = NS[:, 0:self.Nj]
         else:
             self.PrecodingT = [None]
     else:
         self.PrecodingT = [None]
示例#5
0
def checkit(a):
    print "a:"
    print a
    r = rank(a)
    print "rank is", r
    ns = nullspace(a)
    print "nullspace:"
    print ns
    if ns.size > 0:
        res = np.abs(np.dot(a, ns)).max()
        print "max residual is", res    
示例#6
0
def solver(data, ls, degree, full=False):
    """Solves the inverse optimization problem
    
    Parameters
    ----------
    data: Aeq, beqs, ffdelays, slopes from get_data(graphs)
    ls: list of link flow vectors in equilibrium
    degree: degree of the polynomial function to estimate
    full: if False, just return theta, if True, return the whole primal solution
    """
    c, A, b = constraints(data, ls, degree)
    print A.size
    r = rn.rank(matrix(A))
    print r
    if full: return solvers.lp(c, G=A, h=b)['x']
    return solvers.lp(c, G=A, h=b)['x'][range(degree)]
示例#7
0
   return numpy.asarray(x).reshape(-1)

def matMulFlatten(a,x):
   return flatten(a * numpy.matrix(numpy.reshape(x,[a.shape[1],1])))

nSides = 6
for kFlips in range(1,4):
  print
  print '***** nSides =',nSides,'kFlips =',kFlips
  # first check insisting on unbiasedness
  # completely determines the estimate for
  # one flip from a nSides-slides system
  sNK = freqSystem(nSides,kFlips)
  # print sNK
  print 'full rank'
  print rank(sNK['a'].T * sNK['a'])==kFlips+1
  print 'bias free determined solution'
  printEsts(flatten(numpy.linalg.solve(sNK['a'].T * sNK['a'], \
     sNK['a'].T * sNK['b'])))
  print 'standard empirical solution'
  printEsts(empiricalMeansEstimates(nSides,kFlips))
  print 'losses for standard empirical solution'
  print printLosses(losses(nSides,empiricalMeansEstimates(nSides,kFlips)))

  # now show the Bayes solution has smaller loss
  bayesSoln = bayesMeansEstimates(nSides,kFlips)
  print 'Bayes solution'
  printEsts(bayesSoln)
  print 'losses for Bayes solution'
  printLosses(losses(nSides,bayesSoln))
  print 'Bayes max loss improvement'
示例#8
0
def feasible_pathflows(graph,
                       l_obs,
                       obs=None,
                       update=False,
                       with_cell_paths=False,
                       with_ODs=False,
                       x_true=None,
                       wp_trajs=None):
    """Attempts to find feasible pathflows given partial of full linkflows
    
    Parameters:
    ----------
    graph: Graph object
    l_obs: observations of link flows
    obs: indices of the observed links
    update: if True, update path flows in graph
    with_cell_paths: if True, include cell paths as constraints
    with_ODs: if True, include ODs in the constraints if no with_cell_paths or in the objective if with_cell_paths
    """
    assert with_cell_paths or with_ODs  # we must have some measurements!
    n = graph.numpaths
    # route to links flow constraints
    A, b = linkpath_incidence(graph), l_obs
    if obs: A = A[obs, :]  # trim matrix if we have partial observations
    Aineq, bineq = spmatrix(-1.0, range(n),
                            range(n)), matrix(0.0,
                                              (n, 1))  # positive constraints
    if not with_cell_paths:  # if just with ODs flow measurements:
        Aeq, beq = path_to_OD_simplex(graph)  # route to OD flow constraints
    else:  # if we have cellpath flow measurements:
        assert wp_trajs is not None
        Aeq, beq = WP.simplex(graph,
                              wp_trajs)  # route to cellpath flow constraints
        if with_ODs:  # if we have ODs + cellpaths measurements
            T, d = path_to_OD_simplex(
                graph)  # route to OD flow constraints included in objective
            A, b = matrix([A, T]), matrix(
                [b, d])  # add the constraints to the objective

    if x_true is not None:
        err1 = np.linalg.norm(A * x_true - b, 1) / np.linalg.norm(b, 1)
        err2 = np.linalg.norm(Aeq * x_true - beq) / np.linalg.norm(beq, 1)
        assert err1 < TOL, 'Ax!=b'
        assert err2 < TOL, 'Aeq x!=beq'
    # construct objective for cvxopt.solvers.qp
    Q, c = A.trans() * A, -A.trans() * b

    #x = solvers.qp(Q + REG_EPS*spmatrix(1.0, range(n), range(n)), c, Aineq, bineq, Aeq, beq)['x']
    # try with cvxopt.solvers.cp
    def qp_objective(x=None, z=None):
        if x is None: return 0, matrix(1.0, (n, 1))
        f = 0.5 * x.trans() * Q * x + c.trans() * x
        Df = (Q * x + c).trans()
        if z is None: return f, Df
        return f, Df, z[0] * Q

    dims = {'l': n, 'q': [], 's': []}
    x = solvers.cp(qp_objective,
                   G=Aineq,
                   h=bineq,
                   A=Aeq,
                   b=beq,
                   kktsolver=get_kktsolver(Aineq, dims, Aeq,
                                           qp_objective))['x']

    if update:
        logging.info('Update link flows, delays in Graph.')
        graph.update_linkflows_linkdelays(P * x)
        logging.info('Update path delays in Graph.')
        graph.update_pathdelays()
        logging.info('Update path flows in Graph object.')
        graph.update_pathflows(x)
    #import ipdb
    #rank = 5
    #if with_ODs == False:
    #    ipdb.set_trace()
    return x, rn.rank(matrix([A, Aeq])), n
示例#9
0
def matMulFlatten(a, x):
    return flatten(a * numpy.matrix(numpy.reshape(x, [a.shape[1], 1])))


nSides = 6
for kFlips in range(1, 4):
    print
    print '***** nSides =', nSides, 'kFlips =', kFlips
    # first check insisting on unbiasedness
    # completely determines the estimate for
    # one flip from a nSides-slides system
    sNK = freqSystem(nSides, kFlips)
    # print sNK
    print 'full rank'
    print rank(sNK['a'].T * sNK['a']) == kFlips + 1
    print 'bias free determined solution'
    printEsts(flatten(numpy.linalg.solve(sNK['a'].T * sNK['a'], \
       sNK['a'].T * sNK['b'])))
    print 'standard empirical solution'
    printEsts(empiricalMeansEstimates(nSides, kFlips))
    print 'losses for standard empirical solution'
    print printLosses(losses(nSides, empiricalMeansEstimates(nSides, kFlips)))

    # now show the Bayes solution has smaller loss
    bayesSoln = bayesMeansEstimates(nSides, kFlips)
    print 'Bayes solution'
    printEsts(bayesSoln)
    print 'losses for Bayes solution'
    printLosses(losses(nSides, bayesSoln))
    print 'Bayes max loss improvement'