-
Notifications
You must be signed in to change notification settings - Fork 0
/
mps.py
296 lines (245 loc) · 9.21 KB
/
mps.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
#!/usr/bin/python
#
# mps.py
#
# Routines for files in MPS format.
#
# Copyright (c) 2007, 2008, 2012 Marco Colombo
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# See http://www.gnu.org/licenses/gpl.txt for a copy of the license.
#
from collections import OrderedDict
from numpy import array, unique
from scipy import sparse
class Mps:
# common error messages
errNumEntries = "Expected exactly %s entries in the %s section."
errFieldOrder = "Unexpected field ordering in the %s section."
errUnknownRow = "Unknown row '%s' in the %s section."
errUnknownCol = "Unknown column '%s' in the %s section."
errLastParsed = "\nLast line parsed: %s."
def __init__(self, mpsfile):
'''Constructor.'''
self.mpsfile = mpsfile
self.rowNames = OrderedDict()
self.rowTypes = {}
self.colNames = {}
self.rhsNames = {}
self.objName = None
try:
# read the MPS file
mps = open(self.mpsfile, 'r')
self.__parseRows(mps)
self.__parseColumns(mps)
self.__parseRhs(mps)
self.__parseBounds(mps)
self.__deleteEmptyRows()
mps.close()
except IOError:
msg = "Could not open file '" + self.mpsfile + "'."
raise IOError(msg)
def getdata(self):
'''Get the coefficient matrix and vectors from the MPS data.'''
A = sparse.csc_matrix((array(self.data), self.rows, self.ptrs))
bounds = self.bup, self.upx, self.blo, self.lox
return A, array(self.rhs), array(self.obj), bounds
def __deleteEmptyRows(self):
# create a list to reorder the rows in case empty rows are found:
# in each position we put the correction to the row index
# e.g.: rows = [0 1 0 1 3 1 3]
# u = [0 1 3] (row 2 is empty)
# adjust = [0 0 0 1]
# so that the reordered array is
# rows = [0 1 0 1 2 1 2]
adjust = []
removed = 0
# get an ordered list of the row indices
u = unique(self.rows)
idxempty = [x for x in range(len(self.rowNames)) if x not in u]
# go through all row indices to compute the adjustment
keys, idx = self.rowNames.keys(), 0
for key in keys:
if idx in idxempty:
del self.rowNames[key]
del self.rowTypes[key]
try:
del self.rhsNames[key]
except KeyError:
pass
removed += 1
adjust.append(removed)
idx += 1
# from each row index subtract the number of removed rows
try:
self.rows = [i - adjust[i] for i in self.rows]
except:
print "Error in __deleteEmptyRows()."
raise
# update the indices stored in self.rowNames and create a dense rhs
self.rhs = [0] * len(self.rowNames)
idx = 0
for key in self.rowNames.iterkeys():
self.rowNames[key] = idx
try:
self.rhs[idx] = self.rhsNames[key]
except:
pass
idx += 1
if (removed):
print "Removed %d empty rows." % removed
def __parseRows(self, mps):
rowIndex = 0
for line in mps:
line = line.split()
try:
if line[0] == "COLUMNS":
break
if line[0] == "N":
self.objName = line[1]
continue
if line[0] == "NAME" or line[0] == "ROWS" or line[0][0] == "*":
continue
except IndexError:
# skip an empty line
continue
if (len(line) != 2):
msg = self.errNumEntries % ("2", "ROWS")
msg += self.errLastParsed % str(line)
raise IndexError(msg)
self.rowNames[line[1]] = rowIndex
self.rowTypes[line[1]] = line[0]
rowIndex += 1
if len(self.rowNames) == 0:
msg = "Empty ROWS section in MPS file."
raise ValueError(msg)
def __parseColumns(self, mps):
prev, nnnz, indx = 0, 0, 0
data, rows, ptrs, obj = [], [], [], []
for line in mps:
line = line.split()
try:
if line[0] == "RHS":
break
if line[0][0] == "*":
continue
except IndexError:
# skip an empty line
continue
if (len(line) != 3 and len(line) != 5):
msg = self.errNumEntries % ("3 or 5", "COLUMNS")
msg += self.errLastParsed % str(line)
raise IndexError(msg)
if line[0] != prev:
self.colNames[line[0]] = indx
ptrs.append(nnnz)
prev = line[0]
indx += 1
obj.append(0)
# no need to access the variable name anymore
line = line[1:]
while len(line) > 0:
rowname = line[0]
try:
if rowname == self.objName:
obj[indx - 1] = float(line[1])
else:
rows.append(self.rowNames[rowname])
data.append(float(line[1]))
nnnz += 1
except KeyError:
msg = self.errUnknownRow % (rowname, "COLUMNS")
raise ValueError(msg)
except ValueError:
msg = self.errFieldOrder % "COLUMNS"
msg += self.errLastParsed % str(line)
raise IndexError(msg)
# remove the parts of the line just parsed
line = line[2:]
if len(self.colNames) == 0:
msg = "Empty COLUMNS section in MPS file."
raise ValueError(msg)
# add the last element
ptrs.append(nnnz)
self.data, self.rows, self.ptrs, self.obj = data, rows, ptrs, obj
def __parseRhs(self, mps):
for line in mps:
line = line.split()
try:
if line[0] == "ENDATA" or line[0] == "BOUNDS":
break
if line[0] == "*":
continue
except IndexError:
# skip an empty line
continue
if (len(line) < 2 or len(line) > 5):
msg = self.errNumEntries % ("3 or 5", "RHS")
msg += self.errLastParsed % str(line)
raise IndexError(msg)
index = len(line) - 1
while (index > 0):
try:
row = line[index - 1]
self.rowNames[row] # ensure that this row name exists
self.rhsNames[row] = float(line[index])
except KeyError:
# ignore the assignment of right-hand side to the objective
if (self.objName == line[index - 1]): pass
else:
msg = self.errUnknownRow % (line[index - 1], "RHS")
raise ValueError(msg)
except ValueError:
# line[index] is not a numerical value
msg = self.errFieldOrder % "RHS"
msg += self.errLastParsed % str(line)
raise IndexError(msg)
line = line[:-2]
index -= 2
def __parseBounds(self, mps):
# create sparse vectors for the bounds
bup, upx = [], []
blo, lox = [], []
for line in mps:
line = line.split()
try:
if line[0] == "ENDATA":
break
if line[0] == "*":
continue
except IndexError:
# skip an empty line
continue
if line[0] == "RANGES":
msg = "RANGES section not supported."
raise NotImplementedError(msg)
if (line[0] == "FR"):
msg = "Bound type FR not supported."
raise NotImplementedError(msg)
if (len(line) < 3 or len(line) > 4):
msg = self.errNumEntries % ("4", "BOUNDS")
msg += self.errLastParsed % str(line)
raise IndexError(msg)
try:
value = float(line[-1])
index = self.colNames[line[-2]]
except KeyError:
msg = self.errUnknownCol % (line[-2], "BOUNDS")
raise ValueError(msg)
if (line[0] == "UP"):
bup.append(value)
upx.append(index)
elif (line[0] == "LO"):
blo.append(value)
lox.append(index)
elif (line[0] == "FX"):
bup.append(value)
upx.append(index)
blo.append(value)
lox.append(index)
self.bup, self.upx = bup, upx
self.blo, self.lox = blo, lox