I want to minimise the following equation:
F=SUM{u 1:20}sum{w 1:10} Quw(ruw-yuw)^2
with the following constraints:
yuw >= yu,w+1
yuw >= yu-1,w
y20,0 = 100
y0,10 = 0
yu,10 = 0
I have a 20*10 ruw and 20*10 quw matrix, I now need to generate a yuw matrix which adheres to the constraints. I am coding in R and am familiar with the lpsolve, optimx and quadprog packages, but don't know how to use them for this particular question. I know that I must use the quadprog package as this is a quadratic programming question. I'm not looking for a complete answer, I am wanting some guidance as to how to structure the constraint matrix and the best way to tackle the question.
Given the similarities of the optimization problem here to your previous question, I will borrow some language directly from my answer to that question. However they are quite a bit different (the previous problem was a linear programming problem and this is a quadratic programming problem, and the constraints differ), so they are not duplicates.
Expanding out the optimization objective we get
Quw*ruw^2 - 2*Quw*ruw*yuw + Quw*yuw^2
. We see that this is a quadratic function of the decision variablesyuw
, and thus thesolve.QP
method of thequadProg
package can be used to solve the optimization problem.To abstract this out a bit, let's assume
R=20
andC=10
describe the dimensions of the input matrices. Then there areR*C
decision variables and we can assign them ordery11, y21, ... yR1, y12, y22, ... yR2, ..., y1C, y2C, ..., yRC
, reading down the columns of the matrix of variables.From
?solve.QP
, we read that the objective takes the form-d'b + 0.5b'Db
for decision variablesb
. The element ofd
corresponding to decision variableyuw
has value2*Quw*ruw
andD
is a diagonal matrix with the element corresponding to decision variableyuw
taking value2*Quw
. Note that thesolve.QP
function requires theD
matrix to be positive definite, so we requireQuw > 0
for everyu, w
pair.The first
R*(C-1)
constraints correspond to theyuw >= yu,w+1
constraints, and the next(R-1)*C
constraints correspond to theyuw >= yu-1,w
constraints. The next2*R
constraints correspond to theyuC = 0
constraints (which are inputted asyuC >= 0
and-yuC >= 0
), and the last constraint is-yR1 >= -100
(which is logically equivalent toyR0 = 100
).We can input this model into the
quadProg
package with the following R commands, using random input data:We can now access the model solution: