[R] Fwd: Quadratic programming, for loop
Maija Sirkjärvi
m@ij@@@irkj@rvi @ending from gm@il@com
Wed Jun 13 13:21:55 CEST 2018
Hi!
I have a quadratic optimization problem and I have some difficulties coding
it with R. The math version of the problem looks like this:
min sum(mj -mj^)^2 which goes from 1 to J
st.
mj-1 <= mj - delta1
1/(Qj-1 -Qj-2)(mj-2 -mj-1) <= 1/(Qj -Qj-1 ) (mj-1 - mj) -delta2
And I'm coding it like this:
Dmat <- matrix(0, J,J)
diag(Dmat) <- 1
dvec <- -hsmooth
Aeq <- 0
beq <- 0
Amat <- matrix(0,2*J-3,J)
bvec <- rep(0,2*J-3)
for(j in 1:J)
{
Amat[j-1,j-1] = -1
Amat[j-1,j] = 1
bvec[j-1] = Delta1
}
for(j in 2:J)
{
Amat[J-1+j-2,j] = -1/ (Q[j] - Q[j-1])
Amat[J-1+j-2,j-1]= 1/(Q[j] - Q[j-1]) + 1/(Q[j-1] - Q[j-2])
Amat[J-1+j-2,j-2]= -1/(Q[j-1] - Q[j-2])
bvec[J-1+j-1]= Delta2
}
solution <- solve.QP(Dmat, dvec, Amat, bvec)
I get errors:
Error in Amat[J - 1 + j - 2, j - 1] <- 1/(Q[j] - Q[j - 1]) + 1/(Q[j - :
replacement has length zero
And
Error in solve.QP(Dmat, dvec, Amat, bvec) :
Amat and dvec are incompatible!
I'm not sure what I'm doing wrong here, and I really could use some help
with this. Thanks in advance!
[[alternative HTML version deleted]]
More information about the R-help
mailing list