Input type to tupledict.sum() in gurobipy
AnsweredFor a tupledict x, I am trying to sum all entries which include y as one of their indices:
x.sum(y,'*','*',...,'*')+x.sum('*',y,'*',....,'*')+x.sum('*','*',y,...,'*')+...+x.sum('*','*','*',...,y)
The dimension of x is large, so I cannot do this explicitly. Therefore, I want to gradually add up the terms: starting with an empty linear expression, first add x.sum(y,'*','*',...,'*'), then add x.sum('*',y,'*',....,'*'), and so on. To do that, I need to be able to create the input to the sum function, and only then plug it in. I figured that the input to the sum function would be a tuple, but that turned out false. In the documentation, the input to tupledict.sum() is denoted as "pattern". Is this the same pattern object from the RegEx module? What should it look like to work as an input to the sum function? Thank you!
-
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,
Jonasz0 -
No, the pattern does not mean a RegEx pattern module. It refers to a value in a field. As an example
import gurobipy as gp
m = 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ł0 -
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!
0 -
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,'*'))
instead of
d.select(1,'*')
One way to avoid this, is to unpack the tuple
x = (1,'*')
d.select(*x)Best regards,
Jaromił0 -
Thank you Jaromil, this resolves my issue.
0
Please sign in to leave a comment.
Comments
5 comments