Пример #1
0
def difference_constraints_with_arbitrary_weight(A, b):
    ''' An variant to the above difference constraints function
    that the weight of the edge from the auxiliary vertex to any
    other vertex can be arbitrary value.
    '''
    row = len(A)
    col = len(A[0])
    vertices_num = col
    vertices = []
    edges = []
    weights = dict()
    distribute = sample(xrange(-10, 10), vertices_num)
    for i in range(0, vertices_num + 1):
        vertices.append(Vertex(i))
    for i in range(1, vertices_num + 1):
        edges.append((vertices[0], vertices[i]))
        weights[(vertices[0], vertices[i])] = distribute[i - 1]
    for i in range(0, row):
        u = A[i].index(-1) + 1
        v = A[i].index(1) + 1
        edges.append((vertices[u], vertices[v]))
        weights[(vertices[u], vertices[v])] = b[i]
    G = Graph(vertices, edges)
    if G.Bellman_Ford(lambda x, y: weights[(x, y)], vertices[0]):
        return [v.d for v in vertices[1:]]
    else:
        return None
Пример #2
0
def difference_constraints(A, b):
    '''A algorithm to solve a system of difference constraints Ax <= b
    by solving a single-source shortest-paths problem using Bellman-Ford
    algorithm.  Each row of the linear-programming matrix A contains
    one 1 and one 1, and all other entries of A are 0.

    Calling convention: A is a two-dimensional list, b is a list.
    Return value: This function returns a list representing x if
    there exists feasible solution; otherwise, this function returns None.
    '''

    row = len(A)
    col = len(A[0])
    vertices_num = col
    vertices = []
    edges = []
    weights = dict()
    for i in range(0, vertices_num + 1):
        vertices.append(Vertex(i))
    for i in range(1, vertices_num + 1):
        edges.append((vertices[0], vertices[i]))
        weights[(vertices[0], vertices[i])] = 0
    for i in range(0, row):
        u = A[i].index(-1) + 1
        v = A[i].index(1) + 1
        edges.append((vertices[u], vertices[v]))
        weights[(vertices[u], vertices[v])] = b[i]
    G = Graph(vertices, edges)
    if G.Bellman_Ford(lambda x, y: weights[(x, y)], vertices[0]):
        return [v.d for v in vertices[1:]]
    else:
        return None
Пример #3
0
 def testBellmanFord(self):
     s = Vertex('s')
     t = Vertex('t')
     y = Vertex('y')
     x = Vertex('x')
     z = Vertex('z')
     vertices = [s, t, y, x, z]
     edges = [(s, t), (s, y), (t, y), (t, x), (t, z), (y, x), (y, z), (x, t), (z, s), (z, x)]
     weight = [6, 7, 8, 5, -4, -3, 9, -2, 2, 7]
     G = Graph(vertices, edges)
     we = dict()
     for x,y in zip(edges, weight):
         we[x] = y    
     def w(x, y):
         return we[(x, y)]        
     G.Bellman_Ford(w, z)
Пример #4
0
def equality_constraints(A, b):
    row = len(A)
    col = len(A[0])
    vertices_num = col
    vertices = []
    edges = []
    weights = dict()
    for i in range(0, vertices_num + 1):
        vertices.append(Vertex(i))
    for i in range(1, vertices_num + 1):
        edges.append((vertices[0], vertices[i]))
        weights[(vertices[0], vertices[i])] = 0
    for i in range(0, row):
        u = A[i].index(-1) + 1
        v = A[i].index(1) + 1
        edges.append((vertices[u], vertices[v]))
        weights[(vertices[u], vertices[v])] = b[i]
        edges.append((vertices[v], vertices[u]))
        weights[(vertices[v], vertices[u])] = -1 * b[i]
    G = Graph(vertices, edges)
    if G.Bellman_Ford(lambda x, y: weights[(x, y)], vertices[0]):
        return [v.d for v in vertices[1:]]
    else:
        return None
Пример #5
0
def single_variable_constraints(A, b):
    row = len(A)
    col = len(A[0])
    vertices_num = col
    vertices = []
    edges = []
    weights = dict()
    for i in range(0, vertices_num + 1):
        vertices.append(Vertex(i))
    for i in range(0, row):
        for j in range(0, len(A[i])):
            if A[i][j] == 1:
                edges.append((vertices[0], vertices[j + 1]))
                weights[(vertices[0], vertices[j + 1])] = b[i]
                break
            elif A[i][j] == -1:
                edges.append((vertices[j + 1], vertices[0]))
                weights[(vertices[j + 1], vertices[0])] = b[i]
                break
    G = Graph(vertices, edges)
    if G.Bellman_Ford(lambda x, y: weights[(x, y)], vertices[0]):
        return [v.d for v in vertices[1:]]
    else:
        return None