Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support .gz and .bz2 files in reader #72

Merged
merged 1 commit into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ authors = ["Mathieu Tanneau <mathieu.tanneau@gmail.com>"]
version = "0.7.0"

[deps]
CodecBzip2 = "523fee87-0ab8-5b00-afb7-3ecf72e48cfd"
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
Krylov = "ba0b0d4f-ebba-5204-a429-3ac8c609bfb7"
LDLFactorizations = "40e66cde-538c-5869-a4ad-c39174c6795b"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand All @@ -17,6 +19,8 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"

[compat]
CodecBzip2 = "0.7.2"
CodecZlib = "0.7.0"
Krylov = "0.6"
LDLFactorizations = "0.6"
MathOptInterface = "0.9.5"
Expand Down
6 changes: 4 additions & 2 deletions src/Interfaces/tulip_julia_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ function load_problem!(m::Model{T}, fname::String) where{T}
Base.empty!(m)

dat = with_logger(Logging.NullLogger()) do
readqps(fname, mpsformat=:free)
_open(fname) do io
readqps(io, mpsformat=:free)
end
end

# TODO: avoid allocations when T is Float64
Expand Down Expand Up @@ -244,4 +246,4 @@ function get_attribute(m::Model{T}, ::DualObjectiveValue) where{T}
z = m.solution.z_dual
return m.pbdata.objsense ? z : -z
end
end
end
21 changes: 21 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
using CodecBzip2
using CodecZlib

"""
_open(f, fname)

Open a file with decompression stream as required.
"""
function _open(f::Function, fname::String)

ext = Symbol(split(fname, ".")[end])

if ext == :gz
return Base.open(f, CodecZlib.GzipDecompressorStream, fname, "r")
elseif ext == :bz2
return Base.open(f, CodecBzip2.Bzip2DecompressorStream, fname, "r")
else
return Base.open(f, fname, "r")
end
end

# Positive and negative part of a number
pos_part(x::T) where{T} = x >= zero(T) ? x : zero(T)
neg_part(x::T) where{T} = x >= zero(T) ? zero(T) : -x
Expand Down
57 changes: 57 additions & 0 deletions test/Interfaces/julia_api.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Tulip
using Test

function test_reader()
lp = Tulip.Model{Float64}()

Tulip.load_problem!(lp, joinpath(@__DIR__, "lp.mps"))
check_data(lp)

Tulip.load_problem!(lp, joinpath(@__DIR__, "lp.mps.gz"))
check_data(lp)

Tulip.load_problem!(lp, joinpath(@__DIR__, "lp.mps.bz2"))
check_data(lp)

end

function check_data(lp)

pb = lp.pbdata

@test pb.name == "LP1"

@test pb.ncon == 2
@test pb.nvar == 2

@test pb.objsense
@test pb.obj0 == 0.0
@test pb.obj == [1.0, 2.0]

@test pb.lvar == [0.0, 0.0]
@test pb.uvar == [1.0, 1.0]
@test pb.lcon == [1.0, 0.0]
@test pb.ucon == [1.0, 0.0]

@test pb.con_names == ["ROW1", "ROW2"]
@test pb.var_names == ["X1", "X2"]

col1, col2 = pb.acols[1], pb.acols[2]
@test col1.nzind == [1, 2]
@test col1.nzval == [1.0, 1.0]
@test col2.nzind == [1, 2]
@test col2.nzval == [1.0, -1.0]

row1, row2 = pb.arows[1], pb.arows[2]
@test row1.nzind == [1, 2]
@test row1.nzval == [1.0, 1.0]
@test row2.nzind == [1, 2]
@test row2.nzval == [1.0, -1.0]



end

@testset "Reader" begin
test_reader()
end
23 changes: 23 additions & 0 deletions test/Interfaces/lp.mps
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
NAME LP1

* Problem:
* min x1 + 2*x2
* s.t. x1 + x2 = 1
* x1 - x2 = 0
* 0 <= x1, x2, <= 1

ROWS
E ROW1
E ROW2
N COST
COLUMNS
X1 ROW1 1. ROW2 1.
X1 COST 1.
X2 ROW1 1. ROW2 -1.
X2 COST 2.
RHS
B ROW1 1. ROW2 0.
BOUNDS
UP BND1 X1 1.
UP BND1 X2 1.
ENDATA
Binary file added test/Interfaces/lp.mps.bz2
Binary file not shown.
Binary file added test/Interfaces/lp.mps.gz
Binary file not shown.
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ end
end

@testset "Interfaces" begin
include("Interfaces/julia_api.jl")
end

@testset "MOI" begin
include("Interfaces/MOI_wrapper.jl")
end

Expand Down