I want minimize the following equation:
F=SUM{u 1:20}sum{w 1:10} Quw(ruw-yuw)
with the following constraints:
yuw >= yu,w+1
yuw >= yu-1,w
y20,0 >= 100
y0,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 and optimx packages, but don't know how to use them for this particular question.
Because
Quw
andruw
are both data, all constraints as well as the objective are linear in theyuw
decision variables. As a result, this is a linear programming problem that can be approached by thelpSolve
package.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.The coefficient of each
yuw
variable in the objective is-Quw
; note that theQuw*ruw
terms in the summation are constants (aka not affected by the values we select for the decision variables) and therefore not inputted to the linear programming solver. Interestingly, this means thatruw
actually has no effect on the optimization model solution.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 final two constraints correspond to they20,1 >= 100
andy1,10 >= 0
constraints.We can input this model into the
lpsolve
package with the following R command, taking as input a simple Q matrix where each entry is -1 (the resulting solution should have all decision variables set to 0 except the bottom left corner, which should be 100):We can now access the model solution:
Note that your model could easily become infeasible if
Quw
changed (for instance if we filled it with 1 instead of -1). In these cases the model will exit with status 3 (you can see this by runningmod
ormod$status
).