我正在尝试使用包alabama来解决R中的非线性优化问题。 在定义了这样的目标函数之后:
fn <- function(x)
{
-(database4[1,1] * x[1] + database4[1,2] * x[1] * x[1] +
database4[2,1] * x[2] + database4[2,2] * x[2] * x[2]+database4[3,1] * x[3] + database4[3,2] * x[3]* x[3]
database4[4,1] * x[4] + database4[4,2] * x[4] * x[4])
}
我已经定义了这样的不平等:
hin <- function(x) {
h <- rep(NA, 1)
h[0] <- (database4[1,3]+database4[2,3] +database4[3,3] +database4[4,3]+ 1) -
(x[1] + x[2] + x[3] + x[4])
h[1] <- x[1] - database4[1,5]
h[2] <- database4[1,6] - x[1]
h[3] <- x[2] - database4[2,5]
h[4] <- database4[2,6] - x[2]
h[5] < x[3] - database4[3,5]
h[6] <- database4[3,6] - x[3]
h[4] <- x[4] - database4[4,5]
h[6] <- database4[4,6] - x[4]
h
}
然后使用hin函数调用目标函数,如下所示:
constrOptim.nl(par=c(database4[1,3], database4[2,3], database4[3,3], database4[4,3]), fn=fn, heq=NULL, hin= hin, control.outer = list(itmax= 3000))
使用的值是自动化的,因此使用单元格引用。 这是给定的数据库4
我得到的错误是......
"Error in if (any(hin(theta, ...) <= 0)) stop("initial value not feasible") :
missing value where TRUE/FALSE needed"
traceback() - "2: adpbar(par, fn, gr, hin = hin, hin.jac = hin.jac, control.outer = outer.ctrl,
control.optim = optim.ctrl, ...)
1: constrOptim.nl(par = c(database4[1, 3], database4[2, 3], database4[3,
3], database4[4, 3]), fn = fn, heq = NULL, hin = hin, control.outer = list(itmax = 3000))"
请帮忙!