From 528e652985798bca825bf1fed09edfe42a4037ee Mon Sep 17 00:00:00 2001 From: Miles Lubin Date: Fri, 10 Apr 2015 22:01:49 -0400 Subject: [PATCH] handle duplicates in MPS writer, fixes #424 --- src/writers.jl | 23 +++++++++++------------ test/model.jl | 6 +++--- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/writers.jl b/src/writers.jl index a217e7e0b60..8661ba20364 100644 --- a/src/writers.jl +++ b/src/writers.jl @@ -6,7 +6,7 @@ function writeMPS(m::Model, fname::String) f = open(fname, "w") - write(f,"NAME MathProgModel\n") + write(f,"NAME JuMPModel\n") numRows = length(m.linconstr) @@ -47,31 +47,30 @@ function writeMPS(m::Model, fname::String) nnz += length(objaff.coeffs) - colval = Array(Int,nnz) - rownzval = Array(Float64,nnz) + I = Array(Int,nnz) + J = Array(Int,nnz) + V = 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] + I[nnz] = c + J[nnz] = vars[ind].col + V[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] + I[nnz] = numRows+1 + J[nnz] = objaff.vars[ind].col + V[nnz] = objlincoef[ind] end - rowptr[numRows+2] = nnz + 1 - rowmat = SparseMatrixCSC(m.numCols,numRows+1, rowptr, colval, rownzval) - colmat = rowmat' + colmat = sparse(I,J,V,numRows+1,m.numCols) colptr = colmat.colptr rowval = colmat.rowval nzval = colmat.nzval diff --git a/test/model.jl b/test/model.jl index 845f1f656ea..cfde8186848 100644 --- a/test/model.jl +++ b/test/model.jl @@ -54,7 +54,7 @@ facts("[model] Test printing a model") do @defConstrRef constraints[1:3] constraints[1] = @addConstraint(modA, 2 <= x+y <= 4) constraints[2] = @addConstraint(modA, sum{r[i],i=3:5} <= (2 - x)/2.0) - constraints[3] = @addConstraint(modA, 7.0*y <= z + r[6]/1.9) + constraints[3] = @addConstraint(modA, 6y + y <= z + r[6]/1.9) ##################################################################### # Test LP writer writeLP(modA, modPath * "A.lp") @@ -65,7 +65,7 @@ facts("[model] Test printing a model") do "c1: 1 VAR1 + 1 VAR2 >= 2", "c2: 1 VAR1 + 1 VAR2 <= 4", "c3: 1 VAR4 + 1 VAR5 + 1 VAR6 + .5 VAR1 <= 1", - "c4: 7 VAR2 - 1 VAR3 - .5263157894736842 VAR7 <= 0", + "c4: 6 VAR2 + 1 VAR2 - 1 VAR3 - .5263157894736842 VAR7 <= 0", "Bounds", "0 <= VAR1 <= +inf", "-inf <= VAR2 <= 5", @@ -89,7 +89,7 @@ facts("[model] Test printing a model") do # Test MPS writer writeMPS(modA, modPath * "A.mps") modAMPS = ASCIIString[ - "NAME MathProgModel", + "NAME JuMPModel", "ROWS", " N CON4", " E CON1",