-
Notifications
You must be signed in to change notification settings - Fork 1
/
decomp_simple.py
323 lines (252 loc) · 9.99 KB
/
decomp_simple.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
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
from gurobipy import Model, GRB, read, LinExpr, disposeDefaultEnv
import gurobipy as gp
import matplotlib.pyplot as plt
import numpy as np
from scipy.sparse import csr_matrix
import time
import os
import pandas as pd
import importlib
def read_model(instance):
m = read('collection/'+instance+'.mps.gz')
m_new = read('collection/'+instance+'.mps.gz')
m_new1 = read('collection/'+instance+'.mps.gz')
m_new2 = read('collection/'+instance+'.mps.gz')
return m, m_new, m_new1, m_new2
def get_info(m):
A = m.getA()
#print(A)
# plot sparsity of A
#plt.spy(A,markersize=0.5)
#plt.show()
x = m.getVars()
for i in range(len(x)):
x[i].VarName = 'x_'+str(i)
con = m.getConstrs()
#print(len(x),len(con))
sizeA = A.get_shape()
#print(sizeA)
A.eliminate_zeros()
nonzeros = A.nonzero()
# get rhs of orgininal problem
RHS = m.getAttr('RHS')
SENSE = m.getAttr('Sense')
# Get the variables in the model
vars = m.getVars()
return A, x, con, sizeA, nonzeros, RHS, SENSE, vars
# def solve_LP():
def decomp_model(A, sizeA, con, nonzeros, vars, HGtype, instance, nBlocks):
if HGtype == 'r':
print("Create Row-Net Hypergraph")
# Create Row-Net Hypergraph
rNetHg = []
for i in range(sizeA[0]):
rNetHg.append(A.getrow(i).nonzero()[1])
nNodes = sizeA[1]+round(len(nonzeros)*0.2)
nHedges = sizeA[0]
wrtStr = str(nHedges)+'\t'+str(nNodes)+'\n'
for i in range(nHedges):
for k in range(len(rNetHg[i])):
wrtStr += str(rNetHg[i][k]+1)
if k < len(rNetHg[i])-1:
wrtStr += '\t'
wrtStr += '\n'
print(os.getcwd())
os.chdir('Tests/readMPS/')
f = open('HGraphFiles/'+instance+'rHG','w')
f.write(wrtStr)
f.close
# Use hmetis to decompose the constraint matrix
os.chdir('../../hmetis-1.5-osx-i686/')
#os.chdir('../../hmetis-1.5-linux/')
open('../Tests/readMPS/HGraphFiles/'+instance+'rHG')
print('shmetis ../Tests/readMPS/HGraphFiles/'+instance+'rHG '+str(nBlocks)+' 1')
print("Current directory:", os.getcwd())
#os.system('/bin/bash shmetis ../Tests/readMPS/HGraphFiles/'+instance+'rHG '+str(nBlocks)+' 1')
if not os.path.exists('HGraphFiles/'+instance+'rHG.part.'+str(nBlocks)):
os.system('./shmetis ../Tests/readMPS/HGraphFiles/'+instance+'rHG '+str(nBlocks)+' 1')
else:
print("Hypergraph exist!!")
# Read and plot the reordered constraint matrix
#os.chdir('../')
os.chdir('../Tests/readMPS/')
print(os.getcwd())
f = open('HGraphFiles/'+instance+'rHG.part.'+str(nBlocks))
xx = f.readlines()
f.close
colgroup = {}
rowToGroup = {}
for i in range(nBlocks+1):
colgroup[i] = []
for i in range(sizeA[1]):
colgroup[int(xx[i].strip('\n'))].append(i)
rowToGroup[i] = int(xx[i].strip('\n'))
varmap = {}
ind = 0
for i in range(nBlocks):
for k in colgroup[i]:
varmap[k] = ind
ind += 1
rowgroup = {}
for i in range(nBlocks+1):
rowgroup[i] = []
for i in range(sizeA[0]):
groupind = rowToGroup[A.getrow(i).nonzero()[1][0]]
if all([rowToGroup[A.getrow(i).nonzero()[1][k]] == groupind for k in range(len(A.getrow(i).nonzero()[1]))]):
rowgroup[groupind].append(i)
else:
rowgroup[nBlocks].append(i)
rowmap = {}
ind = 0
for i in range(nBlocks+1):
for k in rowgroup[i]:
rowmap[k] = ind
ind += 1
A_coo = A.tocoo(copy=True)
A_row = A_coo.row
A_col = A_coo.col
A_data = A_coo.data
A_reord_row = [rowmap[i] for i in A_row]
A_reord_col = [varmap[i] for i in A_col]
# A_reord is only for plotting, its corresponding data is incorrect
A_reord = csr_matrix((A_data,(A_reord_row,A_reord_col)))
# plot
plt.spy(A_reord,markersize=0.5)
plt.show()
os.chdir('../../')
# Print the names of the variables
#for v in vars:
#print(v.varName)
var_group = [x.strip() for x in xx]
counter = {}
for element in var_group:
counter[element] = counter.get(element, 0) + 1
element_counts = list(counter.items())
element_counts.sort(key=lambda x: x[1])
lowest_count = element_counts[0][1]
highest_count = element_counts[-1][1]
#get the row need to add slack variable
add_slack = []
for i in range(A.shape[0]):
ct = []
for n in A.getrow(i).nonzero()[1]:
ct.append(var_group[n])
#if i % 10 ==0:
#print(np.unique(np.array(ct)))
if len(np.unique(np.array(ct))) == 1:
#print('no add')
add_slack.append(True)
else:
#print('add')
add_slack.append(False)
num_linking_cons = np.sum(add_slack)
return add_slack, num_linking_cons, lowest_count, highest_count
def solve_decomp_with_slack(A, vars, RHS, SENSE, add_slack):
new_model = Model()
new_model.modelSense = GRB.MINIMIZE
new_model.update()
# add variables to the model
X_vars = [0 for i in range(A.shape[1])]
for i in range(A.shape[1]):
curVar = new_model.addVar(lb = vars[i].lb, ub = vars[i].ub, vtype = vars[i].vtype,
name = "X" + str(i))
X_vars[i] = curVar
new_model.update()
slack_vars = []
for i in range(A.shape[0]):
ConsExpr = LinExpr()
for j in A.getrow(i).nonzero()[1]:
ConsExpr += A[i,j]*X_vars[j]
if add_slack[i] and SENSE[i] == "<":
cur_slack = new_model.addVar(vtype = GRB.CONTINUOUS,
name = "S" + str(i))
slack_vars.append(cur_slack)
ConsExpr -= cur_slack
elif add_slack[i] and SENSE[i] == ">":
cur_slack = new_model_all.addVar(vtype = GRB.CONTINUOUS,
name = "S" + str(i))
slack_vars_all.append(cur_slack)
ConsExpr += cur_slack
elif add_slack[i] and SENSE[i] == "=":
cur_slack1 = new_model.addVar(vtype = GRB.CONTINUOUS,
name = "S" + str(i) + '_1')
cur_slack2 = new_model.addVar(vtype = GRB.CONTINUOUS,
name = "S" + str(i) + '_2')
slack_vars.append(cur_slack1)
slack_vars.append(cur_slack2)
ConsExpr += cur_slack1 - cur_slack2
new_model.addConstr(lhs = ConsExpr, sense = SENSE[i], rhs = RHS[i], name = 'Constr'+str(i))
new_model.update()
objExpr = LinExpr()
for i in range(len(slack_vars)):
objExpr += slack_vars[i]
new_model.setObjective(objExpr, GRB.MINIMIZE)
new_model.setParam(GRB.Param.Seed, 77)
start_time = time.time()
new_model.optimize()
end_time = time.time()
feasibility_time = end_time - start_time
status = new_model.status
print(new_model.getObjective().getValue())
return feasibility_time, status
def solve_obj_0(m_new1):
# set the obj to 0 for the original problem to get a feasible solution
# Set a new objective function
m_new1.setObjective(0, GRB.MINIMIZE)
m_new1.setParam(GRB.Param.Seed, 77)
start_time = time.time()
# Optimize the modified model
m_new1.optimize()
end_time = time.time()
obj_to_0_time = end_time - start_time
status = m_new1.status
print(m_new1.getObjective().getValue())
return obj_to_0_time, status
def solve_all_add_slack(A, vars, SENSE, RHS):
# all add slack variable
new_model_all = Model()
new_model_all.modelSense = GRB.MINIMIZE
new_model_all.update()
X_vars_all = [0 for i in range(A.shape[1])]
for i in range(A.shape[1]):
curVar = new_model_all.addVar(lb = vars[i].lb, ub = vars[i].ub, vtype = vars[i].vtype,
name = "X" + str(i))
X_vars_all[i] = curVar
new_model_all.update()
slack_vars_all = []
for i in range(A.shape[0]):
ConsExpr = LinExpr()
for j in A.getrow(i).nonzero()[1]:
ConsExpr += A[i,j]*X_vars_all[j]
if SENSE[i] == "<":
cur_slack = new_model_all.addVar(vtype = GRB.CONTINUOUS,
name = "S" + str(i))
slack_vars_all.append(cur_slack)
ConsExpr -= cur_slack
elif SENSE[i] == ">":
cur_slack = new_model_all.addVar(vtype = GRB.CONTINUOUS,
name = "S" + str(i))
slack_vars_all.append(cur_slack)
ConsExpr += cur_slack
elif SENSE[i] == "=":
cur_slack1 = new_model_all.addVar(vtype = GRB.CONTINUOUS,
name = "S" + str(i) + '_1')
cur_slack2 = new_model_all.addVar(vtype = GRB.CONTINUOUS,
name = "S" + str(i) + '_2')
slack_vars_all.append(cur_slack1)
slack_vars_all.append(cur_slack2)
ConsExpr += cur_slack1 - cur_slack2
new_model_all.addConstr(lhs = ConsExpr, sense = SENSE[i], rhs = RHS[i], name = 'Constr'+str(i))
new_model_all.update()
objExpr = LinExpr()
for i in range(len(slack_vars_all)):
objExpr += slack_vars_all[i]
new_model_all.setObjective(objExpr, GRB.MINIMIZE)
new_model_all.setParam(GRB.Param.Seed, 77)
start_time = time.time()
new_model_all.optimize()
end_time = time.time()
all_add_slack_time = end_time - start_time
status = new_model_all.status
print(new_model_all.getObjective().getValue())
return all_add_slack_time, status