Skip to content

L-BFGS-B needs finite values of 'fn'

5 messages · Paul Smith, Brian Ripley, Zaihra T

#
Dear All,

I am trying to solve the optimization problem below, but I am always
getting the following error:

Error in optim(rep(20, nvar), f, gr, method = "L-BFGS-B", lower = rep(0,  :
  L-BFGS-B needs finite values of 'fn'

Any ideas?

Thanks in advance,

Paul

-----------------------------------------------

k <- 10000
b <- 0.3

f <- function(x) {

  n <- length(x)

  r <- sum((b^(0:(n-1)))*log(x)) - 2000000*(sum(x)-k)^2

  return(r)

}

gr <- function(x) {

  n <- length(x)

  r <- (b^(0:(n-1)))*(1/x) - 4000000*(sum(x)-k)

  return(r)

}

nvar <- 10
(sols <- optim(rep(20,nvar),f,gr,method="L-BFGS-B",lower=rep(0,nvar),upper=rep(k,nvar),control=list(fnscale=-1,parscale=rep(2000,nvar),factr=1e-300,pgtol=1e-300)))
#
Your function gives -Inf at the lower bound on the parameters, so you are 
minimizing a function without a lower bound.

Using the trace facilities of optim() would have got you thereeasily 
enough.
On Mon, 31 Mar 2008, Paul Smith wrote:

            

  
    
#
Thanks for you help and for having called my attention to the trace
facilities of optim(), which are really helpful.

Paul



On Mon, Mar 31, 2008 at 12:43 PM, Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote:
#
try something like  this before wrapping up your function else i guess u'll
   have to stick to Prof Brian Ripley  suggestion his suggestions are usually
   best bet .

    f <- function(x) {

   n <- length(x)
   r <- sum((b^(0:(n-1)))*log(x)) - 2000000*(sum(x)-k)^2
    if(!is.finite(r))

   r<-1e+20 return(r)
   }

   have a nice day.
On Mon, 31 Mar 2008 12:24:09 +0100 "Paul Smith" wrote:
> Dear All,
   >
   > I am trying to solve the optimization problem below, but I am always
   > getting the following error:
   >
   > Error in optim(rep(20, nvar), f, gr, method = "L-BFGS-B", lower = rep(0, :
   > L-BFGS-B needs finite values of 'fn'
   >
   > Any ideas?
   >
   > Thanks in advance,
   >
   > Paul
   >
   > -----------------------------------------! ------
   >
   > k <- 10000
   > b <- 0.3
   >
   > f <- function(x) {
   >
   > n <- length(x)
   >
   > r <- sum((b^(0:(n-1)))*log(x)) - 2000000*(sum(x)-k)^2
   >
   > return(r)
   >
   > }
   >
   > gr <- function(x) {
   >
   > n <- length(x)
   >
   > r <- (b^(0:(n-1)))*(1/x) - 4000000*(sum(x)-k)
   >
   > return(r)
   >
   > }
   >
   > nvar <- 10
   > (sols <-
   >
   optim(rep(20,nvar),f,gr,method="L-BFGS-B",lower=rep(0,nvar),upper=rep(k,nvar
   ),control=list(fnscale=-1,parscale=rep(2000,nvar),factr=1e-300,pgtol=1e-300)
   ))
   >
   > ______________________________________________
   > R-help at r-project.org mailing list
   > https://stat.ethz.ch/mailman/listinfo/r-help
   > PLEASE do read the posting guide
   http://www.R-project.org/posting-guide.htm! l
   > and provide commented, minimal, self-contained, reproduci ble code.
#
On Mon, Mar 31, 2008 at 2:57 PM, Zaihra T <zaihra at uwindsor.ca> wrote:
Not much progress, Zaihra. Unfortunately! I am wondering whether one
can transform the original problem into an equivalent one and solvable
with optim.

I know the analytical solution; I am just trying to check how far can
R go regarding optimization problems.

Paul