[R] Translation of Linear minimization probelm from matlab to r

Petr Savicky savicky at cs.cas.cz
Tue May 8 21:45:51 CEST 2012


On Tue, May 08, 2012 at 10:21:59AM -0700, Haio wrote:
> Hi everyone, i´m a new user of R and i´m trying to translate an linear
> optimization problem from Matlab into r.
> 
> The matlab code is as follow:
>  options = optimset('Diagnostics','on');
> 
>  [x fval exitflag] = linprog(f,A,b,Aeq,beq,lb,ub,[],options);
> 
>  exitflag
>  fval
>  x=round(x);
>  Where:
> f = Linear objective function vector (vector of 45,rows) 
> A = Matrix for linear inequality constraints (3colums 45 rows matrix)
> b = Vector for linear inequality constraints (3 rows vector)
> Aeq = Matrix for linear equality constraints (45 colums, 8 rows )
> beq = Vector for linear equality constraints (8 rows vector)
> lb      =Vector of lower bounds (45 rows)
> ub  =   Vector of upper bounds (45 rows)
> 
> I have tryed the package "linprog"   although i can´t find anyway to include
> the linear inequality constraints into the "solveLP" function. 

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.



More information about the R-help mailing list