forked from jump-dev/JuMP.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriters.jl
323 lines (299 loc) · 9.42 KB
/
writers.jl
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
function writeMPS(m::Model, fname::String)
f = open(fname, "w")
write(f,"NAME MathProgModel\n")
numRows = length(m.linconstr)
# Objective and constraint names
gc_disable()
write(f,"ROWS\n")
write(f," N CON$(numRows+1)\n")
hasrange = false
for c in 1:numRows
rowsense = sense(m.linconstr[c])
if rowsense == :(<=)
senseChar = 'L'
elseif rowsense == :(==)
senseChar = 'E'
elseif rowsense == :(>=)
senseChar = 'G'
else
hasrange = true
senseChar = 'E'
end
@printf(f," %c CON%d\n",senseChar,c)
end
gc_enable()
# Load rows into SparseMatrixCSC
gc_disable()
rowptr = Array(Int,numRows+2)
nnz = 0
for c in 1:numRows
nnz += length(m.linconstr[c].terms.coeffs)
end
objaff::AffExpr = m.obj.aff
objlincoef = objaff.coeffs
if m.objSense == :Max
println("Warning, MPS does not support maximization sense. Flipping objective coefficients.")
objlincoef = -objaff.coeffs
end
nnz += length(objaff.coeffs)
colval = Array(Int,nnz)
rownzval = Array(Float64,nnz)
nnz = 0
for c in 1:numRows
rowptr[c] = nnz + 1
# TODO: type assertion shouldn't be necessary
constr::LinearConstraint = m.linconstr[c]
coeffs = constr.terms.coeffs
vars = constr.terms.vars
for ind in 1:length(coeffs)
nnz += 1
colval[nnz] = vars[ind].col
rownzval[nnz] = coeffs[ind]
end
end
rowptr[numRows+1] = nnz + 1
for ind in 1:length(objaff.coeffs)
nnz += 1
colval[nnz] = objaff.vars[ind].col
rownzval[nnz] = objlincoef[ind]
end
rowptr[numRows+2] = nnz + 1
rowmat = SparseMatrixCSC(m.numCols,numRows+1, rowptr, colval, rownzval)
colmat = rowmat'
colptr = colmat.colptr
rowval = colmat.rowval
nzval = colmat.nzval
gc_enable()
# Output each column
gc_disable()
inintegergroup = false
write(f,"COLUMNS\n")
for col in 1:m.numCols
t = m.colCat[col]
(t == :SemiCont || t == :SemiInt) && error("The MPS file writer does not currently support semicontinuous or semi-integer variables")
if (t == :Bin || t == :Int) && !inintegergroup
@printf(f," MARKER 'MARKER' 'INTORG'\n")
inintegergroup = true
elseif (t == :Cont || t == :Fixed) && inintegergroup
@printf(f," MARKER 'MARKER' 'INTEND'\n")
inintegergroup = false
end
for ind in colmat.colptr[col]:(colmat.colptr[col+1]-1)
@printf(f," VAR%d CON%d ",col,rowval[ind])
print_shortest(f,nzval[ind])
println(f)
end
end
if inintegergroup
@printf(f," MARKER 'MARKER' 'INTEND'\n")
end
gc_enable()
# RHSs
gc_disable()
write(f,"RHS\n")
for c in 1:numRows
rowsense = sense(m.linconstr[c])
if rowsense != :range
@printf(f," rhs CON%d ",c)
print_shortest(f,rhs(m.linconstr[c]))
else
@printf(f," rhs CON%d ",c)
print_shortest(f,m.linconstr[c].lb)
end
println(f)
end
gc_enable()
# RANGES
if hasrange
gc_disable()
write(f,"RANGES\n")
for c in 1:numRows
rowsense = sense(m.linconstr[c])
if rowsense == :range
@printf(f," rhs CON%d ",c)
print_shortest(f,m.linconstr[c].ub-m.linconstr[c].lb)
println(f)
end
end
end
# BOUNDS
gc_disable()
write(f,"BOUNDS\n")
for col in 1:m.numCols
if m.colLower[col] == 0
if m.colUpper[col] != Inf
# Default lower 0, and an upper
@printf(f," UP BOUND VAR%d ", col)
print_shortest(f, m.colUpper[col])
println(f)
end
elseif m.colLower[col] == -Inf && m.colUpper[col] == +Inf
# Free
@printf(f, " FR BOUND VAR%d\n", col)
elseif m.colLower[col] != -Inf && m.colUpper[col] == +Inf
# No upper, but a lower
@printf(f, " PL BOUND VAR%d\n LO BOUND VAR%d ",col,col)
print_shortest(f,m.colLower[col])
println(f)
elseif m.colLower[col] == -Inf && m.colUpper[col] != +Inf
# No lower, but a upper
@printf(f," MI BOUND VAR%d\n UP BOUND VAR%d ",col,col)
print_shortest(f,m.colUpper[col])
println(f)
else
# Lower and upper
@printf(f, " LO BOUND VAR%d ",col)
print_shortest(f,m.colLower[col])
println(f)
@printf(f, " UP BOUND VAR%d ",col)
print_shortest(f,m.colUpper[col])
println(f)
end
end
gc_enable()
# Quadratic objective
gc_disable()
if length(m.obj.qvars1) != 0
write(f,"QMATRIX\n")
qv1 = m.obj.qvars1
qv2 = m.obj.qvars2
qc = m.obj.qcoeffs
for ind = 1:length(qv1)
if qv1[ind].col == qv2[ind].col
# Diagonal element
@printf(f," VAR%d VAR%d ", qv1[ind].col,qv2[ind].col)
print_shortest(f,2qc[ind])
println(f)
else
# Off diagonal, and we're gonna assume no duplicates
@printf(f, " VAR%d VAR%d ", qv1[ind].col,qv2[ind].col)
print_shortest(f, qc[ind])
println(f)
@printf(f, " VAR%d VAR%d ", qv2[ind].col,qv1[ind].col)
print_shortest(f, qc[ind])
println(f)
end
end
end
write(f,"ENDATA\n")
close(f)
gc_enable()
end
###############################################################################
# LP File Writer
# We use the formatting defined at:
# http://lpsolve.sourceforge.net/5.0/CPLEX-format.htm
function writeLP(m::Model, fname::String)
f = open(fname, "w")
if length(m.obj.qvars1) != 0
error("LP writer does not support quadratic objectives.\n")
end
# Objective
if m.objSense == :Max
write(f,"Maximize\n")
else
write(f,"Minimize\n")
end
objaff::AffExpr = m.obj.aff
write(f, " obj: ")
nnz = length(objaff.coeffs)
for ind in 1:(nnz-1)
print_shortest(f, objaff.coeffs[ind])
@printf(f, " VAR%d + ", objaff.vars[ind].col)
end
if nnz >= 1
print_shortest(f, objaff.coeffs[nnz])
@printf(f, " VAR%d\n", objaff.vars[nnz].col)
end
# Constraints
function writeconstrterms(c::LinearConstraint)
nnz = length(c.terms.coeffs)
for ind in 1:(nnz-1)
print_shortest(f, c.terms.coeffs[ind])
@printf(f, " VAR%d + ", c.terms.vars[ind].col)
end
if nnz >= 1
print_shortest(f, c.terms.coeffs[nnz])
@printf(f, " VAR%d", c.terms.vars[nnz].col)
end
end
write(f,"Subject To\n")
constrcount = 1
for i in 1:length(m.linconstr)
@printf(f, " c%d: ", constrcount)
c::LinearConstraint = m.linconstr[i]
rowsense = sense(c)
if rowsense != :range
writeconstrterms(c)
if rowsense == :(==)
@printf(f, " = ")
print_shortest(f, rhs(c))
println(f)
elseif rowsense == :<=
@printf(f, " <= ")
print_shortest(f, rhs(c))
println(f)
else
@assert rowsense == :>=
@printf(f, " >= ")
print_shortest(f, rhs(c))
println(f)
end
constrcount += 1
else
writeconstrterms(c)
@printf(f, " >= ")
print_shortest(f, c.lb)
println(f)
@printf(f, " c%d: ", constrcount+1)
writeconstrterms(c)
@printf(f, " <= ")
print_shortest(f, c.ub)
println(f)
constrcount += 2
end
end
# Bounds
write(f,"Bounds\n")
for i in 1:m.numCols
if m.colLower[i] == -Inf
# No low bound
if m.colUpper[i] == +Inf
# Free
@printf(f, " VAR%d free\n", i)
else
# x <= finite
@printf(f, " -inf <= VAR%d <= ", i)
print_shortest(f, m.colUpper[i])
println(f)
end
else
# Low bound exists
if m.colUpper[i] == +Inf
# x >= finite
@printf(f, " ")
print_shortest(f, m.colLower[i])
@printf(f," <= VAR%d <= +inf\n", i)
else
# finite <= x <= finite
@printf(f, " ")
print_shortest(f, m.colLower[i])
@printf(f, " <= VAR%d <= ", i)
print_shortest(f, m.colUpper[i])
println(f)
end
end
end
# Integer - don't handle binaries specially
write(f,"General\n")
for i in 1:m.numCols
t = m.colCat[i]
(t == :SemiCont || t == :SemiInt) && error("The LP file writer does not currently support semicontinuous or semi-integer variables")
if t == :Bin || t == :Int
@printf(f, " VAR%d\n", i)
end
end
# Done
write(f,"End\n")
close(f)
end