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 promotion rules for GenericNonlinearExpr #3483

Merged
merged 7 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
29 changes: 20 additions & 9 deletions src/nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -673,11 +673,25 @@ function _evaluate_expr(
end
end

# MutableArithmetics.jl
# MutableArithmetics.jl and promotion

# These converts are used in the {add,sub}mul definition for AbstractJuMPScalar.
function Base.promote_rule(
::Type{GenericNonlinearExpr{V}},
::Type{V},
) where {V<:AbstractVariableRef}
return GenericNonlinearExpr{V}
end

function Base.promote_rule(
::Type{GenericNonlinearExpr{V}},
::Type{<:Union{GenericAffExpr{C,V},GenericQuadExpr{C,V}}},
) where {C,V<:AbstractVariableRef}
return GenericNonlinearExpr{V}
end

Base.convert(::Type{<:GenericNonlinearExpr}, x::AbstractVariableRef) = x
function Base.convert(::Type{GenericNonlinearExpr{V}}, x::V) where {V}
return GenericNonlinearExpr{V}(:+, Any[x])
end

function Base.convert(
::Type{<:GenericNonlinearExpr},
Expand Down Expand Up @@ -1097,17 +1111,14 @@ function jump_function(
]
end

# We use `AbstractJuMPScalar` as a catch-all fallback for any mix of JuMP
# scalars that have not been dispatched by some other method.

function moi_function_type(::Type{<:Vector{<:AbstractJuMPScalar}})
function moi_function_type(::Type{<:AbstractVector{<:GenericNonlinearExpr}})
return MOI.VectorNonlinearFunction
end

function moi_function(f::Vector{<:AbstractJuMPScalar})
function moi_function(f::AbstractVector{<:GenericNonlinearExpr})
return MOI.VectorNonlinearFunction(f)
end

function MOI.VectorNonlinearFunction(f::Vector{<:AbstractJuMPScalar})
function MOI.VectorNonlinearFunction(f::AbstractVector{<:GenericNonlinearExpr})
odow marked this conversation as resolved.
Show resolved Hide resolved
odow marked this conversation as resolved.
Show resolved Hide resolved
return MOI.VectorNonlinearFunction(map(moi_function, f))
end
31 changes: 25 additions & 6 deletions test/test_nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -741,26 +741,45 @@ function test_VectorNonlinearFunction_moi_function()
return
end

function test_VectorNonlinearFunction_moi_function_AbstractJuMPScalar()
function test_VectorNonlinearFunction_moi_function_conversion()
model = Model()
@variable(model, x)
F = [sin(x), x]
@test F isa Vector{AbstractJuMPScalar}
F = [sin(x), x, x + 1, x^2]
@test F isa Vector{NonlinearExpr}
@test moi_function_type(typeof(F)) == MOI.VectorNonlinearFunction
@test isapprox(
moi_function(F),
MOI.VectorNonlinearFunction([
MOI.ScalarNonlinearFunction(:sin, Any[index(x)]),
MOI.ScalarNonlinearFunction(:+, Any[index(x)]),
MOI.ScalarNonlinearFunction(:+, Any[index(x), 1.0]),
MOI.ScalarNonlinearFunction(:*, Any[index(x), index(x)]),
]),
)
@test MOI.VectorNonlinearFunction(F) ≈ moi_function(F)
@test jump_function_type(model, MOI.VectorNonlinearFunction) ==
Vector{NonlinearExpr}
@test isequal_canonical(
jump_function(model, moi_function(F)),
[sin(x), NonlinearExpr(:+, x)],
@test isequal_canonical(jump_function(model, moi_function(F)), F)
return
end

function test_VectorNonlinearFunction_moi_function_conversion_variable()
model = Model()
@variable(model, x)
F = [sin(x), x]
@test F isa Vector{NonlinearExpr}
@test moi_function_type(typeof(F)) == MOI.VectorNonlinearFunction
@test isapprox(
moi_function(F),
MOI.VectorNonlinearFunction([
MOI.ScalarNonlinearFunction(:sin, Any[index(x)]),
MOI.ScalarNonlinearFunction(:+, Any[index(x)]),
]),
)
@test MOI.VectorNonlinearFunction(F) ≈ moi_function(F)
@test jump_function_type(model, MOI.VectorNonlinearFunction) ==
Vector{NonlinearExpr}
@test isequal_canonical(jump_function(model, moi_function(F)), F)
return
end

Expand Down