Skip to content

Commit 957328f

Browse files
authored
Fix converting 0-valued Scalar(Affine,Quadratic)Function to Nonlinear (#2388)
1 parent 084a899 commit 957328f

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/functions.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ end
12151215

12161216
function Base.convert(F::Type{ScalarNonlinearFunction}, f::ScalarAffineFunction)
12171217
args = Any[convert(ScalarNonlinearFunction, term) for term in f.terms]
1218-
if !iszero(f.constant)
1218+
if isempty(args) || !iszero(f.constant)
12191219
push!(args, f.constant)
12201220
end
12211221
return ScalarNonlinearFunction(:+, args)
@@ -1243,7 +1243,7 @@ function Base.convert(
12431243
for term in f.affine_terms
12441244
push!(args, convert(F, term))
12451245
end
1246-
if !iszero(f.constant)
1246+
if isempty(args) || !iszero(f.constant)
12471247
push!(args, f.constant)
12481248
end
12491249
return ScalarNonlinearFunction(:+, args)

test/functions.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,20 @@ function test_convert_ScalarNonlinearFunction_ScalarAffineFunction()
363363
return
364364
end
365365

366+
function test_convert_ScalarNonlinearFunction_zero()
367+
affine_terms = MOI.ScalarAffineTerm{Float64}[]
368+
quad_terms = MOI.ScalarQuadraticTerm{Float64}[]
369+
for f in (
370+
MOI.ScalarAffineFunction(affine_terms, 0.0),
371+
MOI.ScalarQuadraticFunction(quad_terms, affine_terms, 0.0),
372+
)
373+
g = convert(MOI.ScalarNonlinearFunction, f)
374+
@test g.head == :+
375+
@test g.args == Any[0.0]
376+
end
377+
return
378+
end
379+
366380
function test_convert_ScalarNonlinearFunction_ScalarQuadraticTerm()
367381
x = MOI.VariableIndex(1)
368382
y = MOI.VariableIndex(2)

0 commit comments

Comments
 (0)