Axel,
Your objective function has the wrong sign. The maximum is infinity, so it
does not make sense to maximize. YOu should be minimizing the product. So,
remove the negative sign and it works as you had expected.
ff <- function (x) {
mm <- matrix(c(10, 25, 5, 10), 2, 2)
matx <- matrix(x, 2, 2)
# - sum(apply(mm ^ matx, 1, prod)) # this is the problem
sum(apply(mm ^ matx, 1, prod))
}
### constraints
heq <- function(x) {
h <- rep(NA, 1)
h[1] <- x[1] + x[3] -1
h[2] <- x[2] + x[4] -1
h[3] <- x[1] * x[3]
h[4] <- x[2] * x[4]
h
}
res <- auglag(par = rep(1,4), fn = ff, heq = heq)
Ravi
________________________________
From: Axel Urbiz [[email protected]]
Sent: Sunday, February 10, 2013 3:16 PM
To: [email protected]; Ravi Varadhan
Subject: Constrained Optimization in R (alabama)
Dear List,
I'm trying to solve this simple optimization problem in R. The parameters are
the exponents to the matrix mm. The constraints specify that each row of the
parameter matrix should sum to 1 and their product to 0. I don't understand why
the constraints are not satisfied at the solution. I must be misinterpreting
how to specify the constrains somehow.
library(alabama)
ff <- function (x) {
mm <- matrix(c(10, 25, 5, 10), 2, 2)
matx <- matrix(x, 2, 2)
-sum(apply(mm ^ matx, 1, prod))
}
### constraints
heq <- function(x) {
h <- rep(NA, 1)
h[1] <- x[1] + x[3] -1
h[2] <- x[2] + x[4] -1
h[3] <- x[1] * x[3]
h[4] <- x[2] * x[4]
h
}
res <- constrOptim.nl(par = c(1, 1, 1, 1), fn = ff,
heq = heq)
res$convergence #why NULL?
matrix(round(res$par, 2), 2, 2) #why constraints are not satisfied?
Axel.
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.