Skip to content
Prev 293970 / 398506 Next

Translation of Linear minimization probelm from matlab to r

On Tue, May 08, 2012 at 10:21:59AM -0700, Haio wrote:
Hi.

I am not sure, what is the problem with the constraints. According to
the documentation of the function solveLP(), the constraints are
provided as the arguments "bvec" and "Amat". However, i am using "lpSolve"
package and have no experience with "linprog" package. An example
of solving a simple linear program using lpSolve follows.

Consider the problem to maximize 3*x + 2*y with the constraints

    x >= 0
    y >= 0

    x +   y <= 3
  2*x +   y <= 5
    x + 2*y <= 5

then try

  library(lpSolve)
  crit <- c(3, 2)
  mat <- rbind(c(1, 1), c(1, 2), c(2, 1))
  rhs <- c(3, 5, 5)
  dir <- rep("<=", times=3)
  out <- lp("max", objective.in=crit, const.mat=mat, const.dir=dir, const.rhs=rhs)
  out

  Success: the objective function is 8 

  out$solution 
 
  [1] 2 1

Hope this helps.

Petr Savicky.