Skip to content

Commit

Permalink
Use data.table in extractElites.
Browse files Browse the repository at this point in the history
  • Loading branch information
MLopez-Ibanez committed Jul 23, 2024
1 parent 7c2654b commit 50a9b44
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion R/irace.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 50a9b44

Please sign in to comment.