I am currently working on cvxpy and I have the following constraint:
constraints.append(z[p][offices.index(office), offices.index(linked_office)] != 3/M)
I need each value of z to belong to [0,3/M[ U ]3/M,1], how can I proceed ?
Here is the definition of my variable z, with e a matrix n*p which take values between 0 and M-1
z = []
for p in range(P):
z.append(cp.Variable((n, n)))
for p in range(P):
for i in range(n):
for j in range(n):
constraints.append(z[p][i, j] >= 0)
for p in range(P):
for office in offices:
for linked_office in linked_offices[office]:
constraints.append(z[p][offices.index(office), offices.index(linked_office)]
<= e[offices.index(office), p] / M)
constraints.append(z[p][offices.index(office), offices.index(linked_office)]
<= e[offices.index(linked_office), p] / M)
constraints.append(z[p][offices.index(office), offices.index(linked_office)]
>= e[offices.index(office), p] / M + e[offices.index(linked_office), p] / M - 1)
CVXPY doesn't explicitly allow for not equal to constraints, and instead the (simple) constraints it allows are
==,>=and<=, but that's okay! It just means you instead need to get a little tricky with how you formulate the problem.For ease of notation, let's say we want to ensure that some value
xis not equal to some value, N, say. In this case we can write our constraint ofx != Nas followsWhere this will work if
Nis either an integer, or a floating point value.So, for your problem you would need to modify your code to something like
However, for more efficient constraint formulations for CVXPY, you may find this thread to be helpful