将参数传递给nloptr目标函数

编程入门 行业动态 更新时间:2024-10-24 18:18:05
将参数传递给nloptr目标函数 - R.(Passing parameters to nloptr objective function - R)

我打算在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.

更多推荐

本文发布于:2023-07-04 10:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023531.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   参数   目标   nloptr

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!