Skip to content
Merged
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
1 change: 1 addition & 0 deletions .github/ci/cmake-consumer-smoke.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ find_library(GECODE_SUPPORT_LIBRARY
NO_DEFAULT_PATH
REQUIRED)
add_executable(manual_consumer main.cpp)
target_compile_features(manual_consumer PRIVATE cxx_std_17)
target_include_directories(manual_consumer PRIVATE "${GECODE_INCLUDE_DIR}")
target_link_libraries(manual_consumer PRIVATE "${GECODE_SUPPORT_LIBRARY}")
'@ | Set-Content -Encoding utf8 (Join-Path $work "manual\CMakeLists.txt")
Expand Down
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
31 changes: 31 additions & 0 deletions changelog.in
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,37 @@ step, which would admit solutions that are not better. 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.
The randomized selectors of Gecode's branchers split their state on commit,
so cloning and recomputation make the same random decisions at a node, and
meta search gives the branchers of each spoke their own stream. Parallel
scheduling and weakly monotonic propagation can still change the search.
[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, and a seed or state option
may be given only once. Seeded sequences change. A copy of an Rnd no
longer advances the original, so relax() and other functions taking an Rnd
by value draw the same numbers from the same generator: pass a distinct
split to each call. 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
37 changes: 33 additions & 4 deletions gecode/driver.hh
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,35 @@ namespace Gecode {
static void strdel(const char* s);
};

/**
* \brief Checked seed or complete state of an Rnd generator
*
* Option \a o takes a seed, \c time, or \c hw, and the state option of
* the instance takes a complete state. Each may be given once, and not
* together, so the replay state printed for \c time and \c hw is the
* state that rnd() returns. Several instances need distinct state
* option names.
*/
class GECODE_DRIVER_EXPORT RandomOption : public BaseOption {
Rnd initial;
const char* sopt; ///< State option name, without hyphen
const char* siopt; ///< State option name, with hyphen
bool seed_given = false;
bool state_given = false;
public:
/// Initialize seed option \a o with explanation \a e, seed \a v, and state option \a s
RandomOption(const char* o, const char* e, uint64_t v,
const char* s="state");
/// Seed the initial generator with \a 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);
/// Destructor
virtual ~RandomOption(void);
};

/**
* \brief String-valued option
*
Expand Down Expand Up @@ -454,7 +483,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 +579,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
67 changes: 67 additions & 0 deletions gecode/driver/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,73 @@ namespace Gecode {
}


RandomOption::RandomOption(const char* o, const char* e, uint64_t v,
const char* s)
: BaseOption(o,e), sopt(strdup(s)), siopt(stredup(s)) {
value(v);
}
RandomOption::~RandomOption(void) {
strdel(sopt);
strdel(siopt);
}
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 = false;
if ((argc >= 2) && (argv[1][0] == '-')) {
const char* name = argv[1] + ((argv[1][1] == '-') ? 2 : 1);
full = !strcmp(name,sopt);
}
const char* arg;
if (full) {
if (argc < 3) {
std::cerr << "Missing argument for option " << siopt << 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 (seed_given || state_given)
throw std::invalid_argument(std::string(full ? siopt : iopt) +
" given more than once");
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: " << siopt << " " << 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" << siopt << " (complete " << Rnd::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
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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
Loading
Loading