forked from netcontract/ncflow
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpre_solve_path.py
executable file
·80 lines (62 loc) · 2.18 KB
/
pre_solve_path.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
#! /usr/bin/env python
import os
import pickle
from pathos import multiprocessing
import sys
sys.path.append("..")
from lib.problems import get_problem
from lib.algorithms.path_formulation import PathFormulation, PATHS_DIR
from lib.path_utils import graph_copy_with_edge_weights, find_paths
global G
global num_paths
global edge_disjoint
global LOAD_FROM_DISK
LOAD_FROM_DISK = True
def find_paths_wrapper(commod):
k, (s_k, t_k, d_k) = commod
if LOAD_FROM_DISK:
if (s_k, t_k) not in paths_dict:
paths = find_paths(G, s_k, t_k, num_paths, edge_disjoint)
return ((s_k, t_k), paths)
else:
paths = find_paths(G, s_k, t_k, num_paths, edge_disjoint)
return ((s_k, t_k), paths)
if __name__ == "__main__":
problem = get_problem(sys.argv[1], model="gravity", random=False)
assert problem.traffic_matrix.is_full
global num_paths
num_paths = int(sys.argv[2])
dist_metric = sys.argv[3]
global edge_disjoint
if sys.argv[4] == "True":
edge_disjoint = True
elif sys.argv[4] == "False":
edge_disjoint = False
else:
raise Exception("invalid argument for edge_disjoint: {}".format(sys.argv[4]))
if not os.path.exists(PATHS_DIR):
os.makedirs(PATHS_DIR)
paths_fname = PathFormulation.paths_full_fname(
problem, num_paths, edge_disjoint, dist_metric
)
if LOAD_FROM_DISK:
print("Loading paths from pickle file", paths_fname)
try:
with open(paths_fname, "rb") as f:
paths_dict = pickle.load(f)
print("paths_dict: ", len(paths_dict))
except FileNotFoundError:
print("Unable to find {}".format(paths_fname))
paths_dict = {}
global G
G = graph_copy_with_edge_weights(problem.G, dist_metric)
pool = multiprocessing.ProcessPool(28)
new_paths_dict = pool.map(find_paths_wrapper, problem.commodity_list)
for ret_val in new_paths_dict:
if ret_val is not None:
k, v = ret_val
paths_dict[k] = v
print("paths_dict: ", len(paths_dict))
print("Saving paths to pickle file")
with open(paths_fname, "wb") as w:
pickle.dump(paths_dict, w)