Alicia Perez-alonso
2009-Jul-02 16:37 UTC
[R] lpSolve: how to allow variables to become negative
Dear all, I am interested in solving a MIP problem with binary outcomes and continuous variables, which ARE NOT RESTRICTED TO BE NEGATIVE. In particular, Max {z1,z2,z3,b1} z1 + z2 + z3 (s.t.) # 7 z1 + 0 z2 + 0 z3 + b1 <= 5 # 0 z1 + 8 z2 + 0 z3 - b1 <= 5 # 0 z1 + 0 z2 + 6 z3 + b1 <= 7 # z1, z2, z3 BINARY {0,1} # -5<= b1 <=5 (i.e. b1 <= 5; -b1 <= 5 ) Using the lpSolve package of R, I wrote: library (lpSolve) f.obj <- c(1, 1, 1, 0) f.con <- matrix (c(7, 0, 0, -1, 0, 8, 0, 1, 0, 0, 6, -1, 0, 0, 0, 1 ), nrow=4, byrow=TRUE) f.dir <- c("<=", "<=", "<=", "<=") f.rhs <- c(5, 5, 7, 5) lp ("max", f.obj, f.con, f.dir, f.rhs, binary.vec=1:3) lp ("max", f.obj, f.con, f.dir, f.rhs, binary.vec=1:3)$solution The problem is that BY DEFAULT, "b1" is constrained to be ">=0", thus, the constraint "-b1 <= 5" does not apply. Given that there is not any global variable in the lp() command such that I can change the default value of 0 (to -5 or -inf), I do not get the correct solution to the problem, which is indeed b1= -2. Thank you very much in advance for your help, Alicia -- Before printing this message, make sure you really need to. Taking care of our environment is everyone's responsibility. _________________________________________________________ Alicia P?rez-Alonso Departament of Economics European University Institute via della Piazzuola 43 50133 Firenze (FI) ITALY Room: 40 (Second Floor) Phone: (+39) 0554685955 Fax: (+39) 0554685902 E-mail: alicia.perez-alonso at eui.eu URL: http://www.eui.eu/Personal/Fellows/Perez-Alonso/
Hi Alicia, I think the trick may be to split b1 into the sum of two non-negative variables. You will then also have to alter your constraints and objective to include the two new variables with negative values in appropriate places, but I believe that this will solve the problem. On Thu, Jul 2, 2009 at 12:37 PM, Alicia Perez-alonso<alicia.perez-alonso at eui.eu> wrote:> Dear all, > > I am interested in solving a MIP problem with binary outcomes and continuous > variables, which ARE NOT RESTRICTED TO BE NEGATIVE. In particular, > > Max {z1,z2,z3,b1} ?z1 + ?z2 + z3 (s.t.) > # ? 7 z1 + 0 z2 + 0 z3 + b1 <= 5 > # ? 0 z1 + 8 z2 + 0 z3 - b1 <= 5 > # ? 0 z1 + 0 z2 + 6 z3 + b1 <= 7 > # ? z1, z2, z3 ? BINARY {0,1} > # ?-5<= b1 <=5 (i.e. ?b1 <= 5; -b1 <= 5 ) > > Using the lpSolve package of R, I wrote: > > library (lpSolve) > f.obj <- c(1, 1, 1, 0) > f.con <- matrix (c(7, 0, 0, -1, 0, 8, 0, 1, 0, 0, 6, -1, 0, 0, 0, 1 ), > nrow=4, byrow=TRUE) > f.dir <- c("<=", "<=", "<=", "<=") > f.rhs <- c(5, 5, 7, 5) > > lp ("max", f.obj, f.con, f.dir, f.rhs, binary.vec=1:3) > lp ("max", f.obj, f.con, f.dir, f.rhs, binary.vec=1:3)$solution > > The problem is that BY DEFAULT, ?"b1" is constrained to be ">=0", ?thus, the > constraint "-b1 <= 5" does not apply. > > Given that there is not any global variable in the lp() command such that I > can change the default value of 0 (to -5 or -inf), I do not get the correct > solution to the problem, which is indeed b1= -2. > > Thank you very much in advance for your help, > Alicia > > -- > Before printing this message, make sure you really need to. > Taking care of our environment is everyone's responsibility. > > _________________________________________________________ > Alicia P?rez-Alonso > Departament of Economics > European University Institute > via della Piazzuola 43 > 50133 Firenze (FI) > ITALY > > Room: 40 (Second Floor) > Phone: (+39) 0554685955 > Fax: (+39) 0554685902 > E-mail: alicia.perez-alonso at eui.eu > URL: http://www.eui.eu/Personal/Fellows/Perez-Alonso/ > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >