add_variables#
- add_variables(model, indexset, vartype, lb=None, ub=None, name=None, key_format=None)[source]#
Create and add multiple variables (corresponding to an index-set) to the DOcplex model.
This function extends DOcplex’s Model.xyz_var_dict methods by wrapping them and returning VarDict1D/VarDictND data structures - that can efficiently sum subsets of variables with a user-friendly syntax based on wildcard patterns.
- Parameters:
- modeldocplex.mp.model.Model
DOcplex model.
- indexsetIndexSet1D or IndexSetND
Index-set for defining the variables. The
name/namesattribute of IndexSet1D/IndexSetND will be set as thekey_name/key_namesattribute of VarDict1D/VarDictND.- vartypestr
Variable type, in one of the following forms:
'continuous'or'C''binary'or'B''integer'or'I''semicontinuous'or'SC''semiinteger'or'SI'
- lbint or float or sequence or function or ParamDict, optional
Lower bound, in one of the following forms:
A number - if all variables share the same lower bound.
A sequence of numbers - one for each variable.
A function - that returns a number when called on each element of index-set.
A ParamDict - with keys following the same structute as the index-set elements and values representing the lower bound; will fallback to
Nonefor index-set elements not found in ParamDict keys.
Default
Nonecorresponds to:Continuous: 0
Binary: 0
Integer: 0
Semicontinuous: not applicable… will raise ValueError
Semiinteger: not applicable… will raise ValueError
- ubint or float or sequence or function or ParamDict, optional
Upper bound, in one of the following forms:
A number - if all variables share the same upper bound.
A sequence of numbers - one for each variable.
A function - that returns a number when called on each element of the index-set.
A ParamDict - with keys following the same structute as the index-set elements and values representing the upper bound; will fallback to
Nonefor index-set elements not found in ParamDict keys.
Default
Nonecorresponds to1for binary variable type andinfinityfor other types.- namestr or function, optional
For naming variables, in one of the following forms:
A string - applied as a prefix to the string representation of each element of the index-set.
A function - that generates a name when called on each element of the index-set.
None.
Default is None. It will also be used as the
value_nameattribute of VarDict1D/VarDictND.- key_formatformat str (should include ‘%s’), optional
Defines how index-set elements are incorporated into variable names, by default
'_%s'.
- Returns:
- VarDict1D or VarDictND
- Raises:
- ValueError
If the index-set is empty.
- ValueError
If the variable type is invalid.
- ValueError
If no lower bound is specified for Semicontinuous or Semiinteger variable type.
See also
add_variableFor a single variable.
Examples
Create DOcplex model:
>>> from docplex.mp.model import Model >>> mdl = Model()
Create index-sets:
>>> nodes = IndexSet1D(['A', 'B', 'C'], name='node') >>> arcs = IndexSetND([('A', 'B'), ('B', 'C'), ('C', 'B')], names=['ori', 'des'])
Add variables:
>>> node_select = add_variables(mdl, nodes, 'B', name='select') >>> node_select VarDict1D: node -> select {'A': docplex.mp.Var(type=B,name='select_A'), 'B': docplex.mp.Var(type=B,name='select_B'), 'C': docplex.mp.Var(type=B,name='select_C')}
>>> arc_flow = add_variables(mdl, arcs, 'C', ub=10, name='flow') >>> arc_flow VarDictND: (ori, des) -> flow {('A', 'B'): docplex.mp.Var(type=C,name='flow_A_B',ub=10), ('B', 'C'): docplex.mp.Var(type=C,name='flow_B_C',ub=10), ('C', 'B'): docplex.mp.Var(type=C,name='flow_C_B',ub=10)}