[R] Can R solve this optimization problem?

Gabor Grothendieck ggrothendieck at gmail.com
Tue Jan 8 03:58:42 CET 2008


On Jan 7, 2008 8:09 PM, Paul Smith <phhs80 at gmail.com> wrote:
> Thanks again, Gabor. Apparently, something is missing in your
> approach, as I tried to apply it to the original problem (without sin)
> and the result seems not being correct::
>
> f <- function(z) {
>  a <- sum(-(999:1)*z)
>  return(a)
> }
>
> result <- optim(rep(0,999), f,
> NULL,lower=rep(-1/1000,999),upper=rep(1/1000,999),method="L-BFGS-B")
> b <- result$par
>
> x <- 0
>
> for (i in 1:999) {
>  x[i+1] <- b[i] + x[i]
> }
>
> The vector x contains the solution, but it does not correspond to the
> analytical solution. The analytical solution is
>
> x(t) = t, if t <= 1/2 and
>
> x(t) = 1 - t, if t >= 1/2.
>
> Am I missing something?
>

No, but I was -- the x[n] = 0 constraint.  Add it through
a penalty term and try this:

theta <- 1000
n <- 100
x <-  rep(1/n, n)
f <- function(x) sum(n:1 * x) - theta * sum(x)^2
optim(x, f, lower = rep(-1, n), upper = rep(1, n), method = "L-BFGS-B",
  control = list(maxit = 1000, fnscale = -1, lmm = 25))
plot(cumsum(res$par))




More information about the R-help mailing list