mathematical optimization - Linear Programs using R -
how can solve linear program using r? want solve following example:
min -a -2b +4c constraints + b + s1 = 5 + 3c -s2 = 10 2b - 3c = 20 >= 0, b >= 0, c >= 0, s1 >= 0, s2 >= 0 the equations might not make total sense. need know syntax of writing these equations in r. might write above equations
require(lpsolve) r.obj <- c(-1,-2,4) r.con <- matrix(c(1,1,1,1,3,-1,2,-3),nrow=3,byrow=true) r.dir <- c("=","=","=") r.rhs <- c(5,10,20) lp("min",r.obj,r.con,r.dir,r.rhs) would correct? in documentation, matrix m*m, if matrix m*n n != m?
your constraint matrix has 3 rows , 5 columns, you've provided 8 non-zero values when building constraint matrix. further, have 5 variables, r.obj needs 5 values:
require(lpsolve) r.obj <- c(-1, -2, 4, 0, 0) r.con <- matrix(c(1, 1, 0, 1, 0, 2, 0, 3, -3, 1, 0, 0, 0, -1, 0), nrow=3) r.dir <- c("=", "=", "=") r.rhs <- c(5, 10, 20) lp("min", r.obj, r.con, r.dir, r.rhs) # error: no feasible solution found a bit of math shows lp indeed infeasible. lp equivalent -a - b >= -5, a + 3c >= 10, b = 10 + 1.5c. can substitute last equation first yield -a - 1.5c >= 5 , a + 3c >= 10, , adding yields c >= 10. third equation, b >= 25, means first equation can never hold due non-negativity of a , s1.
Comments
Post a Comment