sum#
- VarDictND.sum(*pattern)[source]#
Sum all variables, or a subset based on wildcard pattern, in an expression.
- Parameters:
- *patternAny, optional
For subsets, the pattern requires one value for each dimension of the N-dim tuple key. The single-character string
'*'(asterisk) can be used as a wildcard to represent all possible values for a dimension.
- Returns:
- docplex.mp.linear.LinearExpr or docplex.mp.linear.ZeroExpr
- Raises:
- TypeError
If the pattern includes non-scalar(s).
- ValueError
If the pattern is not the same as the length of N-dim tuple keys.
- ValueError
If the pattern has no wildcard or all wildcards.
Examples
Create DOcplex model:
>>> from docplex.mp.model import Model >>> mdl = Model()
Create index-set:
>>> arcs = IndexSetND([('A', 'B'), ('B', 'C'), ('C', 'B')], names=['ori', 'des'])
Add variables:
>>> from opti_extensions.docplex import add_variables >>> arc_flow = add_variables(mdl, arcs, 'C', ub=10, name='arc-flow')
Sum all variables:
>>> arc_flow.sum() docplex.mp.LinearExpr(arc-flow_A_B+arc-flow_B_C+arc-flow_C_B)
Sum subset of variables having
'B'at the second dimension index:>>> arc_flow.sum('*', 'B') docplex.mp.LinearExpr(arc-flow_A_B+arc-flow_C_B)
Sum subset of variables having
'Z'at the first dimension index:>>> arc_flow.sum('Z', '*') docplex.mp.ZeroExpr()