Containers

Containers

Containers can be created using the generatecontainer function

generatecontainer(T, indexvars, indexsets, requestedtype)

Return a tuple, the first element of which is code that generates a container for objects of type T given the index variables, index sets, and requestedtype. requestedtype may be one of :Array, :JuMPArray, :Dict, or :Auto. Return error-producing code if requested type is incompatible. For the case of :Auto, the following rules are used to determine the appropriate container:

  1. If all index sets are either explicit 1:B objects for any B or symbols which refer to objects of type Base.OneTo, then an Array is generated of the appropriate size. Types of symbols/expressions are not known at compile time, so we defer to type-safe functions to check the Base.OneTo condition.

  2. If condition (1) does not hold, and the index sets are independent (the index variable for one set does not appear in the definition of another), then an JuMPArray is generated of the appropriate size.

  3. Otherwise, generate an empty Dict{Any,T}.

The second element of the return tuple is a Bool, true if the container type automatically checks for duplicate terms in the index sets and false otherwise.

Examples

generatecontainer(VariableRef, [:i,:j], [:(1:N), :(1:T)], :Auto)
# Returns code equivalent to:
# :(Array{VariableRef}(length(1:N), length(1:T))

generatecontainer(VariableRef, [:i,:j], [:(1:N), :(2:T)], :Auto)
# Returns code equivalent to:
# :(JuMPArray(Array{VariableRef}(length(1:N), length(2:T)), $indexvars...))

generatecontainer(VariableRef, [:i,:j], [:(1:N), :(S)], :Auto)
# Returns code that generates an Array if S is of type Base.OneTo,
# otherwise an JuMPArray.

generatecontainer(VariableRef, [:i,:j], [:(1:N), :(1:j)], :Auto)
# Returns code equivalent to:
# :(Dict{Any,VariableRef}())

Containers in macro

In the @variable (resp. @constraint) macro, containers of variables (resp. constraints) can be created the following syntax

Each expression index_set_i can either be

The macro then creates the container using the JuMP.generatecontainer function with the following arguments:

  1. VariableRef for the @variable macro and ConstraintRef for the @constraint macro.
  2. The index variables and arbitrary symbols for dimensions for which no variable index is specified.
  3. The index sets specified.
  4. The value of the keyword argument if given or :Auto.