Dear List,

I'm new in R. I'm trying to solve a simple constrained optimization
problem.

Essentially, let's say I have a matrix as in the object 'mm' inside the
function below. My objective function should have a matrix of parameters,
one parameter for each element 'mm'  (4 in this case). The problem is to
select the value of the parameters to maximize the function 'ff' s.t. (1)
each parameter being either 0 or 1, and (2) the sum of each row in the
parameter matrix should equal 1.

I'm using the function constrOptim as shown below, but obviously I'm not
doing things right. Any help is much appreciated.

ff <- function (x) {

  mm <- matrix(c(10, 25, 5, 10), 2, 2)
  matx <- matrix(NA, 2, 2)

  for (i in 1:nrow(x)) {
    for (j in 1:ncol(x)) {

      matx[i, j] <- x[i, j]
    }

  }

  -sum(apply(mm ^ matx, 1, prod))

}


constrOptim(theta = c(0, 0, 0, 0), f = ff, ui=rbind(c(1, 1),
                                                    c(1, 1)),
                                                    ci=c(1, 1))


Best,
Axel.

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to