Skip to content

Commit

Permalink
Improve MPS reader (#36)
Browse files Browse the repository at this point in the history
* more efficient implementation of split

* Remove unused code

* Make MPS reader stand-alone.

* Rename readmps! to loadproblem!
  • Loading branch information
mtanneau authored Jan 12, 2020
1 parent d4de72c commit 1a347d0
Show file tree
Hide file tree
Showing 6 changed files with 478 additions and 294 deletions.
2 changes: 1 addition & 1 deletion examples/infeasible.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function ex_infeasible(::Tv) where{Tv<:Real}
m.env.verbose = 1

# Read problem from .mps file and solve
TLP.readmps!(m, joinpath(INSTANCE_DIR, "lpex_inf.mps"))
TLP.loadproblem!(m, joinpath(INSTANCE_DIR, "lpex_inf.mps"))
TLP.optimize!(m)

# Check status
Expand Down
2 changes: 1 addition & 1 deletion examples/optimal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function ex_optimal(::Tv) where{Tv<:Real}
m.env.verbose = 1

# Read problem and solve
TLP.readmps!(m, joinpath(INSTANCE_DIR, "lpex_opt.mps"))
TLP.loadproblem!(m, joinpath(INSTANCE_DIR, "lpex_opt.mps"))
TLP.optimize!(m)

# Check status
Expand Down
2 changes: 1 addition & 1 deletion examples/unbounded.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function ex_unbounded(::Tv) where{Tv<:Real}
m.env.verbose = 1

# Read problem from .mps file and solve
TLP.readmps!(m, joinpath(INSTANCE_DIR, "lpex_ubd.mps"))
TLP.loadproblem!(m, joinpath(INSTANCE_DIR, "lpex_ubd.mps"))
TLP.optimize!(m)

# Check status
Expand Down
46 changes: 46 additions & 0 deletions src/Model/Model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,52 @@ mutable struct Model{Tv<:Real}
return m
end
end

"""
loadprolem!(m, filename)
Read problem from file `filename` and load it into `m`.
"""
function loadproblem!(m::Model{Tv}, filename::String) where{Tv<:Real}
empty!(m)

pb = ProblemData{Tv}()

dat = readmps(filename)

# Create rows
conidx = ConstrId[]
sizehint!(conidx, dat.ncon)
for (i, (cname, (bt, lb, ub))) in enumerate(zip(dat.connames, dat.conbounds))
cidx = new_constraint_index!(pb)
constr = LinearConstraint{Tv}(cidx, cname, lb, ub)
add_constraint!(pb, constr)
push!(conidx, cidx)
end

# Create variables
varidx = VarId[]
sizehint!(varidx, dat.nvar)
for (j, (vname, c, (bt, lb, ub))) in enumerate(zip(dat.varnames, dat.c, dat.varbounds))
vidx = new_variable_index!(pb)
var = Variable{Tv}(vidx, vname, c, lb, ub)
add_variable!(pb, var)
push!(varidx, vidx)
end

# Add coefficients
for (i, j, v) in zip(dat.aI, dat.aJ, dat.aV)
set_coeff!(pb, varidx[j], conidx[i], v)
end

# Set objective sense and offset
pb.obj_const = dat.c0

m.name = dat.name
m.pbdata_raw = pb

return m
end

function empty!(m::Model{Tv}) where{Tv<:Real}
# Empty model
Expand Down
Loading

0 comments on commit 1a347d0

Please sign in to comment.