-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathComputing.py
More file actions
153 lines (119 loc) · 4.58 KB
/
Computing.py
File metadata and controls
153 lines (119 loc) · 4.58 KB
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
import numpy as np
import scipy.optimize
import accessify
class Computing:
def __init__(self, matrix):
self.__params = np.array(matrix)
@accessify.private
def FindMaxInColumnWithExcludedRows(self, column_id, excluded_rows):
col = self.__params[:, column_id]
n_rows = col.shape[0]
mask = np.ones(n_rows, dtype=bool)
if excluded_rows:
mask[list(excluded_rows)] = False
if not np.any(mask):
return -1, -1
filtered_col = col[mask]
original_indices = np.where(mask)[0]
max_idx_local = np.argmax(filtered_col)
max_val = filtered_col[max_idx_local]
row_idx = original_indices[max_idx_local]
return max_val, row_idx
@accessify.private
def FindKMinInColumnWithExcludedRows(self, column_id, excluded_rows, k=1):
col = self.__params[:, column_id]
n_rows = col.shape[0]
mask = np.ones(n_rows, dtype=bool)
if excluded_rows:
mask[list(excluded_rows)] = False
if not np.any(mask):
return -1, -1
filtered_col = col[mask]
original_indices = np.where(mask)[0]
if k == 1:
min_idx_local = np.argmin(filtered_col)
return filtered_col[min_idx_local], original_indices[min_idx_local]
k = min(k, len(filtered_col))
partitioned_indices = np.argpartition(filtered_col, k - 1)
kth_smallest_idx_local = partitioned_indices[k - 1]
kth_val = filtered_col[kth_smallest_idx_local]
return kth_val, original_indices[kth_smallest_idx_local]
def HungarianMinimum(self):
row_ind, col_ind = scipy.optimize.linear_sum_assignment(self.__params)
values = self.__params[row_ind, col_ind]
cost = values.sum()
return cost, values
def HungarianMaximum(self):
row_ind, col_ind = scipy.optimize.linear_sum_assignment(-self.__params)
values = self.__params[row_ind, col_ind]
cost = values.sum()
return cost, values
def ThriftyMethod(self):
cost = 0
_, cols = self.__params.shape
assigned_rows = set()
values = []
for i in range(cols):
min_val, row = self.FindKMinInColumnWithExcludedRows(i, assigned_rows)
if row != -1:
cost += min_val
assigned_rows.add(row)
values.append(min_val)
return cost, np.array(values)
def GreedyMethod(self):
cost = 0
_, cols = self.__params.shape
assigned_rows = set()
values = []
for i in range(cols):
max_val, row = self.FindMaxInColumnWithExcludedRows(i, assigned_rows)
if row != -1:
cost += max_val
assigned_rows.add(row)
values.append(max_val)
return cost, np.array(values)
def Greedy_ThriftyMethodX(self, x):
cost = 0
_, cols = self.__params.shape
assigned_rows = set()
values = []
for i in range(cols):
if i < x:
val, row = self.FindMaxInColumnWithExcludedRows(i, assigned_rows)
else:
val, row = self.FindKMinInColumnWithExcludedRows(i, assigned_rows)
if row != -1:
cost += val
assigned_rows.add(row)
values.append(val)
return cost, np.array(values)
def Thrifty_GreedyMethodX(self, x):
cost = 0
_, cols = self.__params.shape
assigned_rows = set()
values = []
for i in range(cols):
if i < x:
val, row = self.FindKMinInColumnWithExcludedRows(i, assigned_rows)
else:
val, row = self.FindMaxInColumnWithExcludedRows(i, assigned_rows)
if row != -1:
cost += val
assigned_rows.add(row)
values.append(val)
return cost, np.array(values)
def TkG_MethodX(self, k, x):
cost = 0
_, cols = self.__params.shape
assigned_rows = set()
values = []
for i in range(cols):
if (i < x) and (i + k < cols):
val, row = self.FindKMinInColumnWithExcludedRows(i, assigned_rows, k)
else:
val, row = self.FindMaxInColumnWithExcludedRows(i, assigned_rows)
if row != -1:
cost += val
assigned_rows.add(row)
values.append(val)
return cost, np.array(values)