lower <- c(-Inf, 0, 0)
upper <- c( Inf, 0.5, 1)
Tf <- transfinite(lower, upper)
g <- Tf$g; h <- Tf$ginv
## Solve Rosenbrock with one variable restricted
rosen <- function(x) {
n <- length(x)
x1 <- x[2:n]; x2 <- x[1:(n-1)]
sum(100*(x1-x2^2)^2 + (1-x2)^2)
}
f <- function(x) rosen(h(x)) # f must be defined on all of R^n
x0 <- c(0.1, 0.1, 0.1) # starting point not on the boundary!
nm <- nelder_mead(g(x0), f) # unconstraint Nelder-Mead
h(nm$xmin); nm$fmin # box/bound constraint solution
# [1] 0.7085596 0.5000000 0.2500004
# [1] 0.3353605
Run the code above in your browser using DataLab