R/run.R

Defines functions do_method do_benchmark

Documented in do_benchmark do_method

library(tidyverse)
library(magrittr)
library(furrr)
library(cec2017)


BENCHMARK <- cec2017::cec2017
BOUNDS <- c(-100, 100)
CONTROL <- list("Lamarckism" = FALSE, "diag.bestVal" = TRUE)
RECTIMES <- c(0.01, 0.02, 0.03, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0)


#' Run method
#'
#' @description 
#' `do_method` run method specified by user in config file on given benchmark i.e. set of
#' functions.
#'
#' @param .func algorithm to run :: function
#' @param .dim dimension of function (i.e. 2, 3, 100, etc.) :: integer
#' @param .pnum number of problem in functions set :: integer
#' @param .rep number of repetitions of benchmark :: integer
#' @param .bench name of benchmark :: function
#' @param .bounds lower and upper bounds for domain :: list
#' @param .control specific algorithm params :: list
#'
#' @examples
#' do_method(cma_es, 2, 5, 10, cec2017::cec2017, c(-100, 100))
#' @export

do_method <- function(.func, .dim, .pnum, .rep, .bench = BENCHMARK, .bounds = BOUNDS, .control = CONTROL) {
  result <- .func(
    rep(0, .dim),
    fn = function(x) {
      .bench(.pnum, x)
    },
    lower = .bounds[1],
    upper = .bounds[2],
    control = .control
  )
  data.table(result = list(result), dim = .dim, pnum = .pnum, rep = .rep)
}


#' Run benchmark
#' 
#' @description
#' `do_benchmark` runs benchmark function on given algorithm. Function wrapps `do_method`.
#'
#' @param .alg algorithm to run :: function
#' @param .rep number of repetitions of benchmark :: integer
#' @param .dims dimension of function (i.e. 2, 3, 100, etc.) :: integer
#' @param .pnums number of problem in functions set :: integer
#' 
#' @examples
#' do_benchmark(cma_es, c(2, 10, 30), 1:30, 50)
#' @export

do_benchmark <- function(.alg, .dims, .pnums, .rep) {
  cat(stringr::str_interp("Dimensions: ${.dims}\nProblems: ${.pnums}\nRepeated: ${.rep}\n"))
  expand.grid(dim = .dims, pnum = .pnums, rep = 1:.rep) %>%
    furrr::future_pmap_dfr(function(dim, pnum, rep) {
      do_method(.func = .alg, .dim = dim, .pnum = pnum, .rep = rep)
    })
}
warbarbye/cecs-benchmark documentation built on March 10, 2020, 12:09 a.m.