Toby - 2 months ago 19
R Question

# R DEoptim() function: How to use smaller initial population number than Npop

You'll need the

`DEoptim`
package for this question:

``````install.packages("DEoptim")
library(DEoptim)
``````

From the
`help(DEoptim)`

``````Rosenbrock <- function(x){
x1 <- x[1]
x2 <- x[2]
100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}

lower <- c(-10,-10)
upper <- -lower

## run DEoptim and set a seed first for replicability
set.seed(1234)
DEoptim(Rosenbrock, lower, upper)
``````

Now this works fine:
However, if I provide an initial population
`Npop`
with
`dim(Npop)[1] < 10*length(lower)`

``````xini <- cbind(runif(10),runif(10))
ran <- abs(lower - upper)
Npop <- apply(xini,2,function(x) x*ran+lower)
DEoptim(Rosenbrock, lower, upper,DEoptim.control(initialpop = Npop))
``````

I get the following error:

``````Error in DEoptim(Rosenbrock, lower, upper, DEoptim.control(initialpop = Npop)) :
Initial population is not a matrix with dim. NP x length(upper).
``````

How can I do this?

Related to this question: R DEoptim() function: How to select parameters to be opimised?

where e.g. 1 parameter is not estimated and I do not have to account for it in the
`DEoptim.control(initialpop)`

Answer

Here's the workaround I got from the author of `DEoptim()`

``````library(DEoptim)

#From help(DEoptim)

Rosenbrock <- function(x){
x1 <- x[1]
x2 <- x[2]
100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}

lower <- c(-10,-10)
upper <- -lower

## run DEoptim and set a seed first for replicability

set.seed(1234)
DEoptim(Rosenbrock, lower, upper)

n <- length(lower) * 10
# n <- length(lower) * 5

xini <- cbind(runif(n),runif(n))
ran <- upper - lower
Npop <- as.matrix(apply(xini,2,function(x) x*ran+lower))
DEoptim(Rosenbrock, lower, upper, DEoptim.control(NP=n,initialpop = Npop))
``````
Comments