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

Add write_to_file and read_from_file #2114

Merged
merged 8 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MathOptFormat = "f4570300-c277-12e8-125c-4912f86ce65d"
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand All @@ -18,6 +19,7 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Calculus = "0.5"
DataStructures = "0.17"
ForwardDiff = "~0.5.0, ~0.6, ~0.7, ~0.8, ~0.9, ~0.10"
MathOptFormat = "0.3"
MathOptInterface = "~0.9.1"
NaNMath = "0.3"
OffsetArrays = "≥ 0.2.13"
Expand Down
12 changes: 12 additions & 0 deletions docs/src/solvers.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,15 @@ set_time_limit_sec
unset_time_limit_sec
time_limit_sec
```

## File formats

JuMP can write models to a variety of file-formats using [`write_to_file`](@ref).
```@docs
write_to_file
```

JuMP models can be created from file formats using [`read_from_file`](@ref).
```@docs
read_from_file
```
10 changes: 8 additions & 2 deletions src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const MOIU = MOI.Utilities
import Calculus
import DataStructures.OrderedDict
import ForwardDiff
import MathOptFormat
include("_Derivatives/_Derivatives.jl")
using ._Derivatives

Expand All @@ -38,8 +39,12 @@ Base.@deprecate(setlowerbound, JuMP.set_lower_bound)
Base.@deprecate(setupperbound, JuMP.set_upper_bound)
Base.@deprecate(linearterms, JuMP.linear_terms)

writeLP(args...; kargs...) = error("writeLP has been removed from JuMP. Use `MathOptFormat.jl` instead.")
writeMPS(args...; kargs...) = error("writeMPS has been removed from JuMP. Use `MathOptFormat.jl` instead.")
function writeLP(args...; kargs...)
error("writeLP has been removed from JuMP. Use `write_to_file` instead.")
end
function writeMPS(args...; kargs...)
error("writeMPS has been removed from JuMP. Use `write_to_file` instead.")
end

include("utils.jl")

Expand Down Expand Up @@ -787,6 +792,7 @@ include("nlp.jl")
include("print.jl")
include("lp_sensitivity.jl")
include("callbacks.jl")
include("file_formats.jl")

# JuMP exports everything except internal symbols, which are defined as those
# whose name starts with an underscore. If you don't want all of these symbols
Expand Down
134 changes: 134 additions & 0 deletions src/file_formats.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

const MOF = MathOptFormat

"""
write_to_file(
model::Model,
filename::String;
compression::MOF.AbstractCompressionScheme = MOF.AutomaticCompression(),
format::MOF.FileFormat == MOF.AUTOMATIC_FILE_FORMAT
)

Write the JuMP model `model` to `filename` in the format `format` and compress
the file using the compression scheme `compression`.

Supported formats include:
- `format = MOF.FORMAT_CBF`. Inferred if filename ends in `.cbf`.
- `format = MOF.FORMAT_MOF`. Inferred if filename ends in `.mof.json`.
- `format = MOF.FORMAT_MPS`. Inferred if filename ends in `.mps`.
- `format = MOF.FORMAT_LP`. Inferred if filename ends in `.lp`.

See `MathOptFormat.FileFormat` for all supported formats.

Supported compression formats include:
- `compression = MOF.NoCompression()`
- `compression = MOF.Gzip()`. Inferred if filename ends in `.gz`.
- `compression = MOF.Bzip2()`. Inferred if filename ends in `.bz2`.

Default arguments will infer the compression type and format from the filename.
"""
function write_to_file(
model::Model,
filename::String;
compression::MOF.AbstractCompressionScheme = MOF.AutomaticCompression(),
format::MOF.FileFormat = MOF.AUTOMATIC_FILE_FORMAT,
)
if format == MOF.AUTOMATIC_FILE_FORMAT
format = MOF._detect_file_format(filename)
end
dest = MOF._FILE_FORMATS[format][2]()
bridged_dest = MOI.Bridges.full_bridge_optimizer(dest, Float64)
MOI.copy_to(bridged_dest, backend(model))
MOI.write_to_file(dest, filename; compression = compression)
return
end

"""
write_to_file(model::Model, io::IO; format::MOF.FileFormat)

Write the JuMP model `model` to `io` in the format `format`.

Supported formats include:
- `format = MOF.FORMAT_CBF`
- `format = MOF.FORMAT_MOF`
- `format = MOF.FORMAT_MPS`
- `format = MOF.FORMAT_LP`

See `MathOptFormat.FileFormat` for all supported formats.
"""
function write_to_file(model::Model, io::IO; format::MOF.FileFormat)
if format == MOF.AUTOMATIC_FILE_FORMAT
error("Unable to infer the file format from an IO stream.")
end
dest = MOF._FILE_FORMATS[format][2]()
bridged_dest = MOI.Bridges.full_bridge_optimizer(dest, Float64)
MOI.copy_to(bridged_dest, backend(model))
MOI.write_to_file(dest, io)
return
end

"""
read_from_file(
filename::String;
compression::MOF.AbstractCompressionScheme = MOF.AutomaticCompression(),
format::MOF.FileFormat == MOF.AUTOMATIC_FILE_FORMAT
)

Return a JuMP model read from `filename` in the format `format` and de-compress
the file using the compression scheme `compression`.

Supported formats include:
- `format = MOF.FORMAT_CBF`. Inferred if filename ends in `.cbf`.
- `format = MOF.FORMAT_MOF`. Inferred if filename ends in `.mof.json`.
- `format = MOF.FORMAT_MPS`. Inferred if filename ends in `.mps`.
- `format = MOF.FORMAT_LP`. Inferred if filename ends in `.lp`.

See `MathOptFormat.FileFormat` for all supported formats.

Supported compression formats include:
- `compression = MOF.NoCompression()`
- `compression = MOF.Gzip()`. Inferred if filename ends in `.gz`.
- `compression = MOF.Bzip2()`. Inferred if filename ends in `.bz2`.

Default arguments will infer the compression type and format from the filename.
"""
function read_from_file(
filename::String;
compression::MOF.AbstractCompressionScheme = MOF.AutomaticCompression(),
format::MOF.FileFormat = MOF.AUTOMATIC_FILE_FORMAT,
)
src = MathOptFormat.read_from_file(
filename; compression = compression, file_format = format
)
model = Model()
MOI.copy_to(backend(model), src)
return model
end

"""
read_from_file(io::IO; format::MOF.FileFormat)

Return a JuMP model read from `io` in the format `format`.

Supported formats include:
- `format = MOF.FORMAT_CBF`
- `format = MOF.FORMAT_MOF`
- `format = MOF.FORMAT_MPS`
- `format = MOF.FORMAT_LP`

See `MathOptFormat.FileFormat` for all supported formats.
"""
function read_from_file(io::IO; format::MOF.FileFormat)
if format == MOF.AUTOMATIC_FILE_FORMAT
error("Unable to infer the file format from an IO stream.")
end
src = MOF._FILE_FORMATS[format][2]()
MOI.read_from_file(src, io)
model = Model()
MOI.copy_to(backend(model), src)
return model
end
76 changes: 76 additions & 0 deletions test/file_formats.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

@testset "File formats" begin
@testset "MOF" begin
model = Model()
@variable(model, x)
@constraint(model, my_c, 3 * x >= 1)
@objective(model, Min, 2 * x^2 + x + 1)
write_to_file(model, "my_model.mof.json")
model_2 = read_from_file("my_model.mof.json")
@test sprint(print, model) == sprint(print, model_2)
rm("my_model.mof.json")
end
@testset "MPS" begin
model = Model()
@variable(model, x >= 0)
@constraint(model, my_c, 3 * x >= 1)
@objective(model, Min, 2 * x)
write_to_file(model, "my_model.mps")
model_2 = read_from_file("my_model.mps")
@test sprint(print, model) == sprint(print, model_2)
rm("my_model.mps")
end
@testset "LP" begin
model = Model()
@variable(model, x >= 0)
@constraint(model, my_c, 3 * x >= 1)
@objective(model, Min, 2 * x)
write_to_file(model, "my_model.lp")
@test read("my_model.lp", String) ==
"minimize\nobj: 2 x\nsubject to\nmy_c: 3 x >= 1\nBounds\nx >= 0\nEnd\n"
@test_throws(
ErrorException("Read from file is not implemented for LP files."),
read_from_file("my_model.lp")
)
rm("my_model.lp")
end
@testset "CBF" begin
model = Model()
@variable(model, X[1:2, 1:2], PSD)
@constraint(model, my_c, sum(X) >= 1)
@objective(model, Min, sum(X))
write_to_file(model, "my_model.cbf")
@test read("my_model.cbf", String) ==
"VER\n3\n\nOBJSENSE\nMIN\n\nVAR\n3 1\nF 3\n\nOBJACOORD\n3\n0 1.0\n1 2.0\n2 1.0\n\nCON\n1 1\nL+ 1\n\nACOORD\n3\n0 0 1.0\n0 1 2.0\n0 2 1.0\n\nBCOORD\n1\n0 -1.0\n\nPSDCON\n1\n2\n\nHCOORD\n3\n0 0 0 0 1.0\n0 1 1 0 1.0\n0 2 1 1 1.0\n\n"
model_2 = read_from_file("my_model.cbf")
# Note: we replace ' in ' => ' ∈ ' because the unicode doesn't print on
# Windows systems for some reason.
@test replace(sprint(print, model_2), " in " => " ∈ ") ==
"Min noname + 2 noname + noname\nSubject to\n [noname + 2 noname + noname - 1] ∈ MathOptInterface.Nonnegatives(1)\n [noname, noname, noname] ∈ MathOptInterface.PositiveSemidefiniteConeTriangle(2)\n"
rm("my_model.cbf")
end
@testset "MOF io" begin
model = Model()
@variable(model, x)
@constraint(model, my_c, 3 * x >= 1)
@objective(model, Min, 2 * x^2 + x + 1)
io = IOBuffer()
@test_throws(
ErrorException("Unable to infer the file format from an IO stream."),
write_to_file(model, io; format = MOF.AUTOMATIC_FILE_FORMAT)
)
write_to_file(model, io; format = MOF.FORMAT_MOF)
seekstart(io)
@test_throws(
ErrorException("Unable to infer the file format from an IO stream."),
read_from_file(io; format = MOF.AUTOMATIC_FILE_FORMAT)
)
seekstart(io)
model_2 = read_from_file(io; format = MOF.FORMAT_MOF)
@test sprint(print, model) == sprint(print, model_2)
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ include("operator.jl")
include("macros.jl")
include("lp_sensitivity.jl")
include("callbacks.jl")
include("file_formats.jl")
# TODO: The hygiene test should run in a separate Julia instance where JuMP hasn't been loaded via `using`.
include("hygiene.jl")