Skip to content

Commit

Permalink
Merge 455ba1d into e52aa76
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Dec 29, 2019
2 parents e52aa76 + 455ba1d commit d55615c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 27 deletions.
4 changes: 1 addition & 3 deletions Project.toml
Expand Up @@ -8,7 +8,6 @@ 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 @@ -19,8 +18,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.4"
MathOptInterface = "~0.9.1"
MathOptInterface = "~0.9.9"
NaNMath = "0.3"
OffsetArrays = "≥ 0.2.13"
julia = "1"
Expand Down
4 changes: 2 additions & 2 deletions docs/src/solvers.md
Expand Up @@ -119,12 +119,12 @@ JuMP can write models to a variety of file-formats using [`write_to_file`](@ref)
and [`Base.write`](@ref).
```@docs
write_to_file
Base.write(::IO, ::Model; ::JuMP.MathOptFormat.FileFormat)
Base.write(::IO, ::Model; ::MOI.FileFormats.FileFormat)
```

JuMP models can be created from file formats using [`read_from_file`](@ref) and
[`Base.read`](@ref).
```@docs
read_from_file
Base.read(::IO, ::Type{Model}; ::JuMP.MathOptFormat.FileFormat)
Base.read(::IO, ::Type{Model}; ::MOI.FileFormats.FileFormat)
```
1 change: 0 additions & 1 deletion src/JuMP.jl
Expand Up @@ -20,7 +20,6 @@ const MOIU = MOI.Utilities
import Calculus
import DataStructures.OrderedDict
import ForwardDiff
import MathOptFormat
include("_Derivatives/_Derivatives.jl")
using ._Derivatives

Expand Down
32 changes: 16 additions & 16 deletions src/file_formats.jl
Expand Up @@ -7,7 +7,7 @@
write_to_file(
model::Model,
filename::String;
format::MathOptFormat.FileFormat = MathOptFormat.FORMAT_AUTOMATIC
format::MOI.FileFormats.FileFormat = MOI.FileFormats.FORMAT_AUTOMATIC
)
Write the JuMP model `model` to `filename` in the format `format`.
Expand All @@ -18,10 +18,10 @@ 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
format::MOI.FileFormats.FileFormat = MOI.FileFormats.FORMAT_AUTOMATIC
)
dest = MathOptFormat.Model(format = format, filename = filename)
# We add a `full_bridge_optimizer` here because MathOptFormat models may not
dest = MOI.FileFormats.Model(format = format, filename = filename)
# We add a `full_bridge_optimizer` here because MOI.FileFormats 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)
Expand All @@ -35,21 +35,21 @@ end
Base.write(
io::IO,
model::Model;
format::MathOptFormat.FileFormat = MathOptFormat.FORMAT_MOF
format::MOI.FileFormats.FileFormat = MOI.FileFormats.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
format::MOI.FileFormats.FileFormat = MOI.FileFormats.FORMAT_MOF
)
if format == MathOptFormat.FORMAT_AUTOMATIC
if format == MOI.FileFormats.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
dest = MOI.FileFormats.Model(format = format)
# We add a `full_bridge_optimizer` here because MOI.FileFormats 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)
Expand All @@ -62,7 +62,7 @@ end
"""
read_from_file(
filename::String;
format::MathOptFormat.FileFormat = MathOptFormat.FORMAT_AUTOMATIC
format::MOI.FileFormats.FileFormat = MOI.FileFormats.FORMAT_AUTOMATIC
)
Return a JuMP model read from `filename` in the format `format`.
Expand All @@ -72,25 +72,25 @@ If the filename ends in `.bz2`, it will be uncompressed using BZip2.
"""
function read_from_file(
filename::String;
format::MathOptFormat.FileFormat = MathOptFormat.FORMAT_AUTOMATIC
format::MOI.FileFormats.FileFormat = MOI.FileFormats.FORMAT_AUTOMATIC
)
src = MathOptFormat.Model(format = format, filename = filename)
src = MOI.FileFormats.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)
Base.read(io::IO, ::Type{Model}; format::MOI.FileFormats.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
function Base.read(io::IO, ::Type{Model}; format::MOI.FileFormats.FileFormat)
if format == MOI.FileFormats.FORMAT_AUTOMATIC
error("Unable to infer the file format from an IO stream.")
end
src = MathOptFormat.Model(format = format)
src = MOI.FileFormats.Model(format = format)
read!(io, src)
model = Model()
MOI.copy_to(model, src)
Expand Down
9 changes: 4 additions & 5 deletions test/file_formats.jl
Expand Up @@ -4,7 +4,6 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

using JuMP
using MathOptFormat
using Test

@testset "File formats" begin
Expand Down Expand Up @@ -65,16 +64,16 @@ using Test
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 = MOI.FileFormats.FORMAT_AUTOMATIC)
)
write(io, model; format = MathOptFormat.FORMAT_MOF)
write(io, model; format = MOI.FileFormats.FORMAT_MOF)
seekstart(io)
@test_throws(
ErrorException("Unable to infer the file format from an IO stream."),
read(io, Model; format = MathOptFormat.FORMAT_AUTOMATIC)
read(io, Model; format = MOI.FileFormats.FORMAT_AUTOMATIC)
)
seekstart(io)
model_2 = read(io, Model; format = MathOptFormat.FORMAT_MOF)
model_2 = read(io, Model; format = MOI.FileFormats.FORMAT_MOF)
@test sprint(print, model) == sprint(print, model_2)
end
end

0 comments on commit d55615c

Please sign in to comment.