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 1 commit
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
9 changes: 9 additions & 0 deletions src/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,12 @@ end
function Base.deepcopy(::Model)
error("`JuMP.Model` does not support `deepcopy` as the reference to the underlying solver cannot be deep copied, use `copy` instead.")
end

function MOI.copy_to(dest::MOI.ModelLike, src::Model)
bridged_dest = MOI.Bridges.full_bridge_optimizer(dest, Float64)
return MOI.copy_to(bridged_dest, backend(src))
end

function MOI.copy_to(dest::Model, src::MOI.ModelLike)
return MOI.copy_to(backend(dest), src)
end
14 changes: 6 additions & 8 deletions src/file_formats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ function write_to_file(
format::MathOptFormat.FileFormat = MathOptFormat.FORMAT_AUTOMATIC
)
dest = MathOptFormat.Model(format = format, filename = filename)
bridged_dest = MOI.Bridges.full_bridge_optimizer(dest, Float64)
MOI.copy_to(bridged_dest, backend(model))
MOI.copy_to(dest, model)
MOI.write_to_file(dest, filename)
return
end
Expand All @@ -45,8 +44,7 @@ function Base.write(
error("Unable to infer the file format from an IO stream.")
end
dest = MathOptFormat.Model(format = format)
bridged_dest = MOI.Bridges.full_bridge_optimizer(dest, Float64)
MOI.copy_to(bridged_dest, backend(model))
MOI.copy_to(dest, model)
write(io, dest)
return
end
Expand All @@ -69,22 +67,22 @@ function read_from_file(
src = MathOptFormat.Model(format = format, filename = filename)
MOI.read_from_file(src, filename)
model = Model()
MOI.copy_to(backend(model), src)
MOI.copy_to(model, src)
return model
end

"""
Base.read(io::IO, ::Type{Model}; format::FileFormat)
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::FileFormat)
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(backend(model), src)
MOI.copy_to(model, src)
return model
end
2 changes: 1 addition & 1 deletion test/file_formats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ using Test
@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."),
ErrorException("read! is not implemented for LP files."),
read_from_file("my_model.lp")
)
rm("my_model.lp")
Expand Down