forked from Firkraag/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
constraints.py
169 lines (166 loc) · 5.65 KB
/
constraints.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from graph import Vertex, Graph
from random import sample
def Bellman_Ford(G, w, s):
'''A variant to the original Bellman_Ford algorithm
that we use to solve a system of difference constraints
with m inequalities on n unknowns. The running time is
O(nm), faster than O(n * n + nm) of the original Bellman-Ford
algorithm.
'''
edges = G.edges - set([(s, v) for v in G.adj[s]])
G.initialize_signle_source(s)
j = 1
for i in range(1, len(G.vertices)):
if j == 1:
for u,v in G.edges:
G.relax(u, v, w)
else:
for u,v in edges:
G.relax(u, v, w)
j = j + 1
for u,v in G.edges:
if v.d > u.d + w(u, v):
return False
return True
def initialize_signle_source(self, s):
for v in self.vertices:
v.d = float("Inf")
v.p = None
s.d = 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
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
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
def difference_constraints_without_aux_vertex(A, b):
row = len(A)
col = len(A[0])
vertices_num = col
vertices = []
edges = []
weights = dict()
for i in range(vertices_num):
vertices.append(Vertex(i + 1))
for i in range(0, row):
u = A[i].index(-1)
v = A[i].index(1)
edges.append((vertices[u], vertices[v]))
weights[(vertices[u], vertices[v])] = b[i]
G = Graph(vertices, edges)
if Bellman_Ford_without_aux_vertex(G, lambda x, y: weights[(x, y)]):
return [v.d for v in vertices]
else:
return None
def Bellman_Ford_without_aux_vertex(G, w):
initialize_signle_source_without_aux_vertex(G)
for i in range(1, len(G.vertices)):
for u,v in G.edges:
G.relax(u, v, w)
for u,v in G.edges:
if v.d > u.d + w(u, v):
return False
return True
def initialize_signle_source_without_aux_vertex(G):
for v in G.vertices:
v.d = 0
v.p = None
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