Skip to content

Commit

Permalink
Merge branch 'main' into focussearch
Browse files Browse the repository at this point in the history
  • Loading branch information
sumny committed Jul 12, 2021
2 parents 226a85b + b0f2ae8 commit aaa801b
Show file tree
Hide file tree
Showing 49 changed files with 1,081 additions and 461 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
^Meta$
^cran-comments\.md$
^CRAN-RELEASE$
^README\.Rmd$
26 changes: 18 additions & 8 deletions .github/workflows/tic.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## tic GitHub Actions template: linux-macos-windows-deploy
## revision date: 2020-12-11
## revision date: 2021-06-27
# [Custom header]
on:
workflow_dispatch:
Expand Down Expand Up @@ -45,7 +45,7 @@ jobs:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v2.3.4
- uses: actions/checkout@v2

- uses: r-lib/actions/setup-r@master
with:
Expand All @@ -71,22 +71,22 @@ jobs:

- name: "[Cache] Cache R packages"
if: runner.os != 'Windows'
uses: pat-s/always-upload-cache@v2.1.3
uses: pat-s/always-upload-cache@v2
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-r-${{ matrix.config.r }}-${{steps.date.outputs.date}}
restore-keys: ${{ runner.os }}-r-${{ matrix.config.r }}-${{steps.date.outputs.date}}

- name: "[Stage] [Linux] Install required system libs"
if: runner.os == 'Linux'
run: sudo apt install libcurl4-openssl-dev libgit2-dev

# for some strange Windows reason this step and the next one need to be decoupled
- name: "[Stage] Prepare"
run: |
Rscript -e "if (!requireNamespace('remotes')) install.packages('remotes', type = 'source')"
Rscript -e "if (getRversion() < '3.2' && !requireNamespace('curl')) install.packages('curl', type = 'source')"
- name: "[Stage] [Linux] Install curl and libgit2"
if: runner.os == 'Linux'
run: sudo apt install libcurl4-openssl-dev libgit2-dev

- name: "[Stage] [macOS] Install libgit2"
if: runner.os == 'macOS'
run: brew install libgit2
Expand All @@ -99,6 +99,16 @@ jobs:
if: runner.os == 'Linux' && matrix.config.pkgdown != ''
run: sudo apt install libharfbuzz-dev libfribidi-dev

# Try to automatically check for system dependencies and install them
# Note: this might not catch all required system libs and manual action might be needed
- name: "[Stage] [Linux] Install linux system dependencies"
if: runner.os == 'Linux'
run: |
while read -r cmd
do
eval sudo $cmd
done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))')
- name: "[Stage] Install"
if: matrix.config.os != 'macOS-latest' || matrix.config.r != 'devel'
run: Rscript -e "remotes::install_github('ropensci/tic')" -e "print(tic::dsl_load())" -e "tic::prepare_all_stages()" -e "tic::before_install()" -e "tic::install()"
Expand All @@ -119,7 +129,7 @@ jobs:

- name: "[Stage] Upload R CMD check artifacts"
if: failure()
uses: actions/upload-artifact@v2.2.1
uses: actions/upload-artifact@v2
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: check
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/update-tic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@master
- uses: actions/checkout@v2
with:
persist-credentials: false

Expand All @@ -43,7 +43,7 @@ jobs:
Rscript -e "tic::update_yml()"
- name: "[Stage] Create Pull Request"
uses: peter-evans/create-pull-request@master
uses: peter-evans/create-pull-request@v3
with:
author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
token: ${{ secrets.TIC_UPDATE }}
Expand Down
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ License: LGPL-3
URL: https://bbotk.mlr-org.com, https://github.com/mlr-org/bbotk
BugReports: https://github.com/mlr-org/bbotk/issues
Depends:
paradox (>= 0.7.0),
R (>= 3.1.0)
Imports:
checkmate (>= 2.0.0),
data.table,
lgr,
methods,
mlr3misc (>= 0.7.0),
paradox (>= 0.7.0),
R6
Suggests:
adagio,
Expand Down Expand Up @@ -94,6 +94,7 @@ Collate:
'TerminatorStagnation.R'
'TerminatorStagnationBatch.R'
'assertions.R'
'bb_optimize.R'
'bbotk_reflections.R'
'bibentries.R'
'helper.R'
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Generated by roxygen2: do not edit by hand

S3method(as.data.table,Archive)
S3method(bb_optimize,"function")
S3method(bb_optimize,Objective)
export(Archive)
export(ArchiveBest)
export(Objective)
Expand Down Expand Up @@ -33,6 +35,7 @@ export(assert_set)
export(assert_terminable)
export(assert_terminator)
export(assign_result_default)
export(bb_optimize)
export(bbotk_reflections)
export(is_dominated)
export(mlr_optimizers)
Expand Down
4 changes: 1 addition & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# bbotk 0.3.2.9000

- Same as previous version.

- Adds `bb_optimize()` function for quick optimization.

# bbotk 0.3.2

Expand Down Expand Up @@ -43,4 +42,3 @@
# bbotk 0.2

* First version of the Black-Box Optimization Toolkit

3 changes: 1 addition & 2 deletions R/Archive.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ Archive = R6Class("Archive",
} else {
assert_integerish(batch, lower = 1L, upper = self$n_batch, coerce = TRUE)
}
batch_nr = NULL # CRAN check
tab = self$data[batch_nr %in% batch]
tab = self$data[get("batch_nr") %in% batch, ]

max_to_min = mult_max_to_min(self$codomain)
if (self$codomain$length == 1L) {
Expand Down
16 changes: 9 additions & 7 deletions R/Objective.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ Objective = R6Class("Objective",
codomain = NULL,

#' @field constants ([paradox::ParamSet]).\cr
#' Changeable constants or parameters that are not subject to tuning can be stored and accessed here.
#' Changeable constants or parameters that are not subject to tuning can be
#' stored and accessed here. Set constant values are passed to `$.eval()`
#' and `$.eval_many()` as named arguments.
constants = NULL,

#' @field check_values (`logical(1)`)\cr
Expand All @@ -45,8 +47,8 @@ Objective = R6Class("Objective",
#' @param id (`character(1)`).
#' @param properties (`character()`).
initialize = function(id = "f", properties = character(), domain,
codomain = ParamSet$new(list(ParamDbl$new("y", tags = "minimize"))),
constants = ParamSet$new(), check_values = TRUE) {
codomain = ps(y = p_dbl(tags = "minimize")),
constants = ps(), check_values = TRUE) {
self$id = assert_string(id)
self$domain = assert_param_set(domain)
self$codomain = assert_codomain(codomain)
Expand Down Expand Up @@ -94,7 +96,7 @@ Objective = R6Class("Objective",
#' These extra entries are referred to as *extras*.
eval = function(xs) {
if (self$check_values) self$domain$assert(xs)
res = private$.eval(xs)
res = invoke(private$.eval, xs, .args = self$constants$values)
if (self$check_values) self$codomain$assert(res[self$codomain$ids()])
return(res)
},
Expand All @@ -119,7 +121,7 @@ Objective = R6Class("Objective",
#' These extra columns are referred to as *extras*.
eval_many = function(xss) {
if (self$check_values) lapply(xss, self$domain$assert)
res = private$.eval_many(xss)
res = invoke(private$.eval_many, xss, .args = self$constants$values)
if (self$check_values) {
self$codomain$assert_dt(res[, self$codomain$ids(), with = FALSE])
}
Expand Down Expand Up @@ -148,11 +150,11 @@ Objective = R6Class("Objective",
),

private = list(
.eval = function(xs) {
.eval = function(xs, ...) { # ... allows constants
as.list(self$eval_many(list(xs)))
},

.eval_many = function(xss) {
.eval_many = function(xss, ...) {
res = map_dtr(xss, function(xs) {
ys = self$eval(xs)
as.data.table(lapply(ys, function(y) if (is.list(y)) list(y) else y))
Expand Down
45 changes: 21 additions & 24 deletions R/ObjectiveRFun.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,35 @@
#'
#' @description
#' Objective interface where the user can pass a custom R function that expects a list as input.
#'
#' @template param_domain
#' @template param_codomain
#' @template param_check_values
#' @template param_constants
#'
#' @export
#' @examples
#' library(paradox)
#' # Define objective function
#' # define objective function
#' fun = function(xs) {
#' - (xs[[1]] - 2)^2 - (xs[[2]] + 3)^2 + 10
#' }
#'
#' # Set domain
#' domain = ParamSet$new(list(
#' ParamDbl$new("x1", -10, 10),
#' ParamDbl$new("x2", -5, 5)
#' ))
#'
#' # Set codomain
#' codomain = ParamSet$new(list(
#' ParamDbl$new("y", tags = "maximize")
#' ))
#'
#' # Create Objective object
#'
#' # set domain
#' domain = ps(
#' x1 = p_dbl(-10, 10),
#' x2 = p_dbl(-5, 5)
#' )
#'
#' # set codomain
#' codomain = ps(y = p_dbl(tags = "maximize"))
#'
#' # create Objective object
#' obfun = ObjectiveRFun$new(
#' fun = fun,
#' domain = domain,
#' codomain = codomain,
#' properties = "deterministic"
#' )
#'
#' @template param_domain
#' @template param_codomain
#' @template param_check_values
#' @template param_constants
#' @export
ObjectiveRFun = R6Class("ObjectiveRFun",
inherit = Objective,
public = list(
Expand All @@ -48,9 +45,9 @@ ObjectiveRFun = R6Class("ObjectiveRFun",
#' @param id (`character(1)`).
#' @param properties (`character()`).
initialize = function(fun, domain, codomain = NULL, id = "function",
properties = character(), constants = ParamSet$new(), check_values = TRUE) {
properties = character(), constants = ps(), check_values = TRUE) {
if (is.null(codomain)) {
codomain = ParamSet$new(list(ParamDbl$new("y", tags = "minimize")))
codomain = ps(y = p_dbl(tags = "minimize"))
}
private$.fun = assert_function(fun, "xs")
# asserts id, domain, codomain, properties
Expand All @@ -64,7 +61,7 @@ ObjectiveRFun = R6Class("ObjectiveRFun",
#' @param xs Input values.
eval = function(xs) {
if (self$check_values) self$domain$assert(xs)
res = private$.fun(xs)
res = invoke(private$.fun, xs, .args = self$constants$values)
if (self$check_values) self$codomain$assert(as.list(res)[self$codomain$ids()])
return(res)
}
Expand Down
8 changes: 4 additions & 4 deletions R/ObjectiveRFunDt.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ ObjectiveRFunDt = R6Class("ObjectiveRFunDt",
#' @param id (`character(1)`).
#' @param properties (`character()`).
initialize = function(fun, domain, codomain = NULL, id = "function",
properties = character(), constants = ParamSet$new(), check_values = TRUE) {
properties = character(), constants = ps(), check_values = TRUE) {
if (is.null(codomain)) {
codomain = ParamSet$new(list(ParamDbl$new("y", tags = "minimize")))
codomain = ps(y = p_dbl(tags = "minimize"))
}
private$.fun = assert_function(fun, "xdt")
# asserts id, domain, codomain, properties
Expand All @@ -45,7 +45,7 @@ ObjectiveRFunDt = R6Class("ObjectiveRFunDt",
#' `data.table(y = 1:2)` or `data.table(y1 = 1:2, y2 = 3:4)`.
eval_many = function(xss) {
if (self$check_values) lapply(xss, self$domain$assert)
res = private$.fun(rbindlist(xss, use.names = TRUE, fill = TRUE))
res = invoke(private$.fun, rbindlist(xss, use.names = TRUE, fill = TRUE), .args = self$constants$values)
if (self$check_values) self$codomain$assert_dt(res[, self$codomain$ids(), with = FALSE])
return(res)
},
Expand All @@ -58,7 +58,7 @@ ObjectiveRFunDt = R6Class("ObjectiveRFunDt",
#' `data.table(y = 1:2)` or `data.table(y1 = 1:2, y2 = 3:4)`.
eval_dt = function(xdt) {
if (self$check_values) self$domain$assert_dt(xdt)
res = private$.fun(xdt)
res = invoke(private$.fun, xdt, .args = self$constants$values)
if (self$check_values) self$codomain$assert_dt(res[, self$codomain$ids(), with = FALSE])
return(res)
}
Expand Down
Loading

0 comments on commit aaa801b

Please sign in to comment.