It sounds like optim
is not able to handle the upper and lower matching. I suppose you could parameterize your function with the known values and use some simple ifelse
statements to check if you should be using the passed value from optim
or the known value:
# Slightly redefined function to optimize
fr2 <- function(opt.x, known.x) {
x <- ifelse(is.na(known.x), opt.x, known.x)
100 * (x[2] - x[1] * x[1])^2 + (1 - x[1])^2 +
100 * (x[3] - x[2] * x[2])^2 + (1 - x[2])^2 +
100 * (x[4] - x[3] * x[3])^2 + (1 - x[3])^2
}
# Optimize, and then replace the appropriate indices of the result with known vals
known.x <- c(NA, 1, NA, 1)
opt.result <- optim(par = c(0, 1, 1, 2), fr2, method = "L-BFGS-B",
lower = c(0, 0, 0, 0), upper = c(3, 3, 3, 3), known.x=known.x)
opt.result$par <- ifelse(is.na(known.x), opt.result$par, known.x)
opt.result
# $par
# [1] 0.9999995 1.0000000 0.9999996 1.0000000
#
# $value
# [1] 1.795791e-10
#
# $counts
# function gradient
# 13 13
#
# $convergence
# [1] 0
#
# $message
# [1] "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH"
This code basically ignores the indices passed from optim
if they are already known, and just uses the known values in those cases.