Expressions
More information can be found in the Expressions section of the manual.
Macros
JuMP.@expression
— Macro@expression(args...)
Efficiently builds a linear or quadratic expression but does not add to model immediately. Instead, returns the expression which can then be inserted in other constraints. For example:
@expression(m, shared, sum(i*x[i] for i=1:5))
@constraint(m, shared + y >= 5)
@constraint(m, shared + z <= 10)
The ref
accepts index sets in the same way as @variable
, and those indices can be used in the construction of the expressions:
@expression(m, expr[i=1:3], i*sum(x[j] for j=1:3))
Anonymous syntax is also supported:
expr = @expression(m, [i=1:3], i*sum(x[j] for j=1:3))
JuMP.@expressions
— Macro@expressions(model, args...)
Adds multiple expressions to model at once, in the same fashion as the @expression
macro.
The model must be the first argument, and multiple expressions can be added on multiple lines wrapped in a begin ... end
block.
The macro returns a tuple containing the expressions that were defined.
Examples
@expressions(model, begin
my_expr, x^2 + y^2
my_expr_1[i = 1:2], a[i] - z[i]
end)
Affine expressions
JuMP.GenericAffExpr
— Typemutable struct GenericAffExpr{CoefType,VarType} <: AbstractJuMPScalar
constant::CoefType
terms::OrderedDict{VarType,CoefType}
end
An expression type representing an affine expression of the form: $\sum a_i x_i + c$.
Fields
.constant
: the constantc
in the expression..terms
: anOrderedDict
, with keys ofVarType
and values ofCoefType
describing the sparse vectora
.
JuMP.AffExpr
— TypeAffExpr
Alias for GenericAffExpr{Float64,VariableRef}
, the specific GenericAffExpr
used by JuMP.
JuMP.linear_terms
— Functionlinear_terms(aff::GenericAffExpr{C, V})
Provides an iterator over coefficient-variable tuples (a_i::C, x_i::V)
in the linear part of the affine expression.
linear_terms(quad::GenericQuadExpr{C, V})
Provides an iterator over tuples (coefficient::C, variable::V)
in the linear part of the quadratic expression.
Quadratic expressions
JuMP.GenericQuadExpr
— Typemutable struct GenericQuadExpr{CoefType,VarType} <: AbstractJuMPScalar
aff::GenericAffExpr{CoefType,VarType}
terms::OrderedDict{UnorderedPair{VarType}, CoefType}
end
An expression type representing an quadratic expression of the form: $\sum q_{i,j} x_i x_j + \sum a_i x_i + c$.
Fields
.aff
: anGenericAffExpr
representing the affine portion of the expression..terms
: anOrderedDict
, with keys ofUnorderedPair{VarType}
and values ofCoefType
, describing the sparse list of termsq
.
JuMP.QuadExpr
— TypeQuadExpr
An alias for GenericQuadExpr{Float64,VariableRef}
, the specific GenericQuadExpr
used by JuMP.
JuMP.UnorderedPair
— TypeUnorderedPair(a::T, b::T)
A wrapper type used by GenericQuadExpr
with fields .a
and .b
.
JuMP.quad_terms
— Functionquad_terms(quad::GenericQuadExpr{C, V})
Provides an iterator over tuples (coefficient::C, var_1::V, var_2::V)
in the quadratic part of the quadratic expression.
Utilities and modifications
JuMP.constant
— Functionconstant(aff::GenericAffExpr{C, V})::C
Return the constant of the affine expression.
constant(aff::GenericQuadExpr{C, V})::C
Return the constant of the quadratic expression.
JuMP.coefficient
— Functioncoefficient(v1::VariableRef, v2::VariableRef)
Return 1.0
if v1 == v2
, and 0.0
otherwise.
This is a fallback for other coefficient
methods to simplify code in which the expression may be a single variable.
coefficient(a::GenericAffExpr{C,V}, v::V) where {C,V}
Return the coefficient associated with variable v
in the affine expression a
.
coefficient(a::GenericAffExpr{C,V}, v1::V, v2::V) where {C,V}
Return the coefficient associated with the term v1 * v2
in the quadratic expression a
.
Note that coefficient(a, v1, v2)
is the same as coefficient(a, v2, v1)
.
coefficient(a::GenericQuadExpr{C,V}, v::V) where {C,V}
Return the coefficient associated with variable v
in the affine component of a
.
JuMP.isequal_canonical
— Functionisequal_canonical(
aff::GenericAffExpr{C,V},
other::GenericAffExpr{C,V}
) where {C,V}
Return true
if aff
is equal to other
after dropping zeros and disregarding the order. Mainly useful for testing.
JuMP.add_to_expression!
— Functionadd_to_expression!(expression, terms...)
Updates expression
in place to expression + (*)(terms...)
. This is typically much more efficient than expression += (*)(terms...)
. For example, add_to_expression!(expression, a, b)
produces the same result as expression += a*b
, and add_to_expression!(expression, a)
produces the same result as expression += a
.
Only a few methods are defined, mostly for internal use, and only for the cases when (1) they can be implemented efficiently and (2) expression
is capable of storing the result. For example, add_to_expression!(::AffExpr, ::VariableRef, ::VariableRef)
is not defined because a GenericAffExpr
cannot store the product of two variables.
JuMP.drop_zeros!
— Functiondrop_zeros!(expr::GenericAffExpr)
Remove terms in the affine expression with 0
coefficients.
drop_zeros!(expr::GenericQuadExpr)
Remove terms in the quadratic expression with 0
coefficients.
JuMP.map_coefficients
— Functionmap_coefficients(f::Function, a::GenericAffExpr)
Apply f
to the coefficients and constant term of an GenericAffExpr
a
and return a new expression.
See also: map_coefficients_inplace!
Example
julia> a = GenericAffExpr(1.0, x => 1.0)
x + 1
julia> map_coefficients(c -> 2 * c, a)
2 x + 2
julia> a
x + 1
map_coefficients(f::Function, a::GenericQuadExpr)
Apply f
to the coefficients and constant term of an GenericQuadExpr
a
and return a new expression.
See also: map_coefficients_inplace!
Example
julia> a = @expression(model, x^2 + x + 1)
x² + x + 1
julia> map_coefficients(c -> 2 * c, a)
2 x² + 2 x + 2
julia> a
x² + x + 1
JuMP.map_coefficients_inplace!
— Functionmap_coefficients_inplace!(f::Function, a::GenericAffExpr)
Apply f
to the coefficients and constant term of an GenericAffExpr
a
and update them in-place.
See also: map_coefficients
Example
julia> a = GenericAffExpr(1.0, x => 1.0)
x + 1
julia> map_coefficients_inplace!(c -> 2 * c, a)
2 x + 2
julia> a
2 x + 2
map_coefficients_inplace!(f::Function, a::GenericQuadExpr)
Apply f
to the coefficients and constant term of an GenericQuadExpr
a
and update them in-place.
See also: map_coefficients
Example
julia> a = @expression(model, x^2 + x + 1)
x² + x + 1
julia> map_coefficients_inplace!(c -> 2 * c, a)
2 x² + 2 x + 2
julia> a
2 x² + 2 x + 2
JuMP-to-MOI converters
JuMP.variable_ref_type
— Functionvariable_ref_type(::GenericAffExpr{C, V}) where {C, V}
A helper function used internally by JuMP and some JuMP extensions. Returns the variable type V
from a GenericAffExpr
JuMP.jump_function
— Functionjump_function(x)
Given an MathOptInterface object x
, return the JuMP equivalent.
See also: moi_function
.
JuMP.jump_function_type
— Functionjump_function_type(::Type{T}) where {T}
Given an MathOptInterface object type T
, return the JuMP equivalent.
See also: moi_function_type
.
JuMP.moi_function
— Functionmoi_function(x)
Given a JuMP object x
, return the MathOptInterface equivalent.
See also: jump_function
.
JuMP.moi_function_type
— Functionmoi_function_type(::Type{T}) where {T}
Given a JuMP object type T
, return the MathOptInterface equivalent.
See also: jump_function_type
.