I am trying to use indices as parameters in a DPP. I crafted this simple example which is similar to my original problem to clarify the problem.
import cvxpy as cp
import numpy as np
N = 10
c = np.random.rand(N)
x = cp.Variable(N,nonneg = True)
idx = cp.Parameter()
z = cp.Variable()
constraints = [cp.sum(x) == 1]
for i in range(N):
if i != idx.value:
constraints.append(z >= (cp.inv_pos(x[i]) + cp.inv_pos(x[idx]))/c[i])
obj = cp.Minimize(z)
prob = cp.Problem(obj,constraints)
prob.solve()
Since cvxpy parameters cannot be used as numpy indices, the error raised is
IndexError: only integers, slices (:
), ellipsis (...
), numpy.newaxis (None
) and integer or boolean arrays are valid indices.
Note that, If I remove the parameter with an integer (e.g. 1 in the example below), I obtain a solution
import cvxpy as cp
import numpy as np
N = 10
c = np.random.rand(N)
x = cp.Variable(N,nonneg = True)
idx = 1
z = cp.Variable()
constraints = [cp.sum(x) == 1]
for i in range(N):
if i != idx:
constraints.append(z >= (cp.inv_pos(x[i]) + cp.inv_pos(x[idx]))/c[i])
obj = cp.Minimize(z)
prob = cp.Problem(obj,constraints)
prob.solve()
The problem is that I need to solve this program many times and hence I need a parametric representation of it.
I tried using boolean variables and using the fact that w[i] = w^\top 1_i, where 1_i is the standard basis vector in N dimensions (modeled as a cvxpy Parameter), but nothing worked.
Is there any trick to use cvxpy parameters as indices that can be used in numpy arrays or there is no hope at all?