Skip to content

Commit b1f7515

Browse files
authored
Fix method ambiguities and limit operate to Number subtypes (#2224)
1 parent 6462671 commit b1f7515

File tree

10 files changed

+232
-214
lines changed

10 files changed

+232
-214
lines changed

src/Bridges/Constraint/bridges/functionize.jl

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ function MOI.get(
3333
return MOI.get(model, attr, bridge.constraint)
3434
end
3535

36+
function MOI.get(
37+
model::MOI.ModelLike,
38+
::MOI.CanonicalConstraintFunction,
39+
bridge::AbstractFunctionConversionBridge,
40+
)
41+
f = MOI.get(model, MOI.ConstraintFunction(), bridge)
42+
return MOI.Utilities.canonical(f)
43+
end
44+
3645
function MOI.supports(
3746
model::MOI.ModelLike,
3847
attr::MOI.AbstractConstraintAttribute,
@@ -175,14 +184,6 @@ function MOI.delete(model::MOI.ModelLike, c::ScalarFunctionizeBridge)
175184
return
176185
end
177186

178-
function MOI.get(
179-
model::MOI.ModelLike,
180-
attr::MOI.CanonicalConstraintFunction,
181-
b::ScalarFunctionizeBridge,
182-
)
183-
return convert(MOI.VariableIndex, MOI.get(model, attr, b.constraint))
184-
end
185-
186187
function MOI.get(
187188
model::MOI.ModelLike,
188189
attr::MOI.ConstraintFunction,
@@ -305,15 +306,6 @@ function MOI.set(
305306
return
306307
end
307308

308-
function MOI.get(
309-
model::MOI.ModelLike,
310-
attr::MOI.CanonicalConstraintFunction,
311-
b::VectorFunctionizeBridge,
312-
)
313-
f = MOI.get(model, attr, b.constraint)
314-
return MOI.Utilities.convert_approx(MOI.VectorOfVariables, f)
315-
end
316-
317309
function MOI.get(
318310
model::MOI.ModelLike,
319311
attr::MOI.ConstraintFunction,

src/Bridges/Constraint/bridges/slack.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,32 @@ function MOI.get(
4646
return [bridge.slack_in_set]
4747
end
4848

49+
function MOI.get(
50+
::_AbstractSlackBridge{T,VF,S,F,S},
51+
::MOI.NumberOfConstraints{VF,S},
52+
)::Int64 where {T,VF,F,S}
53+
# This method is needed to resolve a possible ambiguity reported by
54+
# Test.detect_ambiguities. It can't happen in practice because it would mean
55+
# that the original set was the same as the slacked set.
56+
return error(
57+
"Internal error: this method should never be called because it " *
58+
"represents and invalid state. Please open an issue to report.",
59+
)
60+
end
61+
62+
function MOI.get(
63+
bridge::_AbstractSlackBridge{T,VF,S,F,S},
64+
::MOI.ListOfConstraintIndices{F,S},
65+
) where {T,VF,S,F}
66+
# This method is needed to resolve a possible ambiguity reported by
67+
# Test.detect_ambiguities. It can't happen in practice because it would mean
68+
# that the original set was the same as the slacked set.
69+
return error(
70+
"Internal error: this method should never be called because it " *
71+
"represents and invalid state. Please open an issue to report.",
72+
)
73+
end
74+
4975
function MOI.delete(model::MOI.ModelLike, bridge::_AbstractSlackBridge)
5076
MOI.delete(model, bridge.equality)
5177
MOI.delete(model, bridge.slack)

src/Utilities/functions.jl

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ function Base.:+(
14081408
MOI.ScalarQuadraticFunction{T},
14091409
},
14101410
g::T,
1411-
) where {T}
1411+
) where {T<:Number}
14121412
return operate(+, T, f, g)
14131413
end
14141414

@@ -1419,7 +1419,7 @@ function Base.:+(
14191419
MOI.ScalarAffineFunction{T},
14201420
MOI.ScalarQuadraticFunction{T},
14211421
},
1422-
) where {T}
1422+
) where {T<:Number}
14231423
return operate(+, T, f, g)
14241424
end
14251425

@@ -1452,7 +1452,7 @@ function Base.:+(
14521452
MOI.VectorQuadraticFunction{T},
14531453
},
14541454
g::AbstractVector{T},
1455-
) where {T}
1455+
) where {T<:Number}
14561456
return operate(+, T, f, g)
14571457
end
14581458

@@ -1463,7 +1463,7 @@ function Base.:+(
14631463
MOI.VectorAffineFunction{T},
14641464
MOI.VectorQuadraticFunction{T},
14651465
},
1466-
) where {T}
1466+
) where {T<:Number}
14671467
return operate(+, T, f, g)
14681468
end
14691469

@@ -1503,7 +1503,7 @@ function Base.:-(
15031503
MOI.VectorQuadraticFunction{T},
15041504
},
15051505
g::T,
1506-
) where {T}
1506+
) where {T<:Number}
15071507
return operate(-, T, f, g)
15081508
end
15091509

@@ -1517,7 +1517,7 @@ function Base.:-(
15171517
MOI.VectorAffineFunction{T},
15181518
MOI.VectorQuadraticFunction{T},
15191519
},
1520-
) where {T}
1520+
) where {T<:Number}
15211521
return operate(-, T, f, g)
15221522
end
15231523

@@ -1550,7 +1550,7 @@ function Base.:-(
15501550
MOI.VectorQuadraticFunction{T},
15511551
},
15521552
g::AbstractVector{T},
1553-
) where {T}
1553+
) where {T<:Number}
15541554
return operate(-, T, f, g)
15551555
end
15561556

@@ -1561,7 +1561,7 @@ function Base.:-(
15611561
MOI.VectorAffineFunction{T},
15621562
MOI.VectorQuadraticFunction{T},
15631563
},
1564-
) where {T}
1564+
) where {T<:Number}
15651565
return operate(-, T, f, g)
15661566
end
15671567

@@ -1616,13 +1616,13 @@ end
16161616
# https://github.com/JuliaLang/julia/pull/33205
16171617
function Base.:*(
16181618
f::Union{
1619-
MOI.ScalarAffineFunction,
1620-
MOI.ScalarQuadraticFunction,
1621-
MOI.VectorAffineFunction,
1622-
MOI.VectorQuadraticFunction,
1619+
MOI.ScalarAffineFunction{T},
1620+
MOI.ScalarQuadraticFunction{T},
1621+
MOI.VectorAffineFunction{T},
1622+
MOI.VectorQuadraticFunction{T},
16231623
},
16241624
g::Bool,
1625-
)
1625+
) where {T<:Number}
16261626
if g
16271627
return MA.copy_if_mutable(f)
16281628
else
@@ -1659,34 +1659,6 @@ function Base.:*(
16591659
return g * f
16601660
end
16611661

1662-
# !!! warning
1663-
# MathOptInterface includes these methods to support coefficient types which
1664-
# are not subtypes of `Number`. We shoud consider removing them in MOI v2.0.
1665-
1666-
function Base.:*(
1667-
f::Union{
1668-
MOI.ScalarAffineFunction{T},
1669-
MOI.ScalarQuadraticFunction{T},
1670-
MOI.VectorAffineFunction{T},
1671-
MOI.VectorQuadraticFunction{T},
1672-
},
1673-
g::T,
1674-
) where {T}
1675-
return operate_coefficients(Base.Fix2(*, g), f)
1676-
end
1677-
1678-
function Base.:*(
1679-
f::T,
1680-
g::Union{
1681-
MOI.ScalarAffineFunction{T},
1682-
MOI.ScalarQuadraticFunction{T},
1683-
MOI.VectorAffineFunction{T},
1684-
MOI.VectorQuadraticFunction{T},
1685-
},
1686-
) where {T}
1687-
return g * f
1688-
end
1689-
16901662
### Base.:/
16911663

16921664
function Base.:/(
@@ -1697,7 +1669,7 @@ function Base.:/(
16971669
MOI.VectorQuadraticFunction{T},
16981670
},
16991671
g::T,
1700-
) where {T}
1672+
) where {T<:Number}
17011673
return operate(/, T, f, g)
17021674
end
17031675

@@ -1732,8 +1704,8 @@ end
17321704
### LinearAlgebra
17331705

17341706
LinearAlgebra.dot(f::ScalarLike, g::ScalarLike) = f * g
1735-
LinearAlgebra.dot::T, func::TypedLike{T}) where {T} = α * func
1736-
LinearAlgebra.dot(func::TypedLike{T}, α::T) where {T} = func * α
1707+
LinearAlgebra.dot::T, func::TypedLike{T}) where {T<:Number} = α * func
1708+
LinearAlgebra.dot(func::TypedLike{T}, α::T) where {T<:Number} = func * α
17371709
LinearAlgebra.adjoint(f::ScalarLike) = f
17381710
LinearAlgebra.transpose(f::ScalarLike) = f
17391711
LinearAlgebra.symmetric_type(::Type{F}) where {F<:ScalarLike} = F
@@ -2254,9 +2226,16 @@ function Base.promote_rule(
22542226
end
22552227

22562228
function Base.promote_rule(
2257-
F::Type{<:Union{MOI.ScalarAffineFunction,MOI.ScalarQuadraticFunction}},
2229+
F::Type{MOI.ScalarAffineFunction{T}},
22582230
::Type{MOI.VariableIndex},
2259-
)
2231+
) where {T}
2232+
return F
2233+
end
2234+
2235+
function Base.promote_rule(
2236+
F::Type{MOI.ScalarQuadraticFunction{T}},
2237+
::Type{MOI.VariableIndex},
2238+
) where {T}
22602239
return F
22612240
end
22622241

src/Utilities/mutable_arithmetics.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ function MA.promote_operation(
102102
op::PROMOTE_IMPLEMENTED_OP,
103103
F::Type{T},
104104
G::Type{<:TypedLike{T}},
105-
) where {T}
105+
) where {T<:Number}
106106
return promote_operation(op, T, F, G)
107107
end
108108

109109
function MA.promote_operation(
110110
op::PROMOTE_IMPLEMENTED_OP,
111111
F::Type{<:TypedLike{T}},
112112
G::Type{T},
113-
) where {T}
113+
) where {T<:Number}
114114
return promote_operation(op, T, F, G)
115115
end
116116

@@ -270,7 +270,7 @@ function _add_sub_affine_terms(
270270
α::T,
271271
f::MOI.VariableIndex,
272272
β::T,
273-
) where {T}
273+
) where {T<:Number}
274274
push!(terms, MOI.ScalarAffineTerm(op* β), f))
275275
return
276276
end
@@ -280,7 +280,7 @@ function _add_sub_affine_terms(
280280
terms::Vector{MOI.ScalarAffineTerm{T}},
281281
f::MOI.VariableIndex,
282282
β::T,
283-
) where {T}
283+
) where {T<:Number}
284284
push!(terms, MOI.ScalarAffineTerm(op(β), f))
285285
return
286286
end

0 commit comments

Comments
 (0)