Objective

Objective

This page describes macros and functions related to linear and quadratic objective functions only, unless otherwise indicated. For nonlinear objective functions, see Nonlinear Modeling.

Use the `@objective` macro to set linear and quadratic objective functions in a JuMP model. The functions `set_objective_sense` and `set_objective_function` provide an equivalent lower-level interface.

To update a term in the objective function, see `set_objective_coefficient`.

To query the objective function from a model, see `objective_sense`, `objective_function`, and `objective_function_type`.

To query the optimal objective value or best known bound after a solve, see `objective_value` and `objective_bound`. These two functions apply to nonlinear objectives also. The optimal value of the dual objective can be obtained via `dual_objective_value`.

Reference

``@objective(model::Model, sense, func)``

Set the objective sense to `sense` and objective function to `func`. The objective sense can be either `Min`, `Max`, `MathOptInterface.MIN_SENSE`, `MathOptInterface.MAX_SENSE` or `MathOptInterface.FEASIBILITY_SENSE`; see `MathOptInterface.ObjectiveSense`. In order to set the sense programatically, i.e., when `sense` is a Julia variable whose value is the sense, one of the three `MathOptInterface.ObjectiveSense` values should be used. The function `func` can be a single JuMP variable, an affine expression of JuMP variables or a quadratic expression of JuMP variables.

Examples

To minimize the value of the variable `x`, do as follows:

``````julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, x)
x

julia> @objective(model, Min, x)
x``````

To maximize the value of the affine expression `2x - 1`, do as follows:

``````julia> @objective(model, Max, 2x - 1)
2 x - 1``````

To set a quadratic objective and set the objective sense programatically, do as follows:

``````julia> sense = MOI.MIN_SENSE
MIN_SENSE::OptimizationSense = 0

julia> @objective(model, sense, x^2 - 2x + 1)
x² - 2 x + 1``````
``set_objective_sense(model::Model, sense::MathOptInterface.OptimizationSense)``

Sets the objective sense of the model to the given sense. See `set_objective_function` to set the objective function. These are low-level functions; the recommended way to set the objective is with the `@objective` macro.

``````set_objective_function(
model::Model,
func::Union{AbstractJuMPScalar, MathOptInterface.AbstractScalarFunction})``````

Sets the objective function of the model to the given function. See `set_objective_sense` to set the objective sense. These are low-level functions; the recommended way to set the objective is with the `@objective` macro.

``set_objective_coefficient(model::Model, variable::VariableRef, coefficient::Real)``

Set the linear objective coefficient associated with `Variable` to `coefficient`.

Note: this function will throw an error if a nonlinear objective is set.

``objective_sense(model::Model)::MathOptInterface.OptimizationSense``

Return the objective sense.

``````objective_function(model::Model,
T::Type{<:AbstractJuMPScalar}=objective_function_type(model))``````

Return an object of type `T` representing the objective function. Error if the objective is not convertible to type `T`.

Examples

``````julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, x)
x

julia> @objective(model, Min, 2x + 1)
2 x + 1

julia> objective_function(model, AffExpr)
2 x + 1

2 x + 1

We see with the last two commands that even if the objective function is affine, as it is convertible to a quadratic function, it can be queried as a quadratic function and the result is quadratic.

However, it is not convertible to a variable.

``````julia> objective_function(model, VariableRef)
ERROR: InexactError: convert(MathOptInterface.SingleVariable, MathOptInterface.ScalarAffineFunction{Float64}(MathOptInterface.ScalarAffineTerm{Float64}[MathOptInterface.ScalarAffineTerm{Float64}(2.0, MathOptInterface.VariableIndex(1))], 1.0))
[...]``````
``objective_function_type(model::Model)::AbstractJuMPScalar``

Return the type of the objective function.

``objective_bound(model::Model)``

Return the best known bound on the optimal objective value after a call to `optimize!(model)`.

``objective_value(model::Model; result::Int = 1)``

Return the objective value associated with result index `result` of the most-recent solution returned by the solver.

See also: `result_count`.

``dual_objective_value(model::Model; result::Int = 1)``

Return the value of the objective of the dual problem associated with result index `result` of the most-recent solution returned by the solver.

Throws `MOI.UnsupportedAttribute{MOI.DualObjectiveValue}` if the solver does not support this attribute.

See also: `result_count`.