sum_squares#

VarDictND.sum_squares(*pattern)[source]#

Sum squares of 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:
gurobipy.QuadExpr
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 gurobipy model:

>>> from gurobipy import GRB, Model
>>> mdl = Model()

Create index-set:

>>> arcs = IndexSetND([('A', 'B'), ('B', 'C'), ('C', 'B')], names=['ori', 'des'])

Add variables:

>>> from opti_extensions.gurobipy import addVars
>>> arc_flow = addVars(mdl, arcs, ub=10, vtype=GRB.CONTINUOUS, name='arc-flow')
>>> mdl.update()

Sum squares of all variables:

>>> arc_flow.sum_squares()
<gurobi.QuadExpr: 0.0 + [ arc-flow[A,B] ^ 2 + arc-flow[B,C] ^ 2 + arc-flow[C,B] ^ 2 ]>

Sum squares of subset of variables having 'B' at the second dimension index:

>>> arc_flow.sum_squares('*', 'B')
<gurobi.QuadExpr: 0.0 + [ arc-flow[A,B] ^ 2 + arc-flow[C,B] ^ 2 ]>

Sum squares of subset of variables having 'Z' at the first dimension index:

>>> arc_flow.sum_squares('Z', '*')
<gurobi.LinExpr: 0.0>