0
votes

I am exploring using R optim() or optimx() for a (very) nonlinear optimization. Essentially I wrote a function that takes as its inputs:

1) a data.frame with specific column names/types
2) a numeric vector of length 1
3) a numeric vector of length > 1

The function then takes the inputs, performs some calculations and logic tests, then either returns a very negative value if the logic tests are FALSE, or the value of input #2 if the logic tests are TRUE. The goal is to maximize #2 without tripping the logic tests to FALSE.

I tried using optimx() with the following code (the par values correspond to the inputs I refer to above):

optimout <- 
  optimx:::optimx(
    par = c(inputDF, 5000, rep(99,20)),
    fn = MyFunction,
    maximize = TRUE)

I received the following error message:

Error in optimx.check(par, optcfg$ufn, optcfg$ugr, optcfg$uhess, lower, : Cannot evaluate function at initial parameters

Ralph

1
Try to evaluate MyFunction at the initial conditions c(inputDF, 5000, rep(99,20)). What do you get ?Marco Sandri
It returns the value I'd expect. However I am explicitly entering the arguments inputDF, 5000, rep(99,20) in the function call. I am wondering if I need to format the input arguments differently when calling MyFunction via optimx.Ralph
Does your MyFunction has 3 or 1 inputs ?Marco Sandri
3 inputs, same data type as I listed above in #1/2/3Ralph
From the help of optimx: fn = A function to be minimized (or maximized), with first argument the vector of parameters over which minimization is to take place.Marco Sandri

1 Answers

0
votes

If I understand your problem correctly and you only want to maximize the function based the second input parameter (a numeric vector of length 1), you need to call optimxdifferently, which would make sense given that the data.frame is probably some given input data.

So, try to do the following:

optimout <- optimx(par = c(5000), fn = MyFunction, par1=inputDF,
par2=rep(99,20), maximize = TRUE)

where par1 and par2 are the names of the input variables for your function. Essentially, you are providing optimxwith initial values for the input parameters par1 & par2, which are then not being optimized. Thus, the maximum is searched by only changing the value of your second parameter (a numeric vector of length 1), which you chose to start at 5000.