I have a matrix S
of size VxV. I want to impose several contraints on it of the form: . I have attempted to do so, but this code runs into the issue
ValueError: Atoms must be at most 2D.
I put together a simplified example of my problem:
def ILP_example(scores):
V = scores.shape[0]
u, v, w = np.meshgrid(range(V), range(V), range(V))
arr = cp.Variable(scores.shape)
objective = cp.Maximize(
cp.sum(cp.multiply(scores, arr))
)
constraints = [
arr[u, v] + arr[v, w] - arr[u, w] <= 1,
]
prob = cp.Problem(objective, constraints)
prob.solve()
return
Attempting to run this, for example with ILP_example(np.random.rand(5, 5))
results in the error ValueError: Atoms must be at most 2D.
How do I fix this?
It seems that cvxpy doesn't support more than 2 dimensions, which is what you are doing when you are indexing arr
with u
, v
and w
.
As an alternative you can simply reshape those indexing variables so they are 1 dimensional:
u, v, w = [x.reshape(-1) for x in np.meshgrid(range(V), range(V), range(V))]
Then this works just fine:
constraints = [arr[u, v] + arr[v, w] + arr[u, w] <= 1]
arr[u, v]
is a 125 length vector now:
Expression(AFFINE, UNKNOWN, (125,))