addVariables#
- addVariables(problem, indexset, name='x', lb=0, ub=1e+20, threshold=1, vartype=0)[source]#
Create and add multiple variables (corresponding to an index-set) to the xpress problem.
This function extends xpress’s problem.addVariables method by wrapping it and returning VarDict1D/VarDictND data structures - that can efficiently sum subsets of variables with a user-friendly syntax based on wildcard patterns.
- Parameters:
- problemxpress.problem
Xpress problem.
- 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.- namestr, optional
Variable name, by default
'x'. It will also be used as thevalue_nameattribute of VarDict1D/VarDictND.- lbint or float or dict or ParamDict, optional
Lower bound, in one of the following forms:
A number - if all variables share the same lower bound.
A dict/ParamDict - with keys following the same structute as the index-set elements and values representing the lower bound; will fallback to the default for index-set elements not found in dict/ParamDict keys.
Default is
0.0.- ubint or float or dict or ParamDict, optional
Upper bound, in one of the following forms:
A number - if all variables share the same upper bound.
A dict/ParamDict - with keys following the same structute as the index-set elements and values representing the upper bound; will fallback to the default for index-set elements not found in dict/ParamDict keys.
Default is
1.0for binary variable type andinfinityfor other types.- thresholdint or float or dict or ParamDict, optional
Threshold, in one of the following forms:
A number - if all variables share the same threshold.
A dict/ParamDict - with keys following the same structute as the index-set elements and values representing the threshold; will fallback to the default for index-set elements not found in dict/ParamDict keys.
Only applies to semi-continuous, semi-integer, and partially integer variables; it must be between its lower and its upper bound. Default is
1.- vartypeint
Variable type, in one of the following forms:
xpress.continuousxpress.binaryxpress.integerxpress.semicontinuousxpress.semiintegerxpress.partiallyinteger
Default is
xpress.continuous.
- Returns:
- VarDict1D or VarDictND
- Raises:
- ValueError
If the index-set is empty.
Examples
Create xpress problem:
>>> import xpress as xp >>> import warnings >>> warnings.filterwarnings('ignore', category=xp.LicenseWarning)
>>> prob = xp.problem()
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 = addVariables(prob, nodes, name='select', vartype=xp.binary) >>> node_select VarDict1D: node -> select {'A': select(A), 'B': select(B), 'C': select(C)}
>>> arc_flow = addVariables(prob, arcs, name='flow', ub=10, vartype=xp.continuous) >>> arc_flow VarDictND: (ori, des) -> flow {('A', 'B'): flow(('A', 'B')), ('B', 'C'): flow(('B', 'C')), ('C', 'B'): flow(('C', 'B'))}