From 30c8d4702960f76b31cdf4bf82c66082ab23934b Mon Sep 17 00:00:00 2001 From: MLopez-Ibanez <2620021+MLopez-Ibanez@users.noreply.github.com> Date: Tue, 10 Dec 2024 10:04:38 +0000 Subject: [PATCH] * R/: Cannot use rep.int(maxBound) because of NULL. Use rep_len where appropriate. --- R/irace.R | 6 +++--- R/model.R | 4 ++-- R/race.R | 14 +++++++------- R/testing.R | 5 +++-- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/R/irace.R b/R/irace.R index 77bee0fa..d38908e7 100644 --- a/R/irace.R +++ b/R/irace.R @@ -49,7 +49,7 @@ recoverFromFile <- function(filename) # FIXME: This function is too slow and it shows up in profiles. numeric.configurations.equal <- function(x, configurations, parameters, threshold, param.names) { - d <- rep(0.0, nrow(configurations)) + d <- numeric(nrow(configurations)) isSimilar <- matrix(TRUE, nrow = nrow(configurations), ncol = length(param.names)) selected <- seq_nrow(configurations) for (i in seq_along(param.names)) { @@ -348,10 +348,10 @@ generateInstances <- function(race_state, scenario, n, update = FALSE) blockSize <- scenario$blockSize n_blocks <- length(instances) / blockSize # Sample instances index in groups (ntimes) - selected_blocks <- unlist(lapply(rep(n_blocks, ntimes), sample.int, replace = FALSE)) + selected_blocks <- unlist(lapply(rep.int(n_blocks, ntimes), sample.int, replace = FALSE)) sindex <- c(outer(seq_len(blockSize), (selected_blocks - 1L) * blockSize, "+")) } else { - sindex <- rep(seq_along(instances), ntimes) + sindex <- rep.int(seq_along(instances), ntimes) } # Sample seeds. race_state$instances_log <- rbind(race_state$instances_log, diff --git a/R/model.R b/R/model.R index e1caf1a8..5da07d77 100644 --- a/R/model.R +++ b/R/model.R @@ -35,8 +35,8 @@ initialiseModel <- function (parameters, configurations) type <- param[["type"]] if (type == "c") { nbValues <- length(param[["domain"]]) - value <- rep(1. / nbValues, nbValues) - param <- rep(list(value), nbConfigurations) + value <- rep_len(1. / nbValues, nbValues) + param <- rep_len(list(value), nbConfigurations) } else { if (type == "r" || type == "i") { sd <- init_sd_numeric(param) diff --git a/R/race.R b/R/race.R index 7bec6d15..8d9a33fd 100644 --- a/R/race.R +++ b/R/race.R @@ -500,7 +500,7 @@ overall_ranks <- function(x, test) ninstances <- colSums2(!is.na(x)) uniq.ninstances <- sort(unique(ninstances), decreasing = TRUE) last.r <- 0L - ranks <- rep(Inf, ncol(x)) + ranks <- rep_len(Inf, ncol(x)) # Iterate from the largest to the lowest number of instances. for (k in uniq.ninstances) { confs <- which(ninstances == k) @@ -580,8 +580,8 @@ elitist_race <- function(race_state, maxExp, capping <- scenario$capping n_configurations <- nrow(configurations) experiment_log <- data.table(instance=integer(0), configuration=integer(0), time=numeric(0), bound=numeric(0)) - alive <- rep(TRUE, n_configurations) - is_rejected <- rep(FALSE, n_configurations) + alive <- rep_len(TRUE, n_configurations) + is_rejected <- logical(n_configurations) ## FIXME: Remove argument checking. This must have been done by the caller. irace.assert(maxExp > 0L) @@ -622,7 +622,7 @@ elitist_race <- function(race_state, maxExp, experiments_used <- 0L # is_elite[i] : number of instances to be seen in this race on which i has # been previously evaluated. - is_elite <- rep(0L, n_configurations) + is_elite <- integer(n_configurations) ## FIXME: Probably, instead of this, we should keep elite_safe in the race_state. if (is.null(elite.data)) { @@ -673,7 +673,7 @@ elitist_race <- function(race_state, maxExp, irace.assert(elitist_new_instances %% blockSize == 0L) # FIXME: This should go into its own function. n_elite <- ncol(elite.data) - which_elites <- which(rep(TRUE, n_elite)) + which_elites <- which(rep_len(TRUE, n_elite)) irace.note("Preliminary execution of ", n_elite, " elite configuration(s) over ", elitist_new_instances, " instance(s).\n") for (k in seq_len(elitist_new_instances)) { @@ -868,10 +868,10 @@ elitist_race <- function(race_state, maxExp, if (nrow(Results) < current_task) { - Results <- rbind(Results, rep(NA_real_, ncol(Results))) + Results <- rbind(Results, rep_len(NA_real_, ncol(Results))) rownames(Results) <- race_instances[seq_nrow(Results)] if (capping) { - experimentsTime <- rbind(experimentsTime, rep(NA_real_, ncol(experimentsTime))) + experimentsTime <- rbind(experimentsTime, rep_len(NA_real_, ncol(experimentsTime))) rownames(experimentsTime) <- race_instances[seq_nrow(experimentsTime)] } } diff --git a/R/testing.R b/R/testing.R index 7d016180..37a30714 100644 --- a/R/testing.R +++ b/R/testing.R @@ -41,8 +41,9 @@ testConfigurations <- function(configurations, scenario) # Create experiment list experiments <- createExperimentList(configurations, parameters = scenario$parameters, - instances = testInstances, instances_ID = instances_id, seeds = instanceSeed, - bounds = rep.int(scenario$boundMax, nrow(configurations))) + instances = testInstances, instances_ID = instances_id, seeds = instanceSeed, + # We cannot use rep.int because scenario$boundMax may be NULL. + bounds = rep(scenario$boundMax, nrow(configurations))) race_state <- RaceState$new(scenario) if (scenario$debugLevel >= 3L) { irace.note ("Memory used before execute_experiments() in testConfigurations():\n")