-
Notifications
You must be signed in to change notification settings - Fork 32
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
Comments
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. |
I am afraid your question is not at all clear. You state:
and further down, you state
for all j=1,2,3....20 (C is a constant vector of length 20) Assuming 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. |
I apologise, it was late at night and my doubt wasnt clear. This is my problem formulation, basically. mu and sigma are not constants, they are also to be calculated. I defined my variables as:
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:
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:
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() |
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 |
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:
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:
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.
The text was updated successfully, but these errors were encountered: