gurobipy.tupledict#
- class tupledict#
Gurobi tuple dict. This is a sub-class of the Python
dictclass that is designed to efficiently support a usage pattern that is quite common when building optimization models. In particular, atupledictis a Pythondictwhere the keys are stored as a Gurobituplelist, and where the values are typically GurobiVarobjects. Objects of this class make it easier to build linear expressions on sets of Gurobi variables, usingtuplelist.select()syntax and semantics.You typically build a
tupledictby callingModel.addVars. Once you’ve created atupledictd, you can used.sum()to create alinear expressionthat captures the sum of the variables in thetupledict. You can also use a command liked.sum(1, '*', 5)to create a sum over a subset of the variables ind. Assuming the keys for thetupledictare tuples containing three fields, this statement would create a linear expression that captures the sum over all variables indwhose keys contain a 1 in the first field of the tuple and a 5 in the third field (the'*'character is a wildcard that indicates that any value is acceptable in that field). You can also used.prod(coeff)to create a linear expression where the coefficients are pulled from the dictionarycoeff. For example, ifd(1,2,5)contains variablexandcoeff(1,2,5)is 2.0, then the resulting expression would include term \(2.0*x\).To access the members of a
tupledict, you can use standard dict indexing. For example,d[1,2]returns the value associated with tuple(1,2).Note that a
tupledictkey must be a tuple of scalar values (int,float,string, …). Thus, you can use(1, 2.0, 'abc')as a key, but you can’t use((1, 2.0), 'abc').Note that
tupledictobjects build and maintain a set of internal data structures to support efficientselectoperations. If you wish to reclaim the storage associated with these data structures, you can call thecleanfunction.- tupledict(args, kwargs)#
tupledictconstructor. Arguments are identical to those of a Pythondictconstructor.Note that you will typically use
Model.addVarsto build atupledict.- Parameters:
args – Positional arguments.
kwargs – Named arguments.
- Returns:
A
tupledictobject.- Example:
d = gp.tupledict([((1,2), 'onetwo'), ((1,3), 'onethree'), ((2,3), 'twothree')]) print(d[1,2]) # prints 'onetwo'
- select(pattern)#
Returns a
listcontaining the values associated with keys that match the specified tuple pattern. The pattern should provide one value for each field in the key tuple. A'*'value indicates that any value is accepted in that field.Without arguments, this method returns a list of all values in the
tupledict.- Parameters:
pattern – Pattern to match for a key tuple.
- Example:
d = gp.tupledict([((1,2), 'onetwo'), ((1,3), 'onethree'), ((2,3), 'twothree')]) print(d.select()) # prints ['onetwo', 'onethree', 'twothree'] print(d.select(1, '*')) # prints ['onetwo', 'onethree'] print(d.select('*', 3)) # prints ['onethree', 'twothree'] print(d.select(1, 3)) # prints ['onethree']
- sum(pattern)#
Returns the sum of the values associated with keys that match the specified pattern. If the values are Gurobi
Varobjects, the result is aLinExpr. The pattern should provide one value for each field in the key tuple. A'*'value indicates that any value is accepted in that field.Without arguments, this method returns the sum of all values in the
tupledict.- Parameters:
pattern – Pattern to match for a key tuple.
- Example:
x = m.addVars([(1,2), (1,3), (2,3)]) expr = x.sum() # LinExpr: x[1,2] + x[1,3] + x[2,3] expr = x.sum(1, '*') # LinExpr: x[1,2] + x[1,3] expr = x.sum('*', 3) # LinExpr: x[1,3] + x[2,3] expr = x.sum(1, 3) # LinExpr: x[1,3]
- prod(coeff, pattern)#
Returns a linear expression that contains one term for each tuple that is present in both the
tupledictand thecoeffargument;coeffshould be a Pythondictobject that maps tuples to coefficient values. For example,x.prod(coeff)would contain term2.0*varifx[1,2] = varandcoeff[1,2] = 2.0.- Parameters:
coeff – Python
dictthat maps tuples to coefficients.pattern – Pattern to match for a key tuple.
- Example:
x = m.addVars([(1,2), (1,3), (2,3)]) coeff = dict([((1,2), 2.0), ((1,3), 2.1), ((2,3), 3.3)]) expr = x.prod(coeff) # LinExpr: 2.0 x[1,2] + 2.1 x[1,3] + 3.3 x[2,3] expr = x.prod(coeff, '*', 3) # LinExpr: 2.1 x[1,3] + 3.3 x[2,3]