Skip to content

Commit

Permalink
Use data.table to speed up sampleUniform and sampleModel.
Browse files Browse the repository at this point in the history
 * DESCRIPTION: Import data.table
 * NEWS.md: Document it.
 * R/configurations.R: New file.
 * R/generation.R (sampleUniform,sampleModel): Use data.table.
 * tests/testthat/bug-13-lookup.rds, tests/testthat/test-bug-13.R: Delete
   because it depends on the previous sampling.
 * R/irace.R: setDTthreads according to parallel.
 * tests/testthat/test-repair.R: Improve tests.
  • Loading branch information
MLopez-Ibanez committed Feb 22, 2024
1 parent 4364898 commit d1e639a
Show file tree
Hide file tree
Showing 16 changed files with 575 additions and 456 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Imports:
utils,
compiler,
fs,
data.table (>= 1.15.0),
matrixStats,
R6,
withr
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export(testConfigurations)
export(testing_fromfile)
export(testing_fromlog)
import(compiler)
import(data.table)
import(matrixStats)
import(stats)
import(utils)
Expand Down
11 changes: 7 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
and will give an error if present. Forbidden configurations are now
specified in the parameter space description. See the example in
`readParameters()`.

* The command-line executables `irace` and `ablation` (`irace.exe` and
`ablation.exe` in Windows) will load the version of the `irace` package that
is found in the same path where the executables are. In earlier versions,
Expand All @@ -37,9 +37,10 @@
a different version of irace, since such attempts typically end up in errors
that are difficult to understand.

* irace warns about using `'&&'` and `'||'` instead of `'&'` and `'|'` in parameter conditions.
A future version of irace will reject those uses as errors.

* irace warns about using `'&&'` and `'||'` instead of `'&'` and `'|'` in
parameter conditions and forbidden expressions. A future version of irace
will reject those uses as errors.

* The internal function `irace.reload.debug()` has been removed.
Use `devtools::reload()` instead.

Expand All @@ -61,6 +62,8 @@

## New features and improvements

* `sampleUniform()` and `sampleModel()` are significantly faster thanks to using [`data.table`](https://r-datatable.com).

* Ablation will report configurations that produced the same results, which
points to parameter values that have the same effect on the target algorithm,
possibly indicating a bug in the target algorithm.
Expand Down
56 changes: 56 additions & 0 deletions R/configurations.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Returns a data.table
configurations_alloc <- function(colnames, nrow, parameters)
{
parameter_type <- function(type) {
stopifnot(type %in% c("i","r","o","c"))
switch(type,
i = NA_integer_,
r = NA_real_,
c = NA_character_,
o = NA_character_,
irace.internal.error("Unknown type '", type, "'"))
}

column_type <- function(x, n, types)
rep(switch(x,
.ID. = NA_integer_,
.PARENT. = NA_integer_,
.WEIGHT. = NA_real_,
parameter_type(types[x])), n)

x <- sapply(colnames, column_type, n=nrow, types = parameters$types,
simplify=FALSE, USE.NAMES=TRUE)
setDT(x)
x
}

# FIXME: It may be faster to create a single expression that concatenates all
# the elements of forbidden using '|'
filter_forbidden <- function(configurations, forbidden)
{
# We have to use a variable name that will never appear in
# configurations, so .FORBIDDEN
for (.FORBIDDEN in forbidden) {
configurations <- configurations[eval(.FORBIDDEN)]
#print(configurations)
#print(str(configurations))
if (nrow(configurations) == 0L) return(configurations)
}
#print(nrow(configurations))
configurations
}

which_satisfied <- function(configurations, condition)
{
# If there is no condition, do not waste time evaluating it.
if (isTRUE(condition))
return(seq_len(nrow(configurations)))
r <- eval(condition, configurations)
# Return TRUE if TRUE, FALSE if FALSE or NA
## FIXME: If we byte-compile the condition, then we should incorporate the
## following into the condition directly.
# r & !is.na(r)
# Return indexes where r is TRUE.
which(r)
}

Loading

0 comments on commit d1e639a

Please sign in to comment.