Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ need none.
Run a focused selection first with `-test <pattern>`, then a suite in
proportion to the change: `check` (the small `TestTags::check()` set),
`check-standard` (all but sweeps; new tests are `standard` by default), or
`check-sweep` (exhaustive). A failure prints `-replay <seed> -test <name>`;
`check-sweep` (exhaustive). A failure prints `-replay <state> -test '<name>'`;
rerun with those options to reproduce it. For deeper checks, configure with
`GECODE_SANITIZER`, and for changes to copying, disposal, or ownership, with
`GECODE_ENABLE_FAULT_INJECTION` and run `check-fault`.
Expand Down
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,15 @@ if(BUILD_TESTING)
list(APPEND GECODE_TEST_LINK_LIBS gecodeflatzinc)
endif()
target_link_libraries(gecode-test PRIVATE ${GECODE_TEST_LINK_LIBS})
add_executable(gecode-random-replay EXCLUDE_FROM_ALL
${GECODE_TEST_MAIN_SOURCE} test/random-replay.cpp)
target_link_libraries(gecode-random-replay PRIVATE gecodetest)
add_dependencies(gecode-test gecode-random-replay)
add_test(NAME random-state-replay
COMMAND ${CMAKE_COMMAND} -DREPLAY=$<TARGET_FILE:gecode-random-replay>
-P ${CMAKE_CURRENT_SOURCE_DIR}/test/random-replay.cmake)
set_tests_properties(random-state-replay PROPERTIES
FIXTURES_REQUIRED gecode-test-built)
if(GECODE_ENABLE_FAULT_INJECTION)
add_executable(gecode-fault-test EXCLUDE_FROM_ALL
${GECODE_FAULT_TEST_SOURCES})
Expand Down
426 changes: 293 additions & 133 deletions Makefile.dep

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ VARIMP = $(VARIMPHDR)

KERNELSRC0 = \
archive core exception gpi \
data/rnd \
branch/action branch/afc branch/chb branch/function \
memory/manager memory/region \
trace/recorder trace/filter trace/tracer trace/general \
Expand Down Expand Up @@ -1294,6 +1293,7 @@ FLATZINCTESTSRC0 = \
test/flatzinc/queen_cp2.cpp \
test/flatzinc/queen_ip.cpp \
test/flatzinc/radiation.cpp \
test/flatzinc/random_branching.cpp \
test/flatzinc/shared_array_element.cpp \
test/flatzinc/simple_sat.cpp \
test/flatzinc/singHoist2.cpp \
Expand Down Expand Up @@ -1378,7 +1378,7 @@ TESTAUXOBJ = $(TESTMAINOBJ) $(TESTPUBLICRUNNERSMOKEOBJ) \
$(TESTPUBLICINTSMOKEOBJ) $(TESTPUBLICSETSMOKEOBJ) \
$(TESTPUBLICFLOATSMOKEOBJ)

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

Expand Down
26 changes: 26 additions & 0 deletions changelog.in
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,32 @@ compare() reports incomparable solutions. An exception thrown in an asset of a
parallel portfolio, or by compare() in parallel BAB, is now rethrown by next()
instead of terminating the program.

[ENTRY]
Module: kernel
What: new
Rank: major
[DESCRIPTION]
Replaced the random number generator by splittable engines with complete
state save/restore: SplitMix for Rnd, or any engine through RndGenerator.
Randomized branching splits its state on commit, so cloning and
recomputation make the same random decisions, and meta search gives the
branchers of each spoke their own stream.
[MORE]
Rnd is a value type with an explicit seed constructor: write
INT_VAL_RND(Rnd(42)) instead of INT_VAL_RND(42). Use state() and
state(text) instead of seed(), and opt.rnd() instead of Rnd(opt.seed()).
Drivers accept -state with a complete state. Seeded sequences change. A
generator owned by the model is copied into every spoke: split it with
rnd.split(sc) in spoke(), for example relax(*this, x, sx, rnd.split(sc), p).

[ENTRY]
Module: test
What: change
Rank: minor
[DESCRIPTION]
The test option -replay takes the complete random state printed for a
failed iteration.

[RELEASE]
Version: 6.6.0
Date: 2026-11-16
Expand Down
3 changes: 2 additions & 1 deletion cmake/GecodeSources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ set(GECODE_KERNEL_SOURCES
gecode/kernel/branch/function.cpp
gecode/kernel/core.cpp
gecode/kernel/data/array.cpp
gecode/kernel/data/rnd.cpp
gecode/kernel/exception.cpp
gecode/kernel/gpi.cpp
gecode/kernel/memory/manager.cpp
Expand Down Expand Up @@ -373,6 +372,7 @@ set(GECODE_TEST_SOURCES
test/flatzinc/queen_ip.cpp
test/flatzinc/queens4.cpp
test/flatzinc/radiation.cpp
test/flatzinc/random_branching.cpp
test/flatzinc/sat_arith1.cpp
test/flatzinc/sat_array_bool_and.cpp
test/flatzinc/sat_array_bool_or.cpp
Expand Down Expand Up @@ -460,6 +460,7 @@ set(GECODE_TEST_SOURCES
test/ldsb.cpp
test/nogoods.cpp
test/region.cpp
test/random.cpp
test/search.cpp
test/set.cpp
test/set/channel.cpp
Expand Down
4 changes: 2 additions & 2 deletions examples/fifteen-puzzle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ neighbors(int blank) {
static Board
initial_board(const PuzzleOptions& opt) {
Board board = goal();
Rnd random(opt.seed());
Rnd random = opt.rnd();
int blank = 15;
int previous = -1;
for (unsigned int i=0; i<opt.scramble(); i++) {
Expand Down Expand Up @@ -441,7 +441,7 @@ main(int argc, char* argv[]) {
PuzzleOptions opt;
opt.parse_strict(argc,argv);
if (opt.mode() == SM_SOLUTION) {
std::cout << "Initial board (seed " << opt.seed() << "):\n";
std::cout << "Initial board (random state " << opt.rnd().state() << "):\n";
print_board(std::cout,initial_board(opt));
}
if (opt.model() == PuzzleOptions::MODEL_OPEN)
Expand Down
6 changes: 3 additions & 3 deletions examples/job-shop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ class JobShopSolve : public JobShopBase {
JobShopSolve(const JobShopOptions& o)
: JobShopBase(o),
sorder(*this, spec.machines()*spec.jobs()*(spec.jobs()-1)/2, 0, 1),
rnd(o.seed()) {
rnd(o.rnd()) {
if (opt.propagation() == PROP_UNARY)
nooverload();

Expand Down Expand Up @@ -687,7 +687,7 @@ print(const Search::Statistics& stat, bool restart) {
/// Solver
void
solve(const JobShopOptions& opt) {
Rnd rnd(opt.seed());
Rnd rnd = opt.rnd();

/*
* Invariant:
Expand Down Expand Up @@ -723,7 +723,7 @@ solve(const JobShopOptions& opt) {

for (unsigned int p=0; p<opt.probes(); p++) {
JobShopProbe* jsp = static_cast<JobShopProbe*>(root->clone());
jsp->branch(p,rnd);
jsp->branch(p,rnd.split(p));
DFS<JobShopProbe> dfs(jsp,so);
JobShopProbe* s = dfs.next();
Search::Statistics statj = dfs.statistics();
Expand Down
4 changes: 2 additions & 2 deletions examples/photo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Photo : public IntMinimizeScript {
spec(opt.size()),
pos(*this,spec.people(), 0, spec.people()-1),
violations(*this,0,spec.preferences()),
rnd(opt.seed()), p(opt.relax())
rnd(opt.rnd()), p(opt.relax())
{
// Map preferences to violation
BoolVarArgs viol(spec.preferences());
Expand Down Expand Up @@ -133,7 +133,7 @@ class Photo : public IntMinimizeScript {
if ((sc.engine() == MetaEngine::Restart) &&
(sc.last() != nullptr) && (p > 0.0)) {
const Photo& l = static_cast<const Photo&>(*sc.last());
relax(*this, pos, l.pos, rnd, p);
relax(*this, pos, l.pos, rnd.split(sc), p);
sc.coverage(SpokeCoverage::Partial);
}
}
Expand Down
22 changes: 18 additions & 4 deletions gecode/driver.hh
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ namespace Gecode {
static void strdel(const char* s);
};

/// Checked seed or complete state for the build-configured random engine.
class GECODE_DRIVER_EXPORT RandomOption : public BaseOption {
Rnd initial;
bool seed_given = false;
bool state_given = false;
public:
RandomOption(const char* o, const char* e, uint64_t v);
void value(uint64_t v);
/// Construct an independent generator at the configured initial state.
Rnd rnd(void) const;
virtual int parse(int argc, char* argv[]);
virtual void help(void);
};

/**
* \brief String-valued option
*
Expand Down Expand Up @@ -454,7 +468,7 @@ namespace Gecode {
Driver::IplOption _ipl; ///< Integer propagation level
Driver::StringOption _branching; ///< Branching options
Driver::DoubleOption _decay; ///< Decay option
Driver::UnsignedIntOption _seed; ///< Seed option
Driver::RandomOption _seed; ///< Seed or complete random state
Driver::DoubleOption _step; ///< Step option
//@}

Expand Down Expand Up @@ -550,9 +564,9 @@ namespace Gecode {
double decay(void) const;

/// Set default seed value
void seed(unsigned int s);
/// Return seed value
unsigned int seed(void) const;
void seed(uint64_t s);
/// Independent generator initialized from the seed or full-state option
Rnd rnd(void) const;

/// Set default step value
void step(double s);
Expand Down
55 changes: 55 additions & 0 deletions gecode/driver/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,61 @@ namespace Gecode {
}


RandomOption::RandomOption(const char* o, const char* e, uint64_t v)
: BaseOption(o,e) {
value(v);
}
void RandomOption::value(uint64_t v) {
initial.seed(v);
seed_given = state_given = false;
}
Rnd RandomOption::rnd(void) const {
return initial;
}
int RandomOption::parse(int argc, char* argv[]) {
bool full = argc >= 2 &&
(!strcmp(argv[1],"-state") || !strcmp(argv[1],"--state"));
const char* arg;
if (full) {
if (argc < 3) {
std::cerr << "Missing argument for option -state" << std::endl;
exit(EXIT_FAILURE);
}
arg = argv[2];
} else {
arg = argument(argc,argv);
if (!arg)
return 0;
}
try {
if ((full && seed_given) || (!full && state_given))
throw std::invalid_argument("Seed and state options cannot be combined");
if (full) {
initial.state(std::string(arg));
state_given = true;
} else {
if (!strcmp(arg,"time") || !strcmp(arg,"hw")) {
if (!strcmp(arg,"time")) initial.time(); else initial.hw();
std::cerr << "% Random state: -state " << initial.state() << std::endl;
} else {
initial.seed(Support::random_seed(arg));
}
seed_given = true;
}
} catch (const std::exception& e) {
std::cerr << "Invalid random option: " << e.what() << std::endl;
exit(EXIT_FAILURE);
}
return 2;
}
void RandomOption::help(void) {
std::cerr << "\t" << iopt << " (64-bit decimal/hex seed, time, hw)\n"
<< "\t\t" << exp << "\n"
<< "\t-state (complete " << Support::RandomGenerator::name()
<< " state; mutually exclusive with " << iopt << ")\n"
<< "\t\tCurrent initial state: " << initial.state() << std::endl;
}

StringValueOption::StringValueOption(const char* o, const char* e,
const char* v)
: BaseOption(o,e), cur(strdup(v)) {}
Expand Down
8 changes: 4 additions & 4 deletions gecode/driver/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,12 @@ namespace Gecode {
}

inline void
Options::seed(unsigned int s) {
Options::seed(uint64_t s) {
_seed.value(s);
}
inline unsigned int
Options::seed(void) const {
return _seed.value();
inline Rnd
Options::rnd(void) const {
return _seed.rnd();
}

inline void
Expand Down
8 changes: 4 additions & 4 deletions gecode/flatzinc.hh
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ namespace Gecode { namespace FlatZinc {
Gecode::Driver::UnsignedLongLongIntOption _fail; ///< Cutoff for number of failures
Gecode::Driver::DoubleOption _time; ///< Cutoff for time
Gecode::Driver::DoubleOption _time_limit; ///< Cutoff for time (for compatibility with flatzinc command line)
Gecode::Driver::IntOption _seed; ///< Random seed
Gecode::Driver::RandomOption _seed; ///< Random seed or state
Gecode::Driver::StringOption _restart; ///< Restart method option
Gecode::Driver::DoubleOption _r_base; ///< Restart base
Gecode::Driver::UnsignedIntOption _r_scale; ///< Restart scale factor
Expand Down Expand Up @@ -349,7 +349,7 @@ namespace Gecode { namespace FlatZinc {
unsigned long long int node(void) const { return _node.value(); }
unsigned long long int fail(void) const { return _fail.value(); }
double time(void) const { return _time.value(); }
int seed(void) const { return _seed.value(); }
Rnd rnd(void) const { return _seed.rnd(); }
double step(void) const { return _step.value(); }
const char* output(void) const { return _output.value(); }

Expand Down Expand Up @@ -410,7 +410,7 @@ namespace Gecode { namespace FlatZinc {
BranchInformation& operator =(const BranchInformation&) = default;
};

/// Uninitialized default random number generator
/// Default random number generator, initialized with seed 0
GECODE_FLATZINC_EXPORT
extern Rnd defrnd;

Expand Down Expand Up @@ -665,7 +665,7 @@ namespace Gecode { namespace FlatZinc {
* If \a ignoreUnknown is true, unknown solve item annotations will be
* ignored, otherwise a warning is written to \a err.
*
* The seed for random branchers is given by the \a seed parameter.
* Random branchers use the seed or complete state configured in \a opt.
*
*/
void createBranchers(Printer& p, AST::Node* ann,
Expand Down
7 changes: 7 additions & 0 deletions gecode/flatzinc/branch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace Gecode { namespace FlatZinc {
unsigned int b) {
const PosIntChoice& c
= static_cast<const PosIntChoice&>(_c);
random_split(b);
int p=c.pos(); int n=c.val();
if (p < x.size()) {
return me_failed(xvsc->commit(home,b,x[p],p,n)) ?
Expand All @@ -78,6 +79,12 @@ namespace Gecode { namespace FlatZinc {
}
}

void
IntBoolBrancherBase::random_split(unsigned int b) {
xvsc->random_split(b);
yvsc->random_split(b);
}

NGL*
IntBoolBrancherBase::ngl(Space& home, const Choice& _c,
unsigned int b) const {
Expand Down
2 changes: 2 additions & 0 deletions gecode/flatzinc/branch.hh
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ namespace Gecode { namespace FlatZinc {
virtual const Choice* choice(const Space& home, Archive& e);
/// Perform commit for choice \a c and alternative \a b
virtual ExecStatus commit(Space& home, const Choice& c, unsigned int b);
/// Split the state of both value selectors for \a b
virtual void random_split(unsigned int b);
/// Create no-good literal for choice \a c and alternative \a b
virtual NGL* ngl(Space& home, const Choice& c, unsigned int b) const;
/// Print branch for choice \a c and alternative \a b
Expand Down
8 changes: 6 additions & 2 deletions gecode/flatzinc/branch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Gecode { namespace FlatZinc {

forceinline
IntBoolVarBranch::IntBoolVarBranch(Select s0, double d)
: VarBranch<IntVar>(d), s(s0) {}
: VarBranch<IntVar>(d,nullptr), s(s0) {}

forceinline
IntBoolVarBranch::IntBoolVarBranch(Select s0, IntAFC i, BoolAFC b)
Expand Down Expand Up @@ -308,6 +308,11 @@ namespace Gecode { namespace FlatZinc {
ValSelCommitBase<Int::IntView,int>* xvsc0,
ValSelCommitBase<Int::BoolView,int>* yvsc0)
: Brancher(home), x(x0), y(y0), start(0), xvsc(xvsc0), yvsc(yvsc0) {
// Key each selector by brancher and role, as for ViewValBrancher.
xvsc->random_split(id());
xvsc->random_split(0U);
yvsc->random_split(id());
yvsc->random_split(1U);
home.notice(*this,AP_DISPOSE,true);
}

Expand Down Expand Up @@ -442,4 +447,3 @@ namespace Gecode { namespace FlatZinc {
}}

// STATISTICS: flatzinc-branch

Loading
Loading