cvxgrp / CVXR

An R modeling language for convex optimization problems.

Home Page:https://cvxr.rbind.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Code that previously worked now does not follow DCP rules

ericcohn opened this issue · comments

Describe the bug
My objective function no longer follows DCP rules, according to a warning, despite the fact that it ran successfully with no warning last week. It appears the issue is with quad_form.

To Reproduce
disp.1 <- function(x){
return(sum(x^2))
}
covs <- c("Murder", "Assault", "UrbanPop")
USArrests$Z <- rbinom(nrow(USArrests), 1, 0.5)
mat.cov <- as.matrix(USArrests[covs])
n <- nrow(mat.cov)
n_1 <- sum(USArrests$Z)
n_0 <- n - n_1
gam <- Variable(n)
constraint.pos <- gam >= 0
constraint.sum <- sum(gam) == 1
mat.cov.std <- scale(mat.cov)
sig <- 1
library(rdetools)
K <- rbfkernel(mat.cov.std, sigma = sig)
W <- USArrests$Z
lambda <- 0.01
objective <- Minimize(lambda^(-1)*(sum(n_1^(-2) * outer(W, W) * K) - sum(2 / (n_0 * n_1) * gam * rowSums(outer(1 - W, W) * K)))^2 + n_0^(-2) * quad_form((1 - W)*gam, K))^2 + sum(disp.1(gam)))
problem <- Problem(objective, constraints = list(constraint.pos, constraint.sum))
result <- solve(problem, solver = "GUROBI")
w16 <- result$getValue(gam)

Expected behavior
Should solve and return a vector of length n.

Version

sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS 10.16

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] C

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] iterpc_0.4.2 spatstat.geom_2.2-2 spatstat.data_2.1-0
[4] Ecume_0.9.1 glmnet_4.1-2 kernlab_0.9-29
[7] stringr_1.4.0 rdetools_1.0 CVXR_1.0-9
[10] Hmisc_4.4-0 ggplot2_3.2.1 Formula_1.2-3
[13] survival_3.2-3 lattice_0.20-38 sbw_1.1.3
[16] slam_0.1-47 quadprog_1.5-8 Matrix_1.2-17

loaded via a namespace (and not attached):
[1] gurobi_9.0-1 nlme_3.1-145 spatstat.sparse_2.0-0
[4] lubridate_1.7.4 bit64_0.9-7 RColorBrewer_1.1-2
[7] Rmosek_1.3.5 tools_3.6.1 backports_1.1.5
[10] R6_2.4.0 rpart_4.1-15 mgcv_1.8-29
[13] lazyeval_0.2.2 colorspace_1.4-1 nnet_7.3-12
[16] withr_2.1.2 tidyselect_1.1.0 gridExtra_2.3
[19] arrangements_1.1.9 bit_1.1-14 compiler_3.6.1
[22] htmlTable_1.13.2 scales_1.0.0 checkmate_1.9.4
[25] pbapply_1.4-3 goftest_1.2-2 spatstat_2.2-0
[28] digest_0.6.21 foreign_0.8-72 spatstat.utils_2.2-0
[31] base64enc_0.1-3 pkgconfig_2.0.3 htmltools_0.5.0
[34] htmlwidgets_1.5.3 rlang_0.4.7 rstudioapi_0.10
[37] shape_1.4.6 generics_0.1.0 acepack_1.4.1
[40] dplyr_1.0.2 ModelMetrics_1.2.2.2 magrittr_1.5
[43] spatstat.linnet_2.3-0 transport_0.12-2 Rcpp_1.0.2
[46] munsell_0.5.0 abind_1.4-5 lifecycle_0.2.0
[49] Rglpk_0.6-4 stringi_1.4.3 pROC_1.17.0.1
[52] MASS_7.3-51.6 plyr_1.8.4 recipes_0.1.16
[55] grid_3.6.1 parallel_3.6.1 crayon_1.3.4
[58] deldir_0.1-25 splines_3.6.1 tensor_1.5
[61] knitr_1.25 pillar_1.4.6 reshape2_1.4.3
[64] codetools_0.2-16 stats4_3.6.1 glue_1.4.1
[67] latticeExtra_0.6-28 data.table_1.12.2 vctrs_0.3.4
[70] foreach_1.5.1 gtable_0.3.0 purrr_0.3.4
[73] spatstat.core_2.3-0 polyclip_1.10-0 xfun_0.10
[76] gower_0.2.1 prodlim_2018.04.18 Rmpfr_0.8-1
[79] e1071_1.7-3 class_7.3-15 timeDate_3043.102
[82] tibble_3.0.3 iterators_1.0.13 cluster_2.1.0
[85] lava_1.6.6 gmp_0.6-2 ellipsis_0.3.0
[88] caret_6.0-88 ipred_0.9-9

You use random numbers without setting a seed which makes your results not be reproducible. One day it works and another it won't, depending on the random (presumably psd) matrix you use in quad_form!