# DPP with indices as parameters

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?

1 Like

This forum is for the MATLAB software, CVX. Please seek assistance at an appropriate CVXPY forum

I’m terribly sorry for reviving this thread but I don’t see a way to message someone privately and I don’t see any contact info for Apprentice1.

@Apprentice1 did you find an answer to this? I’m tearing my hair out trying to find an answer to this that facilitates both efficient DPP compilation and quick solution time.

I don’t know if sharing of contact info is allowed but if you google my name (should be in my profile) you should be able to find my LinkedIn, Google Scholar etc. to contact me in case the thread is shut down due to irrelevance.

To the mods of the CVX Forum, I’m sorry that I had to revive the thread!