Skip to content

Commit

Permalink
instances_log is now a data.table. Simplify generateInstances().
Browse files Browse the repository at this point in the history
  • Loading branch information
MLopez-Ibanez committed Jul 21, 2024
1 parent 383007e commit 7c2654b
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 50 deletions.
13 changes: 5 additions & 8 deletions R/ablation.R
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ report_duplicated_results <- function(experiments, configurations)
dups
}

ab_generate_instances <- function(scenario, nrep, type, instancesFile)
ab_generate_instances <- function(race_state, scenario, nrep, type, instancesFile)
{
nrep <- suppressWarnings(as.integer(nrep))
if (is.na(nrep) || length(nrep) == 0L || nrep <= 0L)
Expand All @@ -219,7 +219,7 @@ ab_generate_instances <- function(scenario, nrep, type, instancesFile)
scenario$instances <- readInstances(instancesFile = path_rel2abs(instancesFile))
}
n_inst <- length(scenario$instances)
instances_log <- generateInstances(scenario, n_inst * nrep)
generateInstances(race_state, scenario, n_inst * nrep)

msg <- if (instancesFile %in% c("train", "test"))
paste0("'", instancesFile, "' instances") else paste0("instances from '", instancesFile, "'")
Expand All @@ -230,7 +230,7 @@ ab_generate_instances <- function(scenario, nrep, type, instancesFile)
cat(sep="", "# Using ", msg, ":\n",
paste0(collapse="\n", scenario$instances[1L:n_inst]),
"\n")
list(instances_log=instances_log, instances=scenario$instances)
scenario$instances
}

#' Performs ablation between two configurations (from source to target).
Expand Down Expand Up @@ -309,11 +309,8 @@ ablation <- function(iraceResults, src = 1L, target = NULL,
scenario$seed <- seed
scenario <- checkScenario(scenario)
race_state <- RaceState$new(scenario)

# Generate instances
res <- ab_generate_instances(scenario, nrep, type, instancesFile)
race_state$instances_log <- res[["instances_log"]]
scenario$instances <- res[["instances"]]
scenario$instances <- ab_generate_instances(race_state, scenario, nrep, type, instancesFile)

if (is.null(target))
target <- iraceResults$iterationElites[length(iraceResults$iterationElites)]
Expand Down Expand Up @@ -362,7 +359,7 @@ ablation <- function(iraceResults, src = 1L, target = NULL,

race_state$start_parallel(scenario)
on.exit(race_state$start_parallel(scenario), add = TRUE)
target.output <- execute.experiments(race_state, experiments, scenario)
target.output <- execute_experiments(race_state, experiments, scenario)
if (!is.null(scenario$targetEvaluator))
target.output <- execute_evaluator (race_state$target_evaluator, experiments, scenario, target.output,
src_configuration)
Expand Down
30 changes: 14 additions & 16 deletions R/irace.R
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,13 @@ checkMinimumBudget <- function(scenario, remainingBudget, minSurvival, nbIterati
}

## Generate instances + seed.
generateInstances <- function(scenario, n, instances_log = NULL)
generateInstances <- function(race_state, scenario, n, update = FALSE)
{
if (!update)
race_state$instances_log <- NULL
# If we are adding and the scenario is deterministic, we have already added all instances.
if (scenario$deterministic && !is.null(instances_log))
return(instances_log)
else if (scenario$deterministic)
return(race_state$instances_log)

instances <- scenario$instances
# Number of times that we need to repeat the set of instances given by the user.
Expand All @@ -354,12 +356,10 @@ generateInstances <- function(scenario, n, instances_log = NULL)
# Sample seeds.
# 2147483647 is the maximum value for a 32-bit signed integer.
# We use replace = TRUE, because replace = FALSE allocates memory for each possible number.
# FIXME: Use data.table() and rbindlist()
instances_log <- rbind.data.frame(instances_log,
data.frame(instanceID = sindex,
seed = sample.int(2147483647L, size = length(sindex), replace = TRUE), stringsAsFactors = FALSE))
rownames(instances_log) <- NULL
instances_log
race_state$instances_log <- rbind(race_state$instances_log,
data.table(instanceID = sindex, seed = sample.int(2147483647L,
size = length(sindex), replace = TRUE)))
race_state$instances_log
}

do_experiments <- function(race_state, configurations, ninstances, scenario, iteration)
Expand Down Expand Up @@ -714,11 +714,9 @@ irace_run <- function(scenario)
minSurvival <- floor(minSurvival)

# Generate initial instance + seed list
race_state$instances_log <- generateInstances(scenario,
n = if (scenario$maxExperiments != 0)
ceiling(scenario$maxExperiments / minSurvival)
else
max(scenario$firstTest, length(scenario$instances)))
generateInstances(race_state, scenario,
n = if (scenario$maxExperiments != 0) ceiling(scenario$maxExperiments / minSurvival)
else max(scenario$firstTest, length(scenario$instances)))
indexIteration <- 1L
timeUsed <- 0
boundEstimate <- NA
Expand Down Expand Up @@ -1159,8 +1157,8 @@ irace_run <- function(scenario)
# Calculate budget needed for old instances assuming non elitist irace.
if ((nrow(race_state$instances_log) - (race_state$next_instance - 1L))
< ceiling(remainingBudget / minSurvival)) {
race_state$instances_log <- generateInstances(scenario, n = ceiling(remainingBudget / minSurvival),
instances_log = race_state$instances_log)
generateInstances(race_state, scenario, n = ceiling(remainingBudget / minSurvival),
update = TRUE)
}

if (debugLevel >= 1L) irace.note("Launch race\n")
Expand Down
2 changes: 1 addition & 1 deletion R/main.R
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ checkTargetFiles <- function(scenario)
cat("# Executing targetRunner (", nrow(configurations), "times)...\n")
result <- TRUE
output <- withCallingHandlers(
tryCatch(execute.experiments(race_state, experiments, scenario),
tryCatch(execute_experiments(race_state, experiments, scenario),
error = function(e) {
cat(sep = "\n",
"\n# Error occurred while executing targetRunner:",
Expand Down
2 changes: 1 addition & 1 deletion R/parameterAnalysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ get_configuration_by_id_helper <- function(allConfigurations, ids, drop_metadata
#' @param index (`integer()`)\cr Indexes of the (instanceID,seed) pairs to be returned. The default returns everything.
#' @param instances (`logical(1)`)\cr Whether to add the actual instances as an additional column (only if the instances are of atomic type).
#'
#' @return With default arguments, a data frame containing two columns
#' @return `data.table()`\cr With default arguments, a `data.table` containing two columns
#' `"instanceID"` and `"seed"`. With `instances=TRUE` and if the instances
#' are of atomic type (see [is.atomic()]) type, another column `instance` is
#' added that contains the actual instance.
Expand Down
3 changes: 1 addition & 2 deletions R/psRace.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ psRace <- function(iraceResults, max_experiments, conf_ids = NULL, iteration_eli
elite_configurations <- iraceResults$allConfigurations[iraceResults$allConfigurations[[".ID."]] %in% conf_ids, , drop=FALSE]

# Generate new instances.
race_state$instances_log <- generateInstances(scenario, max_experiments / nrow(elite_configurations),
instances_log = race_state$instances_log)
generateInstances(race_state, scenario, max_experiments / nrow(elite_configurations), update = TRUE)
elite_data <- if (scenario$elitist)
iraceResults$experiments[, as.character(elite_configurations[[".ID."]]), drop=FALSE] else NULL
race_state$next_instance <- nrow(iraceResults$experiments) + 1L
Expand Down
2 changes: 1 addition & 1 deletion R/race-wrapper.R
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ target_runner_default <- function(experiment, scenario)
call = paste(cmd, args, collapse = " "))
}

execute.experiments <- function(race_state, experiments, scenario)
execute_experiments <- function(race_state, experiments, scenario)
{
parallel <- scenario$parallel
mpi <- scenario$mpi
Expand Down
2 changes: 1 addition & 1 deletion R/race.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ race_wrapper <- function(race_state, configurations, instance_idx, bounds = NULL
# from one to the other.
which_exps <- which(which_alive %in% which_exe)
irace.assert(length(which_exps) == length(which_exe))
target_output[which_exps] <- execute.experiments(race_state, experiments[which_exps], scenario)
target_output[which_exps] <- execute_experiments(race_state, experiments[which_exps], scenario)
}

# targetEvaluator may be NULL. If so, target_output must contain the right
Expand Down
14 changes: 7 additions & 7 deletions R/testing.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,25 @@ testConfigurations <- function(configurations, scenario)
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")
irace.note ("Memory used before execute_experiments() in testConfigurations():\n")
race_state$print_mem_used()
}
race_state$start_parallel(scenario)
on.exit(race_state$stop_parallel())
target.output <- execute.experiments(race_state, experiments, scenario)
# targetEvaluator may be NULL. If so, target.output must contain the right
target_output <- execute_experiments(race_state, experiments, scenario)
# targetEvaluator may be NULL. If so, target_output must contain the right
# output already.
if (!is.null(scenario$targetEvaluator))
target.output <- execute_evaluator(race_state$target_evaluator, experiments,
scenario, target.output, configurations[[".ID."]])
target_output <- execute_evaluator(race_state$target_evaluator, experiments,
scenario, target_output, configurations[[".ID."]])

# FIXME: It would be much faster to get convert target.output$cost to a
# FIXME: It would be much faster to get convert target_output$cost to a
# vector, then initialize the matrix with the vector.
testResults <- matrix(NA, ncol = nrow(configurations), nrow = length(testInstances),
# dimnames = list(rownames, colnames)
dimnames = list(instances_id, configurations$.ID.))

cost <- sapply(target.output, getElement, "cost")
cost <- sapply(target_output, getElement, "cost")
if (scenario$capping)
cost <- applyPAR(cost, boundMax = scenario$boundMax, boundPar = scenario$boundPar)
# FIXME: Vectorize this loop
Expand Down
2 changes: 1 addition & 1 deletion man/get_instanceID_seed_pairs.Rd

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

18 changes: 9 additions & 9 deletions tests/testthat/test-GenericWrapper4AC.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test_that("GenericWrapper4AC", {
instances_ID = rep("cost", 2),
seeds = 0:1)
race_state <- irace:::RaceState$new(scenario)
output <- irace:::execute.experiments(race_state, experiments, scenario)
output <- irace:::execute_experiments(race_state, experiments, scenario)
expect_equal(output[[1]]$status, "SUCCESS")
expect_equal(output[[1]]$cost, 0.5)
expect_true(is.null(output[[1]]$error))
Expand All @@ -39,14 +39,14 @@ test_that("GenericWrapper4AC", {
instances = scenario$instances,
instances_ID = rep("cost", 2),
seeds = 2)
expect_error(irace:::execute.experiments(race_state, experiments, scenario), "CRASHED")
expect_error(irace:::execute_experiments(race_state, experiments, scenario), "CRASHED")

experiments <- irace:::createExperimentList(configurations,
parameters = parameters,
instances = scenario$instances,
instances_ID = rep("cost", 2),
seeds = 3)
expect_error(irace:::execute.experiments(race_state, experiments, scenario), "ABORT")
expect_error(irace:::execute_experiments(race_state, experiments, scenario), "ABORT")


scenario <- modifyList(scenario,
Expand All @@ -59,7 +59,7 @@ test_that("GenericWrapper4AC", {
instances_ID = rep("time", 2),
seeds = 0:1,
bounds = scenario$boundMax)
output <- irace:::execute.experiments(race_state, experiments, scenario)
output <- irace:::execute_experiments(race_state, experiments, scenario)

expect_equal(output[[1]]$status, "SUCCESS")
expect_equal(output[[1]]$cost, 0.8)
Expand All @@ -78,23 +78,23 @@ test_that("GenericWrapper4AC", {
instances_ID = rep("time", 2),
seeds = 2,
bounds = scenario$boundMax)
expect_error(irace:::execute.experiments(race_state, experiments, scenario), "CRASHED")
expect_error(irace:::execute_experiments(race_state, experiments, scenario), "CRASHED")

experiments <- irace:::createExperimentList(configurations,
parameters = parameters,
instances = scenario$instances,
instances_ID = rep("time", 2),
seeds = 3,
bounds = scenario$boundMax)
expect_error(irace:::execute.experiments(race_state, experiments, scenario), "ABORT")
expect_error(irace:::execute_experiments(race_state, experiments, scenario), "ABORT")

experiments <- irace:::createExperimentList(configurations,
parameters = parameters,
instances = scenario$instances,
instances_ID = rep("cost+time", 2),
seeds = 0:1,
bounds = scenario$boundMax)
output <- irace:::execute.experiments(race_state, experiments, scenario)
output <- irace:::execute_experiments(race_state, experiments, scenario)

expect_equal(output[[1]]$status, "SUCCESS")
expect_equal(output[[1]]$cost, 0.5)
Expand All @@ -113,13 +113,13 @@ test_that("GenericWrapper4AC", {
instances_ID = rep("cost+time", 2),
seeds = 2,
bounds = scenario$boundMax)
expect_error(irace:::execute.experiments(race_state, experiments, scenario), "CRASHED")
expect_error(irace:::execute_experiments(race_state, experiments, scenario), "CRASHED")

experiments <- irace:::createExperimentList(configurations,
parameters = parameters,
instances = scenario$instances,
instances_ID = rep("cost+time", 2),
seeds = 3,
bounds = scenario$boundMax)
expect_error(irace:::execute.experiments(race_state, experiments, scenario), "ABORT")
expect_error(irace:::execute_experiments(race_state, experiments, scenario), "ABORT")
})
2 changes: 1 addition & 1 deletion tests/testthat/test-blocksize.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ time_irace <- function(...)

check_blocksize <- function(results)
{
expect_equal(rowMeans(matrix(get_instanceID_seed_pairs(results)[,"instanceID"],nrow=results$scenario$blockSize)),
expect_equal(rowMeans(matrix(get_instanceID_seed_pairs(results)[["instanceID"]],nrow=results$scenario$blockSize)),
rowMeans(matrix(seq_along(results$scenario$instances), nrow=results$scenario$blockSize)))
expect_equal(sum(colSums(!is.na(results$experiments)) %% results$scenario$blockSize), 0)
}
Expand Down
4 changes: 2 additions & 2 deletions vignettes/irace-package.Rnw
Original file line number Diff line number Diff line change
Expand Up @@ -2064,9 +2064,9 @@ pair_index <- which(apply(!is.na(all_exp), 1L, all))
# but in this example we get the indexes of the instances executed for
# the best configuration.
pair_index <- which(!is.na(all_exp[, best_id]))
instanceID <- get_instanceID_seed_pairs(iraceResults)[pair_index, "instanceID"]
instanceID <- get_instanceID_seed_pairs(iraceResults)[["instanceID"]][pair_index]
# or get the seeds
get_instanceID_seed_pairs(iraceResults)[pair_index, "seed"]
get_instanceID_seed_pairs(iraceResults)[["seed"]][pair_index]
# or obtain the actual instances.
iraceResults$scenario$instances[instanceID]
# If the instances are of atomic type (integers, floating-point numbers or
Expand Down

0 comments on commit 7c2654b

Please sign in to comment.