From a548cf8423855f23cfdfc6c8fea7e42e76ea72d6 Mon Sep 17 00:00:00 2001 From: odow Date: Wed, 29 May 2024 15:42:30 +1200 Subject: [PATCH 1/3] [FileFormats] remove show method in favor of summary --- docs/src/submodules/FileFormats/overview.md | 122 +++++++++++--------- docs/src/tutorials/implementing.md | 6 +- src/FileFormats/CBF/CBF.jl | 2 +- src/FileFormats/LP/LP.jl | 5 +- src/FileFormats/MOF/MOF.jl | 5 +- src/FileFormats/MPS/MPS.jl | 5 +- src/FileFormats/NL/NL.jl | 2 +- src/FileFormats/SDPA/SDPA.jl | 4 +- src/MathOptInterface.jl | 3 +- test/FileFormats/CBF/CBF.jl | 2 +- test/FileFormats/LP/LP.jl | 2 +- test/FileFormats/MOF/MOF.jl | 2 +- test/FileFormats/MPS/MPS.jl | 3 +- test/FileFormats/NL/NL.jl | 4 +- test/FileFormats/SDPA/SDPA.jl | 4 +- 15 files changed, 87 insertions(+), 84 deletions(-) diff --git a/docs/src/submodules/FileFormats/overview.md b/docs/src/submodules/FileFormats/overview.md index 4187dd3691..54cc3f4ae2 100644 --- a/docs/src/submodules/FileFormats/overview.md +++ b/docs/src/submodules/FileFormats/overview.md @@ -16,56 +16,83 @@ models using [`write_to_file`](@ref) and [`read_from_file`](@ref). You must read and write files to a [`FileFormats.Model`](@ref) object. Specific the file-type by passing a [`FileFormats.FileFormat`](@ref) enum. For example: -**The Conic Benchmark Format** +### The Conic Benchmark Format ```jldoctest julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_CBF) -A Conic Benchmark Format (CBF) model +MOI.FileFormats.CBF.Model +├ ObjectiveSense: FEASIBILITY_SENSE +├ ObjectiveFunctionType: MOI.ScalarAffineFunction{Float64} +├ NumberOfVariables: 0 +└ NumberOfConstraints: 0 ``` -**The LP file format** +### The LP file format ```jldoctest julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_LP) -A .LP-file model +MOI.FileFormats.LP.Model +├ ObjectiveSense: FEASIBILITY_SENSE +├ ObjectiveFunctionType: MOI.ScalarAffineFunction{Float64} +├ NumberOfVariables: 0 +└ NumberOfConstraints: 0 ``` -**The MathOptFormat file format** +### The MathOptFormat file format ```jldoctest julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MOF) -A MathOptFormat Model +MOI.FileFormats.MOF.Model +├ ObjectiveSense: FEASIBILITY_SENSE +├ ObjectiveFunctionType: MOI.ScalarAffineFunction{Float64} +├ NumberOfVariables: 0 +└ NumberOfConstraints: 0 ``` -**The MPS file format** +### The MPS file format ```jldoctest julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MPS) -A Mathematical Programming System (MPS) model +MOI.FileFormats.MPS.Model +├ ObjectiveSense: FEASIBILITY_SENSE +├ ObjectiveFunctionType: MOI.ScalarAffineFunction{Float64} +├ NumberOfVariables: 0 +└ NumberOfConstraints: 0 ``` -**The NL file format** +### The NL file format ```jldoctest julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_NL) -An AMPL (.nl) model +MOI.FileFormats.NL.Model +├ ObjectiveSense: unknown +├ ObjectiveFunctionType: unknown +├ NumberOfVariables: unknown +└ NumberOfConstraints: unknown ``` -**The REW file format** +### The REW file format ```jldoctest julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_REW) -A Mathematical Programming System (MPS) model +MOI.FileFormats.MPS.Model +├ ObjectiveSense: FEASIBILITY_SENSE +├ ObjectiveFunctionType: MOI.ScalarAffineFunction{Float64} +├ NumberOfVariables: 0 +└ NumberOfConstraints: 0 ``` Note that the REW format is identical to the MPS file format, except that all names are replaced with generic identifiers. -**The SDPA file format** +### The SDPA file format ```jldoctest -julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_SDPA) -A SemiDefinite Programming Algorithm Format (SDPA) model +MOI.FileFormats.SDPA.Model +├ ObjectiveSense: FEASIBILITY_SENSE +├ ObjectiveFunctionType: MOI.ScalarAffineFunction{Float64} +├ NumberOfVariables: 0 +└ NumberOfConstraints: 0 ``` ## Write to file @@ -75,11 +102,9 @@ use: ```jldoctest fileformats julia> src = MOI.Utilities.Model{Float64}(); -julia> MOI.add_variable(src) -MOI.VariableIndex(1) +julia> MOI.add_variable(src); -julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MOF) -A MathOptFormat Model +julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MOF); julia> MOI.copy_to(dest, src) MathOptInterface.Utilities.IndexMap with 1 entry: @@ -110,8 +135,7 @@ julia> print(read("file.mof.json", String)) To read a MathOptFormat file, use: ```jldoctest fileformats -julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MOF) -A MathOptFormat Model +julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MOF); julia> MOI.read_from_file(dest, "file.mof.json") @@ -131,29 +155,14 @@ guess the format from the file extension. For example: ```jldoctest fileformats julia> src = MOI.Utilities.Model{Float64}(); -julia> dest = MOI.FileFormats.Model(filename = "file.cbf.gz") -A Conic Benchmark Format (CBF) model +julia> dest = MOI.FileFormats.Model(filename = "file.cbf.gz"); julia> MOI.copy_to(dest, src) MathOptInterface.Utilities.IndexMap() julia> MOI.write_to_file(dest, "file.cbf.gz") -julia> src_2 = MOI.FileFormats.Model(filename = "file.cbf.gz") -A Conic Benchmark Format (CBF) model - -julia> src = MOI.Utilities.Model{Float64}(); - -julia> dest = MOI.FileFormats.Model(filename = "file.cbf.gz") -A Conic Benchmark Format (CBF) model - -julia> MOI.copy_to(dest, src) -MathOptInterface.Utilities.IndexMap() - -julia> MOI.write_to_file(dest, "file.cbf.gz") - -julia> src_2 = MOI.FileFormats.Model(filename = "file.cbf.gz") -A Conic Benchmark Format (CBF) model +julia> src_2 = MOI.FileFormats.Model(filename = "file.cbf.gz"); julia> MOI.read_from_file(src_2, "file.cbf.gz") @@ -167,14 +176,23 @@ filename. In some cases `src` may contain constraints that are not supported by the file format (for example, the CBF format supports integer variables but not binary). If so, copy `src` to a bridged model using [`Bridges.full_bridge_optimizer`](@ref): -```julia -src = MOI.Utilities.Model{Float64}() -x = MOI.add_variable(model) -MOI.add_constraint(model, x, MOI.ZeroOne()) -dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_CBF) -bridged = MOI.Bridges.full_bridge_optimizer(dest, Float64) -MOI.copy_to(bridged, src) -MOI.write_to_file(dest, "my_model.cbf") +```jldoctest +julia> src = MOI.Utilities.Model{Float64}(); + +julia> x = MOI.add_variable(model); + +julia> MOI.add_constraint(model, x, MOI.ZeroOne()); + +julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_CBF); + +julia> bridged = MOI.Bridges.full_bridge_optimizer(dest, Float64); + +julia> MOI.copy_to(bridged, src) +MathOptInterface.Utilities.IndexMap() + +julia> MOI.write_to_file(dest, "my_model.cbf") + +julia> rm("my_model.cbf") # Clean up after ourselves. ``` !!! note Even after bridging, it may still not be possible to write the model to file @@ -188,8 +206,7 @@ read and write directly from `IO` streams using `Base.write` and `Base.read!`: ```jldoctest julia> src = MOI.Utilities.Model{Float64}(); -julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MPS) -A Mathematical Programming System (MPS) model +julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MPS); julia> MOI.copy_to(dest, src) MathOptInterface.Utilities.IndexMap() @@ -200,8 +217,7 @@ julia> write(io, dest) julia> seekstart(io); -julia> src_2 = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MPS) -A Mathematical Programming System (MPS) model +julia> src_2 = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MPS); julia> read!(io, src_2); ``` @@ -218,14 +234,12 @@ pass the `use_nlp_block = false` keyword argument to the `Model` constructor: julia> model = MOI.FileFormats.Model(; format = MOI.FileFormats.FORMAT_MOF, use_nlp_block = false, - ) -A MathOptFormat Model + ); julia> model = MOI.FileFormats.Model(; format = MOI.FileFormats.FORMAT_NL, use_nlp_block = false, - ) -An AMPL (.nl) model + ); ``` ## Validating MOF files diff --git a/docs/src/tutorials/implementing.md b/docs/src/tutorials/implementing.md index 9c931d40a2..4ca2443e73 100644 --- a/docs/src/tutorials/implementing.md +++ b/docs/src/tutorials/implementing.md @@ -298,10 +298,10 @@ interface. isn't clear, let us know and we will improve the docstrings. It is also very helpful to look at an existing wrapper for a similar solver. -You should also implement `Base.show(::IO, ::Optimizer)` to print a nice string -when someone prints your model. For example +You should also implement `Base.summary(::IO, ::Optimizer)` to print a nice +string when someone shows your model. For example ```julia -function Base.show(io::IO, model::Optimizer) +function Base.summary(io::IO, model::Optimizer) return print(io, "NewSolver with the pointer $(model.ptr)") end ``` diff --git a/src/FileFormats/CBF/CBF.jl b/src/FileFormats/CBF/CBF.jl index d1895d5d28..da7529d537 100644 --- a/src/FileFormats/CBF/CBF.jl +++ b/src/FileFormats/CBF/CBF.jl @@ -61,7 +61,7 @@ Create an empty instance of `FileFormats.CBF.Model`. """ Model(; kwargs...) = Model{Float64}() -Base.show(io::IO, ::Model) = print(io, "A Conic Benchmark Format (CBF) model") +Base.summary(io::IO, ::Model) = print(io, "MOI.FileFormats.CBF.Model") include("read.jl") include("write.jl") diff --git a/src/FileFormats/LP/LP.jl b/src/FileFormats/LP/LP.jl index 5468f82fed..3c9dd7a128 100644 --- a/src/FileFormats/LP/LP.jl +++ b/src/FileFormats/LP/LP.jl @@ -91,10 +91,7 @@ function Model(; maximum_length::Int = 255, warn::Bool = false) return model end -function Base.show(io::IO, ::Model) - print(io, "A .LP-file model") - return -end +Base.summary(io::IO, ::Model) = print(io, "MOI.FileFormats.LP.Model") # ============================================================================== # diff --git a/src/FileFormats/MOF/MOF.jl b/src/FileFormats/MOF/MOF.jl index 3b755cca2b..059bafda16 100644 --- a/src/FileFormats/MOF/MOF.jl +++ b/src/FileFormats/MOF/MOF.jl @@ -157,10 +157,7 @@ function Model(; return model end -function Base.show(io::IO, ::Model) - print(io, "A MathOptFormat Model") - return -end +Base.summary(io::IO, ::Model) = print(io, "MOI.FileFormats.MOF.Model") include("read.jl") include("write.jl") diff --git a/src/FileFormats/MPS/MPS.jl b/src/FileFormats/MPS/MPS.jl index 22e968097b..bebc9611b9 100644 --- a/src/FileFormats/MPS/MPS.jl +++ b/src/FileFormats/MPS/MPS.jl @@ -142,10 +142,7 @@ function Model(; return model end -function Base.show(io::IO, ::Model) - print(io, "A Mathematical Programming System (MPS) model") - return -end +Base.summary(io::IO, ::Model) = print(io, "MOI.FileFormats.MPS.Model") @enum(VType, VTYPE_CONTINUOUS, VTYPE_INTEGER, VTYPE_BINARY) diff --git a/src/FileFormats/NL/NL.jl b/src/FileFormats/NL/NL.jl index 89365ce1c3..d58ddfa2f9 100644 --- a/src/FileFormats/NL/NL.jl +++ b/src/FileFormats/NL/NL.jl @@ -164,7 +164,7 @@ mutable struct Model <: MOI.ModelLike end end -Base.show(io::IO, ::Model) = print(io, "An AMPL (.nl) model") +Base.summary(io::IO, ::Model) = print(io, "MOI.FileFormats.NL.Model") MOI.get(model::Model, ::MOI.SolverName) = "AmplNLWriter" diff --git a/src/FileFormats/SDPA/SDPA.jl b/src/FileFormats/SDPA/SDPA.jl index 0c03177060..d47837333e 100644 --- a/src/FileFormats/SDPA/SDPA.jl +++ b/src/FileFormats/SDPA/SDPA.jl @@ -127,9 +127,7 @@ function Model(; number_type::Type = Float64) return model end -function Base.show(io::IO, ::Model) - return print(io, "A SemiDefinite Programming Algorithm Format (SDPA) model") -end +Base.summary(io::IO, ::Model) = print(io, "MOI.FileFormats.SDPA.Model") # ============================================================================== # diff --git a/src/MathOptInterface.jl b/src/MathOptInterface.jl index 514cb9e267..99f6b9d505 100644 --- a/src/MathOptInterface.jl +++ b/src/MathOptInterface.jl @@ -30,7 +30,8 @@ end function Base.show(io::IO, model::ModelLike) offset = Base.get(io, :offset, "") - Utilities.print_with_acronym(io, "$(typeof(model))\n") + Utilities.print_with_acronym(io, summary(model)) + println(io) # ObjectiveSense sense = _try_get(model, ObjectiveSense(), "unknown") println(io, offset, "├ ObjectiveSense: $sense") diff --git a/test/FileFormats/CBF/CBF.jl b/test/FileFormats/CBF/CBF.jl index 3e48458d9b..10520b27b7 100644 --- a/test/FileFormats/CBF/CBF.jl +++ b/test/FileFormats/CBF/CBF.jl @@ -87,7 +87,7 @@ function _test_read(filename::String, model_string::String) end function test_show() - @test sprint(show, CBF.Model()) == "A Conic Benchmark Format (CBF) model" + @test sprint(summary, CBF.Model()) == "MOI.FileFormats.CBF.Model" return end diff --git a/test/FileFormats/LP/LP.jl b/test/FileFormats/LP/LP.jl index 09317a9286..0a1f8a9dbb 100644 --- a/test/FileFormats/LP/LP.jl +++ b/test/FileFormats/LP/LP.jl @@ -13,7 +13,7 @@ const LP = MOI.FileFormats.LP const LP_TEST_FILE = "test.lp" function test_show() - @test sprint(show, LP.Model()) == "A .LP-file model" + @test sprint(summary, LP.Model()) == "MOI.FileFormats.LP.Model" return end diff --git a/test/FileFormats/MOF/MOF.jl b/test/FileFormats/MOF/MOF.jl index 064f71450e..efd1320e27 100644 --- a/test/FileFormats/MOF/MOF.jl +++ b/test/FileFormats/MOF/MOF.jl @@ -321,7 +321,7 @@ function test_nonlinear_readingwriting() end function test_show() - @test sprint(show, MOF.Model()) == "A MathOptFormat Model" + @test sprint(summary, MOF.Model()) == "MOI.FileFormats.MOF.Model" end function test_nonempty_model() diff --git a/test/FileFormats/MPS/MPS.jl b/test/FileFormats/MPS/MPS.jl index 0ea246025c..3fccdc1801 100644 --- a/test/FileFormats/MPS/MPS.jl +++ b/test/FileFormats/MPS/MPS.jl @@ -60,8 +60,7 @@ function _test_model_equality( end function test_show() - @test sprint(show, MPS.Model()) == - "A Mathematical Programming System (MPS) model" + @test sprint(summary, MPS.Model()) == "MOI.FileFormats.MPS.Model" return end diff --git a/test/FileFormats/NL/NL.jl b/test/FileFormats/NL/NL.jl index 7203d87d2c..fcdb676a46 100644 --- a/test/FileFormats/NL/NL.jl +++ b/test/FileFormats/NL/NL.jl @@ -1145,8 +1145,8 @@ function test_SolverName() end function test_show() - model = NL.Model() - @test sprint(show, model) == "An AMPL (.nl) model" + @test sprint(summary, NL.Model()) == "MOI.FileFormats.NL.Model" + return end function test_linear_constraint_types() diff --git a/test/FileFormats/SDPA/SDPA.jl b/test/FileFormats/SDPA/SDPA.jl index 6ed02ee9de..98f25e26fd 100644 --- a/test/FileFormats/SDPA/SDPA.jl +++ b/test/FileFormats/SDPA/SDPA.jl @@ -88,8 +88,8 @@ function _test_read(filename::String, model_string::String) end function test_show() - @test sprint(show, SDPA.Model()) == - "A SemiDefinite Programming Algorithm Format (SDPA) model" + @test sprint(summary, SDPA.Model()) == "MOI.FileFormats.SDPA.Model" + return end function test_support() From f02c6fc26cc35ae4aca927a2e42e7d390641a966 Mon Sep 17 00:00:00 2001 From: odow Date: Wed, 29 May 2024 15:49:09 +1200 Subject: [PATCH 2/3] Fix --- docs/src/submodules/FileFormats/overview.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/src/submodules/FileFormats/overview.md b/docs/src/submodules/FileFormats/overview.md index 54cc3f4ae2..68c526445c 100644 --- a/docs/src/submodules/FileFormats/overview.md +++ b/docs/src/submodules/FileFormats/overview.md @@ -88,6 +88,7 @@ names are replaced with generic identifiers. ### The SDPA file format ```jldoctest +julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_SDPA) MOI.FileFormats.SDPA.Model ├ ObjectiveSense: FEASIBILITY_SENSE ├ ObjectiveFunctionType: MOI.ScalarAffineFunction{Float64} @@ -179,9 +180,9 @@ If so, copy `src` to a bridged model using [`Bridges.full_bridge_optimizer`](@re ```jldoctest julia> src = MOI.Utilities.Model{Float64}(); -julia> x = MOI.add_variable(model); +julia> x = MOI.add_variable(src); -julia> MOI.add_constraint(model, x, MOI.ZeroOne()); +julia> MOI.add_constraint(src, x, MOI.ZeroOne()); julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_CBF); From 082b2db1ff30fe6f93ce6625c4e1b7b85f7aae59 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 30 May 2024 09:18:34 +1200 Subject: [PATCH 3/3] Update overview.md --- docs/src/submodules/FileFormats/overview.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/src/submodules/FileFormats/overview.md b/docs/src/submodules/FileFormats/overview.md index 68c526445c..3a77f8ee5a 100644 --- a/docs/src/submodules/FileFormats/overview.md +++ b/docs/src/submodules/FileFormats/overview.md @@ -188,8 +188,7 @@ julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_CBF); julia> bridged = MOI.Bridges.full_bridge_optimizer(dest, Float64); -julia> MOI.copy_to(bridged, src) -MathOptInterface.Utilities.IndexMap() +julia> MOI.copy_to(bridged, src); julia> MOI.write_to_file(dest, "my_model.cbf")