Skip to content

Missing bridge to ScalarNonlinearFunction in Objective #2833

@LukasBarner

Description

@LukasBarner

From Slack:

Constraints from scalar affine / quadratic are accurately bridged to nonlinear. However, for the objective, if a solver only declares support for scalar nonlinear, bridging does not work as expected:

@objective(model, Min, x)
ERROR: The solver does not support an objective function of type MathOptInterface.ScalarAffineFunction{Float64}

To illustrate, see the following MWE:

import MathOptInterface as MOI
using JuMP

mutable struct Optimizer <: MOI.AbstractOptimizer
end

function MOI.supports(
    ::Optimizer,
    ::MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{Float64}},
)
    return true
end

function MOI.copy_to(dest::Optimizer, src::MOI.ModelLike)
    println(src)
    return MOI.IndexMap()
end

MOI.is_empty(::Optimizer) = true
MOI.empty!(::Optimizer) = nothing

MOI.optimize!(::Optimizer) = nothing

model = Model(Optimizer)
@variable(model, x[1:2])

@objective(model, Max, x[1] + x[2])

optimize!(model)
# This works fine, the resulting print statement is: 
# Maximize ScalarQuadraticFunction{Float64}:
#  0.0 + 1.0 x[1] + 1.0 x[2]

# Subject to:


# However, this does not work: 
function MOI.supports(
    ::Optimizer,
    ::MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{Float64}},
)
    return false
end

function MOI.supports(
    ::Optimizer,
    ::MOI.ObjectiveFunction{MOI.ScalarNonlinearFunction},
)
    return true
end

model = Model(Optimizer)
@variable(model, x[1:2])

@objective(model, Max, x[1] + x[2])
# ERROR: The solver does not support an objective 
# function of type MathOptInterface.ScalarAffineFunction{Float64}.

optimize!(model)

# Based on the docs I would expect this to work.
# Also, these functions return true:
MOI.Bridges.Objective.supports_objective_function(
    MOI.Bridges.Objective.FunctionConversionBridge{Float64, MOI.ScalarNonlinearFunction},
    MOI.ScalarAffineFunction{Float64}
) # returns true
MOI.Bridges.Objective.supports_objective_function(
    MOI.Bridges.Objective.FunctionConversionBridge{Float64, MOI.ScalarNonlinearFunction},
    MOI.ScalarQuadraticFunction{Float64}
) # returns true

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions