Concepts
Features
Reference
Gurobi tuple dict. This is a sub-class of the Pythondict class thatis designed to efficiently support a usage pattern that is quite commonwhen building optimization models. In particular, atupledict is aPythondict where the keys represent variable indices, and thevalues are typically GurobiVar objects. Objects of thisclass make it easier to build linear expressions on sets of Gurobivariables, usingtuplelist.select() syntaxand semantics.
You typically build atupledict by callingModel.addVars.Once you’ve created atupledictd, you can used.sum() tocreate alinearexpression that captures the sumof 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 thetupledict are tuples containingthree fields, this statement would create a linear expression thatcaptures the sum over all variables ind whose keys contain a 1 inthe first field of the tuple and a 5 in the third field (the'*'character is a wildcard that indicates that any value is acceptable inthat field). You can also used.prod(coeff) to create a linearexpression where the coefficients are pulled from the dictionarycoeff. For example, ifd(1,2,5) contains variablex andcoeff(1,2,5) is 2.0, then the resulting expression would includeterm\(2.0*x\).
To access the members of atupledict, you can use standard dictindexing. For example,d[1,2] returns the value associated withtuple(1,2).
Note that atupledict key must be a tuple of scalar values (int,float,string, …). Thus, you can use(1,2.0,'abc') as akey, but you can’t use((1,2.0),'abc').
Note thattupledict objects build and maintain a set of internaldata structures to support efficientselect operations. If you wishto reclaim the storage associated with these data structures, you cancall theclean function.
tupledict constructor. Arguments are identical to those of a Pythondict constructor.
Note that you will typically useModel.addVars to build atupledict.
args – Positional arguments.
kwargs – Named arguments.
Atupledict object.
d=gp.tupledict([((1,2),'onetwo'),((1,3),'onethree'),((2,3),'twothree')])print(d[1,2])# prints 'onetwo'
Returns alist containing the values associated with keys that matchthe specified tuple pattern. The pattern should provide one value foreach field in the key tuple. A'*' value indicates that any value isaccepted in that field.
Without arguments, this method returns a list of all values in thetupledict.
pattern – Pattern to match for a key tuple.
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']
Returns the sum of the values associated with keys that match thespecified pattern. If the values are GurobiVar objects, the resultis aLinExpr. The pattern should provide one value for each field inthe key tuple. A'*' value indicates that any value is accepted inthat field.
Without arguments, this method returns the sum of all values in thetupledict.
pattern – Pattern to match for a key tuple.
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]
Returns a linear expression that contains one term for each tuple thatis present in both thetupledict and thecoeff argument;coeff should be a Pythondict object that maps tuples tocoefficient values. For example,x.prod(coeff) would contain term2.0*var ifx[1,2]=var andcoeff[1,2]=2.0.
coeff – Pythondict that maps tuples to coefficients.
pattern – Pattern to match for a key tuple.
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]
Help and Feedback