dimod icon indicating copy to clipboard operation
dimod copied to clipboard

TypeError: '<' not supported between instances of 'BinaryQuadraticModel' and 'int'

Open ACE07-Sev opened this issue 3 years ago • 5 comments

Hi, I am trying to set an inequality constraint (I want the sum of some terms to be negative, less than 0 essentially), but get this error :

Traceback (most recent call last): File "C:\Users\elmm\Desktop\CQM\CQM Assignment JRP\CQM_JRP.py", line 182, in cqm.add_constraint(upper_bound_eq < 0, TypeError: '<' not supported between instances of 'BinaryQuadraticModel' and 'int'

I don't think there is a need for a minimal reproducible example, as any cqm with a < sense which has binary vars will get this error but mine is :

for i in range(M): for j in range(N): X_.append(Binary('X_' + str(i + 1) + "" + str(j + 1))) if j == 0: lower_bound_eq = quicksum(Y[y] * list[0][y] * X_[j] - D[i] * k_i[i] * k_val[k] * T for k in range(38) for y in range(3))

        cqm.add_constraint(lower_bound_eq <= 0,
                           label="Constraint lower-bound " + str(i + 1) + str(j + 1))
        upper_bound_eq = quicksum(D[i] * k_i[i] * k_val[k] * T - (Y_[y + 1] * list[0][y + 1] * X_[j] + (1 - X_[j]) * Max_capacity) for k in range(38) for y in range(3))
        cqm.add_constraint(upper_bound_eq < 0,
                           label="Constraint lower-bound " + str(i + 1) + str(j + 1))
    elif j == 1:
        lower_bound_eq = quicksum(Y_[y] * list[1][y] * X_[j] - D[i] * k_i[i] * k_val[k] * T for k in range(38) for y in range(3))
        cqm.add_constraint(lower_bound_eq <= 0,
                           label="Constraint lower-bound " + str(i + 1) + str(j + 1))
        upper_bound_eq = quicksum(D[i] * k_i[i] * k_val[k] * T - (Y_[y + 1] * list[0][y + 1] * X_[j] + (1 - X_[j]) * Max_capacity) for k in range(38) for y in range(3))
        cqm.add_constraint(upper_bound_eq < 0,
                           label="Constraint lower-bound " + str(i + 1) + str(j + 1))
    elif j == 2:
        lower_bound_eq = quicksum(Y_[y] * list[2][y] * X_[j] - D[i] * k_i[i] * k_val[k] * T for k in range(38) for y in range(3))
        cqm.add_constraint(lower_bound_eq <= 0,
                           label="Constraint lower-bound " + str(i + 1) + str(j + 1))
        upper_bound_eq = quicksum(D[i] * k_i[i] * k_val[k] * T - (Y_[y + 1] * list[0][y + 1] * X_[j] + (1 - X_[j]) * Max_capacity) for k in range(38) for y in range(3))
        cqm.add_constraint(upper_bound_eq < 0,
                           label="Constraint lower-bound " + str(i + 1) + str(j + 1))

X_.clear()

ACE07-Sev avatar Jun 22 '22 06:06 ACE07-Sev

In here, Y, X, K_i are binary variables, list is just a list of values

ACE07-Sev avatar Jun 22 '22 06:06 ACE07-Sev

Hi @ACE07-Sev , we do not support strict inequality. You need to use <=, possibly with a constant offset. E.g. sum(Binary(v) for v in range(10)) < 0 should be sum(Binary(v) for v in range(10)) <= -1

arcondello avatar Jun 22 '22 15:06 arcondello

Are you not going to support this either?

TypeError: '>=' not supported between instances of 'BinaryQuadraticModel' and 'BinaryQuadraticModel'

angystallone avatar Jul 04 '22 07:07 angystallone

Unfortunately that is also not supported at the moment, though you can do bqm0 - bqm1 >= 0.

arcondello avatar Jul 04 '22 15:07 arcondello

Can't this be added in the backend? For instance given your understanding of offsets which simply resolve the issue without affecting the result, you could add this offset interpretation given the expectation being risen, same case for @angystallone 's issue. Given the "TypeError: '>=' not supported between instances of 'BinaryQuadraticModel' and 'BinaryQuadraticModel' ", the function could automatically return the BQM - BQM>= or <= 0.

Simply an idea, you're absolutely much much more familiar with the backend process hence I am sure given more time D-wave will find more sophisticated resolves.

Thank you as always for the early replies.

ACE07-Sev avatar Jul 07 '22 14:07 ACE07-Sev