TypeError: unsupported operand type(s) for -: 'bool' and 'NoneType'

5,309 views
Skip to first unread message

zuhal kartal

unread,
Nov 21, 2016, 4:43:45 AM11/21/16
to Gurobi Optimization
Hi everyone,

I defined a variable with four indices in my model which is g[i][j][k][l].

When I try to add a constraint as in the following:

for i in N:
for j in N:
if i != j:
for k in N:
for l in N[:k]:
m.addConstr(g[i][j][k][l] == g[j][i][l][k])

I got the TypeError: unsupported operand type(s) for -: 'bool' and 'NoneType'.

If you have any idea, where the problem stems from, could you please share it with me?

Thank you in advance from now on,

Zuhal.

Renan Garcia

unread,
Nov 21, 2016, 9:47:18 AM11/21/16
to gur...@googlegroups.com
You should verify that the objects stored in g[i][j][k][l] and g[j][i][k][l] are of type Var or LinExpr for all i, j, k and l. This error indicates that at least one is of type bool or None.

--

---
You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

zuhal kartal

unread,
Nov 28, 2016, 3:49:55 AM11/28/16
to Gurobi Optimization
Thank you so much Renan,

I've just solved the problem.

Zuhal.

21 Kasım 2016 Pazartesi 16:47:18 UTC+2 tarihinde Renan Garcia yazdı:

Shirin Naj

unread,
Jan 13, 2017, 11:55:03 AM1/13/17
to Gurobi Optimization
Hi I have the same problem and I couldnt solve it..how can I define Var or LinExpr for all i?

my code is: 

 for i in range(clients):
            T[i] = model.addVar(lb=1, ub=1440,name='T[%s]' % ( i))            

for i in range(clients):
           model.addConstr(start_times[i] <= T[i])


THE ERROR IS:


Traceback (most recent call last):
  File "c:\Users\Admin\Desktop\SampleToolBox\Python\cvrp_model.py", line 2, in <module>
    import math
  File "C:\sh\Lib\site-packages\gurobipy\gurobipy.pyd", line 2466, in gurobipy.Model.addConstr (../../src/python/gurobipy.c:77325)

Renan Garcia

unread,
Jan 17, 2017, 1:08:44 PM1/17/17
to gur...@googlegroups.com
It appears that start_times (not T) is the issue here. You should verify that the object stored in start_times[i] is valid for all i. This error indicates that at least one is of type bool or None.

Shirin Naj

unread,
Jan 30, 2017, 12:32:23 AM1/30/17
to Gurobi Optimization
Thanks :)

chuyu...@gmail.com

unread,
Sep 3, 2018, 12:21:25 AM9/3/18
to Gurobi Optimization
Hi,
I have the same problem. How do you fix it?

Kostja Siefen

unread,
Sep 3, 2018, 3:36:17 AM9/3/18
to Gurobi Optimization
Please make sure that all elements of your expression have a proper type. 

To debug this, you can add a check for None values before creating the expression, for example

for i in range(clients):
    if start_times[i] is None or T[i] is None)
        print("Invalid values for index %d!" % i)
    else:
        model.addConstr(start_times[i] <= T[i])

Kostja
Reply all
Reply to author
Forward
0 new messages