Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix converting 0-valued Scalar(Affine,Quadratic)Function to Nonlinear #2388

Merged
merged 1 commit into from
Jan 4, 2024
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
4 changes: 2 additions & 2 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ end

function Base.convert(F::Type{ScalarNonlinearFunction}, f::ScalarAffineFunction)
args = Any[convert(ScalarNonlinearFunction, term) for term in f.terms]
if !iszero(f.constant)
if isempty(args) || !iszero(f.constant)
push!(args, f.constant)
end
return ScalarNonlinearFunction(:+, args)
Expand Down Expand Up @@ -1243,7 +1243,7 @@ function Base.convert(
for term in f.affine_terms
push!(args, convert(F, term))
end
if !iszero(f.constant)
if isempty(args) || !iszero(f.constant)
push!(args, f.constant)
end
return ScalarNonlinearFunction(:+, args)
Expand Down
14 changes: 14 additions & 0 deletions test/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,20 @@ function test_convert_ScalarNonlinearFunction_ScalarAffineFunction()
return
end

function test_convert_ScalarNonlinearFunction_zero()
affine_terms = MOI.ScalarAffineTerm{Float64}[]
quad_terms = MOI.ScalarQuadraticTerm{Float64}[]
for f in (
MOI.ScalarAffineFunction(affine_terms, 0.0),
MOI.ScalarQuadraticFunction(quad_terms, affine_terms, 0.0),
)
g = convert(MOI.ScalarNonlinearFunction, f)
@test g.head == :+
@test g.args == Any[0.0]
end
return
end

function test_convert_ScalarNonlinearFunction_ScalarQuadraticTerm()
x = MOI.VariableIndex(1)
y = MOI.VariableIndex(2)
Expand Down
Loading