Programming
AnsweredSo I have a minimization problem, my Zi are (-1,0,1), originally I'm trying to minimize ZTWZ, it works fine, and now I want to add an additional constraint, the sum of Zi^2 is less than a number m. m is predetermined. I tried this: I added a sparse matrix Q and calculate ZTQZ which is the sum of Zi^2, but it doesn't work for me, this is my code(R):
Qc <- sparseMatrix(i = 1:n, j = 1:n, x = 1)
model$quadcon <- list(Q = Qc, sense = "<", rhs = m)
but it doesn't work, it says
Error: is.list(model$quadcon[[i]]) == FALSE
I tried then model$quadcon <- list(list(Q = Qc, sense = "<", rhs = m))
then it says
Error: length(model$quadcon[[i]]$Qc) == 0
Could you please tell me how to fix it?
-
The \(\texttt{quadcon}\) named component should be a list of lists. For each element/list, the \(\texttt{Qc}\) named component defines the matrix for the corresponding quadratic constraint. The following should work:
model$quadcon <- list(list(Qc = Qc, sense = "<", rhs = m))
See the The model argument section of the R documentation for more details.
0
Please sign in to leave a comment.
Comments
1 comment