Can be used with optimize_design to improve convergence speed.

shuffle_with_constraints(src = TRUE, dst = TRUE)

Arguments

src

Expression to define possible source locations in the samples/locations table. Usually evaluated based on BatchContainer$get_samples(include_id = TRUE, as_tibble = FALSE) as an environment (see also with()). A single source location is selected from rows where the expression evaluates toTRUE.

dst

Expression to define possible destination locations in the samples/locations table. Usually evaluated based on BatchContainer$get_samples() as an environment. Additionally a special variable .src is available in this environment which describes the selected source row from the table.

Value

Returns a function which accepts a BatchContainer and an iteration number (i). This function returns a list with two names: src vector of length 2 and dst vector of length two. See BatchContainer$move_samples().

Examples

set.seed(43)

samples <- data.frame(
  id = 1:100,
  sex = sample(c("F", "M"), 100, replace = TRUE),
  group = sample(c("treatment", "control"), 100, replace = TRUE)
)

bc <- BatchContainer$new(
  dimensions = c("plate" = 5, "position" = 25)
)

scoring_f <- function(samples) {
  osat_score(
    samples,
    "plate",
    c("sex", "group")
  )$score
}

# in this example we treat all the positions in the plate as equal.
# when shuffling we enforce that source location is non-empty,
# and destination location has a different plate number
bc <- optimize_design(
  bc,
  scoring = scoring_f,
  samples,
  shuffle_proposal = shuffle_with_constraints(
    # source is non-empty location
    !is.na(.sample_id),
    # destination has a different plate
    plate != .src$plate
  ),
  max_iter = 10
)
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Checking variances of 1-dim. score vector.
#> ... (455.222) - OK
#> Initial score: 172.8
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Achieved score: 158.8 at iteration 3
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Achieved score: 138.8 at iteration 4
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Achieved score: 136.8 at iteration 8
#> Warning: NAs in features / batch columns; they will be excluded from scoring
#> Warning: NAs in features / batch columns; they will be excluded from scoring