"TypeError: 'int' object is not subscriptable" while setting a constrain
AnsweredHi,
While formulating constrains for a logistic transport problem, the following part of my model spits out the TypeError: 'int' object is not subscriptable.After playing around with the brackets I lost sense of how this issue could be solved. I am new to programming and wondering if one is out there to clarify the mistake!
model.addConstrs(zTruck[(tr,m1)] + sum((2*o[3] + o[2])*[yTruck[(tr,m1,o)]] for o in range(UBorders)) <= zTruck[(tr,m)] for tr in range(UBtrucks) for m in range(1, UBorders))
Thank you!

Hi Vincent,
I suspect this is the problem:
(2*o[3] + o[2])*[yTruck[(tr,m1,o)]] for o in range(UBorders)
range(UBorders) returns a list of integers, so every value of o you are looping over is an integer. You see this error because writing o[2] and o[3] isn't valid. Brackets used in this way enclose indices or keys, which don't make sense to use on an integer. Using notation like this would work if o were (e.g.) a list or dictionary object. What are you trying to do with the expression 2*o[3] + o[2]?
Also, you should remove the outer brackets from [yTruck[(tr,m1,o)]]. Placing brackets around something will construct a list. Then, your mathematical expression includes the product or sum of a list and a number, which doesn't quite make sense.
Finally, assuming yTruck and zTruck are Gurobi variables, you can remove the parentheses from inside their corresponding index brackets (though keeping the parentheses shouldn't cause any problems).
I hope this helps!
Eli
0
Please sign in to leave a comment.
Comments
1 comment