Objectives
More information can be found in the Objectives section of the manual.
Objective functions
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 programmatically, 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 programmatically, do as follows:
julia> sense = MIN_SENSE
MIN_SENSE::OptimizationSense = 0
julia> @objective(model, sense, x^2 - 2x + 1)
x² - 2 x + 1
JuMP.objective_function
— Functionobjective_function(
model::Model,
T::Type = 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.VariableIndex, MathOptInterface.ScalarAffineFunction{Float64}(MathOptInterface.ScalarAffineTerm{Float64}[MathOptInterface.ScalarAffineTerm{Float64}(2.0, MathOptInterface.VariableIndex(1))], 1.0))
[...]
JuMP.set_objective_function
— Functionset_objective_function(model::Model, func::MOI.AbstractFunction)
set_objective_function(model::Model, func::AbstractJuMPScalar)
set_objective_function(model::Model, func::Real)
set_objective_function(model::Model, func::Vector{<:AbstractJuMPScalar})
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.set_objective_coefficient
— Functionset_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.
JuMP.set_objective
— Functionset_objective(model::AbstractModel, sense::MOI.OptimizationSense, func)
The functional equivalent of the @objective
macro.
Sets the objective sense and objective function simultaneously, and is equivalent to:
set_objective_sense(model, sense)
set_objective_function(model, func)
Examples
model = Model()
@variable(model, x)
set_objective(model, MIN_SENSE, x)
JuMP.objective_function_type
— Functionobjective_function_type(model::Model)::AbstractJuMPScalar
Return the type of the objective function.
JuMP.objective_function_string
— Functionobjective_function_string(mode, model::AbstractModel)::String
Return a String
describing the objective function of the model.
JuMP.show_objective_function_summary
— Functionshow_objective_function_summary(io::IO, model::AbstractModel)
Write to io
a summary of the objective function type.
Objective sense
JuMP.objective_sense
— Functionobjective_sense(model::Model)::MOI.OptimizationSense
Return the objective sense.
JuMP.set_objective_sense
— Functionset_objective_sense(model::Model, sense::MOI.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.