tests/testthat/test-calibrar-optim2.R

library(calibrar)
library(testthat)

tfn = function(x) sum(x^2) + 10
tgr = function(x) 2*x

# optim2 ------------------------------------------------------------------

message("\nTests for optim2() -------- \n")

algorithms = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN", 
               "nlm", "nlminb", "Rcgmin", "Rvmmin", "hjn", 
               "spg", "LBFGSB3", "AHR-ES")

for(alg in algorithms) {
  
  message(alg, " - ", date())
  
  test_that(sprintf("algorithm test: '%s'.", alg), {
    expect_no_error(o0 <- optim2(par=rep(0.5, 2), fn = tfn, method=alg))
    expect_no_error(o1 <- optim2(par=rep(0.5, 2), fn = tfn, gr=tgr, method=alg))
    expect_equal(o0$par, o1$par, tolerance=1e-4)
  })
  
}

Try the calibrar package in your browser

Any scripts or data that you put into this service are public.

calibrar documentation built on May 29, 2024, 7:46 a.m.