-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimperfect_part_errors.py
251 lines (177 loc) · 7.19 KB
/
imperfect_part_errors.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
#!/usr/bin/env python
# coding: utf-8
import os
import sys
import argparse
import networkx as nx
import gurobipy as gp
from gurobipy import GRB
from collections import deque
from bisect import bisect
from copy import deepcopy
def get_edge(raw_edge):
parts = raw_edge.split()
return int(parts[0]), int(parts[1]), float(parts[2])
def get_graph(raw_graph):
graph = {
'n': 0,
'edges': list()
}
try:
lines = raw_graph.split('\n')[1:]
if not lines[-1]:
lines = lines[:-1]
graph['n'], graph['edges'] = int(lines[0]), [get_edge(raw_e) for raw_e in lines[1:]]
finally:
return graph
def read_input_graphs(graph_file):
graphs_raw = open(graph_file, 'r').read().split('#')[1:]
return [get_graph(raw_g) for raw_g in graphs_raw]
def read_input(graph_file):
return read_input_graphs(graph_file)
def mfd_algorithm(data):
data['message'] = 'unsolved'
for i in range(2, len(data['graph'].edges) + 1):
if fd_fixed_size(data, i)['message'] == 'solved':
return data
return data
def build_base_ilp_model(data, size):
graph = data['graph']
max_flow_value = data['max_flow_value']
sources = data['sources']
sinks = data['sinks']
M = 1e3
# create extra sets
T = [(u, v, i, k) for (u, v, i) in graph.edges(keys=True) for k in range(size)]
SC = list(range(size))
# Create a new model
model = gp.Model('MFD')
model.setParam('LogToConsole', 0)
model.setParam('Threads', threads)
# Create variables
x = model.addVars(T, vtype=GRB.BINARY, name='x')
w = model.addVars(SC, vtype=GRB.INTEGER, name='w', lb=0)
pho = model.addVars(SC,vytpe=GRB.INTEGER,name="pho",lb=0)
z = model.addVars(T, vtype=GRB.CONTINUOUS, name='z', lb=0)
phi = model.addVars(T, vtype=GRB.CONTINUOUS, name='z', lb=0)
# flow conservation
for k in range(size):
for v in graph.nodes:
if v in sources:
model.addConstr(sum(x[v, w, i, k] for _, w, i in graph.out_edges(v, keys=True)) == 1)
if v in sinks:
model.addConstr(sum(x[u, v, i, k] for u, _, i in graph.in_edges(v, keys=True)) == 1)
if v not in sources and v not in sinks:
model.addConstr(sum(x[v, w, i, k] for _, w, i in graph.out_edges(v, keys=True)) - sum(x[u, v, i, k] for u, _, i in graph.in_edges(v, keys=True)) == 0)
# flow balance
for (u, v, i, f) in graph.edges(keys=True, data='flow'):
model.addConstr(f - sum(z[u, v, i, k] for k in range(size)) <= sum(phi[u, v, i, k] for k in range(size)))
model.addConstr(f - sum(z[u, v, i, k] for k in range(size)) >= - sum(phi[u, v, i, k] for k in range(size)))
# linearization - x*w
for (u, v, i) in graph.edges(keys=True):
for k in range(size):
model.addConstr(z[u, v, i, k] <= max_flow_value * x[u, v, i, k])
model.addConstr(w[k] - (1 - x[u, v, i, k]) * max_flow_value <= z[u, v, i, k])
model.addConstr(z[u, v, i, k] <= w[k])
# linearization - x*pho
for (u, v, i) in graph.edges(keys=True):
for k in range(size):
model.addConstr(phi[u, v, i, k] <= M * x[u, v, i, k])
model.addConstr(pho[k] - (1 - x[u, v, i, k]) * M <= phi[u, v, i, k])
model.addConstr(phi[u, v, i, k] <= pho[k])
return model, x, w, z
def get_solution(model, data, size):
data['weights'], data['solution'] = list(), list()
if model.status == GRB.OPTIMAL:
graph = data['graph']
T = [(u, v, i, k) for (u, v, i) in graph.edges(keys=True) for k in range(size)]
w_sol = [0] * len(range(size))
paths = [list() for _ in range(size)]
for k in range(size):
w_sol[k] = round(model.getVarByName(f'w[{k}]').x)
for (u, v, i, k) in T:
if round(model.getVarByName(f'x[{u},{v},{i},{k}]').x) == 1:
paths[k].append((u, v, i))
for k in range(len(paths)):
paths[k] = sorted(paths[k])
data['weights'], data['solution'] = w_sol, paths
return data
def update_status(data, model):
if model.status == GRB.OPTIMAL:
data['message'] = 'solved'
data['runtime'] = model.Runtime
if model.status == GRB.INFEASIBLE:
data['message'] = 'unsolved'
data['runtime'] = 0
return data
def fd_fixed_size(data, size):
# calculate a flow decomposition into size paths
try:
# Create a new model
model, _, _, _ = build_base_ilp_model(data, size)
# objective function
model.optimize()
data = update_status(data, model)
data = get_solution(model, data, size)
except gp.GurobiError as e:
print(f'Error code {e.errno}: {e}', file=sys.stderr)
except AttributeError:
print('Encountered an attribute error', file=sys.stderr)
return data
def output_paths(output,paths,weights):
numberOfPaths = len(paths)
for nP in range(0,numberOfPaths):
nodes = set()
for (i,j,k) in paths[nP]:
nodes.add(i)
nodes.add(j)
output.write(str(weights[nP]))
for i in sorted(nodes):
output.write(' '.join(['',str(i)]))
output.write(' \n')
def compute_graph_metadata(graph):
# creation of NetworkX Graph
ngraph = nx.MultiDiGraph()
ngraph.add_weighted_edges_from(graph['edges'], weight='flow')
# calculating source, sinks
sources = [x for x in ngraph.nodes if ngraph.in_degree(x) == 0]
sinks = [x for x in ngraph.nodes if ngraph.out_degree(x) == 0]
# definition of data
return {
'graph': ngraph,
'sources': sources,
'sinks': sinks,
'max_flow_value': max(ngraph.edges(data='flow'), key=lambda e: e[-1])[-1] if len(ngraph.edges) > 0 else -1,
}
def solve_instances(graphs,output_file):
output = open(output_file, 'w+')
for g, graph in enumerate(graphs):
print("#graph ",g)
output.write(f'# graph {g}\n')
if not graph['edges']:
continue
mfd = compute_graph_metadata(graph)
if len(mfd['graph'].edges) > 0:
mfd = mfd_algorithm(mfd)
paths,weights = mfd['solution'],mfd['weights']
output_paths(output,paths,weights)
output.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='''
Computes paths from Minimal Flow Decomposition under Uncertainty using Path-Errors
This script uses the Gurobi ILP solver.
''',
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument('-t', '--threads', type=int, default=0,
help='Number of threads to use for the Gurobi solver; use 0 for all threads (default 0).')
requiredNamed = parser.add_argument_group('required arguments')
requiredNamed.add_argument('-i', '--input', type=str, help='Input filename', required=True)
requiredNamed.add_argument('-o', '--output', type=str, help='Output filename', required=True)
args = parser.parse_args()
threads = args.threads
if threads == 0:
threads = os.cpu_count()
print(f'INFO: Using {threads} threads for the Gurobi solver')
solve_instances(read_input(args.input),args.output)