-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.py
More file actions
339 lines (278 loc) · 11.1 KB
/
Functions.py
File metadata and controls
339 lines (278 loc) · 11.1 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
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
330
331
332
333
334
335
336
337
338
339
from itertools import chain, combinations
import pandas as pd
import time
import matplotlib.pyplot as plt
from gurobipy import *
import numpy as np
import networkx as nx
from sklearn.cluster import KMeans,SpectralClustering
from scipy.sparse import csr_matrix
import os
import sys
def powerset(seq):
"""
Returns all the subsets of this set. This is a generator.
"""
if len(seq) <= 1:
yield seq
yield []
else:
for item in powerset(seq[1:]):
yield [seq[0]]+item
yield item
def transformKey(variableName):
name = variableName
args = name.split('_')
if (args[0] == 'I'):
return ('I', int(args[1]), int(args[2]) )
elif (args[0] == 'z'):
return ('z', int(args[1]), int(args[2]), int(args[3]) )
elif (args[0] == 'q'):
return ('q', int(args[1]), int(args[2]), int(args[3]) )
elif (args[0] == 'y'):
return ('y', int(args[1]), int(args[2]), int(args[3]), int(args[4]) )
else:
print('Format key error!!')
return 0
def IRPCStransformKey(variableName):
name = variableName
args = name.split('_')
if (args[0] == 'I'):
return ('I', int(args[1]), int(args[2]) )
elif (args[0] == 'S'):
return ('S', int(args[1]), int(args[2]) )
elif (args[0] == 'z'):
return ('z', int(args[1]), int(args[2]) )
elif (args[0] == 'q'):
return ('q', int(args[1]), int(args[2]), int(args[3]) )
elif (args[0] == 'p'):
return ('p', int(args[1]), int(args[2]), int(args[3]) )
elif (args[0] == 'y'):
return ('y', int(args[1]), int(args[2]), int(args[3]) )
elif (args[0] == 'v'):
return ('v', int(args[1]), int(args[2]), int(args[3]), int(args[4]) )
elif (args[0] == 'x'):
return ('x', int(args[1]), int(args[2]), int(args[3]), int(args[4]) )
else:
print('Format key error!!')
return 0
def keyOpVRP(key):
elements = key.split('_')
if len(elements) == 3:
return ('z', int(elements[1]), int(elements[2]))
elif len(elements) == 4:
return ('y', int(elements[1]), int(elements[2]), int(elements[3]) )
else:
print('Format Variable Name Errors!!')
return 0
def keyOpMVRPD(key):
elements = key.split('_')
if len(elements) == 4:
return (elements[0], int(elements[1]), int(elements[2]), int(elements[3]) )
else:
print('Format Variable Name Errors!!')
return 0
def keyOpTSP(key):
return (key.split('_')[0], int(key.split('_')[1]), int(key.split('_')[2]))
def get_expr_coos(expr, var_indices):
for i in range(expr.size()):
dvar = expr.getVar(i)
yield expr.getCoeff(i), var_indices[dvar]
def get_matrix_coos(m):
dvars = m.getVars()
constrs = m.getConstrs()
var_indices = {v: i for i, v in enumerate(dvars)}
for row_idx, constr in enumerate(constrs):
for coeff, col_idx in get_expr_coos(m.getRow(constr), var_indices):
yield row_idx, col_idx, coeff
def get_expr_coos_new(expr, var_indices):
for i in range(expr.size()):
dvar = expr.getVar(i)
yield var_indices[dvar], dvar.VarName
def get_matrix_coos_new(m):
dvars = m.getVars()
constrs = m.getConstrs()
var_indices = {v: i for i, v in enumerate(dvars)}
for row_idx, constr in enumerate(constrs):
for col_idx, name in get_expr_coos_new(m.getRow(constr), var_indices):
yield row_idx, col_idx, name
def VisualizeNonZeros(path = os.path.join("MIPLIB", "SomeInstanceIRPCS25_7_3.mps" ) ):
m = read(path)
nzs = pd.DataFrame(get_matrix_coos(m), columns=['row_idx', 'col_idx', 'coeff'])
actRow = 0
varsInRow = []
for index, row in nzs.iterrows():
if row['row_idx'] == actRow:
varsInRow.append(row['col_idx'])
actRow = row.row_idx
varsInRow = []
varsInRow.append(row.col_idx)
plt.scatter(nzs.col_idx, nzs.row_idx,
marker='.', lw=0)
plt.show()
def genAffinityMatrix(
path = os.path.join("MIPLIB", "SomeInstanceIRPCS15_8_3.mps" ),
varFilter = lambda x : x[0] == 'x',
verbose = True ):
starting_time = time.time()
m = read(path)
nzs = pd.DataFrame(get_matrix_coos_new(m), columns=['row_idx', 'col_idx', 'name'])
# An undirected affinity graph is created.
graph = nx.Graph()
actRow = 0
varsInRow = []
if varFilter == None:
varFilter = lambda x : True
for index, row in nzs.iterrows():
if int(row.row_idx) == actRow and varFilter(row['name']):
varsInRow.append(row['name'])
elif int(row.row_idx) != actRow and varFilter(row['name']):
# We update the edge dictionary:
if len(varsInRow) > 1:
for n1 in varsInRow:
for n2 in varsInRow:
if n1 < n2:
if not graph.has_edge(n1, n2):
graph.add_edge(n1, n2, weight = 1)
else:
graph[n1][n2]['weight'] += 1
# The row and varaibles list parameters are set back to 0 and empty respectively.
actRow = int(row.row_idx)
varsInRow = []
if verbose:
print('Completed {} of total rows.'.format( round( index / len(nzs) , 3) ) )
# If is not filtered, we add the variable name to the list.
if varFilter(row['name']):
varsInRow.append(row['name'])
if verbose:
print('------ Affinity Matrix successfully stored. Elapsed : {} ------'.format( round(time.time() - starting_time , 3) ) )
return graph
def genClusterNeighborhoods(
path = os.path.join("MIPLIB", "SomeInstanceIRPCS20_6_3.mps" ),
nClusters = 18,
verbose = True,
fNbhs = False,
varFilter = lambda y : y[0] == 'x'):
graph = genAffinityMatrix(path, varFilter, verbose = verbose)
adj_matrix = nx.to_numpy_matrix(graph)
clusters = SpectralClustering(
affinity = 'precomputed',
assign_labels = "discretize",
random_state = 0,
n_clusters = nClusters).fit_predict(adj_matrix)
node_list = list(graph.nodes)
dLabels = { node_list[i] : clusters[i] for i in range(len(clusters))}
m = read(path)
if varFilter == None:
keyVars = list( map( lambda var: var.VarName, list(m.getVars()) ) )
else:
keyVars = list( filter ( varFilter , map( lambda var: var.VarName, list(m.getVars() ) ) ) )
incomplete = False
for ind in range(len(keyVars)):
if keyVars[ind] not in node_list:
incomplete = True
dLabels[keyVars[ind]] = ind % nClusters
if verbose and incomplete:
print('------ The key variable choice was incomplete ------')
if verbose:
print('------ Cluster labels computed ------')
if not fNbhs:
outer = {}
for i in range(nClusters):
outer[i + 1] = {
0 : tuple(filter( lambda x : dLabels[x] != i , node_list ))
}
#print("------ A {}% of neighborhoods stored ------".format( round(100 * i / nClusters, 3) ) )
return outer
else:
return dLabels
def mps_reader(file_name = os.path.join( 'MIPLIB' , 'abs1n5_1.mps' )):
for row in open(file_name, "r"):
yield row
def diffKeys(
path = os.path.join("MIPLIB", "SomeInstanceIRPCS15_8_3.mps" ),
varFilter = lambda x : x[0] == 'x',
verbose = True ):
starting_time = time.time()
m = read(path)
nzs = pd.DataFrame(get_matrix_coos_new(m), columns=['row_idx', 'col_idx', 'name'])
# An undirected affinity graph is created.
graph = nx.Graph()
actRow = 0
varsInRow = []
if varFilter == None:
varFilter = lambda x : True
nameVal = {}
indexVar = 0
for ind, var in enumerate(m.getVars()):
nameVal[var.VarName] = ind
for index, row in nzs.iterrows():
if int(row.row_idx) == actRow and varFilter(row['name']):
varsInRow.append(row['name'])
elif int(row.row_idx) != actRow and varFilter(row['name']):
# We update the edge dictionary:
if len(varsInRow) > 1:
for n1 in varsInRow:
for n2 in varsInRow:
if n1 < n2:
if not graph.has_edge(nameVal[n1], nameVal[n2]):
graph.add_edge(nameVal[n1], nameVal[n2], weight = 1)
else:
graph[nameVal[n1]][nameVal[n2]]['weight'] += 1
# The row and variables list parameters are set back to 0 and empty respectively.
actRow = int(row.row_idx)
varsInRow = []
if verbose:
print('Completed {} of total rows.'.format( round( index / len(nzs) , 3) ) )
"""# If is not filtered, we add the variable name to the list.
if varFilter(row['name']):
varsInRow.append(row['name'])"""
if verbose:
print('------ Affinity Matrix successfully stored. Elapsed : {} ------'.format( round(time.time() - starting_time , 3) ) )
print('Time Elapsed: {}'.format(time.time() - starting_time))
print('Memory used: {} bytes'.format(sys.getsizeof(graph.edges)))
print('#edges: {}'.format(graph.size()))
return graph
def previousTest(affMAtrix = False, genCluster = False, newGraph = True):
if affMAtrix:
genAffinityMatrix()
if genCluster:
gc1 = genClusterNeighborhoods( path = os.path.join( 'MIPLIB' , 'abs1n5_1.mps' ), fNbhs = False)
if newGraph:
newGraph = diffKeys(path = os.path.join('MIPLIB', 'ajs1n75_h_6.mps'), varFilter= lambda x : x[0] == 'x',verbose = False)
started = False
finished = False
varFilter = lambda x: x[0] == 'x'
rvar = {}
for i in mps_reader(file_name = os.path.join( 'MIPLIB' , 'SomeInstanceIRPCS60_12_3.mps' )):
row = i.strip('\n')
if row == 'COLUMNS':
print('column detected')
started = True
continue
elif row == 'RHS':
print('end of rows')
finished = True
if started and not finished and "'MARKER'" not in row:
tupleRow = tuple( filter(lambda x : x != '', i.strip('\n').strip(' ').split(' ') ) )
varName = tupleRow[0]
constrName = tupleRow[1]
if varFilter(varName):
if constrName in rvar.keys():
rvar[constrName].append(varName)
else:
rvar[constrName] = [varName]
print(sys.getsizeof(rvar))
keys = tuple(rvar.keys())
edges = {}
for constr in keys:
for v1 in rvar[constr]:
for v2 in rvar[constr]:
if v1 < v2:
if (v1, v2) in edges.keys():
edges[(v1, v2)] += 1
else:
edges[(v1, v2)] = 1
del rvar[constr]
print(sys.getsizeof(edges))
if __name__ == '__main__': pass