Skip to content

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

@Arjun-Mohan

Description

@Arjun-Mohan

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions