Skip to content

Commit e868363

Browse files
committed
Add splittable random generators with reproducible streams
Rnd becomes a value type holding the complete state of a splittable engine, SplitMix by default, or any engine through RndGenerator<Engine>. Its state can be printed and restored exactly, which drivers expose as -state and the test harness uses for -replay. Each RandomOption owns its state option, a seed or state option may be given once, and seeds accept negative decimals modulo 2^64, as the former integer seed options did. Randomized selectors draw from a copy of their state and split it by the committed alternative, so cloning, recomputation and parallel search make the same decisions, and choices store no random state. Posting keys each selector by brancher id and role. Branchers record whether any selector is random and skip the split otherwise, so deterministic branching pays no extra cost. RBS and PBS split the branchers of every spoke by its episode or asset after Space::spoke() returns, so spokes do not repeat each other, also when nested. A model-owned generator is split with rnd.split(sc), as photo and FlatZinc do for large neighbourhood search. A copy of an Rnd no longer advances the original, so successive relax() calls need distinct splits. Copies of a random cutoff split its generator instead of reseeding it. The manual CMake consumer of the Windows smoke test now requests C++17, as the exported targets do; MSVC declares std::from_chars only there.
1 parent f003f38 commit e868363

57 files changed

Lines changed: 2072 additions & 615 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/ci/cmake-consumer-smoke.ps1‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ find_library(GECODE_SUPPORT_LIBRARY
125125
NO_DEFAULT_PATH
126126
REQUIRED)
127127
add_executable(manual_consumer main.cpp)
128+
target_compile_features(manual_consumer PRIVATE cxx_std_17)
128129
target_include_directories(manual_consumer PRIVATE "${GECODE_INCLUDE_DIR}")
129130
target_link_libraries(manual_consumer PRIVATE "${GECODE_SUPPORT_LIBRARY}")
130131
'@ | Set-Content -Encoding utf8 (Join-Path $work "manual\CMakeLists.txt")

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ need none.
108108
Run a focused selection first with `-test <pattern>`, then a suite in
109109
proportion to the change: `check` (the small `TestTags::check()` set),
110110
`check-standard` (all but sweeps; new tests are `standard` by default), or
111-
`check-sweep` (exhaustive). A failure prints `-replay <seed> -test <name>`;
111+
`check-sweep` (exhaustive). A failure prints `-replay <state> -test '<name>'`;
112112
rerun with those options to reproduce it. For deeper checks, configure with
113113
`GECODE_SANITIZER`, and for changes to copying, disposal, or ownership, with
114114
`GECODE_ENABLE_FAULT_INJECTION` and run `check-fault`.

‎CMakeLists.txt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,15 @@ if(BUILD_TESTING)
15411541
list(APPEND GECODE_TEST_LINK_LIBS gecodeflatzinc)
15421542
endif()
15431543
target_link_libraries(gecode-test PRIVATE ${GECODE_TEST_LINK_LIBS})
1544+
add_executable(gecode-random-replay EXCLUDE_FROM_ALL
1545+
${GECODE_TEST_MAIN_SOURCE} test/random-replay.cpp)
1546+
target_link_libraries(gecode-random-replay PRIVATE gecodetest)
1547+
add_dependencies(gecode-test gecode-random-replay)
1548+
add_test(NAME random-state-replay
1549+
COMMAND ${CMAKE_COMMAND} -DREPLAY=$<TARGET_FILE:gecode-random-replay>
1550+
-P ${CMAKE_CURRENT_SOURCE_DIR}/test/random-replay.cmake)
1551+
set_tests_properties(random-state-replay PROPERTIES
1552+
FIXTURES_REQUIRED gecode-test-built)
15441553
if(GECODE_ENABLE_FAULT_INJECTION)
15451554
add_executable(gecode-fault-test EXCLUDE_FROM_ALL
15461555
${GECODE_FAULT_TEST_SOURCES})

‎Makefile.dep‎

Lines changed: 293 additions & 133 deletions
Large diffs are not rendered by default.

‎Makefile.in‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ VARIMP = $(VARIMPHDR)
201201

202202
KERNELSRC0 = \
203203
archive core exception gpi \
204-
data/rnd \
205204
branch/action branch/afc branch/chb branch/function \
206205
memory/manager memory/region \
207206
trace/recorder trace/filter trace/tracer trace/general \
@@ -1294,6 +1293,7 @@ FLATZINCTESTSRC0 = \
12941293
test/flatzinc/queen_cp2.cpp \
12951294
test/flatzinc/queen_ip.cpp \
12961295
test/flatzinc/radiation.cpp \
1296+
test/flatzinc/random_branching.cpp \
12971297
test/flatzinc/shared_array_element.cpp \
12981298
test/flatzinc/simple_sat.cpp \
12991299
test/flatzinc/singHoist2.cpp \
@@ -1378,7 +1378,7 @@ TESTAUXOBJ = $(TESTMAINOBJ) $(TESTPUBLICRUNNERSMOKEOBJ) \
13781378
$(TESTPUBLICINTSMOKEOBJ) $(TESTPUBLICSETSMOKEOBJ) \
13791379
$(TESTPUBLICFLOATSMOKEOBJ)
13801380

1381-
TESTSRC0 = $(TESTCORESRC) test/afc.cpp test/ldsb.cpp test/region.cpp \
1381+
TESTSRC0 = $(TESTCORESRC) test/afc.cpp test/ldsb.cpp test/region.cpp test/random.cpp \
13821382
test/groups.cpp
13831383
# FailPoint is CMake-only; keep the Autoconf test executable fault-free.
13841384

‎changelog.in‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,37 @@ step, which would admit solutions that are not better. An exception thrown in
253253
an asset of a parallel portfolio, or by compare() in parallel BAB, is now
254254
rethrown by next() instead of terminating the program.
255255

256+
[ENTRY]
257+
Module: kernel
258+
What: new
259+
Rank: major
260+
[DESCRIPTION]
261+
Replaced the random number generator by splittable engines with complete
262+
state save/restore: SplitMix for Rnd, or any engine through RndGenerator.
263+
The randomized selectors of Gecode's branchers split their state on commit,
264+
so cloning and recomputation make the same random decisions at a node, and
265+
meta search gives the branchers of each spoke their own stream. Parallel
266+
scheduling and weakly monotonic propagation can still change the search.
267+
[MORE]
268+
Rnd is a value type with an explicit seed constructor: write
269+
INT_VAL_RND(Rnd(42)) instead of INT_VAL_RND(42). Use state() and
270+
state(text) instead of seed(), and opt.rnd() instead of Rnd(opt.seed()).
271+
Drivers accept -state with a complete state, and a seed or state option
272+
may be given only once. Seeded sequences change. A copy of an Rnd no
273+
longer advances the original, so relax() and other functions taking an Rnd
274+
by value draw the same numbers from the same generator: pass a distinct
275+
split to each call. A generator owned by the model is copied into every
276+
spoke: split it with rnd.split(sc) in spoke(), for example
277+
relax(*this, x, sx, rnd.split(sc), p).
278+
279+
[ENTRY]
280+
Module: test
281+
What: change
282+
Rank: minor
283+
[DESCRIPTION]
284+
The test option -replay takes the complete random state printed for a
285+
failed iteration.
286+
256287
[RELEASE]
257288
Version: 6.6.0
258289
Date: 2026-11-16

‎cmake/GecodeSources.cmake‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ set(GECODE_KERNEL_SOURCES
1919
gecode/kernel/branch/function.cpp
2020
gecode/kernel/core.cpp
2121
gecode/kernel/data/array.cpp
22-
gecode/kernel/data/rnd.cpp
2322
gecode/kernel/exception.cpp
2423
gecode/kernel/gpi.cpp
2524
gecode/kernel/memory/manager.cpp
@@ -373,6 +372,7 @@ set(GECODE_TEST_SOURCES
373372
test/flatzinc/queen_ip.cpp
374373
test/flatzinc/queens4.cpp
375374
test/flatzinc/radiation.cpp
375+
test/flatzinc/random_branching.cpp
376376
test/flatzinc/sat_arith1.cpp
377377
test/flatzinc/sat_array_bool_and.cpp
378378
test/flatzinc/sat_array_bool_or.cpp
@@ -460,6 +460,7 @@ set(GECODE_TEST_SOURCES
460460
test/ldsb.cpp
461461
test/nogoods.cpp
462462
test/region.cpp
463+
test/random.cpp
463464
test/search.cpp
464465
test/set.cpp
465466
test/set/channel.cpp

‎examples/fifteen-puzzle.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ neighbors(int blank) {
111111
static Board
112112
initial_board(const PuzzleOptions& opt) {
113113
Board board = goal();
114-
Rnd random(opt.seed());
114+
Rnd random = opt.rnd();
115115
int blank = 15;
116116
int previous = -1;
117117
for (unsigned int i=0; i<opt.scramble(); i++) {
@@ -441,7 +441,7 @@ main(int argc, char* argv[]) {
441441
PuzzleOptions opt;
442442
opt.parse_strict(argc,argv);
443443
if (opt.mode() == SM_SOLUTION) {
444-
std::cout << "Initial board (seed " << opt.seed() << "):\n";
444+
std::cout << "Initial board (random state " << opt.rnd().state() << "):\n";
445445
print_board(std::cout,initial_board(opt));
446446
}
447447
if (opt.model() == PuzzleOptions::MODEL_OPEN)

‎examples/job-shop.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ class JobShopSolve : public JobShopBase {
536536
JobShopSolve(const JobShopOptions& o)
537537
: JobShopBase(o),
538538
sorder(*this, spec.machines()*spec.jobs()*(spec.jobs()-1)/2, 0, 1),
539-
rnd(o.seed()) {
539+
rnd(o.rnd()) {
540540
if (opt.propagation() == PROP_UNARY)
541541
nooverload();
542542

@@ -687,7 +687,7 @@ print(const Search::Statistics& stat, bool restart) {
687687
/// Solver
688688
void
689689
solve(const JobShopOptions& opt) {
690-
Rnd rnd(opt.seed());
690+
Rnd rnd = opt.rnd();
691691

692692
/*
693693
* Invariant:
@@ -723,7 +723,7 @@ solve(const JobShopOptions& opt) {
723723

724724
for (unsigned int p=0; p<opt.probes(); p++) {
725725
JobShopProbe* jsp = static_cast<JobShopProbe*>(root->clone());
726-
jsp->branch(p,rnd);
726+
jsp->branch(p,rnd.split(p));
727727
DFS<JobShopProbe> dfs(jsp,so);
728728
JobShopProbe* s = dfs.next();
729729
Search::Statistics statj = dfs.statistics();

‎examples/photo.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Photo : public IntMinimizeScript {
9999
spec(opt.size()),
100100
pos(*this,spec.people(), 0, spec.people()-1),
101101
violations(*this,0,spec.preferences()),
102-
rnd(opt.seed()), p(opt.relax())
102+
rnd(opt.rnd()), p(opt.relax())
103103
{
104104
// Map preferences to violation
105105
BoolVarArgs viol(spec.preferences());
@@ -133,7 +133,7 @@ class Photo : public IntMinimizeScript {
133133
if ((sc.engine() == MetaEngine::Restart) &&
134134
(sc.last() != nullptr) && (p > 0.0)) {
135135
const Photo& l = static_cast<const Photo&>(*sc.last());
136-
relax(*this, pos, l.pos, rnd, p);
136+
relax(*this, pos, l.pos, rnd.split(sc), p);
137137
sc.coverage(SpokeCoverage::Partial);
138138
}
139139
}

0 commit comments

Comments
 (0)