How to pass the parameters into solnl() function in R?

僤鯓⒐⒋嵵緔 提交于 2020-07-10 10:36:05

问题


I have the non-linear optimization problem

I have tried to use the solnl() function from the NlcOptim package. Unfortunately, I don't know how to pass the param into the solnl() function correctly.

And I have the

Error in objfun(X) : argument "param" is missing, with no default
Calls: solnl -> objfun

My code is below:

library(MASS)
library(NlcOptim)

objfun=function(x, param, n){
return( x[n] * (param[1] - param[2] / sum(x[1:(n-1)])  ))
}

#constraint function
confun=function(x, param, n){
f=NULL
f=rbind(f, x[n] - param[1] + param[2] * sum(x[1:(n-1)]))
return(list(ceq=f, c=NULL))
}
A <- 1; B <- 1
param = c(A, B)

x0=c(0.2, 0.2, 0.2, 0.2, 0.2)
n <- length(x0)

solnl(x0, objfun=objfun(x0, param, n), confun=confun)

Question. How to pass the parameters into function?


回答1:


I've never used solnl from NlcOptim BUT I've used optim from stats and I think the functioning is simlar.

I think you should do something like

solnl(x = x0, objfun = objfun, confun = confun)

Unfortunately, I don't see in the help page any way to pass further arguments to the objective or constraint functions as it is possible in optim through .... None of the examples of solnl have such a possibility illustrated. Thus, I don't think you can pass param and n to solnl.

If it is just for a personal script and not for a package or something more formal, maybe you can define your objective function and constraint function just in terms of the decision variables x. If you define param and n before calling solnl it should work:

# functions defined just in terms of decision variables
objfun <- function(x) {
  return( x[n] * (param[1] - param[2] / sum(x[1:n-1])  ))
}
confun <- function(x){
  f <- NULL
  f <- rbind(f, x[n] - param[1] + param[2] * sum(x[1:n-1]))
  return(list(ceq = f, c = NULL))
}

# starting points
x0 = c(0.2, 0.2, 0.2, 0.2, 0.2)

# deifning any parameter before calling solnl
A <- 1; B <- 1
param = c(A, B)
n <- length(x0)

# resolving
solnl(x = x0, objfun = objfun, confun = confun)

Edit

optim does not allow constraints appart from constant lower and upper bounds for the decision variables. I think nloptr might do the work for you. Take a look at the example in page 4 of nloptr's manual. The difference is that nloptr does receive ..., meaning that you can pass further arguments to the objective and constraint functions at the end of the call.

Thus, you can define your objective and constraint functions receiving param and n as arguments

objfun <- function(x, param, n) {
  # fill this
}
confun <- function(x, param, n) {
  # fill this
}

and then use

nloptr(x0 = x0, eval_f = objfun, lb = rep(0,n),
       eval_g_eq = confun, param = param, n = n)

Take care to modify your confun function according to the requirements of nloptr. Use the example in the provided link for that.



来源:https://stackoverflow.com/questions/60093454/how-to-pass-the-parameters-into-solnl-function-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!