Variables
More information can be found in the Variables section of the manual.
Macros
JuMP.@variable
— Macro@variable(model, expr, args..., kw_args...)
Add a variable to the model model
described by the expression expr
, the positional arguments args
and the keyword arguments kw_args
.
Anonymous and named variables
expr
must be one of the forms:
- Omitted, like
@variable(model)
, which creates an anonymous variable - A single symbol like
@variable(model, x)
- A container expression like
@variable(model, x[i=1:3])
- An anoymous container expression like
@variable(model, [i=1:3])
Bounds
In addition, the expression can have bounds, such as:
@variable(model, x >= 0)
@variable(model, x <= 0)
@variable(model, x == 0)
@variable(model, 0 <= x <= 1)
and bounds can depend on the indices of the container expressions:
@variable(model, -i <= x[i=1:3] <= i)
Sets
You can explicitly specify the set to which the variable belongs:
@variable(model, x in MOI.Interval(0.0, 1.0))
For more information on this syntax, read Variables constrained on creation.
Positional arguments
The recognized positional arguments in args
are the following:
Bin
: restricts the variable to theMOI.ZeroOne
set, that is,{0, 1}
. For example,@variable(model, x, Bin)
. Note: you cannot use@variable(model, Bin)
, use thebinary
keyword instead.Int
: restricts the variable to the set of integers, that is, ..., -2, -1, 0, 1, 2, ... For example,@variable(model, x, Int)
. Note: you cannot use@variable(model, Int)
, use theinteger
keyword instead.Symmetric
: Only available when creating a square matrix of variables, i.e., whenexpr
is of the formvarname[1:n,1:n]
orvarname[i=1:n,j=1:n]
, it creates a symmetric matrix of variables.PSD
: A restrictive extension toSymmetric
which constraints a square matrix of variables toSymmetric
and constrains to be positive semidefinite.
Keyword arguments
Four keyword arguments are useful in all cases:
base_name
: Sets the name prefix used to generate variable names. It corresponds to the variable name for scalar variable, otherwise, the variable names are set tobase_name[...]
for each index...
of the axesaxes
.start::Float64
: specify the value passed toset_start_value
for each variablecontainer
: specify the container type. See Forcing the container type for more information.set_string_name::Bool = true
: control whether to set theMOI.VariableName
attribute. Passingset_string_name = false
can improve performance.
Other keyword arguments are needed to disambiguate sitations with anonymous variables:
lower_bound::Float64
: an alternative tox >= lb
, sets the value of the variable lower bound.upper_bound::Float64
: an alternative tox <= ub
, sets the value of the variable upper bound.binary::Bool
: an alternative to passingBin
, sets whether the variable is binary or not.integer::Bool
: an alternative to passingInt
, sets whether the variable is integer or not.set::MOI.AbstractSet
: an alternative to usingx in set
variable_type
: used by JuMP extensions. See Extend@variable
for more information.
Examples
The following are equivalent ways of creating a variable x
of name x
with lower bound 0:
model = Model()
@variable(model, x >= 0)
@variable(model, x, lower_bound = 0)
x = @variable(model, base_name = "x", lower_bound = 0)
Other examples:
model = Model()
@variable(model, x[i=1:3] <= i, Int, start = sqrt(i), lower_bound = -i)
@variable(model, y[i=1:3], container = DenseAxisArray, set = MOI.ZeroOne())
@variable(model, z[i=1:3], set_string_name = false)
JuMP.@variables
— Macro@variables(model, args...)
Adds multiple variables to model at once, in the same fashion as the @variable
macro.
The model must be the first argument, and multiple variables can be added on multiple lines wrapped in a begin ... end
block.
The macro returns a tuple containing the variables that were defined.
Examples
@variables(model, begin
x
y[i = 1:2] >= 0, (start = i)
z, Bin, (start = 0, base_name = "Z")
end)
Keyword arguments must be contained within parentheses (refer to the example above).
Basic utilities
JuMP.VariableRef
— TypeVariableRef <: AbstractVariableRef
Holds a reference to the model and the corresponding MOI.VariableIndex.
JuMP.num_variables
— Functionnum_variables(model::Model)::Int64
Returns number of variables in model
.
JuMP.all_variables
— Functionall_variables(model::Model)::Vector{VariableRef}
Returns a list of all variables currently in the model. The variables are ordered by creation time.
Example
model = Model()
@variable(model, x)
@variable(model, y)
all_variables(model)
# output
2-element Array{VariableRef,1}:
x
y
JuMP.owner_model
— Functionowner_model(s::AbstractJuMPScalar)
Return the model owning the scalar s
.
JuMP.index
— Methodindex(v::VariableRef)::MOI.VariableIndex
Return the index of the variable that corresponds to v
in the MOI backend.
JuMP.optimizer_index
— Methodoptimizer_index(x::VariableRef)::MOI.VariableIndex
optimizer_index(x::ConstraintRef{Model})::MOI.ConstraintIndex
Return the index that corresponds to x
in the optimizer model.
Throws NoOptimizer
if no optimizer is set, and throws an ErrorException
if the optimizer is set but is not attached.
JuMP.check_belongs_to_model
— Functioncheck_belongs_to_model(func::AbstractJuMPScalar, model::AbstractModel)
Throw VariableNotOwned
if the owner_model
of one of the variables of the function func
is not model
.
check_belongs_to_model(constraint::AbstractConstraint, model::AbstractModel)
Throw VariableNotOwned
if the owner_model
of one of the variables of the constraint constraint
is not model
.
JuMP.VariableNotOwned
— Typestruct VariableNotOwned{V <: AbstractVariableRef} <: Exception
variable::V
end
The variable variable
was used in a model different to owner_model(variable)
.
JuMP.VariableConstrainedOnCreation
— TypeVariablesConstrainedOnCreation <: AbstractVariable
Variable scalar_variables
constrained to belong to set
. Adding this variable can be understood as doing:
function JuMP.add_variable(
model::Model,
variable::VariableConstrainedOnCreation,
names,
)
var_ref = add_variable(model, variable.scalar_variable, name)
add_constraint(model, VectorConstraint(var_ref, variable.set))
return var_ref
end
but adds the variables with MOI.add_constrained_variable(model, variable.set)
instead. See the MOI documentation for the difference between adding the variables with MOI.add_constrained_variable
and adding them with MOI.add_variable
and adding the constraint separately.
JuMP.VariablesConstrainedOnCreation
— TypeVariablesConstrainedOnCreation <: AbstractVariable
Vector of variables scalar_variables
constrained to belong to set
. Adding this variable can be thought as doing:
function JuMP.add_variable(
model::Model,
variable::VariablesConstrainedOnCreation,
names,
)
v_names = vectorize(names, variable.shape)
var_refs = add_variable.(model, variable.scalar_variables, v_names)
add_constraint(model, VectorConstraint(var_refs, variable.set))
return reshape_vector(var_refs, variable.shape)
end
but adds the variables with MOI.add_constrained_variables(model, variable.set)
instead. See the MOI documentation for the difference between adding the variables with MOI.add_constrained_variables
and adding them with MOI.add_variables
and adding the constraint separately.
JuMP.ComplexPlane
— TypeComplexPlane
Complex plane object that can be used to create a complex variable in the @variable
macro.
Examples
Consider the following example:
julia> model = Model();
julia> @variable(model, x in ComplexPlane())
real(x) + (0.0 + 1.0im) imag(x)
julia> all_variables(model)
2-element Vector{VariableRef}:
real(x)
imag(x)
We see in the output of the last command that two real variables were created. The Julia variable x
binds to an affine expression in terms of these two variables that parametrize the complex plane.
JuMP.ComplexVariable
— TypeComplexVariable{S,T,U,V} <: AbstractVariable
A struct used when adding complex variables.
See also: ComplexPlane
.
Names
JuMP.name
— Methodname(v::VariableRef)::String
Get a variable's name attribute.
JuMP.set_name
— Methodset_name(v::VariableRef, s::AbstractString)
Set a variable's name attribute.
JuMP.variable_by_name
— Functionvariable_by_name(model::AbstractModel,
name::String)::Union{AbstractVariableRef, Nothing}
Returns the reference of the variable with name attribute name
or Nothing
if no variable has this name attribute. Throws an error if several variables have name
as their name attribute.
julia> model = Model();
julia> @variable(model, x)
x
julia> variable_by_name(model, "x")
x
julia> @variable(model, base_name="x")
x
julia> variable_by_name(model, "x")
ERROR: Multiple variables have the name x.
Stacktrace:
[1] error(::String) at ./error.jl:33
[2] get(::MOIU.Model{Float64}, ::Type{MathOptInterface.VariableIndex}, ::String) at /home/blegat/.julia/dev/MathOptInterface/src/Utilities/model.jl:222
[3] get at /home/blegat/.julia/dev/MathOptInterface/src/Utilities/universalfallback.jl:201 [inlined]
[4] get(::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.AbstractOptimizer,MathOptInterface.Utilities.UniversalFallback{MOIU.Model{Float64}}}, ::Type{MathOptInterface.VariableIndex}, ::String) at /home/blegat/.julia/dev/MathOptInterface/src/Utilities/cachingoptimizer.jl:490
[5] variable_by_name(::Model, ::String) at /home/blegat/.julia/dev/JuMP/src/variables.jl:268
[6] top-level scope at none:0
julia> var = @variable(model, base_name="y")
y
julia> variable_by_name(model, "y")
y
julia> set_name(var, "z")
julia> variable_by_name(model, "y")
julia> variable_by_name(model, "z")
z
julia> @variable(model, u[1:2])
2-element Array{VariableRef,1}:
u[1]
u[2]
julia> variable_by_name(model, "u[2]")
u[2]
Start values
JuMP.has_start_value
— Functionhas_start_value(variable::AbstractVariableRef)
Return true
if the variable has a start value set otherwise return false
.
See also set_start_value
.
JuMP.set_start_value
— Functionset_start_value(con_ref::ConstraintRef, value)
Set the primal start value (MOI.ConstraintPrimalStart
) of the constraint con_ref
to value
. To remove a primal start value set it to nothing
.
See also start_value
.
set_start_value(variable::VariableRef, value::Union{Real,Nothing})
Set the start value (MOI attribute VariablePrimalStart
) of the variable
to value
.
Pass nothing
to unset the start value.
Note: VariablePrimalStart
s are sometimes called "MIP-starts" or "warmstarts".
See also start_value
.
JuMP.start_value
— Functionstart_value(con_ref::ConstraintRef)
Return the primal start value (MOI.ConstraintPrimalStart
) of the constraint con_ref
.
Note: If no primal start value has been set, start_value
will return nothing
.
See also set_start_value
.
start_value(v::VariableRef)
Return the start value (MOI attribute VariablePrimalStart
) of the variable v
.
Note: VariablePrimalStart
s are sometimes called "MIP-starts" or "warmstarts".
See also set_start_value
.
Lower bounds
JuMP.has_lower_bound
— Functionhas_lower_bound(v::VariableRef)
Return true
if v
has a lower bound. If true
, the lower bound can be queried with lower_bound
.
See also LowerBoundRef
, lower_bound
, set_lower_bound
, delete_lower_bound
.
JuMP.lower_bound
— Functionlower_bound(v::VariableRef)
Return the lower bound of a variable. Error if one does not exist.
See also LowerBoundRef
, has_lower_bound
, set_lower_bound
, delete_lower_bound
.
JuMP.set_lower_bound
— Functionset_lower_bound(v::VariableRef, lower::Number)
Set the lower bound of a variable. If one does not exist, create a new lower bound constraint.
See also LowerBoundRef
, has_lower_bound
, lower_bound
, delete_lower_bound
.
JuMP.delete_lower_bound
— Functiondelete_lower_bound(v::VariableRef)
Delete the lower bound constraint of a variable.
See also LowerBoundRef
, has_lower_bound
, lower_bound
, set_lower_bound
.
JuMP.LowerBoundRef
— FunctionLowerBoundRef(v::VariableRef)
Return a constraint reference to the lower bound constraint of v
. Errors if one does not exist.
See also has_lower_bound
, lower_bound
, set_lower_bound
, delete_lower_bound
.
Upper bounds
JuMP.has_upper_bound
— Functionhas_upper_bound(v::VariableRef)
Return true
if v
has a upper bound. If true
, the upper bound can be queried with upper_bound
.
See also UpperBoundRef
, upper_bound
, set_upper_bound
, delete_upper_bound
.
JuMP.upper_bound
— Functionupper_bound(v::VariableRef)
Return the upper bound of a variable. Error if one does not exist.
See also UpperBoundRef
, has_upper_bound
, set_upper_bound
, delete_upper_bound
.
JuMP.set_upper_bound
— Functionset_upper_bound(v::VariableRef, upper::Number)
Set the upper bound of a variable. If one does not exist, create an upper bound constraint.
See also UpperBoundRef
, has_upper_bound
, upper_bound
, delete_upper_bound
.
JuMP.delete_upper_bound
— Functiondelete_upper_bound(v::VariableRef)
Delete the upper bound constraint of a variable.
See also UpperBoundRef
, has_upper_bound
, upper_bound
, set_upper_bound
.
JuMP.UpperBoundRef
— FunctionUpperBoundRef(v::VariableRef)
Return a constraint reference to the upper bound constraint of v
. Errors if one does not exist.
See also has_upper_bound
, upper_bound
, set_upper_bound
, delete_upper_bound
.
Fixed bounds
JuMP.is_fixed
— Functionis_fixed(v::VariableRef)
Return true
if v
is a fixed variable. If true
, the fixed value can be queried with fix_value
.
JuMP.fix_value
— Functionfix_value(v::VariableRef)
Return the value to which a variable is fixed. Error if one does not exist.
JuMP.fix
— Functionfix(v::VariableRef, value::Number; force::Bool = false)
Fix a variable to a value. Update the fixing constraint if one exists, otherwise create a new one.
If the variable already has variable bounds and force=false
, calling fix
will throw an error. If force=true
, existing variable bounds will be deleted, and the fixing constraint will be added. Note a variable will have no bounds after a call to unfix
.
JuMP.unfix
— Functionunfix(v::VariableRef)
Delete the fixing constraint of a variable.
JuMP.FixRef
— FunctionFixRef(v::VariableRef)
Return a constraint reference to the constraint fixing the value of v
. Errors if one does not exist.
Integer variables
JuMP.is_integer
— Functionis_integer(v::VariableRef)
Return true
if v
is constrained to be integer.
See also IntegerRef
, set_integer
, unset_integer
.
JuMP.set_integer
— Functionset_integer(variable_ref::VariableRef)
Add an integrality constraint on the variable variable_ref
.
See also IntegerRef
, is_integer
, unset_integer
.
JuMP.unset_integer
— Functionunset_integer(variable_ref::VariableRef)
Remove the integrality constraint on the variable variable_ref
.
See also IntegerRef
, is_integer
, set_integer
.
JuMP.IntegerRef
— FunctionIntegerRef(v::VariableRef)
Return a constraint reference to the constraint constraining v
to be integer. Errors if one does not exist.
See also is_integer
, set_integer
, unset_integer
.
Binary variables
JuMP.is_binary
— Functionis_binary(v::VariableRef)
Return true
if v
is constrained to be binary.
See also BinaryRef
, set_binary
, unset_binary
.
JuMP.set_binary
— Functionset_binary(v::VariableRef)
Add a constraint on the variable v
that it must take values in the set $\{0,1\}$.
See also BinaryRef
, is_binary
, unset_binary
.
JuMP.unset_binary
— Functionunset_binary(variable_ref::VariableRef)
Remove the binary constraint on the variable variable_ref
.
See also BinaryRef
, is_binary
, set_binary
.
JuMP.BinaryRef
— FunctionBinaryRef(v::VariableRef)
Return a constraint reference to the constraint constraining v
to be binary. Errors if one does not exist.
See also is_binary
, set_binary
, unset_binary
.
Integrality utilities
JuMP.relax_integrality
— Functionrelax_integrality(model::Model)
Modifies model
to "relax" all binary and integrality constraints on variables. Specifically,
- Binary constraints are deleted, and variable bounds are tightened if necessary to ensure the variable is constrained to the interval $[0, 1]$.
- Integrality constraints are deleted without modifying variable bounds.
- An error is thrown if semi-continuous or semi-integer constraints are present (support may be added for these in the future).
- All other constraints are ignored (left in place). This includes discrete constraints like SOS and indicator constraints.
Returns a function that can be called without any arguments to restore the original model. The behavior of this function is undefined if additional changes are made to the affected variables in the meantime.
Example
julia> model = Model();
julia> @variable(model, x, Bin);
julia> @variable(model, 1 <= y <= 10, Int);
julia> @objective(model, Min, x + y);
julia> undo_relax = relax_integrality(model);
julia> print(model)
Min x + y
Subject to
x ≥ 0.0
y ≥ 1.0
x ≤ 1.0
y ≤ 10.0
julia> undo_relax()
julia> print(model)
Min x + y
Subject to
y ≥ 1.0
y ≤ 10.0
y integer
x binary
JuMP.fix_discrete_variables
— Functionfix_discrete_variables([var_value::Function = value,] model::Model)
Modifies model
to convert all binary and integer variables to continuous variables with fixed bounds of var_value(x)
.
Return
Returns a function that can be called without any arguments to restore the original model. The behavior of this function is undefined if additional changes are made to the affected variables in the meantime.
Notes
- An error is thrown if semi-continuous or semi-integer constraints are present (support may be added for these in the future).
- All other constraints are ignored (left in place). This includes discrete constraints like SOS and indicator constraints.
Example
julia> model = Model();
julia> @variable(model, x, Bin, start = 1);
julia> @variable(model, 1 <= y <= 10, Int, start = 2);
julia> @objective(model, Min, x + y);
julia> undo_relax = fix_discrete_variables(start_value, model);
julia> print(model)
Min x + y
Subject to
x = 1.0
y = 2.0
julia> undo_relax()
julia> print(model)
Min x + y
Subject to
y ≥ 1.0
y ≤ 10.0
y integer
x binary
Extensions
JuMP.AbstractVariable
— TypeAbstractVariable
Variable returned by build_variable
. It represents a variable that has not been added yet to any model. It can be added to a given model
with add_variable
.
JuMP.AbstractVariableRef
— TypeAbstractVariableRef
Variable returned by add_variable
. Affine (resp. quadratic) operations with variables of type V<:AbstractVariableRef
and coefficients of type T
create a GenericAffExpr{T,V}
(resp. GenericQuadExpr{T,V}
).
JuMP.parse_one_operator_variable
— Functionparse_one_operator_variable(_error::Function, infoexpr::_VariableInfoExpr, sense::Val{S}, value) where S
Update infoexr
for a variable expression in the @variable
macro of the form variable name S value
.