[R] Optimizing problem R

Petr Savicky savicky at cs.cas.cz
Thu May 24 11:54:35 CEST 2012


On Wed, May 23, 2012 at 10:56:43PM -0700, kylmala wrote:
> Hi,
> 
> and thanks for replying this. Yes, you are right that the term
> min(24/bb,26/cc) is actually min(bb/24,cc/26) - my mistake. But still I
> don't get it. If the objective function is 
> 
> #min(m1,m2,m3)
> f.obj <- c(1, 1, 1)
> 
> #and we now that 
> # a+aa<=15 
> # b+bb+bbb<=35 
> # cc+ccc<=40 
> 
> # so
> 
> # 1a + 0b + 0c + 1aa + 0bb + 0cc + 0aaa + 0bbb + 0ccc <=15
> # 0a + 1b + 0c + 0aa + 1bb + 0cc + 0aaa + 1bbb + 0ccc <=35
> # 0a + 0b + 0c + 0aa + 0bb + 1cc + 0aaa + 0bbb + 1ccc <=40
> # and the matrix of numeric constraint coefficients is 
> 
> C = matrix(nrow=3, data=c(1,0,0,0,1,0,0,0,0,1,1,0,0,0,1,0,1,0,0,0,1))
> 
> #Constraits are: 
> 
> f.rhs <- c(15, 35, 40)
> 
> #and 
> 
> f_dir = c("<=", "<=", "<=")
> 
> Because the command lp is form lp ("max", f.obj, f.con, f.dir, f.rhs), how
> can I get those other constraints in
> 
>  m1 <= a/10 
>   m1 <= b/11 
>   m2 <= aa/13 
>   m2 <= bb/12 
>   m2 <= cc/10 
>   m3 <= bb/24 
>   m3 <= cc/26

Hi.

There are 12 variables, namely a, b, c, aa, bb, cc, aaa, bbb, ccc, m1, m2, m3
and 3 + 7 constraints. So, the matrix should be 10 times 12 and may be created
as follows

  C11 <- rbind(
  c(1, 0, 0, 1, 0, 0, 0, 0, 0),
  c(0, 1, 0, 0, 1, 0, 0, 1, 0),
  c(0, 0, 0, 0, 0, 1, 0, 0, 1))
 
  C12 <- matrix(0, nrow=3, ncol=3)
 
  A <- matrix(0, nrow=7, ncol=12)
  colnames(A) <- c("a", "b", "c", "aa", "bb", "cc", "aaa", "bbb", "ccc", "m1", "m2", "m3")
 
  A[1, c( "a", "m1")] <- c(1/10, -1)
  A[2, c( "b", "m1")] <- c(1/11, -1)
  A[3, c("aa", "m2")] <- c(1/13, -1)
  A[4, c("bb", "m2")] <- c(1/12, -1)
  A[5, c("cc", "m2")] <- c(1/10, -1)
  A[6, c("bb", "m3")] <- c(1/24, -1)
  A[7, c("cc", "m3")] <- c(1/26, -1)
 
  f.mat <- rbind(cbind(C11, C12), A)

The variables "c" and "aaa" are not used at all. Is this correct?

The other parameters and the call are

  library(lpSolve)

  f.obj <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1)
  f.rhs <- c(15, 35, 40, 0, 0, 0, 0, 0, 0, 0)
  f.dir = c("<=", "<=", "<=", rep(">=", times=7))

  out <- lp("max", f.obj, f.mat, f.dir, f.rhs)
  out

  Success: the objective function is 2.612179 

  out$solution

  [1]  0.000000  0.000000  0.000000 15.000000 35.000000 37.916667  0.000000
  [8]  0.000000  0.000000  0.000000  1.153846  1.458333

The constraint on m1 requires only m1 <= min(a/10,b/11), however, since
we maximize m1 + m2 + m3, the optimum will satisfy m1 = min(a/10,b/11).
Similarly for m2, m3.

Hope this helps.

Petr Savicky.



More information about the R-help mailing list