8.6.4. tupledict

class tupledict

A dictionary with tuples as keys. To index multiple variables into a data structure. You can call tupledict.sum and tupledict.prod to quickly generate linear expressions by specifying query criteria. tupledict are generally returned by Model.addVars . The difference is that the tupledict returned by Model.addConstrs is composed of constraints.

Methods

__init__()

Construct a tupledict

clean()

Clear the index to release memory

prod()

Specify a row vector, and a query to produce a column vector, then return the product of the two vectors

select()

Query the tupledict to obtain the matching value list

sum()

Query a list from tupledict, and sum them as an expression

__init__(*args, **kwargs)

Construct a tupledict.

Parameters
  • *args

    Array parameters

  • **kwargs

    Dictionary parameters

example:

tupledict()
tupledict(another_dict)
tupledict([('a', 1), (' B ', 2)])
tupledict(a = 1, B = 2)
clean()

Clear the index to release memory.

Note

After the index is cleared, the index will be rebuilt before a query is performed again.

prod(coeffs, *query=[])

Specify a row vector, and a query to produce a column vector, then return the product of the two vectors. The product is an expression

Parameters
  • coeffs – Row vector

  • *query=[]

    The pattern of the key to be matched. The matching result is used as the column vector.

example:

m = Model()
x = m.addVars([(1,1), (1,2), (1,3)])
coeff = dict([((1,1), 1), ((1,2), 2), ((1,3), 3)])
expr = x.prod(coeff)
expr = x.prod(coeff, '*', 3)
select(*query)

Query the tupledict to obtain the matching value list.

Parameters

*query

The pattern of the key to be matched.

example:

li = tupledict([((1, 2), 3), ((2, 2), 4)])
li.select(1)
li.select('*', 2)

Note

Calling select triggers the creation of index, but modifying the tupledict clears the index. Avoid frequent index creation.

sum(*query=[])

Query a list from tupledict, and sum them as an expression

Parameters

*query=[]

The pattern of the key to be matched.

example:

m = Model()
x = m.addVar()
y = m.addVar()
mapping1 = ((1, 2), 1 * x + 2 * y)
mapping2 = ((2, 1), 2 * x + 1 * y)
td = tupledict([mapping1, mapping2])
linExpr = td.sum('*')