Search code examples
pythondocplex

Indicators constraints (object of type 'generator' has no len())


I'm working in a VRP optimization problem based in a tutorial. When I add the subtours constraint I get an error. I was following this tutorial: https://www.youtube.com/watch?v=eMsqsmftWOQ&t=2398s

from docplex.mp.model import Model
mdl = Model('CVRP')

x = mdl.binary_var_dict(arcos, name = 'x')
u = mdl.continuous_var_dict(nodos, ub = Q, name = 'u')
mdl.minimize(mdl.sum(distancia[i,j]*x[i,j] for i,j in arcos))
mdl.add_constraints(mdl.sum(x[i,j] for j in nodos if i!= j) == 1 for i in clientes)
mdl.add_constraints(mdl.sum(x[i,j] for i in nodos if i!= j) == 1 for j in clientes)
mdl.add_indicator_constraints(mdl.indicator_constraint(x[i,j],u[i]+q[j]==u[j]) for i,j in arcos if i!=0 and j!= 0) #This is the line

Context:

clientes = [x for x in range(1, n+1)]
nodos = [0]+clientes
arcos = {(i,j) for i in nodos for j in nodos if i != j}
Q = 15

The error list is:

C:\Users\Jose Godoy\PycharmProjects\Huevos hermanos\venv\lib\site-packages\docplex\mp\__init__.py:35: RuntimeWarning: docplex is not officially supported on 32 bits. Use it at your own risk.
  warnings.warn("docplex is not officially supported on 32 bits. Use it at your own risk.", RuntimeWarning)
Traceback (most recent call last):

 File "C:/Users/Jose Godoy/PycharmProjects/Huevos hermanos/VRP.py", line 41, in <module>
    mdl.add_indicator_constraints(mdl.indicator_constraint(x[i,j],u[i]+q[j]==u[j]) for i,j in arcos if i!=0 and j!= 0)

File "C:\Users\Jose Godoy\PycharmProjects\Huevos hermanos\venv\lib\site-packages\docplex\mp\model.py", line 3014, in add_indicator_constraints
    ind_indices = self.__engine.create_batch_indicator_constraints(indcts)

  File "C:\Users\Jose Godoy\PycharmProjects\Huevos hermanos\venv\lib\site-packages\docplex\mp\engine.py", line 465, in create_batch_indicator_constraints
    return self.create_batch_cts(indicators)

  File "C:\Users\Jose Godoy\PycharmProjects\Huevos hermanos\venv\lib\site-packages\docplex\mp\engine.py", line 449, in create_batch_cts
    self._increment_cts(len(ct_seq))
TypeError: object of type 'generator' has no len()

I think the syntax of the formulation is ok, is the same as the tutorial, so I think is a problem in the library because I get a warning message

RuntimeWarning: docplex is not officially supported on 32 bits. Use it at your own risk.
  warnings.warn("docplex is not officially supported on 32 bits. Use it at your own risk.", RuntimeWarning)

Do you know how can I fix it?


Solution

  • I resolved this. I added a

    []
    

    So this work for me:

    mdl.add_indicator_constraints(mdl.indicator_constraint([x[i,j],u[i]+q[j]==u[j]) for i,j in arcos if i!=0 and j!= 0]) #This is the line