Skip to content

Add value_type for AbstractFunction #2176

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

Merged
merged 3 commits into from
May 24, 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
92 changes: 58 additions & 34 deletions src/Utilities/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,67 @@
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.

# Functions convertible to a ScalarAffineFunction
const ScalarAffineLike{T} =
Union{T,MOI.VariableIndex,MOI.ScalarAffineFunction{T}}
# Functions convertible to a ScalarQuadraticFunction
const ScalarQuadraticLike{T} =
Union{ScalarAffineLike{T},MOI.ScalarQuadraticFunction{T}}

# `ScalarLike` for which `T` is defined to avoid defining, e.g.,
# `+(::VariableIndex, ::Any)` which should rather be
# `+(::VariableIndex, ::Number)`.
const TypedScalarLike{T} =
Union{MOI.ScalarAffineFunction{T},MOI.ScalarQuadraticFunction{T}}
# Used for overloading Base operator functions so `T` is not in the union to
# avoid overloading e.g. `+(::Float64, ::Float64)`
const ScalarLike{T} = Union{MOI.VariableIndex,TypedScalarLike{T}}

# Functions convertible to a VectorAffineFunction
const VectorAffineLike{T} =
Union{Vector{T},MOI.VectorOfVariables,MOI.VectorAffineFunction{T}}
# Functions convertible to a VectorQuadraticFunction
const VectorQuadraticLike{T} =
Union{VectorAffineLike{T},MOI.VectorQuadraticFunction{T}}

# `VectorLike` for which `T` is defined to avoid defining, e.g.,
# `+(::VectorOfVariables, ::Any)` which should rather be
# `+(::VectorOfVariables, ::Number)`.
const TypedVectorLike{T} =
Union{MOI.VectorAffineFunction{T},MOI.VectorQuadraticFunction{T}}
# Used for overloading Base operator functions so `T` is not in the union to
# avoid overloading e.g. `+(::Float64, ::Float64)`
const VectorLike{T} = Union{MOI.VectorOfVariables,TypedVectorLike{T}}

const TypedLike{T} = Union{TypedScalarLike{T},TypedVectorLike{T}}

variable_function_type(::Type{<:MOI.AbstractScalarSet}) = MOI.VariableIndex
variable_function_type(::Type{<:MOI.AbstractVectorSet}) = MOI.VectorOfVariables

"""
value_type(::Type{T}, ::Type{F}) where {T,F<:AbstractFunction}

Returns the output type that results if a function of type `F` is evaluated
using variables with numeric type `T`.

In other words, this is the return type for
`MOI.Utilities.eval_variables(variable_values::Function, f::F)`
for a function `variable_values(::MOI.VariableIndex)::T`.
"""
function value_type end

value_type(::Type{T}, ::Type{MOI.VariableIndex}) where {T} = T

value_type(::Type{T}, ::Type{MOI.VectorOfVariables}) where {T} = Vector{T}

function value_type(::Type{T}, ::Type{<:TypedScalarLike{C}}) where {C,T}
return MA.promote_operation(*, C, T)
end

function value_type(::Type{T}, ::Type{<:TypedVectorLike{C}}) where {C,T}
return Vector{MA.promote_operation(*, C, T)}
end

"""
eval_variables(varval::Function, f::AbstractFunction)

Expand Down Expand Up @@ -1494,40 +1552,6 @@ function map_terms!(
return map!(op, func.quadratic_terms, func.quadratic_terms)
end

# Functions convertible to a ScalarAffineFunction
const ScalarAffineLike{T} =
Union{T,MOI.VariableIndex,MOI.ScalarAffineFunction{T}}
# Functions convertible to a ScalarQuadraticFunction
const ScalarQuadraticLike{T} =
Union{ScalarAffineLike{T},MOI.ScalarQuadraticFunction{T}}

# `ScalarLike` for which `T` is defined to avoid defining, e.g.,
# `+(::VariableIndex, ::Any)` which should rather be
# `+(::VariableIndex, ::Number)`.
const TypedScalarLike{T} =
Union{MOI.ScalarAffineFunction{T},MOI.ScalarQuadraticFunction{T}}
# Used for overloading Base operator functions so `T` is not in the union to
# avoid overloading e.g. `+(::Float64, ::Float64)`
const ScalarLike{T} = Union{MOI.VariableIndex,TypedScalarLike{T}}

# Functions convertible to a VectorAffineFunction
const VectorAffineLike{T} =
Union{Vector{T},MOI.VectorOfVariables,MOI.VectorAffineFunction{T}}
# Functions convertible to a VectorQuadraticFunction
const VectorQuadraticLike{T} =
Union{VectorAffineLike{T},MOI.VectorQuadraticFunction{T}}

# `VectorLike` for which `T` is defined to avoid defining, e.g.,
# `+(::VectorOfVariables, ::Any)` which should rather be
# `+(::VectorOfVariables, ::Number)`.
const TypedVectorLike{T} =
Union{MOI.VectorAffineFunction{T},MOI.VectorQuadraticFunction{T}}
# Used for overloading Base operator functions so `T` is not in the union to
# avoid overloading e.g. `+(::Float64, ::Float64)`
const VectorLike{T} = Union{MOI.VectorOfVariables,TypedVectorLike{T}}

const TypedLike{T} = Union{TypedScalarLike{T},TypedVectorLike{T}}

###################################### +/- #####################################
## promote_operation

Expand Down
42 changes: 42 additions & 0 deletions test/Utilities/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,48 @@ function test_SingleVariable_operators()
return
end

function test_value_type()
T = Float64
@test MOI.Utilities.value_type(T, MOI.VariableIndex) == T
@test MOI.Utilities.value_type(T, MOI.VectorOfVariables) == Vector{T}
@test MOI.Utilities.value_type(T, MOI.ScalarAffineFunction{Int}) == T
@test MOI.Utilities.value_type(T, MOI.ScalarAffineFunction{T}) == T
@test MOI.Utilities.value_type(T, MOI.ScalarAffineFunction{Complex{Int}}) ==
Complex{T}
@test MOI.Utilities.value_type(T, MOI.ScalarAffineFunction{Complex{T}}) ==
Complex{T}
@test MOI.Utilities.value_type(T, MOI.ScalarQuadraticFunction{Int}) == T
@test MOI.Utilities.value_type(T, MOI.ScalarQuadraticFunction{T}) == T
@test MOI.Utilities.value_type(
T,
MOI.ScalarQuadraticFunction{Complex{Int}},
) == Complex{T}
@test MOI.Utilities.value_type(
T,
MOI.ScalarQuadraticFunction{Complex{T}},
) == Complex{T}
@test MOI.Utilities.value_type(T, MOI.VectorAffineFunction{Int}) ==
Vector{T}
@test MOI.Utilities.value_type(T, MOI.VectorAffineFunction{T}) == Vector{T}
@test MOI.Utilities.value_type(T, MOI.VectorAffineFunction{Complex{Int}}) ==
Vector{Complex{T}}
@test MOI.Utilities.value_type(T, MOI.VectorAffineFunction{Complex{T}}) ==
Vector{Complex{T}}
@test MOI.Utilities.value_type(T, MOI.VectorQuadraticFunction{Int}) ==
Vector{T}
@test MOI.Utilities.value_type(T, MOI.VectorQuadraticFunction{T}) ==
Vector{T}
@test MOI.Utilities.value_type(
T,
MOI.VectorQuadraticFunction{Complex{Int}},
) == Vector{Complex{T}}
@test MOI.Utilities.value_type(
T,
MOI.VectorQuadraticFunction{Complex{T}},
) == Vector{Complex{T}}
return
end

end # module

TestFunctions.runtests()