dot#
- VarDictND.dot(paramdict)[source]#
Sum the products of variables with corresponding coef from ParamDictND, in an expression.
Assumes the coef to be zero if not found in the ParamDictND.
Equivalent to:
model.sum(paramdict.get(k, 0) * v for k, v in vardict.items())
- Parameters:
- paramdictParamDictND
ParamDictND to be used for dot product; should have tuple keys of same length as VarDictND.
- Returns:
- docplex.mp.linear.LinearExpr or docplex.mp.linear.ZeroExpr
- Raises:
- TypeError
If the paramdict is not as instance of ParamDictND.
- ValueError
If the paramdict does not have tuple keys of same length as VarDictND.
Notes
This method is equivalent to using the matrix multiplication operator
@.Both
ParamDictND @ VarDictNDandVarDictND @ ParamDictNDwill also produce the same result.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')
Define parameter:
>>> from opti_extensions import ParamDictND >>> cost = ParamDictND({('A', 'B'): 10, ('B', 'C'): 20})
Compute dot product (three alternative ways):
>>> arc_flow.dot(cost) docplex.mp.LinearExpr(10arc-flow_A_B+20arc-flow_B_C)
>>> cost @ arc_flow docplex.mp.LinearExpr(10arc-flow_A_B+20arc-flow_B_C)
>>> arc_flow @ cost docplex.mp.LinearExpr(10arc-flow_A_B+20arc-flow_B_C)