This repository has been archived by the owner on Sep 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrpc_api.py
116 lines (96 loc) · 4.12 KB
/
rpc_api.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
# Copyright (C) 2015 University of Vienna
# All rights reserved.
# BSD license.
# Author: Ali Baharev <[email protected]>
from __future__ import print_function
from random import shuffle
from itertools import chain
from dm_decomp import dm_coordinate, blt_with_tearing #, dbg_show_coomat
import heap_md as md
from order_util import get_inverse_perm, argsort,\
check_coordinate_format, check_if_indices_are_in_range
from py3compat import irange
from utils import duplicates
def fine_dulmage_mendelsohn(rows, cols, values, n_rows, n_cols, upper, minimize):
shape = (n_rows, n_cols)
error_msg = _check_coordinate_format(rows, cols, values, shape)
if error_msg is not None:
return {'error_msg': error_msg}
tup = dm_coordinate(rows, cols, values, shape, upper, minimize=minimize)
_, rowp, colp, R0, C0, _, _, rowpart, colpart, colors = tup
# add unmatched to the partition, per request
if upper:
rowpart.append(R0)
colpart.insert(0, C0)
else:
rowpart.insert(0, R0)
colpart.append(C0)
#pretty_indent(rowpart)
#pretty_indent(colpart)
_check_partition(rowp, colp, rowpart, colpart)
return _pack(rowp, colp, rowpart, colpart, colors)
def tearing_hand_guided(rows, cols, values, n_rows, n_cols, torn_rows, torn_cols):
shape = (n_rows, n_cols)
error_msg = _check_coordinate_format(rows, cols, values, shape) or \
_check_torn_indices(shape, torn_rows, torn_cols)
if error_msg is not None:
return {'error_msg': error_msg}
tup = blt_with_tearing(rows, cols, values, shape, torn_rows, torn_cols)
rowp, colp, R0, C0, _, _, rowpart, colpart, colors = tup
# Similarly to fine_dulmage_mendelsohn, we add the unmatched but also the
# torn rows/cols as "unmatched".
rowpart.append(list(chain(R0, torn_rows)))
colpart.append(list(chain(C0, torn_cols)))
#print('Returning:')
#dbg_show_coomat(rows, cols, colors, shape, rowp, colp)
#print('Row partition:', rowpart)
#print('Col partition:', colpart)
_check_partition(rowp, colp, rowpart, colpart)
return _pack(rowp, colp, rowpart, colpart, colors)
def _check_partition(rowp, colp, rowpart, colpart):
dbg_rperm = list(_flatten(rowpart))
dbg_cperm = list(_flatten(colpart))
dbg_rowp, dbg_colp = get_inverse_perm(dbg_rperm, dbg_cperm)
assert dbg_rowp == rowp
assert dbg_colp == colp
def _flatten(partition):
if isinstance(partition, list):
for elem in partition:
for leaf in _flatten(elem):
yield leaf
else:
yield partition # leaf
def hessenberg(rows, cols, values, n_rows, n_cols, tie_breaking):
shape = (n_rows, n_cols)
error_msg = _check_coordinate_format(rows, cols, values, shape)
if error_msg is not None:
return {'error_msg': error_msg}
rowp, colp = md.hessenberg(rows, cols, values, n_rows, n_cols, tie_breaking)
return _pack(rowp, colp)
def lexicographical(row_ids, col_ids, sort_rows, sort_cols):
dups = duplicates(row_ids) + duplicates(col_ids)
if dups:
return {'error_msg': 'Duplicate identifiers: {}'.format(dups) }
rpart = argsort(row_ids) if sort_rows else list(irange(len(row_ids)))
cpart = argsort(col_ids) if sort_cols else list(irange(len(col_ids)))
rowp, colp = get_inverse_perm(rpart, cpart)
return _pack(rowp, colp)
def random(n_rows, n_cols):
rowp, colp = list(irange(n_rows)), list(irange(n_cols))
shuffle(rowp)
shuffle(colp)
return _pack(rowp, colp)
def _check_coordinate_format(rows, cols, values, shape):
try:
check_coordinate_format(rows, cols, values, shape)
except AssertionError as e:
return str(e)
def _check_torn_indices(shape, torn_rows, torn_cols):
try:
check_if_indices_are_in_range(torn_rows, shape[0], 'torn row', empty_allowed=True)
check_if_indices_are_in_range(torn_cols, shape[1], 'torn column', empty_allowed=True)
except AssertionError as e:
return str(e)
def _pack(rowp, colp, rpart=[], cpart=[], colors=[]):
return { 'rowp': rowp, 'colp': colp, 'rpart': rpart, 'cpart': cpart,
'color_groups': colors }