diff --git a/Project.toml b/Project.toml index bf486700f75..8fed4af432e 100644 --- a/Project.toml +++ b/Project.toml @@ -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" @@ -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.2" MathOptInterface = "~0.9.1" NaNMath = "0.3" OffsetArrays = "≥ 0.2.13" diff --git a/src/JuMP.jl b/src/JuMP.jl index 010d911975f..f688a37e7d4 100644 --- a/src/JuMP.jl +++ b/src/JuMP.jl @@ -16,6 +16,7 @@ using SparseArrays import MathOptInterface const MOI = MathOptInterface const MOIU = MOI.Utilities +using MathOptFormat import Calculus import DataStructures.OrderedDict @@ -38,8 +39,8 @@ 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.") +writeLP(args...; kargs...) = error("writeLP has been merged into write_to_file and its arguments have changed.") +writeMPS(args...; kargs...) = error("writeMPS has been merged into write_to_file and its arguments have changed.") include("utils.jl") @@ -787,6 +788,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 diff --git a/src/file_formats.jl b/src/file_formats.jl new file mode 100644 index 00000000000..6d070920b9e --- /dev/null +++ b/src/file_formats.jl @@ -0,0 +1,28 @@ +function write_to_file(model::Model, io::IO, format::MathOptFormat.FileFormat; kwargs...) + @assert format != AUTOMATIC_FILE_FORMAT + dest = MathOptFormat._file_formats[format][2]() + MOI.copy_to(dest, backend(model)) + MOI.write_to_file(dest, io) + return +end + +""" + write_to_file(model::Model, filename::String; format::FileFormat=AUTOMATIC_FILE_FORMAT, compression::MathOptFormat.AbstractCompressionScheme=MathOptFormat.AutomaticCompression(), kwargs...) + +Write `model` to the file called `filename` using the format `format`. + +Valid formats are given by the enum [`FileFormat`](@ref). Valid compression algorithms +are given by a concrete subtype of [`AbstractCompressionScheme`](@ref). + +For keyword options, see [MathOptFormat.jl](https://github.com/odow/MathOptFormat.jl). +""" +function write_to_file(model::Model, filename::String; + format::MathOptFormat.FileFormat=MathOptFormat.AUTOMATIC_FILE_FORMAT, + compression::MathOptFormat.AbstractCompressionScheme=MathOptFormat.AutomaticCompression(), + kwargs...) + if format == AUTOMATIC_FILE_FORMAT + format = MathOptFormat._filename_to_format(filename) + end + MOI.write_to_file(model, filename, compression=compression) + return +end