3
$\begingroup$

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")

m.add_constraint(m.logical_and(a, b) == c)
m.add_constraint(a == 1)
m.add_constraint(b == 0)

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
$\endgroup$
4
$\begingroup$

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)

to

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

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.