-
-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add write_to_file and read_from_file (#2114)
* Add write_to_file and read_from_file * Update Project.toml and add file_formats.jl to runtests.jl * Add read/write to io streams * Update docstrings * Change to Base.read and Base.write * Updates for MathOptFormat v0.4 * Fixes for MathOptFormat 0.4 * Update docs and fix copy_to
- Loading branch information
Showing
7 changed files
with
213 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# 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/. | ||
|
||
""" | ||
write_to_file( | ||
model::Model, | ||
filename::String; | ||
format::MathOptFormat.FileFormat = MathOptFormat.FORMAT_AUTOMATIC | ||
) | ||
Write the JuMP model `model` to `filename` in the format `format`. | ||
If the filename ends in `.gz`, it will be compressed using Gzip. | ||
If the filename ends in `.bz2`, it will be compressed using BZip2. | ||
""" | ||
function write_to_file( | ||
model::Model, | ||
filename::String; | ||
format::MathOptFormat.FileFormat = MathOptFormat.FORMAT_AUTOMATIC | ||
) | ||
dest = MathOptFormat.Model(format = format, filename = filename) | ||
# We add a `full_bridge_optimizer` here because MathOptFormat models may not | ||
# support all constraint types in a JuMP model. | ||
bridged_dest = MOI.Bridges.full_bridge_optimizer(dest, Float64) | ||
MOI.copy_to(bridged_dest, model) | ||
# `dest` will contain the underlying model, with constraints bridged if | ||
# necessary. | ||
MOI.write_to_file(dest, filename) | ||
return | ||
end | ||
|
||
""" | ||
Base.write( | ||
io::IO, | ||
model::Model; | ||
format::MathOptFormat.FileFormat = MathOptFormat.FORMAT_MOF | ||
) | ||
Write the JuMP model `model` to `io` in the format `format`. | ||
""" | ||
function Base.write( | ||
io::IO, | ||
model::Model; | ||
format::MathOptFormat.FileFormat = MathOptFormat.FORMAT_MOF | ||
) | ||
if format == MathOptFormat.FORMAT_AUTOMATIC | ||
error("Unable to infer the file format from an IO stream.") | ||
end | ||
dest = MathOptFormat.Model(format = format) | ||
# We add a `full_bridge_optimizer` here because MathOptFormat models may not | ||
# support all constraint types in a JuMP model. | ||
bridged_dest = MOI.Bridges.full_bridge_optimizer(dest, Float64) | ||
MOI.copy_to(bridged_dest, model) | ||
# `dest` will contain the underlying model, with constraints bridged if | ||
# necessary. | ||
write(io, dest) | ||
return | ||
end | ||
|
||
""" | ||
read_from_file( | ||
filename::String; | ||
format::MathOptFormat.FileFormat = MathOptFormat.FORMAT_AUTOMATIC | ||
) | ||
Return a JuMP model read from `filename` in the format `format`. | ||
If the filename ends in `.gz`, it will be uncompressed using Gzip. | ||
If the filename ends in `.bz2`, it will be uncompressed using BZip2. | ||
""" | ||
function read_from_file( | ||
filename::String; | ||
format::MathOptFormat.FileFormat = MathOptFormat.FORMAT_AUTOMATIC | ||
) | ||
src = MathOptFormat.Model(format = format, filename = filename) | ||
MOI.read_from_file(src, filename) | ||
model = Model() | ||
MOI.copy_to(model, src) | ||
return model | ||
end | ||
|
||
""" | ||
Base.read(io::IO, ::Type{Model}; format::MathOptFormat.FileFormat) | ||
Return a JuMP model read from `io` in the format `format`. | ||
""" | ||
function Base.read(io::IO, ::Type{Model}; format::MathOptFormat.FileFormat) | ||
if format == MathOptFormat.FORMAT_AUTOMATIC | ||
error("Unable to infer the file format from an IO stream.") | ||
end | ||
src = MathOptFormat.Model(format = format) | ||
read!(io, src) | ||
model = Model() | ||
MOI.copy_to(model, src) | ||
return model | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# 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/. | ||
|
||
using JuMP | ||
using MathOptFormat | ||
using Test | ||
|
||
@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! 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 "Base read/write via 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(io, model; format = MathOptFormat.FORMAT_AUTOMATIC) | ||
) | ||
write(io, model; format = MathOptFormat.FORMAT_MOF) | ||
seekstart(io) | ||
@test_throws( | ||
ErrorException("Unable to infer the file format from an IO stream."), | ||
read(io, Model; format = MathOptFormat.FORMAT_AUTOMATIC) | ||
) | ||
seekstart(io) | ||
model_2 = read(io, Model; format = MathOptFormat.FORMAT_MOF) | ||
@test sprint(print, model) == sprint(print, model_2) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters