# Logical operation on an array in DOCplex

Here is a simple example of logical operation in docplex taken from here. Code below works fine.

from docplex.mp.model import Model

m = Model("test")

a = m.binary_var(name="a")
b = m.binary_var(name="b")
c = m.binary_var(name="c")

if m.solve():
m.print_solution(print_zeros=True)
else:
print(m.solve_details)


Just for the purpose of testing, I created an array of a binary variable. Code below gives compilation error:

from docplex.mp.model import Model

m = Model("test")

w = {k: m.binary_var(name="w_{0}".format(k)) for k in range(10)}
c = m.binary_var(name="c")

m.add_constraint(m.logical_and(w[k] for k in range(10)) == c)

if m.solve():
m.print_solution(print_zeros=True)
else:
print(m.solve_details)


Error:

docplex.mp.utils.DOcplexException: Model.logical_and, arg#0: Not a logical operand: <generator object <genexpr> at 0x7fd373a8f410>. Expecting binary variable, logical expression


The first argument in logical_and is *args but it's not unpacked in the subroutines inside of logical_and. Your error will be gone if you change the following line from
m.add_constraint(m.logical_and(w[k] for k in range(10)) == c)
m.add_constraint(m.logical_and(*(w[k] for k in range(10))) == c)