Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need some help understanding how to vectorise constraint definition. #46

Open
Arjun-Mohan opened this issue May 3, 2019 · 4 comments
Open

Comments

@Arjun-Mohan
Copy link

Arjun-Mohan commented May 3, 2019

I'm using CVXR for the first time today, to perform a minimization of a max of a set of variables.

So i set my objective function as:

    O_j <- Variable(20)
    objective <- Minimize(max_entries(O_j))

So this should minimise the maximum of the 20 O_j values, as I understand it from the CVXR documentation.

Now in defining constraints, I am having issues in defining sets of constraints. Since I have 20 variables (and 2 more sets of 20 variables, which are mu_j and sigma_j), defining 20 constraints line by line is obviously tedious, and I am sure there is some way of aggregating this, for constraints that work by the universal quantifier/for all.

for example, one constraint is:

O[j] = 1-pnorm((C([j] -mu[j])/sigma[j])
for all j=1,2,3....20 (C is a constant vector of length 20)

I can replace the generic pnorm in R with p_norm in CVXR. But how will I aggregate all 20 constraints in one line of code? I'm new to using this package so I'm a bit confused.

Additionally, can functions like sqrt be used in constraints the same way or is there an equivalent for CVXR? I have a constraint which is:

`sigma[j] = sqrt(sum(P[i] * (1 - P[i]) * X[i,j] ) `

for all j = 1,2,3....20. Here, P is a constant vector of length 30. X is a variable defined as:

X_ij<-Int(30,20)
Now the sum here works like the excel function SUMPRODUCT(), that is,

P[1] * (1-P[1]) * X[1,j] + P[2] * (1-P[2]) * X[2,j] + ... + P[30] * (1-P[30]) * X[30,j]

I would normally do this using dplyr if I knew the values of X[ i , j ]. Will sum_entries work here?

The code works fine if I define each constraint line by line, but there must be a more efficient way to do this right? I know these doubts are pretty basic but I'm just starting to learn how to code with R/CVXR and any help would be appreciated. Thanks.

@Arjun-Mohan
Copy link
Author

This isn't a bug or anything, but I am unable to find any other location to ask doubts about the understanding of the code.

@bnaras
Copy link
Collaborator

bnaras commented May 3, 2019

I am afraid your question is not at all clear. You state:

 O_j <- Variable(20)

and further down, you state

O[j] = 1-pnorm((C([j] -mu[j])/sigma[j])

for all j=1,2,3....20 (C is a constant vector of length 20)

Assuming mu and sigma are constant vectors, what is O ( as opposed to O_j) and how does it relate to the problem? (It surely cannot be the same as O_j, because then you have forced your solution with the equality. Incidentally, use == for equality constraints.)

To avoid having to code a constraint for every index, think matrices. For example, the quick intro on https://cvxr.rbind.io provides one such.

@Arjun-Mohan
Copy link
Author

I apologise, it was late at night and my doubt wasnt clear.

Formulation

This is my problem formulation, basically. mu and sigma are not constants, they are also to be calculated.

I defined my variables as:

O_j <- Variable(20)
mu_j <- Variable(20)
sigma_j <- Variable(20)
x_ij <- Int(30,20)

So now as you can see from the image, those constraints are defined for all j's.

In the line I incorrectly wrote above, I was basically writing how I would define it in regular R coding, that's why I used pnorm() and not p_norm. A single constraint would be:

const1<-1-p_norm((C[1]-mu_j[1])/sigma_j[1])

I could repeat this 20 times, obviously. But how would I do this in a single line? Can I just use the entire matrix, such as:

const1<-1-p_norm((C-mu_j)/sigma_j)

C, and P_i (the values from the image) are two vectors which have precalculated constant values.

So how would I vectorise the constraint definition, if the way I mentioned above wont work?

Finally, do I use the sqrt(), or is there a CVXR equivalent like how p_norm() is for pnorm()

@Arjun-Mohan
Copy link
Author

i tried using the quick intro as reference, but there i get a second issue which is, can I use matrices in p_norm as well? will it index each element of the matrix as desired?

similarly, when I am doing the 4th constraint, I want it to take the row sum of x_ij across [i] and set that equal to N[i] (which is a constant). If I use sum_entries here will it sum just the rows?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants