我打算在for循环中使用nloptr包,如下所示:
for(n in 1:ncol(my.data.matrix.prod)) { alpha.beta <- as.vector(Alpha.beta.Matrix.Init[,n]) opts = list("algorithm"="NLOPT_LN_COBYLA", "xtol_rel"=1.0e-8, "maxeval"= 2000) lb = vector("numeric",length= length(alpha.beta)) result <- nloptr(alpha.beta,eval_f = Error.func.oil,lb=lb, ub = c(Inf,Inf),eval_g_ineq=Const.func.oil, opts = opts) Final.Alpha.beta.Matrix[,n] <- result$solution }除了将“优化参数: alpha.beta ”传递给错误函数(最小化函数)之外,我还想从for循环中发送n 。 反正有没有这样做?
错误函数定义为:
Error.func.oil <- function(my.data.var,n) { my.data.var.mat <- matrix(my.data.var,nrow = 2,ncol = ncol(my.data.matrix.prod) ,byrow = TRUE) qo.est.matrix <- Qo.Est.func(my.data.var.mat) diff.values <- well.oilprod-qo.est.matrix #FIND DIFFERENCE BETWEEN CAL. MATRIX AND ORIGINAL MATRIX Error <- ((colSums ((diff.values^2), na.rm = FALSE, dims = 1))/nrow(well.oilprod))^0.5 #sum of square root of the diff Error[n] }约束函数很简单,定义如下:
Const.func.oil <- function(alpha.beta) { cnst <- alpha.beta[2]-1 cnst }所以,当我运行上面的代码时,我收到一个错误
.checkfunargs中的错误(eval_f,arglist,“eval_f”):eval_f需要参数'n',但这还没有传递给'nloptr'函数。
如何将“n”传递给错误函数? 请注意,“n”不是优化的。 这只是一个索引。
I intend to use nloptr package in a forloop as below:
for(n in 1:ncol(my.data.matrix.prod)) { alpha.beta <- as.vector(Alpha.beta.Matrix.Init[,n]) opts = list("algorithm"="NLOPT_LN_COBYLA", "xtol_rel"=1.0e-8, "maxeval"= 2000) lb = vector("numeric",length= length(alpha.beta)) result <- nloptr(alpha.beta,eval_f = Error.func.oil,lb=lb, ub = c(Inf,Inf),eval_g_ineq=Const.func.oil, opts = opts) Final.Alpha.beta.Matrix[,n] <- result$solution }Apart from passing the "optimization parameters: alpha.beta" to the error function(minimization function) , I also would like to send n from the forloop. Is there anyway to do this?
The error func is defined as:
Error.func.oil <- function(my.data.var,n) { my.data.var.mat <- matrix(my.data.var,nrow = 2,ncol = ncol(my.data.matrix.prod) ,byrow = TRUE) qo.est.matrix <- Qo.Est.func(my.data.var.mat) diff.values <- well.oilprod-qo.est.matrix #FIND DIFFERENCE BETWEEN CAL. MATRIX AND ORIGINAL MATRIX Error <- ((colSums ((diff.values^2), na.rm = FALSE, dims = 1))/nrow(well.oilprod))^0.5 #sum of square root of the diff Error[n] }The constraint function is simple and defined as:
Const.func.oil <- function(alpha.beta) { cnst <- alpha.beta[2]-1 cnst }So, when I run the above code, I get an error
Error in .checkfunargs(eval_f, arglist, "eval_f") : eval_f requires argument 'n' but this has not been passed to the 'nloptr' function.
How do I pass "n" to the error function? note that "n" is not to be optimized. It's just an index.
最满意答案
好的。 我在网上看了一些例子,发现我可以在nloptr本身的定义中提到“n”:
for(n in 1:ncol(my.data.matrix.prod)) { alpha.beta <- as.vector(Alpha.beta.Matrix.Init[,n]) opts = list("algorithm"="NLOPT_LN_COBYLA", "xtol_rel"=1.0e-8, "maxeval"= 5000) lb = c(0,0) result <- nloptr(alpha.beta,eval_f = Error.func.oil,lb=lb, ub = c(Inf,Inf), opts = opts, n=n) #Added 'n' HERE Final.Alpha.beta.Matrix[,n] <- result$solution }这似乎对我有用。 因此,我将此设置为已关闭。
Okay. I read some examples online and found out that I can probably mention "n" in the definition of nloptritself as:
for(n in 1:ncol(my.data.matrix.prod)) { alpha.beta <- as.vector(Alpha.beta.Matrix.Init[,n]) opts = list("algorithm"="NLOPT_LN_COBYLA", "xtol_rel"=1.0e-8, "maxeval"= 5000) lb = c(0,0) result <- nloptr(alpha.beta,eval_f = Error.func.oil,lb=lb, ub = c(Inf,Inf), opts = opts, n=n) #Added 'n' HERE Final.Alpha.beta.Matrix[,n] <- result$solution }This seems to have worked for me. Hence, I am setting this as closed.
更多推荐
发布评论