Nick
Nick

Reputation: 1106

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

I have the non-linear optimization problem

enter image description here

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?

Upvotes: 1

Views: 843

Answers (1)

djbetancourt
djbetancourt

Reputation: 349

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.

Upvotes: 2

Related Questions