From 5882dfd425625ba740e48613f646d49af0c986d4 Mon Sep 17 00:00:00 2001 From: Eliot McIntire Date: Fri, 4 Dec 2026 22:33:18 -0700 Subject: [PATCH] controlSet() keeps the shipped-data digest in control DEoptim.control() rebuilt control and dropped clusterSetup()'s objsDigest, so every fit through controlSet() had one cache key (MPB: all windows of B, C and D loaded the first fit). Also call DEoptim::DEoptim.control. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Lx3DXhV7osHiZpUPDww2xs --- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/DEoptim.R | 6 +++++- tests/testthat/test-DEoptimShippedDataKey.R | 21 ++++++++++++++++++++- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index bcab523..d8b8982 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -6,7 +6,7 @@ URL: http://clusters.predictiveecology.org, https://github.com/PredictiveEcology/clusters Date: 2026-10-16 -Version: 0.0.96 +Version: 0.0.97 Authors@R: c( person("Eliot J B", "McIntire", email = "eliot.mcintire@nrcan-rncan.gc.ca", role = c("aut", "cre"), comment=c(ORCID = "0000-0002-6914-8316")), diff --git a/NEWS.md b/NEWS.md index 8b76edd..62c7262 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # clusters (development version) +* Fits whose `control` went through `controlSet()` now get their own cache keys. `controlSet()` rebuilt `control` with `DEoptim.control()` and dropped `clusterSetup()`'s digest of the shipped data, so every such fit had the same key and loaded the first fit cached (MPB 2026-12-04: every training window of three study areas got one fit). It also calls `DEoptim::DEoptim.control()`, so DEoptim need not be attached. + * An objective's argument that is code, such as mpbRedTopSpread's `quotedSpread` (a `quote({...})`), now reaches the objective as is. `.DEoptimChunk()` and `.rescoreFn()` passed it through `do.call()` without `quote = TRUE`, so it was run in the main process before the objective was called (MPB 2026-12-04: "object 'massAttackRasStart' not found"). * New `requestedWorkers(cores, nCoresNeeded)`: the number of workers `clusterSetup()` asks the planner for (a number of `cores` is that many `"localhost"`; a host named more than once is its count; else `nCoresNeeded`; each host cut to `options(clusters.maxWorkersPerHost)` first). A caller that needs the population size before the cluster exists (fireSense_spreadFit's emulator and start population) calls it, so the rule is no longer copied. `clusterSetup()` calls it; nothing changes in what it builds. diff --git a/R/DEoptim.R b/R/DEoptim.R index 37f6803..a94d853 100644 --- a/R/DEoptim.R +++ b/R/DEoptim.R @@ -674,8 +674,11 @@ DEoptimIterative <- function(fn, lower, upper, control, ..., } controlSet <- function(control, ...) { + ## DEoptim.control() and modifyList2() build a new list and drop clusterSetup()'s digest of the + ## shipped data; without it every fit through here had the same cache key (mpbRedTopSpread, 2026-12-04) + objsDigest <- shippedObjectsDigest(control) if (length(names(control)) < 20) - control <- do.call("DEoptim.control", control) + control <- do.call(DEoptim::DEoptim.control, control) missingElements <- ...names() %in% names(control) if (any(missingElements)) { control <- Require::modifyList2(control, list(...)) @@ -686,6 +689,7 @@ controlSet <- function(control, ...) { # control$c <- .c # } + attr(control, "objsDigest") <- objsDigest control } diff --git a/tests/testthat/test-DEoptimShippedDataKey.R b/tests/testthat/test-DEoptimShippedDataKey.R index c8e32a4..c37d8ce 100644 --- a/tests/testthat/test-DEoptimShippedDataKey.R +++ b/tests/testthat/test-DEoptimShippedDataKey.R @@ -16,11 +16,12 @@ upper <- c(a = 1, b = 1) ## objects for a local run -- the same way the workers see them. fn <- function(par) sum((par - target)^2) -fitWith <- function(target, cachePath) { +fitWith <- function(target, cachePath, viaControlSet = FALSE) { assign("target", target, envir = .GlobalEnv) withr::defer(rm("target", envir = .GlobalEnv)) control <- clusters::clusterSetup(cores = NULL, objsNeeded = "target", envir = environment(), logPath = tempfile(), itermax = 6, strategy = 2L, NP = 8L, trace = FALSE) + if (viaControlSet) control <- clusters:::controlSet(control) # as mpbRedTopSpread does withr::local_options(reproducible.cachePath = cachePath, clusters.cacheDEoptimIterations = TRUE) set.seed(1) # the same starting population for both fits, as two folds of one ELF have DE <- suppressMessages(suppressWarnings( @@ -53,3 +54,21 @@ test_that("two fits that differ only in the shipped data do not share cached gen expect_equal(fitWith(c(0.2, 0.2), cp), best1) expect_identical(entries(), n) }) + +## 2026-12-04, mpbRedTopSpread: the module passes clusterSetup()'s control through controlSet(), which +## rebuilt it with DEoptim.control() and dropped the digest. Every training window of every study area +## then had the same key, and all of them loaded the first fit made. +test_that("controlSet() keeps the shipped-data digest", { + target <- c(0.2, 0.2) + c1 <- clusters::clusterSetup(cores = NULL, objsNeeded = "target", envir = environment(), logPath = tempfile(), trace = FALSE) + rm("target", envir = .GlobalEnv) + expect_identical(shippedObjectsDigest(clusters:::controlSet(c1)), shippedObjectsDigest(c1)) + expect_identical(shippedObjectsDigest(clusters:::controlSet(c1, itermax = 3)), shippedObjectsDigest(c1)) +}) + +test_that("fits through controlSet() that differ only in the shipped data do not share generations", { + cp <- withr::local_tempdir() + best1 <- fitWith(c(0.2, 0.2), cp, viaControlSet = TRUE) + best2 <- fitWith(c(0.8, 0.8), cp, viaControlSet = TRUE) + expect_true(all(abs(best2 - 0.8) < abs(best2 - 0.2))) +})