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 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.
Reference
JuMP.@objective
— Macro.@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
JuMP.set_objective_sense
— Function.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.
JuMP.set_objective_function
— Function.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.
JuMP.objective_sense
— Function.objective_sense(model::Model)::MathOptInterface.OptimizationSense
Return the objective sense.
JuMP.objective_function
— Function.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
julia> objective_function(model, QuadExpr)
2 x + 1
julia> typeof(objective_function(model, QuadExpr))
GenericQuadExpr{Float64,VariableRef}
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}[ScalarAffineTerm{Float64}(2.0, VariableIndex(1))], 1.0))
Stacktrace:
[1] convert at /home/blegat/.julia/dev/MathOptInterface/src/functions.jl:398 [inlined]
[2] get(::JuMP.JuMPMOIModel{Float64}, ::MathOptInterface.ObjectiveFunction{MathOptInterface.SingleVariable}) at /home/blegat/.julia/dev/MathOptInterface/src/Utilities/model.jl:290
[3] get at /home/blegat/.julia/dev/MathOptInterface/src/Utilities/universalfallback.jl:114 [inlined]
[4] get at /home/blegat/.julia/dev/MathOptInterface/src/Utilities/cachingoptimizer.jl:439 [inlined]
[5] get(::MathOptInterface.Bridges.LazyBridgeOptimizer{MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.AbstractOptimizer,MathOptInterface.Utilities.UniversalFallback{JuMP.JuMPMOIModel{Float64}}},MathOptInterface.Bridges.AllBridgedConstraints{Float64}}, ::MathOptInterface.ObjectiveFunction{MathOptInterface.SingleVariable}) at /home/blegat/.julia/dev/MathOptInterface/src/Bridges/bridgeoptimizer.jl:172
[6] objective_function(::Model, ::Type{VariableRef}) at /home/blegat/.julia/dev/JuMP/src/objective.jl:129
[7] top-level scope at none:0
JuMP.objective_function_type
— Function.objective_function_type(model::Model)::AbstractJuMPScalar
Return the type of the objective function.
JuMP.objective_bound
— Function.objective_bound(model::Model)
Return the best known bound on the optimal objective value after a call to optimize!(model)
.
JuMP.objective_value
— Function.objective_value(model::Model)
Return the objective value after a call to optimize!(model)
.