Skip to content

Commit 8a49f18

Browse files
authored
[FileFormats.LP] fix writing of off-diag quadratic elements (#2082)
1 parent f820ac9 commit 8a49f18

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/FileFormats/LP/LP.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ function _write_function(
158158
if !print_half && term.variable_1 == term.variable_2
159159
coefficient /= 2
160160
end
161+
if print_half && term.variable_1 != term.variable_2
162+
coefficient *= 2
163+
end
161164
if is_first_item
162165
_print_shortest(io, coefficient)
163166
is_first_item = false

test/FileFormats/LP/LP.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ minobjective: 1.1 * x + 1.2 * y + 1.5*x*y + 1.3
295295
MOI.write_to_file(model, LP_TEST_FILE)
296296
@test read(LP_TEST_FILE, String) ==
297297
"minimize\n" *
298-
"obj: 1.3 + 1.1 x + 1.2 y + [ 1.5 x * y ]/2\n" *
298+
"obj: 1.3 + 1.1 x + 1.2 y + [ 3 x * y ]/2\n" *
299299
"subject to\n" *
300300
"Bounds\n" *
301301
"x free\n" *
@@ -316,7 +316,7 @@ minobjective: 1.1 * x + 1.2 * y + -1.1 * x * x + 1.5*x*y + 1.3
316316
MOI.write_to_file(model, LP_TEST_FILE)
317317
@test read(LP_TEST_FILE, String) ==
318318
"minimize\n" *
319-
"obj: 1.3 + 1.1 x + 1.2 y + [ -2.2 x ^ 2 + 1.5 x * y ]/2\n" *
319+
"obj: 1.3 + 1.1 x + 1.2 y + [ -2.2 x ^ 2 + 3 x * y ]/2\n" *
320320
"subject to\n" *
321321
"Bounds\n" *
322322
"x free\n" *
@@ -460,7 +460,7 @@ function test_read_model1_tricky()
460460
seekstart(io)
461461
file = read(io, String)
462462
@test occursin("maximize", file)
463-
@test occursin("obj: -1 Var4 + 1 V5 + [ 1 Var4 ^ 2 - 1.2 V5 * V1 ]/2", file)
463+
@test occursin("obj: -1 Var4 + 1 V5 + [ 1 Var4 ^ 2 - 2.4 V5 * V1 ]/2", file)
464464
@test occursin("CON3: 1 V3 <= 2.5", file)
465465
@test occursin("CON4: 1 V5 + 1 V6 + 1 V7 <= 1", file)
466466
@test occursin("CON1: 1 V1 >= 0", file)
@@ -640,7 +640,7 @@ function test_read_tricky_quadratic()
640640
seekstart(io)
641641
file = read(io, String)
642642
@test occursin("minimize", file)
643-
@test occursin("obj: [ 2 x ^ 2 + 1 x * y ]/2", file)
643+
@test occursin("obj: [ 2 x ^ 2 + 2 x * y ]/2", file)
644644
@test occursin("c1: [ 1 x ^ 2 - 1 x * y ] <= 0", file)
645645
@test occursin("c2: [ 0.5 x ^ 2 - 1 x * y ] <= 0", file)
646646
@test occursin("c3: [ 0.5 x ^ 2 - 1 x * y ] <= 0", file)

0 commit comments

Comments
 (0)