• Community Moderator

I would not expect RegEx patterns to be the required input here, but rather the patterns of the type that you were typing in. Perhaps someone from Gurobi support team can confirm this?

Additionally, if the keys in your x dictionary are tuples, you could try the following:

gp.quicksum(x[i] for i in x.keys() if y in i)

It should do the same work in a much faster (and less complicated) way.

Best regards,
Jonasz

• Gurobi Staff

No, the pattern does not mean a RegEx pattern module. It refers to a value in a field. As an example

import gurobipy as gpm = gp.Model()indices = ['x', 'y', 'z']x = m.addVars(indices, indices)m.addConstr(x.sum('y','*') == 0)

generates constraint

var[y,x] + var[y,y] + var[y,z] = 0

It looks like your $$\texttt{y}$$ index is an object and not a string. Is this correct? Depending on the object's properties, it might not be usable as input for the $$\texttt{sum}$$ method. In the case that the above does not answer your question, could you please provide a minimal working example showing what you are trying to achieve?

As a side note, I agree with Jonasz and recommend using quicksum.

Best regards,
Jaromił

Hi Jonasz and Jaromil,

Thank you both for your input. Jonasz suggestion of using quicksum in this fashion worked and solved my immediate problem. However, I still want to gain a deeper understanding of tupledict.select() (or alternatively tupledict.sum(), the issue is the same issue). I find the choice to have tupledict.select() behave differently when the input is packed as a tuple a bit strange, because it seems to exclude the use of this function when one must generate the input separately. Let me be concrete. Consider the following code:

d = gp.tupledict([((1,2), 'onetwo'), ((1,3), 'onethree'), ((2,3), 'twothree')])
x = SOMEEXPRESSION
d.select(x)

Using (1,'*') for SOMEEXPRESSION will not give the same result as d.select(1,'*'). Is there a value of SOMEEXPRESSION which would make the above code yield the same result as d.select(1,'*')? The answer should work for any tupledict d, not just the one in the example. Thank you!

• Gurobi Staff

When you set

x = (1,'*')

you provide a tuple to the $$\texttt{select}$$ method instead of 2 input arguments. So what you actually call is

d.select((1,'*'))

d.select(1,'*')
x = (1,'*')d.select(*x)