CurrentModule = JuMP
DocTestSetup = quote
using JuMP
end
DocTestFilters = [r"≤|<=", r"≥|>=", r" == | = ", r" ∈ | in ", r"MathOptInterface|MOI"]
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 a linear objective function.
Use Min
to create a minimization objective:
julia> @objective(model, Min, 2x + 1)
2 x + 1
Use Max
to create a maximization objective:
julia> @objective(model, Max, 2x + 1)
2 x + 1
Use the @objective
macro to set a quadratic objective function.
Use ^2
to have a variable squared:
julia> @objective(model, Min, x^2 + 2x + 1)
x² + 2 x + 1
You can also have bilinear terms between variables:
julia> @variable(model, x)
x
julia> @variable(model, y)
y
julia> @objective(model, Max, x * y + x + y)
x*y + x + y
Use objective_function
to return the current objective function.
julia> @objective(model, Min, 2x + 1)
2 x + 1
julia> objective_function(model)
2 x + 1
Use value
to evaluate an objective function at a point specifying values for variables.
julia> @variable(model, x[1:2]);
julia> @objective(model, Min, 2x[1]^2 + x[1] + 0.5*x[2])
2 x[1]² + x[1] + 0.5 x[2]
julia> f = objective_function(model)
2 x[1]² + x[1] + 0.5 x[2]
julia> point = Dict(x[1] => 2.0, x[2] => 1.0);
julia> value(z -> point[z], f)
10.5
Use objective_sense
to return the current objective sense.
julia> @objective(model, Min, 2x + 1)
2 x + 1
julia> objective_sense(model)
MIN_SENSE::OptimizationSense = 0
To modify an objective, call @objective
with the new objective
function.
julia> @objective(model, Min, 2x)
2 x
julia> @objective(model, Max, -2x)
-2 x
Alternatively, use set_objective_function
.
julia> @objective(model, Min, 2x)
2 x
julia> new_objective = @expression(model, -2 * x)
-2 x
julia> set_objective_function(model, new_objective)
Use set_objective_coefficient
to modify an objective coefficient.
julia> @objective(model, Min, 2x)
2 x
julia> set_objective_coefficient(model, x, 3)
julia> objective_function(model)
3 x
!!! info There is no way to modify the coefficient of a quadratic term. Set a new objective instead.
Use set_objective_sense
to modify the objective sense.
julia> @objective(model, Min, 2x)
2 x
julia> objective_sense(model)
MIN_SENSE::OptimizationSense = 0
julia> set_objective_sense(model, MAX_SENSE);
julia> objective_sense(model)
MAX_SENSE::OptimizationSense = 1
Alternatively, call @objective
and pass the existing objective
function.
julia> @objective(model, Min, 2x)
2 x
julia> @objective(model, Max, objective_function(model))
2 x
Define a multi-objective optimization problem by passing a vector of objectives:
julia> @variable(model, x[1:2]);
julia> @objective(model, Min, [1 + x[1], 2 * x[2]])
2-element Vector{AffExpr}:
x[1] + 1
2 x[2]
julia> f = objective_function(model)
2-element Vector{AffExpr}:
x[1] + 1
2 x[2]
!!! tip The Multi-objective knapsack tutorial provides an example of solving a multi-objective integer program.
In most cases, multi-objective optimization solvers will return multiple solutions, corresponding to points on the Pareto frontier. See Multiple solutions for information on how to query and work with multiple solutions.
Note that you must set a single objective sense, that is, you cannot have
both minimization and maximization objectives. Work around this limitation by
choosing Min
and negating any objectives you want to maximize:
julia> @variable(model, x[1:2]);
julia> @expression(model, obj1, 1 + x[1])
x[1] + 1
julia> @expression(model, obj2, 2 * x[1])
2 x[1]
julia> @objective(model, Min, [obj1, -obj2])
2-element Vector{AffExpr}:
x[1] + 1
-2 x[1]
Defining your objectives as expressions allows flexibility in how you can solve variations of the same problem, with some objectives removed and constrained to be no worse that a fixed value.
julia> @variable(model, x[1:2]);
julia> @expression(model, obj1, 1 + x[1])
x[1] + 1
julia> @expression(model, obj2, 2 * x[1])
2 x[1]
julia> @expression(model, obj3, x[1] + x[2])
x[1] + x[2]
julia> @objective(model, Min, [obj1, obj2, obj3]) # Three-objective problem
3-element Vector{AffExpr}:
x[1] + 1
2 x[1]
x[1] + x[2]
julia> # optimize!(model), look at the solution, talk to stakeholders, then
# decide you want to solve a new problem where the third objective is
# removed and constrained to be better than 2.0.
nothing
julia> @objective(model, Min, [obj1, obj2]) # Two-objective problem
2-element Vector{AffExpr}:
x[1] + 1
2 x[1]
julia> @constraint(model, obj3 <= 2.0)
x[1] + x[2] ≤ 2.0