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:
gp.quicksum(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:
- gurobipy.LinExpr
- 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 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()
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) <gurobi.LinExpr: 10.0 arc-flow[A,B] + 20.0 arc-flow[B,C] + 0.0 arc-flow[C,B]>
>>> cost @ arc_flow <gurobi.LinExpr: 10.0 arc-flow[A,B] + 20.0 arc-flow[B,C] + 0.0 arc-flow[C,B]>
>>> arc_flow @ cost <gurobi.LinExpr: 10.0 arc-flow[A,B] + 20.0 arc-flow[B,C] + 0.0 arc-flow[C,B]>