Skip to content

Feature request: implement getindex for expressions #2371

@remi-garcia

Description

@remi-garcia

I would like to access the objective coefficients yet it just possible to access the function as an expression.
My solution would be:

"""
    objective_coefficient(model::Model, variable::VariableRef)

Return the coefficient associated with `variable` in the objective function. See
also [`set_objective_coefficient`](@ref).

Note: this function will throw an error if a nonlinear objective is set.
"""
function objective_coefficient(model::Model, variable::VariableRef)
    return get(objective_function(model).terms, variable, 0.0)
end

Yet, I have no idea how quadratic objective functions should be handled and what to do if someone tries the following

model_1 = Model()
@variable(model_1, x >= 0)
@objective(model_1, Min, x)
model_2 = Model()
@variable(model_2, y >= 0)
@objective(model_2, Min, y)

objective_coefficient(model_1, y)

Metadata

Metadata

Assignees

Labels

Status: Help WantedHelp is welcome on this issueType: Feature requestgood first issueThis issue is recommended for new users, it does not require a thorough understanding of the package

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions