From 50a9b44dac133e6d3250760fcebc42a92cb49590 Mon Sep 17 00:00:00 2001 From: MLopez-Ibanez <2620021+MLopez-Ibanez@users.noreply.github.com> Date: Tue, 23 Jul 2024 19:26:08 +0100 Subject: [PATCH] Use data.table in extractElites. --- R/irace.R | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/R/irace.R b/R/irace.R index f1e2e62e..45fef379 100644 --- a/R/irace.R +++ b/R/irace.R @@ -423,7 +423,40 @@ allConfigurationsInit <- function(scenario) allConfigurations } -extractElites <- function(elites, nbElites, debugLevel) +## extractElites +# Input: the configurations with the .RANK. field filled. +# the number of elites wished +# Output: nbElites elites, sorted by ranks, with the weights assigned. +extractElites <- function(configurations, nbElites, debugLevel) +{ + irace.assert(nbElites > 0L) + # Keep only alive configurations. + elites <- as.data.table(configurations) + before <- nrow(elites) + # Remove duplicated. Duplicated configurations may be generated, however, it + # is too slow to check at generation time. Nevertheless, we can check now + # since we typically have very few elites. + elites <- unique(elites, by=which(!startsWith(colnames(elites), "."))) + after <- nrow(elites) + if (debugLevel >= 2L && after < before) + irace.note("Dropped ", before - after, " duplicated elites.\n") + + after <- min(after, nbElites) + .RANK. <- NULL + setorder(elites, .RANK.) + selected <- seq_len(after) + elites <- elites[selected, ] + set(elites, j = ".WEIGHT.", value = ((after + 1L) - selected) / (after * (after + 1L) / 2)) + setDF(elites) + rownames(elites) <- elites[[".ID."]] + print(elites) + old <- old_extractElites(configurations, nbElites, debugLevel) + print(old) + testthat::expect_identical(elites, old) + elites +} + +old_extractElites <- function(elites, nbElites, debugLevel) { irace.assert(nbElites > 0L) # Remove duplicated. Duplicated configurations may be generated, however, it