TypeError: 'Var' object is not iterable

1,210 views
Skip to first unread message

ttde...@gmail.com

unread,
Jan 26, 2014, 4:55:20 PM1/26/14
to gur...@googlegroups.com
I have just started to learn Gurobi (and Python) after previously having used Lingo. I am trying to rewrite one of my models, but keep on getting "TypeError: 'Var' object is not iterable" when I try adding some of the constraints. Just wondering if anyone has some ideas about what I am doing wrong

Old Lingo code:
@for (job(i):
    @sum( time(t)| t #le# interval - duration(i):
        x(i,t))=1);

Gurobi code:
for i in Farmers:
    for t in Timeblocks:
        if t <= interval - duration[i]:
            m.addConstr(quicksum(X[i,t]) == 1)

When I remove the word "quicksum" it works fine, adding it makes the error come up. I am obviously doing something wrong, but don't know what

Thanks
Tonny

Sonja Mars

unread,
Jan 27, 2014, 6:15:34 AM1/27/14
to gur...@googlegroups.com
Hi,

> Old Lingo code:
> @for (job(i):
> @sum( time(t)| t #le# interval - duration(i):
> x(i,t))=1);
>
> Gurobi code:
> for i in Farmers:
> for t in Timeblocks:
> if t <= interval - duration[i]:
> m.addConstr(quicksum(X[i,t]) == 1)

What you try to do here is to sum up for example all X_00. As there is no more index to iterate over, this will not work. In other words: quicksum needs a list as input, but what you give is a single Gurobi Var.

What you try to do is:
sum_{} X_[i,t]=1 for all i in Farmers, for all T in Timeblocks with t <= interval - duration[i]

I guess, what you want to do, is:
sum_{t \in T_i} X_[i,t]=1 for all i in Farmers
where T_i = { t \in Timeblocks : t <= (interval - duration[i]) }

Best regards,
Sonja

-----------------------------------------------------------------
Dr. Sonja Mars
Gurobi Optimization





ttde...@gmail.com

unread,
Jan 30, 2014, 9:29:27 PM1/30/14
to gur...@googlegroups.com
Ah, I see it now :) Thanks that helped!. Now off to find out how to do double summations
Reply all
Reply to author
Forward
0 new messages