dhacademic <at> gmail.com <dhacademic <at> gmail.com> writes:
> Hi, > > I have struggled on this "bound optimization with equality constraint" by > using optim function for two days, but still fail to prepare a good input. > Can anyone help to prepare the input for my specific case? Many thanks. > > Best, > Hao You did not disclose the the function f, is it linear or nonlinear, does it have (many) local minima, do you know its gradient?, etc. With some bloody tricks it is possible to emulate equality and inequality constraints with 'optim', too. But in general, I would suggest to apply 'constrOptim.nl' in the alabama package (or 'solnp' in Rsolnp). These are newer implementations and will handle equality constraints nicely. Assuming your original function f12 is a function of 12 variables, e.g. f12 <- function(x) sum((1:12)*(x - 1:12)^2) define a new function f eliminating x3, x4, and x12 through f <- function(x) { xx1r <- 1.5 - x[1] - sum(x) x12 <- c(x[1], x[2], x[1], x[1], x[3:9], xx1r) f12(x12) } I would suggest 'solnp' in package "Rsolnp", as the bound constraints can be formulated somewhat easier; the start value has to be feasible(!): lower <- c(-1, 0, -1, 0, 0, 0, 0, 0, 0) upper <- c( 0, 1, 0, 1, 1, 1, 1, 1, 1) fun <- function(x) 1.5 - 2*x[1] - x[2] - sum(x[3:9]) start <- c(-0.2, 0.2, -0.2, rep(0.2, 6)) S <- solnp(start, f, LB=lower, UB=upper, ineqfun=fun, ineqLB=0, ineqUB=1) This will return a (local?) minimum (-1, 0, -1, 0, 0, 0.5, 1, 1, 1) as: S$pars # [1] -1.000000e+00 1.209474e-08 -9.999999e-01 4.801754e-08 1.930926e-07 # [6] 4.999999e-01 9.999998e-01 1.000000e+00 1.000000e+00 Hans Werner -------- P. S.: Sorry, Ravi, but I could not resist the temptation to at least **indicate** one complete solution. -------- ______________________________________________ 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.