Skip to content

Commit

Permalink
* R/parameters.R: Optimize conditions (#70).
Browse files Browse the repository at this point in the history
  • Loading branch information
MLopez-Ibanez committed Dec 12, 2024
1 parent 0145cbc commit 38b59f7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

## New features and improvements

* Conditions that are always true because they depend on fixed parameters are
replaced by a `TRUE` value to speed up their evaluation. This speed-up is only
measurable with very large and complicated parameter space.

* The options `src` and `target` of `ablation()` (or `--src` and `--target` of
the command-line `ablation` tool) now accept a character string that points
to a file containing a configuration that will be read with
Expand Down
10 changes: 10 additions & 0 deletions R/parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ ParameterSpace <- R6::R6Class("ParameterSpace", cloneable = TRUE, lock_class = T
self$forbidden <- sapply(forbidden, compile_forbidden)
}

# Optimize always TRUE conditions.
cond_names <- names(which(!unlist(lapply(self$conditions, is.logical))))
for (p in cond_names) {
deps <- self$depends[[p]]
# p depends on deps and they are both fixed and always active.
if (all(self$isFixed[deps]) && all(sapply(self$conditions[deps], isTRUE, USE.NAMES=FALSE))) {
self$conditions[[p]] <- eval(self$conditions[[p]], envir = self$domains[deps], enclos = NULL)
}
}

# Print the hierarchy vector:
if (verbose >= 1L) {
cat ("# --- Parameters Hierarchy ---\n")
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-readParameters.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
withr::with_output_sink("test-readParameters.Rout", {

test_that("optimize conditions", {
p <- readParameters(text='
ROOT "" c ("S")
ROOT_T "" c ("FIC") | ROOT %in% c("S")
ROOT_T_FIC.sum "" r (-50.0, 50.0) | ROOT_T %in% c("FIC")
ROOT_T.FFI "" c ("true") | ROOT_T == "FIC"
ROOT_T.im "" c ("FFI", "FIC") | ROOT == "S" & ROOT_T.FFI == "true"
ROOT_E.FFI "" c ("false") | ROOT_T_FIC.sum < 0
ROOT_E.FIC "" i (0,100) | ROOT_E.FFI == "false"
')
expect_identical(p$conditions,
list(ROOT=TRUE, ROOT_T=TRUE, ROOT_T_FIC.sum=TRUE,
ROOT_T.FFI=TRUE, ROOT_T.im=TRUE,
ROOT_E.FFI=expression(ROOT_T_FIC.sum < 0),
ROOT_E.FIC=expression(ROOT_E.FFI == "false")))
})

test_that("error checking", {

ref <- parametersNew(param_real(name = "tmp", lower = 0, upper=1,
Expand Down

0 comments on commit 38b59f7

Please sign in to comment.