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 method ambiguities and limit operate to Number subtypes #2224

Merged
merged 8 commits into from
Aug 2, 2023
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
26 changes: 9 additions & 17 deletions src/Bridges/Constraint/bridges/functionize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ function MOI.get(
return MOI.get(model, attr, bridge.constraint)
end

function MOI.get(
model::MOI.ModelLike,
::MOI.CanonicalConstraintFunction,
bridge::AbstractFunctionConversionBridge,
)
f = MOI.get(model, MOI.ConstraintFunction(), bridge)
return MOI.Utilities.canonical(f)
odow marked this conversation as resolved.
Show resolved Hide resolved
end

function MOI.supports(
model::MOI.ModelLike,
attr::MOI.AbstractConstraintAttribute,
Expand Down Expand Up @@ -175,14 +184,6 @@ function MOI.delete(model::MOI.ModelLike, c::ScalarFunctionizeBridge)
return
end

function MOI.get(
model::MOI.ModelLike,
attr::MOI.CanonicalConstraintFunction,
b::ScalarFunctionizeBridge,
)
return convert(MOI.VariableIndex, MOI.get(model, attr, b.constraint))
end

function MOI.get(
model::MOI.ModelLike,
attr::MOI.ConstraintFunction,
Expand Down Expand Up @@ -305,15 +306,6 @@ function MOI.set(
return
end

function MOI.get(
model::MOI.ModelLike,
attr::MOI.CanonicalConstraintFunction,
b::VectorFunctionizeBridge,
)
f = MOI.get(model, attr, b.constraint)
return MOI.Utilities.convert_approx(MOI.VectorOfVariables, f)
end

function MOI.get(
model::MOI.ModelLike,
attr::MOI.ConstraintFunction,
Expand Down
26 changes: 26 additions & 0 deletions src/Bridges/Constraint/bridges/slack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,32 @@
return [bridge.slack_in_set]
end

function MOI.get(

Check warning on line 49 in src/Bridges/Constraint/bridges/slack.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/slack.jl#L49

Added line #L49 was not covered by tests
::_AbstractSlackBridge{T,VF,S,F,S},
odow marked this conversation as resolved.
Show resolved Hide resolved
::MOI.NumberOfConstraints{VF,S},
)::Int64 where {T,VF,F,S}
# This method is needed to resolve a possible ambiguity reported by
# Test.detect_ambiguities. It can't happen in practice because it would mean
# that the original set was the same as the slacked set.
return error(

Check warning on line 56 in src/Bridges/Constraint/bridges/slack.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/slack.jl#L56

Added line #L56 was not covered by tests
"Internal error: this method should never be called because it " *
"represents and invalid state. Please open an issue to report.",
)
end

function MOI.get(

Check warning on line 62 in src/Bridges/Constraint/bridges/slack.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/slack.jl#L62

Added line #L62 was not covered by tests
bridge::_AbstractSlackBridge{T,VF,S,F,S},
::MOI.ListOfConstraintIndices{F,S},
) where {T,VF,S,F}
# This method is needed to resolve a possible ambiguity reported by
# Test.detect_ambiguities. It can't happen in practice because it would mean
# that the original set was the same as the slacked set.
return error(

Check warning on line 69 in src/Bridges/Constraint/bridges/slack.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/slack.jl#L69

Added line #L69 was not covered by tests
"Internal error: this method should never be called because it " *
"represents and invalid state. Please open an issue to report.",
)
end

function MOI.delete(model::MOI.ModelLike, bridge::_AbstractSlackBridge)
MOI.delete(model, bridge.equality)
MOI.delete(model, bridge.slack)
Expand Down
71 changes: 25 additions & 46 deletions src/Utilities/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ function Base.:+(
MOI.ScalarQuadraticFunction{T},
},
g::T,
) where {T}
) where {T<:Number}
return operate(+, T, f, g)
end

Expand All @@ -1419,7 +1419,7 @@ function Base.:+(
MOI.ScalarAffineFunction{T},
MOI.ScalarQuadraticFunction{T},
},
) where {T}
) where {T<:Number}
return operate(+, T, f, g)
end

Expand Down Expand Up @@ -1452,7 +1452,7 @@ function Base.:+(
MOI.VectorQuadraticFunction{T},
},
g::AbstractVector{T},
) where {T}
) where {T<:Number}
return operate(+, T, f, g)
end

Expand All @@ -1463,7 +1463,7 @@ function Base.:+(
MOI.VectorAffineFunction{T},
MOI.VectorQuadraticFunction{T},
},
) where {T}
) where {T<:Number}
return operate(+, T, f, g)
end

Expand Down Expand Up @@ -1503,7 +1503,7 @@ function Base.:-(
MOI.VectorQuadraticFunction{T},
},
g::T,
) where {T}
) where {T<:Number}
return operate(-, T, f, g)
end

Expand All @@ -1517,7 +1517,7 @@ function Base.:-(
MOI.VectorAffineFunction{T},
MOI.VectorQuadraticFunction{T},
},
) where {T}
) where {T<:Number}
return operate(-, T, f, g)
end

Expand Down Expand Up @@ -1550,7 +1550,7 @@ function Base.:-(
MOI.VectorQuadraticFunction{T},
},
g::AbstractVector{T},
) where {T}
) where {T<:Number}
return operate(-, T, f, g)
end

Expand All @@ -1561,7 +1561,7 @@ function Base.:-(
MOI.VectorAffineFunction{T},
MOI.VectorQuadraticFunction{T},
},
) where {T}
) where {T<:Number}
return operate(-, T, f, g)
end

Expand Down Expand Up @@ -1616,13 +1616,13 @@ end
# https://github.com/JuliaLang/julia/pull/33205
function Base.:*(
f::Union{
MOI.ScalarAffineFunction,
MOI.ScalarQuadraticFunction,
MOI.VectorAffineFunction,
MOI.VectorQuadraticFunction,
MOI.ScalarAffineFunction{T},
MOI.ScalarQuadraticFunction{T},
MOI.VectorAffineFunction{T},
MOI.VectorQuadraticFunction{T},
},
g::Bool,
)
) where {T<:Number}
if g
return MA.copy_if_mutable(f)
else
Expand Down Expand Up @@ -1659,34 +1659,6 @@ function Base.:*(
return g * f
end

# !!! warning
# MathOptInterface includes these methods to support coefficient types which
# are not subtypes of `Number`. We shoud consider removing them in MOI v2.0.

function Base.:*(
f::Union{
MOI.ScalarAffineFunction{T},
MOI.ScalarQuadraticFunction{T},
MOI.VectorAffineFunction{T},
MOI.VectorQuadraticFunction{T},
},
g::T,
) where {T}
return operate_coefficients(Base.Fix2(*, g), f)
end

function Base.:*(
f::T,
g::Union{
MOI.ScalarAffineFunction{T},
MOI.ScalarQuadraticFunction{T},
MOI.VectorAffineFunction{T},
MOI.VectorQuadraticFunction{T},
},
) where {T}
return g * f
end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is breaking, but only in the sense that it was never documented or expected to work. having functions with non-number coefficients is just asking for trouble.

It's also weird because this is the only case where we'd expect things to work. You can't do *(::NonNumber, ::VariableIndex)


### Base.:/

function Base.:/(
Expand All @@ -1697,7 +1669,7 @@ function Base.:/(
MOI.VectorQuadraticFunction{T},
},
g::T,
) where {T}
) where {T<:Number}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto here. The method below assumes Number, so these are really inconsistent and a bug fix.

return operate(/, T, f, g)
end

Expand Down Expand Up @@ -1732,8 +1704,8 @@ end
### LinearAlgebra

LinearAlgebra.dot(f::ScalarLike, g::ScalarLike) = f * g
LinearAlgebra.dot(α::T, func::TypedLike{T}) where {T} = α * func
LinearAlgebra.dot(func::TypedLike{T}, α::T) where {T} = func * α
LinearAlgebra.dot(α::T, func::TypedLike{T}) where {T<:Number} = α * func
LinearAlgebra.dot(func::TypedLike{T}, α::T) where {T<:Number} = func * α
LinearAlgebra.adjoint(f::ScalarLike) = f
LinearAlgebra.transpose(f::ScalarLike) = f
LinearAlgebra.symmetric_type(::Type{F}) where {F<:ScalarLike} = F
Expand Down Expand Up @@ -2254,9 +2226,16 @@ function Base.promote_rule(
end

function Base.promote_rule(
F::Type{<:Union{MOI.ScalarAffineFunction,MOI.ScalarQuadraticFunction}},
F::Type{MOI.ScalarAffineFunction{T}},
::Type{MOI.VariableIndex},
)
) where {T}
return F
end

function Base.promote_rule(
F::Type{MOI.ScalarQuadraticFunction{T}},
::Type{MOI.VariableIndex},
) where {T}
return F
end

Expand Down
8 changes: 4 additions & 4 deletions src/Utilities/mutable_arithmetics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ function MA.promote_operation(
op::PROMOTE_IMPLEMENTED_OP,
F::Type{T},
G::Type{<:TypedLike{T}},
) where {T}
) where {T<:Number}
return promote_operation(op, T, F, G)
end

function MA.promote_operation(
op::PROMOTE_IMPLEMENTED_OP,
F::Type{<:TypedLike{T}},
G::Type{T},
) where {T}
) where {T<:Number}
return promote_operation(op, T, F, G)
end

Expand Down Expand Up @@ -270,7 +270,7 @@ function _add_sub_affine_terms(
α::T,
f::MOI.VariableIndex,
β::T,
) where {T}
) where {T<:Number}
push!(terms, MOI.ScalarAffineTerm(op(α * β), f))
return
end
Expand All @@ -280,7 +280,7 @@ function _add_sub_affine_terms(
terms::Vector{MOI.ScalarAffineTerm{T}},
f::MOI.VariableIndex,
β::T,
) where {T}
) where {T<:Number}
push!(terms, MOI.ScalarAffineTerm(op(β), f))
return
end
Expand Down
Loading
Loading