Skip to content

Commit

Permalink
Fix write_to_file of NL files and bump MOI to v1.1.1 (#2932)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Mar 24, 2022
1 parent 7fdd4d4 commit d719b92
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Calculus = "0.5"
DataStructures = "0.18"
ForwardDiff = "~0.5.0, ~0.6, ~0.7, ~0.8, ~0.9, ~0.10"
JSON = "0.21"
MathOptInterface = "1"
MathOptInterface = "1.1.1"
MutableArithmetics = "1"
NaNMath = "0.3, 1"
OrderedCollections = "1"
Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Ipopt = "=1.0.2"
JSON = "0.21"
JSONSchema = "1"
Literate = "2.8"
MathOptInterface = "=1.1.0"
MathOptInterface = "=1.1.1"
Plots = "1"
SCS = "=1.1.0"
StatsPlots = "0.14"
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ function _add_moi_pages()
!!! warning
This documentation in this section is a copy of the official
MathOptInterface documentation available at
[https://jump.dev/MathOptInterface.jl/v1.1.0](https://jump.dev/MathOptInterface.jl/v1.1.0).
[https://jump.dev/MathOptInterface.jl/v1.1.1](https://jump.dev/MathOptInterface.jl/v1.1.1).
It is included here to make it easier to link concepts between JuMP and
MathOptInterface.
"""
Expand Down
30 changes: 16 additions & 14 deletions src/file_formats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

function _copy_to_bridged_model(f::Function, model::Model)
inner = MOI.instantiate(f; with_bridge_type = Float64)
MOI.copy_to(inner, model)
@assert inner isa MOI.Bridges.LazyBridgeOptimizer
if inner.model isa MOI.Utilities.CachingOptimizer
MOI.Utilities.attach_optimizer(inner.model)
end
return unsafe_backend(inner)
end

"""
write_to_file(
model::Model,
Expand All @@ -20,13 +30,9 @@ function write_to_file(
filename::String;
format::MOI.FileFormats.FileFormat = MOI.FileFormats.FORMAT_AUTOMATIC,
)
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)
# `dest` will contain the underlying model, with constraints bridged if
# necessary.
dest = _copy_to_bridged_model(model) do
return MOI.FileFormats.Model(format = format, filename = filename)
end
MOI.write_to_file(dest, filename)
return
end
Expand All @@ -48,13 +54,9 @@ function Base.write(
if format == MOI.FileFormats.FORMAT_AUTOMATIC
error("Unable to infer the file format from an IO stream.")
end
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)
# `dest` will contain the underlying model, with constraints bridged if
# necessary.
dest = _copy_to_bridged_model(model) do
return MOI.FileFormats.Model(format = format)
end
write(io, dest)
return
end
Expand Down
16 changes: 16 additions & 0 deletions test/file_formats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ function test_mof_nlp()
@test length(file["constraints"]) == 2
end

function test_nl_nlp()
model = Model()
@variable(model, x)
@variable(model, y)
@NLobjective(model, Min, (1 - x)^2 + 100 * (y - x^2)^2)
@NLconstraint(model, x^2 + y^2 <= 100.0)
@constraint(model, x + y == 10)
io = IOBuffer()
write(io, model; format = MOI.FileFormats.FORMAT_NL)
seekstart(io)
file = read(io, String)
# Check that the constant 100 occurs in the file
@test occursin("n100", file)
return
end

function runtests()
for name in names(@__MODULE__; all = true)
if !startswith("$(name)", "test_")
Expand Down

0 comments on commit d719b92

Please sign in to comment.