f <- function(x) sum(x^2)
F <- Historize(f, len = 1)
c( F(c(1,1)), F(c(1,2)), F(c(2,1)), F(c(2,2)) )
#> [1] 2 5 5 8
F()
#> $input
#> [,1] [,2]
#> [1,] 1 1
#> [2,] 1 2
#> [3,] 2 1
#> [4,] 2 2
#>
#> $values
#> [1] 2 5 5 8
#>
#> $nvars
#> [1] 2
#>
#> $ncalls
#> [1] 4
F(NULL) # reset memory
## Rastrigin under Differential Evolution
Fn <- Historize(fnRastrigin)
dm <- 10
lb <- rep(-5.2, dm); ub <- -lb
sol <- simpleDE(Fn, lower = lb, upper = ub)
fvalues <- Fn()$values
fvals <- cummin(fvalues)
if (FALSE) {
plot(fvalues, type = 'p', col = 7, pch = '.', cex = 2,
main = "Simple DE optimization", xlab = '', ylab = '')
lines(fvals, col = 2, lwd = 2)
legend(length(fvalues), max(fvalues),
c("Intermediate values", "cummulated min"),
xjust = 1, col = c(7, 2), lwd = 2)
grid()}
Run the code above in your browser using DataLab