Skip to content

Commit

Permalink
feat: add ties method option to best function of archive (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
be-marc authored Jan 22, 2024
1 parent 8114ae2 commit 61fa378
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Encoding: UTF-8
Language: en-US
NeedsCompilation: no
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.2.3.9000
Collate:
'Archive.R'
'ArchiveBest.R'
Expand Down
10 changes: 8 additions & 2 deletions R/Archive.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,26 @@ Archive = R6Class("Archive",
#' @param n_select (`integer(1L)`)\cr
#' Amount of points to select.
#' Ignored for multi-crit optimization.
#' @param ties_method (`character(1L)`)\cr
#' Method to break ties when multiple points have the same score.
#' Either `"first"` (default) or `"random"`.
#' Ignored for multi-crit optimization.
#' If `n_select > 1L`, the tie method is ignored and the first point is returned.
#'
#' @return [data.table::data.table()]
best = function(batch = NULL, n_select = 1L) {
best = function(batch = NULL, n_select = 1L, ties_method = "first") {
if (!self$n_batch) return(data.table())
assert_subset(batch, seq_len(self$n_batch))
assert_int(n_select, lower = 1L)
assert_choice(ties_method, c("first", "random"))

tab = if (is.null(batch)) self$data else self$data[list(batch), , on = "batch_nr"]

if (self$codomain$target_length == 1L) {
if (n_select == 1L) {
# use which_max to find the best point
y = tab[[self$cols_y]] * -self$codomain$maximization_to_minimization
ii = which_max(y, ties_method = "random")
ii = which_max(y, ties_method = ties_method)
tab[ii]
} else {
# copy table to avoid changing the order of the archive
Expand Down
8 changes: 7 additions & 1 deletion man/Archive.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 61fa378

Please sign in to comment.