-
Notifications
You must be signed in to change notification settings - Fork 0
/
QPLIBReader.py
395 lines (322 loc) · 14.2 KB
/
QPLIBReader.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 7 09:56:21 2020
@author: aoust
"""
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 6 16:48:36 2020
@author: aoust
"""
from QuadraticPolynomial import QuadraticPolynomial
import numpy as np
import os
import pandas as pd
class QPLIBReader():
def __init__(self,filename):
self.f = open(filename)
self.name = str(self.f.readline()).replace("\n","")
self.instance_type = str(self.f.readline()).replace("\n","")
self.sense = self.f.readline().replace("\n","")
line = self.f.readline()
self.n = int(line.split("#")[0])
self.__objective_polynomial = QuadraticPolynomial(self.n,[],[])
self.__constraints_polynomials = []
#Parsing
self.__read_m()
self.__read_obj_quad_part()
self.__read_obj_lin_part()
self.__read_obj_const_part()
self.__read_const_quad_part()
self.__read_const_lin_part()
self.__read_infinite_value()
self.__read_const_bounds()
self.__read_bounds()
self.__read_var_types()
#Processing
self.__set_binary_bounds()
#Security check
self.__check_pol()
self.__check_bounds()
#Closing file
self.f.close()
print("Nb const polynomial = {0}".format(len(self.__constraints_polynomials)))
def is_treatable(self):
return (-self.infinity<min(self.__LB)) and (self.infinity>max(self.__UB))
def objective_polynomial(self):
assert(self.sense in ["minimize","maximize"])
if self.sense=="minimize":
return self.__objective_polynomial
else:
newPoly = QuadraticPolynomial(self.n,list(self.__objective_polynomial.tuples), np.array(list(-self.__objective_polynomial.coefs)))
return newPoly
def nonnegative_constraints_polynomials(self):
"""Return the nonnegative polynomials defining the set of constraints """
output = []
#Constraints polynomials
for i in range(len(self.__constraints_polynomials)):
poly = self.__constraints_polynomials[i]
if self.__constraints_LHS[i]>-self.infinity:
new_poly = QuadraticPolynomial(poly.n, list(poly.tuples), list(poly.coefs))
new_poly.tuples.append((-1,-1))
new_poly.coefs.append(-self.__constraints_LHS[i])
new_poly.check()
output.append(new_poly)
if self.__constraints_RHS[i]<self.infinity:
new_poly = QuadraticPolynomial(poly.n, list(poly.tuples), list(-poly.coefs))
new_poly.tuples.append((-1,-1))
new_poly.coefs.append(self.__constraints_RHS[i])
new_poly.check()
output.append(new_poly)
#TO DELETE: Binary constraints as quadratic constraints
# for i in range(self.n):
# if self.__type_var[i]==2:
# new_poly = QuadraticPolynomial(self.n, [(-1,i),(i,i)], [1,-1])
# new_poly.check()
# output.append(new_poly)
# new_poly = QuadraticPolynomial(self.n, [(-1,i),(i,i)], [-1,1])
# new_poly.check()
# output.append(new_poly)
return output
def bounds(self):
return self.__LB, self.__UB
def __read_m(self):
self.m = 0
if not(self.instance_type[2] in ["N","B"]):
line = self.f.readline()
sp = (line.split("#"))
assert("constraints" in sp[1])
self.m = int(sp[0])
for i in range(self.m):
self.__constraints_polynomials.append(QuadraticPolynomial(self.n,[],[]))
def __read_obj_quad_part(self):
if not(self.instance_type[0] in ["L"]):
line = self.f.readline()
sp = (line.split("#"))
assert("quadratic terms in objective" in sp[1])
terms_number = int(sp[0])
for k in range(terms_number):
line = self.f.readline()
split = line.split(" ")
assert(len(split)==3)
i,j, coef = int(split[0]),int(split[1]), float(split[2])
assert(i>=j)
assert(j>=1)
# Offset in variables
self.__objective_polynomial.tuples.append((j-1,i-1))
#Reminder : the quadratic term is 1/2* x^TQx
if (i>j):
self.__objective_polynomial.coefs.append(coef)
else:
assert(i==j)
self.__objective_polynomial.coefs.append(0.5*coef)
def __read_obj_lin_part(self):
line = self.f.readline()
sp = (line.split("#"))
assert("default value for linear coefficients in objective" in sp[1])
defaut_value = float(sp[0])
line = self.f.readline()
sp = (line.split("#"))
assert("number of non-default linear coefficients in objective" in sp[1])
terms_number = int(sp[0])
if defaut_value!=0.0:
b = [defaut_value] * self.n
for k in range(terms_number):
line = self.f.readline()
split = line.split(" ")
assert(len(split)==2)
i, coef = int(split[0]),float(split[1])
assert(i>=1)
b[i-1] = coef
self.__objective_polynomial.coefs.extend(b)
tup = [(-1,i) for i in range(self.n)]
self.__objective_polynomial.tuples.extend(tup)
else:
for k in range(terms_number):
line = self.f.readline()
split = line.split(" ")
assert(len(split)==2)
i, coef = int(split[0]),float(split[1])
assert(i>=1)
assert(i<=self.n)
self.__objective_polynomial.coefs.append(coef)
self.__objective_polynomial.tuples.append((-1,i-1))
def __read_obj_const_part(self):
line = self.f.readline()
sp = (line.split("#"))
assert("objective constant" in sp[1])
obj_constant = float(sp[0])
self.__objective_polynomial.coefs.append(obj_constant)
self.__objective_polynomial.tuples.append((-1,-1))
def __read_const_quad_part(self):
if not(self.instance_type[2] in ["N","B","L"]):
line = self.f.readline()
sp = (line.split("#"))
assert("number of quadratic terms in all constraints" in sp[1])
terms_number = int(sp[0])
for k in range(terms_number):
line = self.f.readline()
split = line.split(" ")
assert(len(split)==4)
idx,i,j,coef = int(split[0]),int(split[1]),int(split[2]),float(split[3])
assert((idx<=self.m) and (idx>=1))
assert((i<=self.n) and (i>=1))
assert((j<=self.n) and (j>=1))
assert(i>=j)
self.__constraints_polynomials[idx-1].tuples.append((j-1,i-1))
if (i>j):
self.__constraints_polynomials[idx-1].coefs.append(coef)
else:
self.__constraints_polynomials[idx-1].coefs.append(0.5*coef)
def __read_const_lin_part(self):
if not(self.instance_type[2] in ["N","B"]):
line = self.f.readline()
sp = (line.split("#"))
assert("number of linear terms in all constraints" in sp[1])
terms_number = int(sp[0])
for k in range(terms_number):
line = self.f.readline()
split = line.split(" ")
assert(len(split)==3)
idx,i,coef = int(split[0]),int(split[1]),float(split[2])
assert((idx<=self.m) and (idx>=1))
assert((i<=self.n) and (i>=1))
self.__constraints_polynomials[idx-1].tuples.append((-1,i-1))
self.__constraints_polynomials[idx-1].coefs.append(coef)
def __read_infinite_value(self):
line = self.f.readline()
sp = line.split("#")
assert("value for infinity" in sp[1])
self.infinity = float(sp[0])
def __read_const_bounds(self):
if not(self.instance_type[2] in ["N","B"]):
#LHS of constraints
line = self.f.readline()
sp = (line.split("#"))
assert("default left-hand-side value" in sp[1])
default_value_lb = float(sp[0])
self.__constraints_LHS = [default_value_lb]*self.m
line = self.f.readline()
sp = (line.split("#"))
assert("number of non-default left-hand-sides" in sp[1])
number = int(sp[0])
for k in range(number):
line = self.f.readline()
split = line.split(" ")
assert(len(split)==2)
idx,bound = int(split[0]),float(split[1])
assert((idx<=self.m) and (idx>=1))
self.__constraints_LHS[idx-1] = bound
#RHS of constraints
line = self.f.readline()
sp = (line.split("#"))
assert("default right-hand-side value" in sp[1])
default_value_ub = float(sp[0])
self.__constraints_RHS = [default_value_ub]*self.m
line = self.f.readline()
sp = (line.split("#"))
assert("number of non-default right-hand-sides" in sp[1])
number = int(sp[0])
for k in range(number):
line = self.f.readline()
split = line.split(" ")
assert(len(split)==2)
idx,bound = int(split[0]),float(split[1])
assert((idx<=self.m) and (idx>=1))
self.__constraints_RHS[idx-1] = bound
def __read_bounds(self):
if not(self.instance_type[1] in ["B"]):
#Lower-bounds
line = self.f.readline()
sp = (line.split("#"))
assert("default variable lower bound value" in sp[1])
default_value_lb = float(sp[0])
self.__LB = [default_value_lb]*self.n
line = self.f.readline()
sp = (line.split("#"))
assert("number of non-default variable lower bounds" in sp[1])
number = int(sp[0])
for k in range(number):
line = self.f.readline()
split = line.split(" ")
assert(len(split)==2)
idx,bound = int(split[0]),float(split[1])
assert((idx<=self.n) and (idx>=1))
self.__LB[idx-1] = bound
#Upper_bounds
line = self.f.readline()
sp = (line.split("#"))
assert("default variable upper bound value" in sp[1])
default_value_ub = float(sp[0])
self.__UB = [default_value_ub]*self.n
line = self.f.readline()
sp = (line.split("#"))
assert("number of non-default variable upper bounds" in sp[1])
number = int(sp[0])
for k in range(number):
line = self.f.readline()
split = line.split(" ")
assert(len(split)==2)
idx,bound = int(split[0]),float(split[1])
assert((idx<=self.n) and (idx>=1))
self.__UB[idx-1] = bound
else:
self.__LB = [0]*self.n
self.__UB = [1]*self.n
def __read_var_types(self):
if not(self.instance_type[1] in ["B","C","I"]):
line = self.f.readline()
sp = (line.split("#"))
assert("default variable type" in sp[1])
default_var_type = int(sp[0])
self.__type_var = [default_var_type]*self.n
line = self.f.readline()
sp = (line.split("#"))
assert("number of non-default variable types" in sp[1])
number = int(sp[0])
for k in range(number):
line = self.f.readline()
split = line.split(" ")
assert(len(split)==2)
idx, typevar = int(split[0]), int(split[1])
assert((idx<=self.n) and (idx>=1))
self.__type_var[idx-1] = typevar
else:
if self.instance_type[1] == "C":
self.__type_var = [0]*self.n
if self.instance_type[1] == "I":
self.__type_var = [1]*self.n
if self.instance_type[1] == "B":
self.__type_var = [2]*self.n
self.binary_variables = set()
for i in range(self.n):
if self.__type_var[i]==2:
self.binary_variables.add(i)
def __check_pol(self):
self.__objective_polynomial.check()
for poly in self.__constraints_polynomials:
poly.check()
assert(not((-1,-1) in poly.tuples))
def __check_bounds(self):
for i in range(self.n):
assert(self.__LB[i]<=self.__UB[i])
def __set_binary_bounds(self):
for i in range(self.n):
if self.__type_var[i]==2:
self.__UB[i] = 1.0
self.__LB[i] = 0.0
# folder = "QPLIB/qplib/html/qplib"
# files = os.listdir(folder)
# count = 0
# sizes = []
# names = []
# for f in files:
# print(f)
# P = QPLIBparser(folder+"/"+f)
# if P.is_treatable():
# sizes.append(P.n)
# names.append(P.name)
# df = pd.DataFrame()
# df["name"] = names
# df["size"] = sizes
# df.to_csv("instances.csv")