-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteger_program.py
329 lines (252 loc) · 8.21 KB
/
integer_program.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import networkx as nx
from gurobipy import *
import graph_helper as gh
import ip_generator as ig
import weighted_subgraph_problem as wsp
def setup_ip(G, mode='max', root=None, induced=True, flow=False):
"""Setup IP for WSP on graph G.
Parameters:
G : NetworkX graph
mode : 'max' or 'min'
root : root node for solution
induced : bool for induced solution
flow : bool for using flow formulation
Returns:
ip : integer program"""
ip = ig.OP()
# Create variables
ip.add_node_variables(G)
ip.add_edge_variables(G)
if not root:
ip.add_root_variables(G)
if flow:
G_flow = gh.construct_flow_graph(G)
ip.add_flow_variables(G_flow)
# Set objective function
ip.set_wsp_objective(G, mode)
# Add constraints
if induced:
ip.add_induce_constraints(G)
if not root:
ip.add_root_constraints(G)
if flow:
ip.add_flow_constraints(G_flow)
else:
ip.add_root_constraints(G, root)
if flow:
ip.add_flow_constraints(G_flow, root)
return ip
def solve_rooted_ip(G, root, mode='max', relaxed=False):
"""Compute opt weighted subgraph in graph G containing root.
Parameters:
G : NetworkX graph
root : root node for solution
mode : 'min' or 'max'
Returns:
H : NetworkX graph (opt weighted subgraph containing root)
weight: objective value (weight of H)"""
ip = setup_ip(G, mode=mode, root=root)
ip.add_connectivity_constraints(G, root=root)
if relaxed:
lp = ip.relax()
lp.optimize()
weight = lp.objVal
return weight
else:
ip.optimize()
H = wsp.construct_weighted_subgraph(G, ip)
weight = ip.objVal
return H, weight
def solve_rooted_flow_ip(G, root, mode='max', relaxed=False):
"""Compute opt weighted subgraph in graph G containing root.
Parameters:
G : NetworkX graph
root : root node for solution
mode : 'min' or 'max'
Returns:
H : NetworkX graph (opt weighted subgraph containing root)
weight: objective value (weight of H)"""
ip = setup_ip(G, mode=mode, root=root, flow=True)
if relaxed:
lp = ip.relax()
lp.optimize()
weight = lp.objVal
return weight
else:
ip.optimize()
H = wsp.construct_weighted_subgraph(G, ip)
weight = ip.objVal
return H, weight
def solve_full_ip__rooted(G, mode='max', relaxed=False):
"""Compute opt weighted subgraph in graph G using multiple IPs.
Parameters:
G : NetworkX graph
mode : 'min' or 'max'
Returns:
H : NetworkX graph (opt weighted subgraph)
weight: objective value (weight of H)"""
H = nx.empty_graph()
weight = 0
if not relaxed:
for v in G.nodes():
(H1, objVal) = solve_rooted_ip(G, v, mode)
if mode == 'max':
if objVal > weight:
H = H1
weight = objVal
elif objVal == weight:
if H1.number_of_nodes() < H.number_of_nodes():
H = H1
weight = objVal
elif mode == 'min':
if objVal < weight:
H = H1
weight = objVal
elif objVal == weight:
if H1.number_of_nodes() < H.number_of_nodes():
H = H1
weight = objVal
return H, weight
else:
for v in G.nodes():
objVal = solve_rooted_ip(G, v, mode, relaxed=True)
if mode == 'max':
if objVal > weight:
weight = objVal
elif mode == 'min':
if objVal < weight:
weight = objVal
return weight
def solve_full_ip(G, mode='max', relaxed=False):
"""Compute opt weighted subgraph in graph G with an IP.
Parameters:
G : NetworkX graph
Returns:
H : NetworkX graph (opt weighted subgraph)
objVal: objective value (weight of H)"""
ip = setup_ip(G, mode=mode)
ip.add_connectivity_constraints(G)
if relaxed:
lp = ip.relax()
lp.optimize()
weight = lp.objVal
if (mode == 'max' and weight < 0) or (mode == 'min' and weight > 0):
weight = 0
return weight
else:
ip.optimize()
H = wsp.construct_weighted_subgraph(G, ip)
weight = ip.objVal
if (mode == 'max' and weight < 0) or (mode == 'min' and weight > 0):
H = nx.empty_graph()
weight = 0
return H, weight
def solve_separation_ip(G, mode='max', relaxed=False):
"""Compute opt weighted subgraph in graph G by separating the connectivity constraints.
Parameters:
G : NetworkX graph
mode : 'min' or 'max'
Returns:
H : NetworkX graph (opt weighted subgraph)
weight: objective value (weight of H)"""
ip = setup_ip(G, mode=mode)
connected = False
i = 0
H = nx.empty_graph()
while not connected:
ip.optimize()
H = wsp.construct_weighted_subgraph(G, ip)
if nx.is_connected(H):
connected = True
else:
ip.add_violated_constraint(G, nx.connected_components(H))
i += 1
weight = ip.objVal
return H, weight, i
def solve_flow_ip__rooted(G, mode='max', relaxed=False):
"""Compute opt weighted subgraph in graph G using multiple flow IPs.
Parameters:
G : NetworkX graph
mode : 'min' or 'max'
Returns:
H : NetworkX graph (opt weighted subgraph)
weight: objective value (weight of H)"""
H = nx.empty_graph()
weight = 0
for v in G.nodes():
(H1, objVal) = solve_rooted_flow_ip(G, v, mode, relaxed)
if mode == 'max':
if objVal > weight:
H = H1
weight = objVal
elif objVal == weight:
if H1.number_of_nodes() < H.number_of_nodes():
H = H1
weight = objVal
elif mode == 'min':
if objVal < weight:
H = H1
weight = objVal
elif objVal == weight:
if H1.number_of_nodes() < H.number_of_nodes():
H = H1
weight = objVal
return H, weight
def solve_flow_ip(G, mode='max', induced=True, relaxed=False):
"""Compute opt weighted subgraph in graph G using a flow IP.
Parameters:
G : NetworkX graph
mode : 'min' or 'max'
Returns:
H : NetworkX graph (opt weighted subgraph)
weight: objective value (weight of H)"""
ip = setup_ip(G, mode, induced=induced, flow=True)
if relaxed:
lp = ip.relax()
lp.optimize()
weight = lp.objVal
if (mode == 'max' and weight < 0) or (mode == 'min' and weight > 0):
weight = 0
return weight
else:
ip.optimize()
H = wsp.construct_weighted_subgraph(G, ip)
weight = ip.objVal
if (mode == 'max' and weight < 0) or (mode == 'min' and weight > 0):
H = nx.empty_graph()
weight = 0
return H, weight
def solve_ip_on_path(G, mode='max'):
"""Compute opt weighted subgraph on path G using a IP.
Parameters:
G : NetworkX graph
mode : 'min' or 'max'
Returns:
H : NetworkX graph (opt weighted subgraph)
weight: objective value (weight of H)"""
if not gh.is_path(G):
print('G is not a path!')
ip = setup_ip(G, mode)
x = ip.get_x()
y = ip.get_y()
z = ip.get_z()
# Add connectivity constraints
path = list(G.nodes)
subpaths = [[]]
for i in range(len(path) + 1):
for j in range(i + 1, len(path) + 1):
sub = path[i:j]
subpaths.append(sub)
for s in subpaths:
elist = [e for e in G.edges() if (e[0] in s) ^ (e[1] in s)]
for v in s:
ip.addConstr(y[v] <= (quicksum(x[u] for u in s)) + (quicksum(z[v1][v2] for v1, v2 in elist[:])))
# Solve
ip.optimize()
# Construct subgraph
H = wsp.construct_weighted_subgraph(G, ip)
weight = ip.objVal
if (mode == 'max' and weight < 0) or (mode == 'min' and weight > 0):
H = nx.empty_graph()
weight = 0
return H, weight