Example #1
0
def romeFlow(vertPos, edgeWei, xi, inj, ext):

    # initialise params; init. weight matrix and make a copy to be updated
    w = weiMaker(vertPos, edgeWei)
    wei = cp.copy(w)
    C = np.zeros((58, 201))
    moving = np.zeros(58)
    # create Uu to count which edges get used
    Uu = cp.copy(w)

    # set the non-zero values in the weight matrix to 0 in Uu
    for y, i in np.ndenumerate(Uu):
        if i > 0:
            Uu[y] = 1

    # initialise the system, before any iterations
    C[inj, 0] = 20

    # begin the time loop of 200 iterations
    for t in range(200):
        #predetermine the amount of cars to move
        for i in range(len(moving)):
            moving[i] = round(0.7 * C[i, t])

        #loop through nodes and move them to next best
        for n in range(58):
            if (C[n, t] > 0 and n != ext):
                # find next move: next node on shortest path to exit node
                shpath = Dijkstra.Dijkst(n, ext, wei)
                nm = shpath[1]
                # set used edges to zero
                Uu[n, nm] = 0
                # update n by removing from it the cars that are moving
                C[n, t + 1] += (C[n, t] - moving[n])
                # update the next move node with the cars moving from n
                C[nm, t + 1] += moving[n]
            elif n == ext:
                # let 40% of the cars exit
                C[n, t + 1] += np.round(0.6 * C[n, t])

        # inject 20 cars into the injection node for the first 180 iterations
        if t < 179:
            C[inj, t + 1] += 20

        # update the weight matrix co-ordinate wise, making sure node i and j
        # are truly connected, first
        for i in range(58):
            for j in range(58):
                if w[i, j] > 0:
                    wei[i,
                        j] = w[i, j] + xi * 0.5 * (C[i, t + 1] + C[j, t + 1])

    # count edges as utilised if they are not one way and
    # have been used in either direction
    # delete double entries
    for i in range(58):
        for j in range(58):
            if (w[i, j] == w[j, i] and Uu[i, j] != Uu[j, i]):
                Uu[i, j] = 0
            if Uu[i, j] == Uu[j, i] == 1:
                Uu[i, j] = 0

    # return C, the 'flow matrix'
    # return matrix of unused edges
    return C, Uu
Example #2
0
amm = np.zeros(27)
amm[1::2] = duration
adj = np.diag(amm, 1)
adj[0, 1::2] = zer0
adj[::2, 27] = zer0

conx = [2, 2, 2, 4, 4, 6, 12, 14, 14, 20, 22]
cony = [3, 15, 21, 9, 25, 7, 15, 11, 19, 23, 25]

adj[conx, cony] = zer0

Wei = -copy.copy(adj)

# use system time to time Dijkstra's algorithm on pos. conn/wei matrix
t00 = t()
print 'Dijk path;', di.Dijkst(0, 27, adj)
t01 = t()

# use system time to time the Bellman-Ford algorithm on neg. conn/wei matrix
t10 = t()
print 'Bell path:', bf.BellmanFord(0, 27, Wei)
t11 = t()

# average the time by path length
t0 = (t01 - t00) / len(di.Dijkst(0, 27, adj))
t1 = (t11 - t10) / len(di.Dijkst(0, 27, Wei))

if __name__ == '__main__':
    print 'Dijkstra Takes', t0, 's'
    print 'Bellman Ford Takes', t1, 's'
    print 'Dijkstra is', (t1 / t0), 'times quicker'