0

I am trying to solve the following optimization problem in CVXR using MOSEK solver. Objective_Fn The input matrix is data_C2 (50 X 30) and clust_center_C2 is a column matrix ( 50 X 1). S_C2 is the matrix (50 X 50) that I am trying to minimize. This is my R code

library(CVXR)
N <- ncol(data_C2)
predictors <- nrow(data_C2)
ones <- matrix(1, nrow = predictors, ncol = 1)
lambda <- 20

#==================================================================#
# Optimization variable
S_C2 <- Variable(rows = predictors, cols = predictors) 
X_C2 <- data_C2

# Constraint
constr_C2 <- list(norm1(S_C2 %*% ones) <= lambda_req, S_C2[,] >= 0, S_C2[,] <= 1)

# Objective Function
objective_C2 <- 0
for (i in 1:N) {
  objective_C2 <- objective_C2 + 
    square(norm2(clust_center_C2 - S_C2 %*% X_C2[,i]))
}

# CVXR
prob_C2 <- Problem(Minimize(objective_C2), constr_C2)
CVXR_result_C2 <- solve(prob_C2, solver = "MOSEK",  verbose = TRUE)

# solution status by solver
CVXR_result_C2$status  

# optimal value of beta
cvxrBeta_C2 <- CVXR_result_C2$getValue(S_C2)  

min(cvxrBeta_C2)
max(cvxrBeta_C2)

The output obtained in R console

> CVXR_result_C2$status
[1] "optimal"
> min(cvxrBeta_C2)
[1] -2.11832e-09
> max(cvxrBeta_C2)
[1] 0.4402582

Though the solver says that the answer is optimal, the lower bound is negative when the constraint says that it must lie between zero and one. Can anyone tell what's wrong with the code?

user98059
  • 101
  • 2
  • Nothing is wrong. -2.11832e-09 is perfectly within numerical tolerance for zero. You can read about numerical precision in https://docs.mosek.com/modeling-cookbook/practical.html#the-quality-of-a-solution If you really need to clean up such tiny violations you will need to do it yourself. – Michal Adamaszek Jul 09 '19 at 04:21
  • @MichalAdamaszek Thank you. How should the constraint be modified for the elements of S to be either 1 or 0? – user98059 Jul 09 '19 at 09:49
  • If you want either 0 or 1 that is a binary variable. Unfortunately I don't know cvxr so I can't help with syntax. Remember that even for a binary variable nothing prevents the solvers from returning -2.11832e-09. – Michal Adamaszek Jul 09 '19 at 10:11

0 Answers0