From 0e56e004936ec41b605c98d73afa7dcaac38007f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 7 Aug 2024 11:52:05 +0200 Subject: [PATCH 1/6] Allow changing coefficient type in read_from_file --- src/file_formats.jl | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/file_formats.jl b/src/file_formats.jl index 8dd0367bb0a..9dc1f62bc53 100644 --- a/src/file_formats.jl +++ b/src/file_formats.jl @@ -105,14 +105,12 @@ Other `kwargs` are passed to the `Model` constructor of the chosen format. function read_from_file( filename::String; format::MOI.FileFormats.FileFormat = MOI.FileFormats.FORMAT_AUTOMATIC, + coefficient_type::Type = Float64, kwargs..., ) - src = - MOI.FileFormats.Model(; format = format, filename = filename, kwargs...) + src = MOI.FileFormats.Model(; format, filename, coefficient_type, kwargs...) MOI.read_from_file(src, filename) - # TODO(odow): what number type to choose? Are there any non-Float64 file - # formats? - model = GenericModel{Float64}() + model = GenericModel{coefficient_type}() MOI.copy_to(model, src) return model end From c9e73b9d7d9f6a7c2e2df3276a023e9861f750fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 8 Aug 2024 09:08:02 +0200 Subject: [PATCH 2/6] New approach --- src/file_formats.jl | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/file_formats.jl b/src/file_formats.jl index 9dc1f62bc53..35f1dc5cf5e 100644 --- a/src/file_formats.jl +++ b/src/file_formats.jl @@ -88,6 +88,14 @@ function Base.write( return end +_value_type(model::MOI.Utilities.AbstractModelLike{T}) where {T} = T + +# This fallback may not get the correct value type. However, since +# all models defined in `MOI.FileFormats` are created with +# `MOI.Utilities.@model` except `NL` which only supports `Float64`, +# this does the job for now. +_value_type(model::MOI.ModelLike) = Float64 + """ read_from_file( filename::String; @@ -105,12 +113,11 @@ Other `kwargs` are passed to the `Model` constructor of the chosen format. function read_from_file( filename::String; format::MOI.FileFormats.FileFormat = MOI.FileFormats.FORMAT_AUTOMATIC, - coefficient_type::Type = Float64, kwargs..., ) - src = MOI.FileFormats.Model(; format, filename, coefficient_type, kwargs...) + src = MOI.FileFormats.Model(; format, filename, kwargs...) MOI.read_from_file(src, filename) - model = GenericModel{coefficient_type}() + model = GenericModel{_value_type(model)}() MOI.copy_to(model, src) return model end From 0bffc9552bc3229f7a070a01fb5618f94f3969d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 8 Aug 2024 09:16:18 +0200 Subject: [PATCH 3/6] Mention MOF --- src/file_formats.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/file_formats.jl b/src/file_formats.jl index 35f1dc5cf5e..c20e2332cc3 100644 --- a/src/file_formats.jl +++ b/src/file_formats.jl @@ -91,9 +91,9 @@ end _value_type(model::MOI.Utilities.AbstractModelLike{T}) where {T} = T # This fallback may not get the correct value type. However, since -# all models defined in `MOI.FileFormats` are created with -# `MOI.Utilities.@model` except `NL` which only supports `Float64`, -# this does the job for now. +# all models defined in `MOI.FileFormats` return a +# `MOI.Utilities.GenericModel` except `NL` and `MOF` which only supports +# `Float64`, this does the job for now. _value_type(model::MOI.ModelLike) = Float64 """ From 93fd1f93fbfc20ec2a03837b43929d7016334988 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 8 Aug 2024 09:25:37 +0200 Subject: [PATCH 4/6] Fix --- src/file_formats.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/file_formats.jl b/src/file_formats.jl index c20e2332cc3..09931ea614c 100644 --- a/src/file_formats.jl +++ b/src/file_formats.jl @@ -88,13 +88,13 @@ function Base.write( return end -_value_type(model::MOI.Utilities.AbstractModelLike{T}) where {T} = T +_value_type(::MOI.Utilities.AbstractModelLike{T}) where {T} = T # This fallback may not get the correct value type. However, since # all models defined in `MOI.FileFormats` return a # `MOI.Utilities.GenericModel` except `NL` and `MOF` which only supports # `Float64`, this does the job for now. -_value_type(model::MOI.ModelLike) = Float64 +_value_type(::MOI.ModelLike) = Float64 """ read_from_file( @@ -117,7 +117,7 @@ function read_from_file( ) src = MOI.FileFormats.Model(; format, filename, kwargs...) MOI.read_from_file(src, filename) - model = GenericModel{_value_type(model)}() + model = GenericModel{_value_type(src)}() MOI.copy_to(model, src) return model end From cce1fad96a58cde6ea6a2147c4dbdf0a7252b2f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 8 Aug 2024 09:36:34 +0200 Subject: [PATCH 5/6] Add test --- test/test_file_formats.jl | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/test/test_file_formats.jl b/test/test_file_formats.jl index 6f3f12b78fd..86956daadee 100644 --- a/test/test_file_formats.jl +++ b/test/test_file_formats.jl @@ -6,7 +6,7 @@ module TestFileFormats using JuMP -using Test +using LinearAlgebra, Test function test_mof_file() model = Model() @@ -138,4 +138,16 @@ function test_nl_round_trip() return end +function test_sdpa_bigfloat() + model = Model() + @variable(model, x) + @constraint(model, Symmetric([1 x; x 1]) in PSDCone()) + @objective(model, Max, 2x) + write_to_file(model, "model.dat-s") + model_2 = read_from_file("model.dat-s"; number_type = BigFloat) + @test model_2 isa GenericModel{BigFloat} + rm("model.dat-s") + return +end + end From c6a56889558c7ce8c051bf10345f1dc2d06aa3d8 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 8 Aug 2024 22:13:51 +1200 Subject: [PATCH 6/6] Update test_file_formats.jl --- test/test_file_formats.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/test_file_formats.jl b/test/test_file_formats.jl index 86956daadee..5569c868d46 100644 --- a/test/test_file_formats.jl +++ b/test/test_file_formats.jl @@ -6,7 +6,9 @@ module TestFileFormats using JuMP -using LinearAlgebra, Test +using Test + +import LinearAlgebra function test_mof_file() model = Model() @@ -141,7 +143,7 @@ end function test_sdpa_bigfloat() model = Model() @variable(model, x) - @constraint(model, Symmetric([1 x; x 1]) in PSDCone()) + @constraint(model, LinearAlgebra.Symmetric([1 x; x 1]) in PSDCone()) @objective(model, Max, 2x) write_to_file(model, "model.dat-s") model_2 = read_from_file("model.dat-s"; number_type = BigFloat)