Skip to content

Constrined dependent optimization.

1 message · Ben Bolker

#
rkevinburton at charter.net wrote:
If you look more closely at the docs for method="SANN" (and
the examples), you'll see that SANN allows you to pass the
"gradient" argument (gr) as a custom function to provide the
candidate distribution.  Here's an example:

N <- 10
xvec <- seq(0,1,length=N)
target <- rank((xvec-0.2)^2)

objfun <- function(x) {
  sum((x-target)^2)/1e6
}

objfun(1:100)

swapfun <- function(x,N=10) {
  loc <- sample(N,size=2,replace=FALSE)
  tmp <- x[loc[1]]
  x[loc[1]] <- x[loc[2]]
  x[loc[2]] <- tmp
  x
}

set.seed(1001)
opt1 <- optim(fn=objfun,
              par=1:N,
              gr=swapfun,method="SANN",
              control=list(trace=10))

plot(opt1$par,target)