diff --git a/src/FileFormats/LP/write.jl b/src/FileFormats/LP/write.jl index c4d41a8750..e21083b6b5 100644 --- a/src/FileFormats/LP/write.jl +++ b/src/FileFormats/LP/write.jl @@ -47,7 +47,7 @@ function _write_function( kwargs..., ) is_first_item = true - if !iszero(func.constant) + if !iszero(func.constant) || isempty(func.terms) _print_shortest(io, func.constant) is_first_item = false end @@ -77,7 +77,8 @@ function _write_function( kwargs..., ) is_first_item = true - if !iszero(func.constant) + needs_constant = isempty(func.affine_terms) && isempty(func.quadratic_terms) + if !iszero(func.constant) || needs_constant _print_shortest(io, func.constant) is_first_item = false end diff --git a/src/MathOptInterface.jl b/src/MathOptInterface.jl index a1fca86501..7a90e98bf0 100644 --- a/src/MathOptInterface.jl +++ b/src/MathOptInterface.jl @@ -176,7 +176,7 @@ julia> MOI.write_to_file(model, filename; coefficient_type = Int); julia> print(read(filename, String)) minimize -obj: +obj: 0 subject to Bounds 2 <= x <= 3 diff --git a/test/FileFormats/LP/test_LP.jl b/test/FileFormats/LP/test_LP.jl index 9bef8d8718..a2c58dddbf 100644 --- a/test/FileFormats/LP/test_LP.jl +++ b/test/FileFormats/LP/test_LP.jl @@ -345,7 +345,7 @@ function test_quadratic_constraint_diag() MOI.write_to_file(model, LP_TEST_FILE) @test read(LP_TEST_FILE, String) == "minimize\n" * - "obj: \n" * + "obj: 0\n" * "subject to\n" * "c: [ 1 x ^ 2 ] <= 1.4\n" * "Bounds\n" * @@ -366,7 +366,7 @@ function test_quadratic_constraint_off_diag() MOI.write_to_file(model, LP_TEST_FILE) @test read(LP_TEST_FILE, String) == "minimize\n" * - "obj: \n" * + "obj: 0\n" * "subject to\n" * "c: 1.3 + 1.1 x + 1.2 y + [ 1.5 x * y ] = 1.5\n" * "Bounds\n" * @@ -388,7 +388,7 @@ function test_quadratic_constraint_complicated() MOI.write_to_file(model, LP_TEST_FILE) @test read(LP_TEST_FILE, String) == "minimize\n" * - "obj: \n" * + "obj: 0\n" * "subject to\n" * "c: -1.1 <= 1.3 + 1.1 x + 1.2 y + [ -1.1 x ^ 2 + 1.5 x * y ] <= 1.4\n" * "Bounds\n" * @@ -417,7 +417,7 @@ function test_write_indicator() MOI.write_to_file(model, LP_TEST_FILE) @test read(LP_TEST_FILE, String) == "minimize\n" * - "obj: \n" * + "obj: 0\n" * "subject to\n" * "c4: z = 1 -> 2 x <= 0\n" * "c1: z = 1 -> x <= 0\n" * @@ -722,7 +722,7 @@ function test_infinite_interval() MOI.add_constraint(model, 1.0 * x, MOI.Interval(3.0, 4.0)) @test sprint(write, model) == "minimize\n" * - "obj: \n" * + "obj: 0\n" * "subject to\n" * "c1: -inf <= 1 x1 <= inf\n" * "c2: -inf <= 1 x1 <= 1\n" * @@ -1849,6 +1849,30 @@ function test_unsupported_kwarg() return end +function test_empty_function() + model = LP.Model() + x = MOI.add_variable(model) + c = MOI.add_constraint( + model, + zero(MOI.ScalarAffineFunction{Float64}), + MOI.LessThan(1.0), + ) + MOI.set(model, MOI.ConstraintName(), c, "c") + d = MOI.add_constraint( + model, + zero(MOI.ScalarQuadraticFunction{Float64}), + MOI.LessThan(1.0), + ) + MOI.set(model, MOI.ConstraintName(), d, "d") + io = IOBuffer() + write(io, model) + seekstart(io) + contents = read(io, String) + @test occursin("c: 0 <= 1", contents) + @test occursin("d: 0 <= 1", contents) + return +end + end # module TestLP.runtests()