Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions src/FileFormats/LP/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/MathOptInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 29 additions & 5 deletions test/FileFormats/LP/test_LP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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" *
Expand All @@ -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" *
Expand All @@ -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" *
Expand Down Expand Up @@ -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" *
Expand Down Expand Up @@ -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" *
Expand Down Expand Up @@ -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()
Loading