From 3d01baf55b2a8c3efd2ecfdf9137828e7551b7fa Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Sun, 1 Nov 2026 15:07:56 +0100 Subject: [PATCH 1/7] Post nothing for empty no_overlap Zdev-Change-Id: Zaf7a888eb914257e90f519223bfacc19df79b6e81c1f7fdcfb32ce9d12bda351 --- changelog.in | 8 ++++++++ gecode/int/no-overlap.cpp | 8 ++++++++ gecode/kernel/core.hpp | 14 ++++++++++++- gecode/kernel/memory/manager.hpp | 6 +++++- test/int/no-overlap.cpp | 34 ++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 2 deletions(-) diff --git a/changelog.in b/changelog.in index eda8ed76ed..2e4c3e594c 100644 --- a/changelog.in +++ b/changelog.in @@ -117,6 +117,14 @@ Rank: minor Define the GECODE_FREELIST32_SIZE_MAX and GECODE_FREELIST64_SIZE_MAX build options as numbers in config.hpp. A quoted value broke compilation. +[ENTRY] +Module: int +What: bug +Rank: minor +[DESCRIPTION] +Post nothing for no_overlap with zero rectangles. Debug builds aborted with a +failed assertion in the memory manager. + [ENTRY] Module: other What: performance diff --git a/gecode/int/no-overlap.cpp b/gecode/int/no-overlap.cpp index 58805046cb..ec9b2ae309 100644 --- a/gecode/int/no-overlap.cpp +++ b/gecode/int/no-overlap.cpp @@ -60,6 +60,8 @@ namespace Gecode { Limits::check(static_cast(y[i].max()) + h[i], "Int::nooverlap"); } GECODE_POST; + if (n_boxes == 0) + return; ManBox* b = static_cast(home).alloc>(n_boxes); for (int i = 0; i < n_boxes; i++) { @@ -85,6 +87,8 @@ namespace Gecode { Limits::check(static_cast(y[i].max()) + h[i], "Int::nooverlap"); } GECODE_POST; + if (n_boxes == 0) + return; if (optional(m)) { OptBox* b = static_cast(home).alloc>(n_boxes); @@ -117,6 +121,8 @@ namespace Gecode { (n_boxes != y1.size())) throw ArgumentSizeMismatch("Int::nooverlap"); GECODE_POST; + if (n_boxes == 0) + return; for (int i = 0; i < n_boxes; i++) { GECODE_ME_FAIL(IntView(w[i]).gq(home, 0)); @@ -150,6 +156,8 @@ namespace Gecode { (n_boxes != y1.size()) || (n_boxes != m.size())) throw ArgumentSizeMismatch("Int::nooverlap"); GECODE_POST; + if (n_boxes == 0) + return; for (int i = 0; i < n_boxes; i++) { GECODE_ME_FAIL(IntView(w[i]).gq(home, 0)); diff --git a/gecode/kernel/core.hpp b/gecode/kernel/core.hpp index c3b67f83a8..4264bb555c 100644 --- a/gecode/kernel/core.hpp +++ b/gecode/kernel/core.hpp @@ -2874,6 +2874,8 @@ namespace Gecode { * * Note that this function implements C++ semantics: the default * constructor of \a T is run for all \a n objects. + * + * The number of objects \a n must be greater than zero. */ template T* alloc(long unsigned int n); @@ -2882,6 +2884,8 @@ namespace Gecode { * * Note that this function implements C++ semantics: the default * constructor of \a T is run for all \a n objects. + * + * The number of objects \a n must be greater than zero. */ template T* alloc(long int n); @@ -2890,6 +2894,8 @@ namespace Gecode { * * Note that this function implements C++ semantics: the default * constructor of \a T is run for all \a n objects. + * + * The number of objects \a n must be greater than zero. */ template T* alloc(unsigned int n); @@ -2898,6 +2904,8 @@ namespace Gecode { * * Note that this function implements C++ semantics: the default * constructor of \a T is run for all \a n objects. + * + * The number of objects \a n must be greater than zero. */ template T* alloc(int n); @@ -3033,7 +3041,11 @@ namespace Gecode { */ template T** realloc(T** b, int n, int m); - /// Allocate memory on space heap + /** + * \brief Allocate memory of size \a s on space heap + * + * The size \a s must be greater than zero. + */ void* ralloc(size_t s); /// Free memory previously allocated with alloc (might be reused later) void rfree(void* p, size_t s); diff --git a/gecode/kernel/memory/manager.hpp b/gecode/kernel/memory/manager.hpp index 62de8f10af..d830665a3a 100644 --- a/gecode/kernel/memory/manager.hpp +++ b/gecode/kernel/memory/manager.hpp @@ -143,7 +143,11 @@ namespace Gecode { namespace Kernel { void alloc_fill(SharedMemory& sm, size_t s, bool first); public: - /// Allocate memory of size \a s + /** + * \brief Allocate memory of size \a s + * + * The size \a s must be greater than zero. + */ void* alloc(SharedMemory& sm, size_t s); /// Get the memory area for subscriptions void* subscriptions(void) const; diff --git a/test/int/no-overlap.cpp b/test/int/no-overlap.cpp index ebb56cac6f..e9f6aa9db0 100644 --- a/test/int/no-overlap.cpp +++ b/test/int/no-overlap.cpp @@ -266,6 +266,40 @@ namespace Test { namespace Int { } }; + /// %Test that every no-overlap overload posts nothing for zero boxes + class Empty : public Base { + private: + /// Space without variables + class Fixture : public Gecode::Space { + public: + Fixture(void) = default; + Fixture(Fixture& fixture) : Gecode::Space(fixture) {} + Gecode::Space* + copy(void) override { + return new Fixture(*this); + } + }; + + public: + Empty(void) : Base("Int::NoOverlap::Empty") {} + bool + run(void) override { + using namespace Gecode; + IntVarArgs x; + IntArgs w; + BoolVarArgs m; + Fixture fix, fix_opt, var, var_opt; + no_overlap(fix, x, w, x, w); + no_overlap(fix_opt, x, w, x, w, m); + no_overlap(var, x, x, x, x, x, x); + no_overlap(var_opt, x, x, x, x, x, x, m); + return (fix.status() == SS_SOLVED) && (fix_opt.status() == SS_SOLVED) && (var.status() == SS_SOLVED) && + (var_opt.status() == SS_SOLVED); + } + }; + + Empty empty; + /// Help class to create and register tests class Create { public: From 63f752028470ce66f4fb853d978d1345d315bb4f Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Sun, 1 Nov 2026 15:55:58 +0100 Subject: [PATCH 2/7] Post nothing for empty GCC and bin-packing input Zdev-Change-Id: Z29f5a5de8dcaeb9c7e36ad57a6f0f8b6da3a7cbdb6796476784b9a4bfd45dd76 --- gecode/int/bin-packing.cpp | 4 ++ gecode/int/bin-packing/propagate.cpp | 3 +- gecode/int/gcc.cpp | 27 ++++++++++++ test/int/bin-packing.cpp | 41 ++++++++++++++++++ test/int/gcc.cpp | 64 ++++++++++++++++++++++++++++ 5 files changed, 138 insertions(+), 1 deletion(-) diff --git a/gecode/int/bin-packing.cpp b/gecode/int/bin-packing.cpp index ed33fb9d82..4bcd2d4c1d 100644 --- a/gecode/int/bin-packing.cpp +++ b/gecode/int/bin-packing.cpp @@ -110,6 +110,10 @@ namespace Gecode { } } + // Without items there are no conflicts + if (n == 0) + return IntSet::empty; + // Clique Finding and distinct posting { // First construct the conflict graph diff --git a/gecode/int/bin-packing/propagate.cpp b/gecode/int/bin-packing/propagate.cpp index 8e07b5be6a..2e85dbb22e 100644 --- a/gecode/int/bin-packing/propagate.cpp +++ b/gecode/int/bin-packing/propagate.cpp @@ -364,7 +364,8 @@ namespace Gecode { namespace Int { namespace BinPacking { ExecStatus Pack::post(Home home, ViewArray& l, ViewArray& bs, IntPropLevel ipl) { // Sort according to size - Support::quicksort(&bs[0], bs.size()); + if (bs.size() > 0) + Support::quicksort(&bs[0], bs.size()); // Total size of items int s = 0; // Constrain bins diff --git a/gecode/int/gcc.cpp b/gecode/int/gcc.cpp index 6f93f7b00b..58af0822a5 100644 --- a/gecode/int/gcc.cpp +++ b/gecode/int/gcc.cpp @@ -110,6 +110,18 @@ namespace Gecode { GECODE_POST; + if (x.size() == 0) { + // Without variables, every value occurs zero times + for (int i = 0; i < c.size(); i++) + GECODE_ME_FAIL(IntView(c[i]).eq(home, 0)); + return; + } + if (v.size() == 0) { + // No value is allowed for the variables + home.fail(); + return; + } + remove_duplicates(home, c, v); ViewArray xv(home, x); @@ -156,6 +168,21 @@ namespace Gecode { GECODE_POST; + if (x.size() == 0) { + // Without variables, every value occurs zero times + for (int i = 0; i < c.size(); i++) + if (!c[i].in(0)) { + home.fail(); + return; + } + return; + } + if (v.size() == 0) { + // No value is allowed for the variables + home.fail(); + return; + } + remove_duplicates(home, c, v); ViewArray xv(home, x); diff --git a/test/int/bin-packing.cpp b/test/int/bin-packing.cpp index fabf507705..9125652607 100644 --- a/test/int/bin-packing.cpp +++ b/test/int/bin-packing.cpp @@ -360,6 +360,47 @@ namespace Test { namespace Int { } }; + /// %Test that both bin-packing overloads post nothing for zero items + class Empty : public Base { + private: + /// Space without variables + class Fixture : public Gecode::Space { + public: + Fixture(void) = default; + Fixture(Fixture& fixture) : Gecode::Space(fixture) {} + Gecode::Space* + copy(void) override { + return new Fixture(*this); + } + }; + + public: + Empty(void) : Base("Int::BinPacking::Empty") {} + bool + run(void) override { + using namespace Gecode; + IntVarArgs e; + IntArgs s; + Fixture none, loads, loaded, multi_none, multi_loads, multi_loaded; + bin_packing(none, e, e, s); + IntVarArgs l(loads, 2, 0, 3); + bin_packing(loads, l, e, s); + bin_packing(loaded, IntVarArgs(loaded, 1, 1, 3), e, s); + IntSet clique_none = bin_packing(multi_none, 1, e, e, s, IntArgs({1})); + IntVarArgs ml(multi_loads, 2, 0, 3); + IntSet clique_loads = bin_packing(multi_loads, 2, ml, e, s, IntArgs({1, 1})); + bin_packing(multi_loaded, 1, IntVarArgs(multi_loaded, 1, 1, 3), e, s, IntArgs({3})); + return (none.status() == SS_SOLVED) && (loads.status() == SS_SOLVED) && l[0].assigned() && + (l[0].val() == 0) && l[1].assigned() && (l[1].val() == 0) && (loaded.status() == SS_FAILED) && + (multi_none.status() == SS_SOLVED) && (clique_none.size() == 0) && + (multi_loads.status() == SS_SOLVED) && ml[0].assigned() && (ml[0].val() == 0) && + ml[1].assigned() && (ml[1].val() == 0) && (clique_loads.size() == 0) && + (multi_loaded.status() == SS_FAILED); + } + }; + + Empty empty; + /// Large weights must preserve every feasible bin assignment. class DFFLargeWeights : public Test { int capacity; diff --git a/test/int/gcc.cpp b/test/int/gcc.cpp index 77a27adabe..d59a05c448 100644 --- a/test/int/gcc.cpp +++ b/test/int/gcc.cpp @@ -303,6 +303,70 @@ namespace Test { namespace Int { } }; + /// %Test every count overload with no variables or no values + class Empty : public Base { + private: + /// Space without variables + class Fixture : public Gecode::Space { + public: + Fixture(void) = default; + Fixture(Fixture& fixture) : Gecode::Space(fixture) {} + Gecode::Space* + copy(void) override { + return new Fixture(*this); + } + }; + + public: + Empty(void) : Base("Int::GCC::Empty") {} + bool + run(void) override { + using namespace Gecode; + IntVarArgs x; + IntVarArgs c; + IntSetArgs cs; + IntArgs v; + Fixture var, var_v, var_val, var_bnd, var_dom, set, set_v, set_c; + count(var, x, c); + count(var_v, x, c, v); + count(var_val, x, c, v, IPL_VAL); + count(var_bnd, x, c, v, IPL_BND); + count(var_dom, x, c, v, IPL_DOM); + count(set, x, cs); + count(set_v, x, cs, v); + count(set_c, x, IntSet(0, 1), v); + bool empty_solved = (var.status() == SS_SOLVED) && (var_v.status() == SS_SOLVED) && + (var_val.status() == SS_SOLVED) && (var_bnd.status() == SS_SOLVED) && + (var_dom.status() == SS_SOLVED) && (set.status() == SS_SOLVED) && + (set_v.status() == SS_SOLVED) && (set_c.status() == SS_SOLVED); + + // Without variables, every value occurs zero times + Fixture card, card_zero, card_one, card_hole; + IntVarArgs y(card, 2, 0, 2); + count(card, x, y, IntArgs({1, 1})); + count(card_zero, x, IntSetArgs({IntSet({0, 2})}), IntArgs({1})); + count(card_one, x, IntSetArgs({IntSet(1, 2)}), IntArgs({1})); + count(card_hole, x, IntSet({1, 3}), IntArgs({1})); + bool card_counted = (card.status() == SS_SOLVED) && y[0].assigned() && (y[0].val() == 0) && + y[1].assigned() && (y[1].val() == 0) && (card_zero.status() == SS_SOLVED) && + (card_one.status() == SS_FAILED) && (card_hole.status() == SS_FAILED); + + // Without values, no variable can take a value + Fixture no_var, no_var_v, no_set, no_set_v, no_set_c; + count(no_var, IntVarArgs(no_var, 1, 0, 2), c); + count(no_var_v, IntVarArgs(no_var_v, 1, 0, 2), c, v); + count(no_set, IntVarArgs(no_set, 1, 0, 2), cs); + count(no_set_v, IntVarArgs(no_set_v, 1, 0, 2), cs, v); + count(no_set_c, IntVarArgs(no_set_c, 1, 0, 2), IntSet(0, 1), v); + bool no_value_failed = (no_var.status() == SS_FAILED) && (no_var_v.status() == SS_FAILED) && + (no_set.status() == SS_FAILED) && (no_set_v.status() == SS_FAILED) && + (no_set_c.status() == SS_FAILED); + return empty_solved && card_counted && no_value_failed; + } + }; + + Empty empty; + /// Help class to create and register tests class Create { public: From 9d5846e44bb45fd57ecf274adeba15012f0c7888 Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Sun, 1 Nov 2026 16:43:31 +0100 Subject: [PATCH 3/7] Branch with empty symmetries and relax empty arrays Zdev-Change-Id: Za38592943e829d0d5f0639f54bb56ed7b2929c5997714a009f9fb274ee681e37 --- gecode/float/relax.cpp | 2 + gecode/int/ldsb.cpp | 65 ++++++++---- gecode/int/ldsb.hh | 11 ++- gecode/int/ldsb/brancher.hpp | 10 +- gecode/int/relax.cpp | 4 + gecode/set/ldsb.cpp | 35 +++++-- gecode/set/relax.cpp | 2 + test/ldsb.cpp | 185 +++++++++++++++++++++++++++++++++++ test/search.cpp | 64 ++++++++++++ 9 files changed, 344 insertions(+), 34 deletions(-) diff --git a/gecode/float/relax.cpp b/gecode/float/relax.cpp index c80b5931da..93d123ba0b 100644 --- a/gecode/float/relax.cpp +++ b/gecode/float/relax.cpp @@ -58,6 +58,8 @@ namespace Gecode { throw Float::ArgumentSizeMismatch("Float::relax"); if ((p < 0.0) || (p > 1.0)) throw Float::OutOfLimits("Float::relax"); + if (x.size() == 0) + return; Float::Relax fr; Search::relax(home, x, sx, r, p, fr); } diff --git a/gecode/int/ldsb.cpp b/gecode/int/ldsb.cpp index 36a9efcc09..67390f7e6a 100644 --- a/gecode/int/ldsb.cpp +++ b/gecode/int/ldsb.cpp @@ -53,6 +53,19 @@ namespace Gecode { namespace Int { namespace LDSB { return std::pair(-1, -1); } + bool + is_empty(const SymmetryHandle& s) { + if (VariableSymmetryObject* varref = dynamic_cast(s.ref)) + return varref->nxs == 0; + if (ValueSymmetryObject* valref = dynamic_cast(s.ref)) + return valref->values.size() == 0; + if (VariableSequenceSymmetryObject* varseqref = dynamic_cast(s.ref)) + return varseqref->nxs == 0; + if (ValueSequenceSymmetryObject* valseqref = dynamic_cast(s.ref)) + return valseqref->values.size() == 0; + return false; + } + }}} // namespace Gecode::Int::LDSB namespace Gecode { @@ -242,6 +255,30 @@ namespace Gecode { namespace Int { namespace LDSB { GECODE_NEVER; return nullptr; } + + /** + * \brief Create implementations of the non-empty symmetries in \a syms + * + * Each implementation is created by \a create_sym. Stores their number + * in \a nsyms and returns nullptr if there are none. + */ + template + SymmetryImp** + create_syms(Space& home, const Symmetries& syms, const VariableMap& variable_map, + SymmetryImp* (*create_sym)(Space&, const SymmetryHandle&, VariableMap), int& nsyms) { + nsyms = 0; + for (int i = 0; i < syms.size(); i++) + if (!is_empty(syms[i])) + nsyms++; + if (nsyms == 0) + return nullptr; + SymmetryImp** array = home.alloc*>(nsyms); + int created = 0; + for (int i = 0; i < syms.size(); i++) + if (!is_empty(syms[i])) + array[created++] = create_sym(home, syms[i], variable_map); + return array; + } }}} // namespace Gecode::Int::LDSB namespace Gecode { @@ -281,11 +318,8 @@ namespace Gecode { // Convert the modelling-level Symmetries object into an array of // SymmetryImp objects. - int n = syms.size(); - SymmetryImp** array = static_cast(home).alloc*>(n); - for (int i = 0; i < n; i++) { - array[i] = create_int_sym(home, syms[i], variable_map); - } + int n = 0; + SymmetryImp** array = create_syms(home, syms, variable_map, create_int_sym, n); post_ldsb_brancher(home, xv, vs, Branch::value_selection_commit(home, vals), array, n, bf, vvp); @@ -319,11 +353,8 @@ namespace Gecode { // Convert the modelling-level Symmetries object into an array of // SymmetryImp objects. - int n = syms.size(); - SymmetryImp** array = static_cast(home).alloc*>(n); - for (int i = 0; i < n; i++) { - array[i] = create_int_sym(home, syms[i], variable_map); - } + int n = 0; + SymmetryImp** array = create_syms(home, syms, variable_map, create_int_sym, n); ViewArray xv(home, x); if (vars.c.select() == IntVarBranch::SEL_NONE) { @@ -413,11 +444,8 @@ namespace Gecode { // Convert the modelling-level Symmetries object into an array of // SymmetryImp objects. - int n = syms.size(); - SymmetryImp** array = static_cast(home).alloc*>(n); - for (int i = 0; i < n; i++) { - array[i] = create_bool_sym(home, syms[i], variable_map); - } + int n = 0; + SymmetryImp** array = create_syms(home, syms, variable_map, create_bool_sym, n); // Technically these "bad" value selection could in fact work with // LDSB, because they degenerate to binary splitting for @@ -463,11 +491,8 @@ namespace Gecode { // Convert the modelling-level Symmetries object into an array of // SymmetryImp objects. - int n = syms.size(); - SymmetryImp** array = static_cast(home).alloc*>(n); - for (int i = 0; i < n; i++) { - array[i] = create_bool_sym(home, syms[i], variable_map); - } + int n = 0; + SymmetryImp** array = create_syms(home, syms, variable_map, create_bool_sym, n); // Technically these "bad" value selection could in fact work with // LDSB, because they degenerate to binary splitting for diff --git a/gecode/int/ldsb.hh b/gecode/int/ldsb.hh index 2eb534c0bf..306a36b871 100644 --- a/gecode/int/ldsb.hh +++ b/gecode/int/ldsb.hh @@ -155,6 +155,15 @@ namespace Gecode { namespace Int { namespace LDSB { ValueSequenceSymmetryObject(IntArgs vs, int ss); }; + /** + * \brief Test whether the symmetry \a s describes neither variables nor + * values + * + * Such a symmetry maps nothing, so branching ignores it. The test does + * not allocate. + */ + GECODE_INT_EXPORT bool is_empty(const SymmetryHandle& s); + /// Implementation of a single symmetry. template class SymmetryImp { @@ -331,7 +340,7 @@ namespace Gecode { namespace Int { namespace LDSB { class LDSBBrancher : public ViewValBrancher { public: using typename ViewValBrancher::Var; - /// Array of symmetry implementations + /// Array of symmetry implementations, nullptr when \a _nsyms is 0 SymmetryImp** _syms; /// Number of symmetry implementations int _nsyms; diff --git a/gecode/int/ldsb/brancher.hpp b/gecode/int/ldsb/brancher.hpp index fba1491148..3f2ae9c2e2 100644 --- a/gecode/int/ldsb/brancher.hpp +++ b/gecode/int/ldsb/brancher.hpp @@ -111,9 +111,13 @@ namespace Gecode { namespace Int { namespace LDSB { LDSBBrancher::LDSBBrancher(Space& home, LDSBBrancher& b) : ViewValBrancher(home, b), _nsyms(b._nsyms), _prev_pos(b._prev_pos) { - _syms = home.alloc*>(_nsyms); - for (int i = 0; i < _nsyms; i++) - _syms[i] = b._syms[i]->copy(home); + if (_nsyms > 0) { + _syms = home.alloc*>(_nsyms); + for (int i = 0; i < _nsyms; i++) + _syms[i] = b._syms[i]->copy(home); + } else { + _syms = nullptr; + } } template diff --git a/gecode/int/relax.cpp b/gecode/int/relax.cpp index 46dcfcbc84..e79eb0b351 100644 --- a/gecode/int/relax.cpp +++ b/gecode/int/relax.cpp @@ -66,6 +66,8 @@ namespace Gecode { throw Int::ArgumentSizeMismatch("Int::relax"); if ((p < 0.0) || (p > 1.0)) throw Int::OutOfLimits("Int::relax"); + if (x.size() == 0) + return; Int::Relax ir; Search::relax(home, x, sx, r, p, ir); } @@ -76,6 +78,8 @@ namespace Gecode { throw Int::ArgumentSizeMismatch("Int::relax"); if ((p < 0.0) || (p > 1.0)) throw Int::OutOfLimits("Int::relax"); + if (x.size() == 0) + return; Int::Relax ir; Search::relax(home, x, sx, r, p, ir); } diff --git a/gecode/set/ldsb.cpp b/gecode/set/ldsb.cpp index 33d40d86ce..64a4294f1a 100644 --- a/gecode/set/ldsb.cpp +++ b/gecode/set/ldsb.cpp @@ -121,6 +121,27 @@ namespace Gecode { namespace Set { namespace LDSB { GECODE_NEVER; return nullptr; } + + /** + * \brief Create implementations of the non-empty symmetries in \a syms + * + * Stores their number in \a nsyms and returns nullptr if there are none. + */ + SymmetryImp** + create_set_syms(Space& home, const Symmetries& syms, const VariableMap& variable_map, int& nsyms) { + nsyms = 0; + for (int i = 0; i < syms.size(); i++) + if (!is_empty(syms[i])) + nsyms++; + if (nsyms == 0) + return nullptr; + SymmetryImp** array = home.alloc*>(nsyms); + int created = 0; + for (int i = 0; i < syms.size(); i++) + if (!is_empty(syms[i])) + array[created++] = create_set_sym(home, syms[i], variable_map); + return array; + } }}} // namespace Gecode::Set::LDSB namespace Gecode { @@ -145,11 +166,8 @@ namespace Gecode { // Convert the modelling-level Symmetries object into an array of // SymmetryImp objects. - int n = syms.size(); - SymmetryImp** array = static_cast(home).alloc*>(n); - for (int i = 0; i < n; i++) { - array[i] = create_set_sym(home, syms[i], variable_map); - } + int n = 0; + SymmetryImp** array = create_set_syms(home, syms, variable_map, n); post_ldsb_set_brancher(home, xv, vs, Branch::value_selection_commit(home, vals), array, n, bf, vvp); @@ -182,11 +200,8 @@ namespace Gecode { // Convert the modelling-level Symmetries object into an array of // SymmetryImp objects. - int n = syms.size(); - SymmetryImp** array = static_cast(home).alloc*>(n); - for (int i = 0; i < n; i++) { - array[i] = Set::LDSB::create_set_sym(home, syms[i], variable_map); - } + int n = 0; + SymmetryImp** array = create_set_syms(home, syms, variable_map, n); ViewArray xv(home, x); ValSelCommitBase* vsc = Branch::value_selection_commit(home, vals); diff --git a/gecode/set/relax.cpp b/gecode/set/relax.cpp index 201fb45ace..6482628431 100644 --- a/gecode/set/relax.cpp +++ b/gecode/set/relax.cpp @@ -63,6 +63,8 @@ namespace Gecode { throw Set::ArgumentSizeMismatch("Set::relax"); if ((p < 0.0) || (p > 1.0)) throw Set::OutOfLimits("Set::relax"); + if (x.size() == 0) + return; Set::Relax ir; Search::relax(home, x, sx, r, p, ir); } diff --git a/test/ldsb.cpp b/test/ldsb.cpp index b0320748dc..0fc9c04bad 100644 --- a/test/ldsb.cpp +++ b/test/ldsb.cpp @@ -1709,6 +1709,188 @@ namespace Test { namespace LDSB { } }; +#endif + + /// Space with an array \a x of variables for the empty-symmetry tests + template + class EmptyFixture : public Space { + public: + /// Variables + VarArray x; + /// Constructor for creation of \a n variables with domain \a d + template + EmptyFixture(int n, Domain... d) : x(*this, n, d...) {} + /// Constructor for cloning \a s + EmptyFixture(EmptyFixture& s) : Space(s) { + x.update(*this, s.x); + } + /// Copy during cloning + Space* + copy(void) override { + return new EmptyFixture(*this); + } + }; + + /// Symmetries that the empty-symmetry tests branch with + enum class SymmetrySpec { + NONE, ///< No symmetry handles + EMPTY, ///< Handles that describe no variables and no values + INTERCHANGEABLE ///< The empty handles and interchangeable variables + }; + + /// Return the symmetries described by \a spec for the variables \a x + template + Symmetries + symmetries(SymmetrySpec spec, const VarArgs& x) { + Symmetries s; + if (spec == SymmetrySpec::NONE) + return s; + VarArgs none; + Matrix m(none, 0, 0); + s << variable_symmetry(none); + s << variable_sequence_symmetry(none, 2); + s << rows_interchange(m); + s << columns_interchange(m); + s << value_symmetry(IntArgs()); + s << value_symmetry(IntSet::empty); + s << value_sequence_symmetry(IntArgs(), 2); + s << values_reflect(0, -1); + if (spec == SymmetrySpec::INTERCHANGEABLE) + s << variable_symmetry(x); + return s; + } + + /// Return the number of solutions of \a s and delete \a s + template + int + count_solutions(T* s) { + DFS e(s); + delete s; + int n = 0; + while (T* t = e.next()) { + n++; + delete t; + } + return n; + } + + /// %Test that int LDSB branching ignores symmetries without variables or values + class EmptyInt : public Base { + private: + /// Space with variables of domain 0..1 + using Fixture = EmptyFixture; + /// Branch on the variables of \a s with symmetries \a spec, tie-breaking if \a tie + static void + post_branching(Fixture& s, SymmetrySpec spec, bool tie, IntValBranch vals) { + IntVarArgs x(s.x); + Symmetries syms = symmetries(spec, x); + if (spec != SymmetrySpec::NONE) + syms << variable_symmetry(x, IntArgs()); + if (tie) + branch(s, x, tie_break(INT_VAR_DEGREE_MAX(), INT_VAR_SIZE_MIN()), vals, syms); + else + branch(s, x, INT_VAR_NONE(), vals, syms); + } + /// Return the number of solutions for \a n variables + static int + solutions(int n, SymmetrySpec spec, bool tie) { + Fixture* s = new Fixture(n, 0, 1); + post_branching(*s, spec, tie, INT_VAL_MIN()); + return count_solutions(s); + } + /// Test whether splitting value selection is rejected without variables + static bool + rejects_split(bool tie) { + Fixture s(0, 0, 1); + try { + post_branching(s, SymmetrySpec::NONE, tie, INT_VAL_SPLIT_MIN()); + } catch (const Int::LDSBBadValueSelection&) { + return true; + } + return false; + } + + public: + /// Initialize test + EmptyInt(void) : Base("LDSB::EmptyInt") {} + /// Perform actual tests + bool + run(void) override { + for (bool tie : {false, true}) + if ((solutions(0, SymmetrySpec::NONE, tie) != 1) || (solutions(0, SymmetrySpec::EMPTY, tie) != 1) || + (solutions(2, SymmetrySpec::NONE, tie) != 4) || (solutions(2, SymmetrySpec::EMPTY, tie) != 4) || + (solutions(2, SymmetrySpec::INTERCHANGEABLE, tie) != 3) || !rejects_split(tie)) + return false; + return true; + } + }; + + /// %Test that Boolean LDSB branching ignores symmetries without variables or values + class EmptyBool : public Base { + private: + /// Space with Boolean variables + using Fixture = EmptyFixture; + /// Return the number of solutions for \a n variables, tie-breaking if \a tie + static int + solutions(int n, SymmetrySpec spec, bool tie) { + Fixture* s = new Fixture(n, 0, 1); + BoolVarArgs x(s->x); + if (tie) + branch(*s, x, tie_break(BOOL_VAR_DEGREE_MAX(), BOOL_VAR_DEGREE_MIN()), BOOL_VAL_MIN(), + symmetries(spec, x)); + else + branch(*s, x, BOOL_VAR_NONE(), BOOL_VAL_MIN(), symmetries(spec, x)); + return count_solutions(s); + } + + public: + /// Initialize test + EmptyBool(void) : Base("LDSB::EmptyBool") {} + /// Perform actual tests + bool + run(void) override { + for (bool tie : {false, true}) + if ((solutions(0, SymmetrySpec::NONE, tie) != 1) || (solutions(0, SymmetrySpec::EMPTY, tie) != 1) || + (solutions(2, SymmetrySpec::NONE, tie) != 4) || (solutions(2, SymmetrySpec::EMPTY, tie) != 4) || + (solutions(2, SymmetrySpec::INTERCHANGEABLE, tie) != 3)) + return false; + return true; + } + }; + +#ifdef GECODE_HAS_SET_VARS + /// %Test that set LDSB branching ignores symmetries without variables or values + class EmptySet : public Base { + private: + /// Space with set variables + using Fixture = EmptyFixture; + /// Return the number of solutions for \a n subsets of {0}, tie-breaking if \a tie + static int + solutions(int n, SymmetrySpec spec, bool tie) { + Fixture* s = new Fixture(n, IntSet::empty, 0, 0); + SetVarArgs x(s->x); + if (tie) + branch(*s, x, tie_break(SET_VAR_DEGREE_MAX(), SET_VAR_SIZE_MIN()), SET_VAL_MIN_INC(), + symmetries(spec, x)); + else + branch(*s, x, SET_VAR_NONE(), SET_VAL_MIN_INC(), symmetries(spec, x)); + return count_solutions(s); + } + + public: + /// Initialize test + EmptySet(void) : Base("LDSB::EmptySet") {} + /// Perform actual tests + bool + run(void) override { + for (bool tie : {false, true}) + if ((solutions(0, SymmetrySpec::NONE, tie) != 1) || (solutions(0, SymmetrySpec::EMPTY, tie) != 1) || + (solutions(2, SymmetrySpec::NONE, tie) != 4) || (solutions(2, SymmetrySpec::EMPTY, tie) != 4) || + (solutions(2, SymmetrySpec::INTERCHANGEABLE, tie) != 3)) + return false; + return true; + } + }; #endif LDSB varsym1("VarSym1"); @@ -1738,6 +1920,8 @@ namespace Test { namespace LDSB { LDSBLatin latin("Latin"); LDSB recomp("Recomputation", 999, 999); LDSB tie_break("TieBreak"); + EmptyInt empty_int; + EmptyBool empty_bool; #ifdef GECODE_HAS_SET_VARS LDSB reflectsym1("ReflectSym1"); @@ -1749,6 +1933,7 @@ namespace Test { namespace LDSB { LDSBSet setvalsym2("SetValSym2", 0, 1); LDSBSet setvarseqsym1("SetVarSeqSym1"); LDSBSet setvarseqsym2("SetVarSeqSym2"); + EmptySet empty_set; #endif }} // namespace Test::LDSB diff --git a/test/search.cpp b/test/search.cpp index e671b207e8..c4bdd6f201 100644 --- a/test/search.cpp +++ b/test/search.cpp @@ -2647,6 +2647,70 @@ namespace Test { return true; } }; + /// Relaxing empty variable arrays posts nothing + class RelaxEmpty : public Base { + private: + /// Space without variables + class Fixture : public Gecode::Space { + public: + Fixture(void) = default; + Fixture(Fixture& fixture) : Gecode::Space(fixture) {} + Gecode::Space* + copy(void) override { + return new Fixture(*this); + } + }; + /// Test whether relaxing empty \a VarArgs leaves a space with exactly one solution + template + static bool + relaxes_empty(void) { + Fixture* s = new Fixture; + Gecode::relax(*s, VarArgs(), VarArgs(), Gecode::Rnd(1U), 0.5); + bool solved = s->status() == Gecode::SS_SOLVED; + Gecode::DFS e(s); + delete s; + int n = 0; + while (Fixture* t = e.next()) { + n++; + delete t; + } + return solved && (n == 1); + } + /// Test whether relaxing empty arrays still checks the arguments + static bool + checks_arguments(void) { + Fixture s; + bool mismatch = false; + try { + Gecode::relax(s, Gecode::IntVarArgs(), Gecode::IntVarArgs(s, 1, 0, 0), Gecode::Rnd(1U), 0.5); + } catch (const Gecode::Int::ArgumentSizeMismatch&) { + mismatch = true; + } + bool limits = false; + try { + Gecode::relax(s, Gecode::IntVarArgs(), Gecode::IntVarArgs(), Gecode::Rnd(1U), 2.0); + } catch (const Gecode::Int::OutOfLimits&) { + limits = true; + } + return mismatch && limits; + } + + public: + RelaxEmpty(void) : Base("Search::Relax::Empty") {} + bool + run(void) override { + bool ok = relaxes_empty() && relaxes_empty(); +#ifdef GECODE_HAS_SET_VARS + ok = ok && relaxes_empty(); +#endif +#ifdef GECODE_HAS_FLOAT_VARS + ok = ok && relaxes_empty(); +#endif + return ok && checks_arguments(); + } + }; + + RelaxEmpty relax_empty; /// %Test for depth-first search template From b754f0cf008a231e537e0c5ce6078f95f12dff9d Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Sun, 1 Nov 2026 18:47:59 +0100 Subject: [PATCH 4/7] Decide empty set channel, element, rel-op and weights at posting Zdev-Change-Id: Z59edca85bfdb7c2701928e5f8262679749d118c3a98e03042baf26879102ba98 --- gecode/set/channel/bool.hpp | 4 ++ gecode/set/channel/sorted.hpp | 2 + gecode/set/element/disjoint.hpp | 3 ++ gecode/set/element/inter.hpp | 8 +++ gecode/set/element/union.hpp | 5 ++ gecode/set/element/unionConst.hpp | 5 ++ gecode/set/int/weights.hpp | 6 +++ gecode/set/rel-op/partition.hpp | 8 +++ gecode/set/rel-op/union.hpp | 8 +++ test/set/channel.cpp | 59 ++++++++++++++++++++++ test/set/element.cpp | 81 +++++++++++++++++++++++++++++++ test/set/int.cpp | 51 +++++++++++++++++++ test/set/rel-op-const.cpp | 56 +++++++++++++++++++++ 13 files changed, 296 insertions(+) diff --git a/gecode/set/channel/bool.hpp b/gecode/set/channel/bool.hpp index 29bf83bc61..a62407d14a 100644 --- a/gecode/set/channel/bool.hpp +++ b/gecode/set/channel/bool.hpp @@ -111,6 +111,10 @@ namespace Gecode { namespace Set { namespace Channel { template forceinline ExecStatus ChannelBool::post(Home home, ViewArray& x, View y) { + if (x.size() == 0) { + GECODE_ME_CHECK(y.card_max(home, 0)); + return ES_OK; + } GECODE_ME_CHECK(y.intersect(home, 0, x.size() - 1)); (void)new (home) ChannelBool(home, x, y); return ES_OK; diff --git a/gecode/set/channel/sorted.hpp b/gecode/set/channel/sorted.hpp index 47a4ca094f..e87b0e487d 100644 --- a/gecode/set/channel/sorted.hpp +++ b/gecode/set/channel/sorted.hpp @@ -62,6 +62,8 @@ namespace Gecode { namespace Set { namespace Channel { unsigned int xs_size = static_cast(xs.size()); GECODE_ME_CHECK(x0.card_min(home, xs_size)); GECODE_ME_CHECK(x0.card_max(home, xs_size)); + if (xs_size == 0) + return ES_OK; if (xs_size == 1) { SingletonView sv(xs[0]); GECODE_ES_CHECK((Rel::Eq::post(home, x0, sv))); diff --git a/gecode/set/element/disjoint.hpp b/gecode/set/element/disjoint.hpp index 82f7fd56cf..d479bd21d0 100644 --- a/gecode/set/element/disjoint.hpp +++ b/gecode/set/element/disjoint.hpp @@ -59,6 +59,9 @@ namespace Gecode { namespace Set { namespace Element { // s2 \subseteq {0,...,n-1} Iter::Ranges::Singleton s(0, n - 1); GECODE_ME_CHECK(x1.intersect_i(home, s)); + // Nothing can be selected, so the selected sets are disjoint + if (n == 0) + return ES_OK; (void)new (home) ElementDisjoint(home, xs, x1); return ES_OK; } diff --git a/gecode/set/element/inter.hpp b/gecode/set/element/inter.hpp index c5fa4bda66..70089aa4bd 100644 --- a/gecode/set/element/inter.hpp +++ b/gecode/set/element/inter.hpp @@ -92,6 +92,14 @@ namespace Gecode { namespace Set { namespace Element { // x0 \subseteq {1,...,n} Iter::Ranges::Singleton s(0, n - 1); GECODE_ME_CHECK(x0.intersect_i(home, s)); + if (n == 0) { + // Nothing can be selected, and the empty intersection is the universe + IntSetRanges glb(universe); + GECODE_ME_CHECK(x1.include_i(home, glb)); + IntSetRanges lub(universe); + GECODE_ME_CHECK(x1.intersect_i(home, lub)); + return ES_OK; + } (void)new (home) ElementIntersection(home, xs, x0, x1, universe); return ES_OK; } diff --git a/gecode/set/element/union.hpp b/gecode/set/element/union.hpp index c1d0cf287b..7b105f1c3a 100644 --- a/gecode/set/element/union.hpp +++ b/gecode/set/element/union.hpp @@ -88,6 +88,11 @@ namespace Gecode { namespace Set { namespace Element { // x0 \subseteq {1,...,n} Iter::Ranges::Singleton s(0, n - 1); GECODE_ME_CHECK(x0.intersect_i(home, s)); + if (n == 0) { + // Nothing can be selected, and the empty union is empty + GECODE_ME_CHECK(x1.card_max(home, 0)); + return ES_OK; + } (void)new (home) ElementUnion(home, xs, x0, x1); return ES_OK; } diff --git a/gecode/set/element/unionConst.hpp b/gecode/set/element/unionConst.hpp index c8aa018132..da25e443cf 100644 --- a/gecode/set/element/unionConst.hpp +++ b/gecode/set/element/unionConst.hpp @@ -92,6 +92,11 @@ namespace Gecode { namespace Set { namespace Element { // s2 \subseteq {1,...,n} Iter::Ranges::Singleton s(0, n - 1); GECODE_ME_CHECK(x1.intersect_i(home, s)); + if (n == 0) { + // Nothing can be selected, and the empty union is empty + GECODE_ME_CHECK(x0.card_max(home, 0)); + return ES_OK; + } (void)new (home) ElementUnionConst(home, x0, xs, x1); return ES_OK; } diff --git a/gecode/set/int/weights.hpp b/gecode/set/int/weights.hpp index 9b792114d2..724cd3de5d 100644 --- a/gecode/set/int/weights.hpp +++ b/gecode/set/int/weights.hpp @@ -160,6 +160,12 @@ namespace Gecode { namespace Set { namespace Int { Gecode::Int::IntView y) { if (elements.size() != weights.size()) throw ArgumentSizeMismatch("Weights"); + if (elements.size() == 0) { + // The set must be empty, and the empty set weighs 0 + GECODE_ME_CHECK(x.card_max(home, 0)); + GECODE_ME_CHECK(y.eq(home, 0)); + return ES_OK; + } Region r; int* els_arr = r.alloc(elements.size()); for (int i = elements.size(); i--;) diff --git a/gecode/set/rel-op/partition.hpp b/gecode/set/rel-op/partition.hpp index b99792c626..69d98630bf 100644 --- a/gecode/set/rel-op/partition.hpp +++ b/gecode/set/rel-op/partition.hpp @@ -90,6 +90,14 @@ namespace Gecode { namespace Set { namespace RelOp { template ExecStatus PartitionN::post(Home home, ViewArray& x, const IntSet& z, View1 y) { + if (x.size() == 0) { + // With no operands, y equals z + IntSetRanges glb(z); + GECODE_ME_CHECK(y.include_i(home, glb)); + IntSetRanges lub(z); + GECODE_ME_CHECK(y.intersect_i(home, lub)); + return ES_OK; + } (void)new (home) PartitionN(home, x, z, y); return ES_OK; } diff --git a/gecode/set/rel-op/union.hpp b/gecode/set/rel-op/union.hpp index 6f61f6de76..f88c1bc5f3 100644 --- a/gecode/set/rel-op/union.hpp +++ b/gecode/set/rel-op/union.hpp @@ -232,6 +232,14 @@ namespace Gecode { namespace Set { namespace RelOp { template ExecStatus UnionN::post(Home home, ViewArray& x, const IntSet& z, View1 y) { + if (x.size() == 0) { + // With no operands, y equals z + IntSetRanges glb(z); + GECODE_ME_CHECK(y.include_i(home, glb)); + IntSetRanges lub(z); + GECODE_ME_CHECK(y.intersect_i(home, lub)); + return ES_OK; + } (void)new (home) UnionN(home, x, z, y); return ES_OK; } diff --git a/test/set/channel.cpp b/test/set/channel.cpp index 6b56b98e55..ee5cd58abe 100644 --- a/test/set/channel.cpp +++ b/test/set/channel.cpp @@ -95,6 +95,47 @@ namespace Test { namespace Set { }; ChannelSorted _channel_sorted("Channel::Sorted"); + /// Space with one set variable for tests without integer variables + class EmptyFixture : public Gecode::Space { + public: + /// Set variable to channel to + SetVar y; + /// Create fixture, where \a glb is the greatest lower bound of \a y + EmptyFixture(const IntSet& glb) : y(*this, glb, IntSet(0, 3)) {} + /// Constructor for cloning \a fixture + EmptyFixture(EmptyFixture& fixture) : Gecode::Space(fixture) { + y.update(*this, fixture.y); + } + /// Copy during cloning + Gecode::Space* + copy(void) override { + return new EmptyFixture(*this); + } + /// Test whether the space is solved with \a y empty + bool + is_solved_empty(void) { + return (status() == SS_SOLVED) && (y.lub_size() == 0); + } + }; + + /// %Test sorted channeling without integer variables + class ChannelSortedEmpty : public Base { + public: + /// Create and register test + ChannelSortedEmpty(void) : Base("Set::Channel::Sorted::Empty") {} + /// Perform test + bool + run(void) override { + IntVarArgs x; + EmptyFixture empty(IntSet::empty); + channel_sorted(empty, x, empty.y); + EmptyFixture nonempty(IntSet(1, 1)); + channel_sorted(nonempty, x, nonempty.y); + return empty.is_solved_empty() && (nonempty.status() == SS_FAILED); + } + }; + ChannelSortedEmpty _channel_sorted_empty; + /// %Test for integer channel constraint class ChannelInt : public SetTest { private: @@ -181,6 +222,24 @@ namespace Test { namespace Set { ChannelBool _channelbool2(TestTags::sweep(), "Channel::Bool::2", d3, 3); ChannelBool _channelbool3(TestTags::sweep(), "Channel::Bool::3", d4, 5); + /// %Test Boolean channeling without Boolean variables + class ChannelBoolEmpty : public Base { + public: + /// Create and register test + ChannelBoolEmpty(void) : Base("Set::Channel::Bool::Empty") {} + /// Perform test + bool + run(void) override { + BoolVarArgs x; + EmptyFixture empty(IntSet::empty); + channel(empty, x, empty.y); + EmptyFixture nonempty(IntSet(1, 1)); + channel(nonempty, x, nonempty.y); + return empty.is_solved_empty() && (nonempty.status() == SS_FAILED); + } + }; + ChannelBoolEmpty _channel_bool_empty; + /// %Test for set channel constraint class ChannelSet : public SetTest { private: diff --git a/test/set/element.cpp b/test/set/element.cpp index 6cc931d292..8b2e411b0b 100644 --- a/test/set/element.cpp +++ b/test/set/element.cpp @@ -350,6 +350,87 @@ namespace Test { namespace Set { }; ElementSetConst _elementsetconst("Element::SetConst"); + /// Space with an index and a result set variable for tests without sets + class EmptyFixture : public Gecode::Space { + public: + /// Index set + SetVar y; + /// Result set + SetVar z; + /// Create fixture, where \a glb is the greatest lower bound of \a y + EmptyFixture(const IntSet& glb = IntSet::empty) + : y(*this, glb, IntSet(0, 3)), z(*this, IntSet::empty, IntSet(0, 3)) {} + /// Constructor for cloning \a fixture + EmptyFixture(EmptyFixture& fixture) : Gecode::Space(fixture) { + y.update(*this, fixture.y); + z.update(*this, fixture.z); + } + /// Copy during cloning + Gecode::Space* + copy(void) override { + return new EmptyFixture(*this); + } + /// Test whether the space is solved with \a y empty and \a z equal to \a result + bool + is_solved(const IntSet& result) { + if ((status() != SS_SOLVED) || (y.lub_size() != 0) || !z.assigned()) + return false; + SetVarGlbRanges zr(z); + IntSetRanges rr(result); + return Iter::Ranges::equal(zr, rr); + } + }; + + /** + * \brief Test element over an empty array of type \a Args + * + * An empty union is empty and an empty intersection is the universe. + */ + template + bool + is_decided_empty(void) { + Args x; + IntSet universe(0, 3); + EmptyFixture union_x, dunion_x, inter_x; + element(union_x, SOT_UNION, x, union_x.y, union_x.z); + element(dunion_x, SOT_DUNION, x, dunion_x.y, dunion_x.z); + element(inter_x, SOT_INTER, x, inter_x.y, inter_x.z, universe); + bool decided = + union_x.is_solved(IntSet::empty) && dunion_x.is_solved(IntSet::empty) && inter_x.is_solved(universe); + + // No index can be selected, and the default universe does not fit into z + EmptyFixture selected(IntSet(0, 0)), inter_default; + element(selected, SOT_UNION, x, selected.y, selected.z); + element(inter_default, SOT_INTER, x, inter_default.y, inter_default.z); + return decided && (selected.status() == SS_FAILED) && (inter_default.status() == SS_FAILED); + } + + /// %Test element over no constant sets + class ElementConstEmpty : public Base { + public: + /// Create and register test + ElementConstEmpty(void) : Base("Set::Element::Const::Empty") {} + /// Perform test + bool + run(void) override { + return is_decided_empty() && is_decided_empty(); + } + }; + ElementConstEmpty _element_const_empty; + + /// %Test element over no set or integer variables + class ElementVarEmpty : public Base { + public: + /// Create and register test + ElementVarEmpty(void) : Base("Set::Element::Var::Empty") {} + /// Perform test + bool + run(void) override { + return is_decided_empty() && is_decided_empty(); + } + }; + ElementVarEmpty _element_var_empty; + /// %Test for matrix element with integer set array and set variable class MatrixIntSet : public SetTest { protected: diff --git a/test/set/int.cpp b/test/set/int.cpp index 779ec2f040..cb7e368e94 100644 --- a/test/set/int.cpp +++ b/test/set/int.cpp @@ -499,6 +499,57 @@ namespace Test { namespace Set { IntArgs w4(7, w4v); Weights _weights4("Int::Weights::4", el1, w4); + /// Space with a set and a weight variable for tests without elements + class WeightsFixture : public Gecode::Space { + public: + /// Weighted set + SetVar x; + /// Weight of \a x + IntVar y; + /// Create fixture with greatest lower bound \a glb for \a x and minimal weight \a min + WeightsFixture(const IntSet& glb, int min) : x(*this, glb, ds_33), y(*this, min, 2) {} + /// Constructor for cloning \a fixture + WeightsFixture(WeightsFixture& fixture) : Gecode::Space(fixture) { + x.update(*this, fixture.x); + y.update(*this, fixture.y); + } + /// Copy during cloning + Gecode::Space* + copy(void) override { + return new WeightsFixture(*this); + } + }; + + /// %Test weights without elements: the set is empty and weighs 0 + class WeightsEmpty : public Base { + public: + /// Create and register test + WeightsEmpty(void) : Base("Set::Int::Weights::Empty") {} + /// Perform test + bool + run(void) override { + IntArgs none; + WeightsFixture empty(IntSet::empty, -2); + weights(empty, none, none, empty.x, empty.y); + bool solved = (empty.status() == SS_SOLVED) && (empty.x.lub_size() == 0) && empty.y.assigned() && + (empty.y.val() == 0); + + WeightsFixture nonempty(IntSet(0, 0), -2), heavy(IntSet::empty, 1); + weights(nonempty, none, none, nonempty.x, nonempty.y); + weights(heavy, none, none, heavy.x, heavy.y); + bool failed = (nonempty.status() == SS_FAILED) && (heavy.status() == SS_FAILED); + + WeightsFixture mismatch(IntSet::empty, -2); + try { + weights(mismatch, none, IntArgs({1}), mismatch.x, mismatch.y); + return false; + } catch (Gecode::Set::ArgumentSizeMismatch&) { + return solved && failed; + } + } + }; + WeightsEmpty _weights_empty; + } // namespace Int }} // namespace Test::Set diff --git a/test/set/rel-op-const.cpp b/test/set/rel-op-const.cpp index 521dc2d4df..809d1909fb 100644 --- a/test/set/rel-op-const.cpp +++ b/test/set/rel-op-const.cpp @@ -313,6 +313,62 @@ namespace Test { namespace Set { } }; + /// Space with one set variable for tests without operands + class EmptyFixture : public Gecode::Space { + public: + /// Result set + SetVar y; + /// Create fixture, where \a glb is the greatest lower bound of \a y + EmptyFixture(const IntSet& glb = IntSet::empty) : y(*this, glb, ds_33) {} + /// Constructor for cloning \a fixture + EmptyFixture(EmptyFixture& fixture) : Gecode::Space(fixture) { + y.update(*this, fixture.y); + } + /// Copy during cloning + Gecode::Space* + copy(void) override { + return new EmptyFixture(*this); + } + /// Test whether the space is solved with \a y equal to \a result + bool + is_solved(const IntSet& result) { + if ((status() != SS_SOLVED) || !y.assigned()) + return false; + SetVarGlbRanges yr(y); + IntSetRanges rr(result); + return Iter::Ranges::equal(yr, rr); + } + }; + + /// Test whether the operation \a op on empty \a Args and a constant \a z gives \a z + template + bool + is_result_constant(SetOpType op) { + Args x; + IntSet z(-1, 1); + EmptyFixture equal, larger(IntSet(2, 2)); + rel(equal, op, x, z, equal.y); + rel(larger, op, x, z, larger.y); + return equal.is_solved(z) && (larger.status() == SS_FAILED); + } + + /// %Test operations with a constant and no variables + class Empty : public Base { + public: + /// Create and register test + Empty(void) : Base("Set::RelOpConst::Empty") {} + /// Perform test + bool + run(void) override { + for (SetOpType op : {SOT_UNION, SOT_DUNION, SOT_INTER}) { + if (!is_result_constant(op) || !is_result_constant(op)) + return false; + } + return true; + } + }; + Empty _empty; + /// Help class to create and register tests class Create { public: From f6cbab7914b68750858c6f9524143fde01305646 Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Sun, 1 Nov 2026 19:55:23 +0100 Subject: [PATCH 5/7] Skip empty term buffers in linear posting and minimodel expressions Zdev-Change-Id: Z0fbe6f16aff95c9a39ff5430e3bc4f73d4399ffde75421d1d68b5d450ff5b34b --- gecode/float/linear.cpp | 28 +++++++++-------- gecode/int/linear-bool.cpp | 16 +++++----- gecode/int/linear-int.cpp | 28 +++++++++-------- gecode/minimodel/bool-expr.cpp | 6 ++-- gecode/minimodel/float-arith.cpp | 14 ++++++--- gecode/minimodel/float-expr.cpp | 4 +-- gecode/minimodel/int-expr.cpp | 6 ++-- test/float/arithmetic.cpp | 39 +++++++++++++++++++++++ test/float/linear.cpp | 49 +++++++++++++++++++++++++++++ test/float/mm-lin.cpp | 39 +++++++++++++++++++++++ test/int/linear.cpp | 53 ++++++++++++++++++++++++++++++++ test/int/mm-bool.cpp | 39 +++++++++++++++++++++++ test/int/mm-lin.cpp | 46 +++++++++++++++++++++++++++ 13 files changed, 324 insertions(+), 43 deletions(-) diff --git a/gecode/float/linear.cpp b/gecode/float/linear.cpp index 2877bbb439..fa196d712b 100644 --- a/gecode/float/linear.cpp +++ b/gecode/float/linear.cpp @@ -41,12 +41,13 @@ namespace Gecode { using namespace Float; GECODE_POST; Region re; - Linear::Term* t = re.alloc(x.size()); - for (int i = x.size(); i--;) { + int n = x.size(); + Linear::Term* t = (n > 0) ? re.alloc(n) : nullptr; + for (int i = 0; i < n; i++) { t[i].a = 1.0; t[i].x = x[i]; } - Linear::post(home, t, x.size(), frt, c); + Linear::post(home, t, n, frt, c); } void @@ -54,12 +55,13 @@ namespace Gecode { using namespace Float; GECODE_POST; Region re; - Linear::Term* t = re.alloc(x.size()); - for (int i = x.size(); i--;) { + int n = x.size(); + Linear::Term* t = (n > 0) ? re.alloc(n) : nullptr; + for (int i = 0; i < n; i++) { t[i].a = 1.0; t[i].x = x[i]; } - Linear::post(home, t, x.size(), frt, c, r); + Linear::post(home, t, n, frt, c, r); } void @@ -69,12 +71,13 @@ namespace Gecode { throw ArgumentSizeMismatch("Float::linear"); GECODE_POST; Region re; - Linear::Term* t = re.alloc(x.size()); - for (int i = x.size(); i--;) { + int n = x.size(); + Linear::Term* t = (n > 0) ? re.alloc(n) : nullptr; + for (int i = 0; i < n; i++) { t[i].a = a[i]; t[i].x = x[i]; } - Linear::post(home, t, x.size(), frt, c); + Linear::post(home, t, n, frt, c); } void @@ -84,12 +87,13 @@ namespace Gecode { throw ArgumentSizeMismatch("Float::linear"); GECODE_POST; Region re; - Linear::Term* t = re.alloc(x.size()); - for (int i = x.size(); i--;) { + int n = x.size(); + Linear::Term* t = (n > 0) ? re.alloc(n) : nullptr; + for (int i = 0; i < n; i++) { t[i].a = a[i]; t[i].x = x[i]; } - Linear::post(home, t, x.size(), frt, c, r); + Linear::post(home, t, n, frt, c, r); } void diff --git a/gecode/int/linear-bool.cpp b/gecode/int/linear-bool.cpp index 6cebc7ce98..9418d7ac7d 100644 --- a/gecode/int/linear-bool.cpp +++ b/gecode/int/linear-bool.cpp @@ -42,7 +42,7 @@ namespace Gecode { int n = x.size(); Region re; - Linear::Term* t = re.alloc>(n); + Linear::Term* t = (n > 0) ? re.alloc>(n) : nullptr; for (int i = 0; i < n; i++) { t[i].a = 1; t[i].x = x[i]; @@ -57,7 +57,7 @@ namespace Gecode { int n = x.size(); Region re; - Linear::Term* t = re.alloc>(n); + Linear::Term* t = (n > 0) ? re.alloc>(n) : nullptr; for (int i = 0; i < n; i++) { t[i].a = 1; t[i].x = x[i]; @@ -75,7 +75,7 @@ namespace Gecode { int n = x.size(); Region re; - Linear::Term* t = re.alloc>(n); + Linear::Term* t = (n > 0) ? re.alloc>(n) : nullptr; for (int i = 0; i < n; i++) { t[i].a = a[i]; t[i].x = x[i]; @@ -93,7 +93,7 @@ namespace Gecode { int n = x.size(); Region re; - Linear::Term* t = re.alloc>(n); + Linear::Term* t = (n > 0) ? re.alloc>(n) : nullptr; for (int i = 0; i < n; i++) { t[i].a = a[i]; t[i].x = x[i]; @@ -125,7 +125,7 @@ namespace Gecode { int n = x.size(); Region re; - Linear::Term* t = re.alloc>(n); + Linear::Term* t = (n > 0) ? re.alloc>(n) : nullptr; for (int i = 0; i < n; i++) { t[i].a = 1; t[i].x = x[i]; @@ -140,7 +140,7 @@ namespace Gecode { int n = x.size(); Region re; - Linear::Term* t = re.alloc>(n); + Linear::Term* t = (n > 0) ? re.alloc>(n) : nullptr; for (int i = 0; i < n; i++) { t[i].a = 1; t[i].x = x[i]; @@ -157,7 +157,7 @@ namespace Gecode { int n = x.size(); Region re; - Linear::Term* t = re.alloc>(n); + Linear::Term* t = (n > 0) ? re.alloc>(n) : nullptr; for (int i = 0; i < n; i++) { t[i].a = a[i]; t[i].x = x[i]; @@ -174,7 +174,7 @@ namespace Gecode { int n = x.size(); Region re; - Linear::Term* t = re.alloc>(n); + Linear::Term* t = (n > 0) ? re.alloc>(n) : nullptr; for (int i = 0; i < n; i++) { t[i].a = a[i]; t[i].x = x[i]; diff --git a/gecode/int/linear-int.cpp b/gecode/int/linear-int.cpp index c0da12e986..af656eca12 100644 --- a/gecode/int/linear-int.cpp +++ b/gecode/int/linear-int.cpp @@ -40,24 +40,26 @@ namespace Gecode { linear(Home home, const IntVarArgs& x, IntRelType irt, int c, IntPropLevel ipl) { GECODE_POST; Region re; - Linear::Term* t = re.alloc>(x.size()); - for (int i = 0; i < x.size(); i++) { + int n = x.size(); + Linear::Term* t = (n > 0) ? re.alloc>(n) : nullptr; + for (int i = 0; i < n; i++) { t[i].a = 1; t[i].x = x[i]; } - Linear::post(home, t, x.size(), irt, c, ipl); + Linear::post(home, t, n, irt, c, ipl); } void linear(Home home, const IntVarArgs& x, IntRelType irt, int c, Reify r, IntPropLevel) { GECODE_POST; Region re; - Linear::Term* t = re.alloc>(x.size()); - for (int i = 0; i < x.size(); i++) { + int n = x.size(); + Linear::Term* t = (n > 0) ? re.alloc>(n) : nullptr; + for (int i = 0; i < n; i++) { t[i].a = 1; t[i].x = x[i]; } - Linear::post(home, t, x.size(), irt, c, r); + Linear::post(home, t, n, irt, c, r); } void @@ -66,12 +68,13 @@ namespace Gecode { throw ArgumentSizeMismatch("Int::linear"); GECODE_POST; Region re; - Linear::Term* t = re.alloc>(x.size()); - for (int i = 0; i < x.size(); i++) { + int n = x.size(); + Linear::Term* t = (n > 0) ? re.alloc>(n) : nullptr; + for (int i = 0; i < n; i++) { t[i].a = a[i]; t[i].x = x[i]; } - Linear::post(home, t, x.size(), irt, c, ipl); + Linear::post(home, t, n, irt, c, ipl); } void @@ -80,12 +83,13 @@ namespace Gecode { throw ArgumentSizeMismatch("Int::linear"); GECODE_POST; Region re; - Linear::Term* t = re.alloc>(x.size()); - for (int i = 0; i < x.size(); i++) { + int n = x.size(); + Linear::Term* t = (n > 0) ? re.alloc>(n) : nullptr; + for (int i = 0; i < n; i++) { t[i].a = a[i]; t[i].x = x[i]; } - Linear::post(home, t, x.size(), irt, c, r); + Linear::post(home, t, n, irt, c, r); } void diff --git a/gecode/minimodel/bool-expr.cpp b/gecode/minimodel/bool-expr.cpp index b0b38f76c0..5cc28714e9 100644 --- a/gecode/minimodel/bool-expr.cpp +++ b/gecode/minimodel/bool-expr.cpp @@ -666,13 +666,15 @@ namespace Gecode { }; BElementExpr::BElementExpr(const BoolVarArgs& b, const LinIntExpr& idx) - : a(static_cast(heap.ralloc(sizeof(BoolExpr) * b.size()))), n(b.size()), idx(idx) { + : a((b.size() > 0) ? static_cast(heap.ralloc(sizeof(BoolExpr) * b.size())) : nullptr), n(b.size()), + idx(idx) { for (int i = b.size(); i--;) new (&a[i]) BoolExpr(b[i]); } BElementExpr::~BElementExpr(void) { - heap.free(a, n); + if (a != nullptr) + heap.free(a, n); } void diff --git a/gecode/minimodel/float-arith.cpp b/gecode/minimodel/float-arith.cpp index 5a68115ddc..f2d620c2da 100644 --- a/gecode/minimodel/float-arith.cpp +++ b/gecode/minimodel/float-arith.cpp @@ -69,14 +69,18 @@ namespace Gecode { namespace MiniModel { int n; /// Integer argument (used in nroot for example) int a_int; + /// Allocate \a n expressions, or none if \a n is zero + static LinFloatExpr* + allocate(int n) { + return (n > 0) ? heap.alloc(n) : nullptr; + } /// Constructors - ArithNonLinFloatExpr(ArithNonLinFloatExprType t0, int n0) - : t(t0), a(heap.alloc(n0)), n(n0), a_int(-1) {} - ArithNonLinFloatExpr(ArithNonLinFloatExprType t0, int n0, int a0) - : t(t0), a(heap.alloc(n0)), n(n0), a_int(a0) {} + ArithNonLinFloatExpr(ArithNonLinFloatExprType t0, int n0) : t(t0), a(allocate(n0)), n(n0), a_int(-1) {} + ArithNonLinFloatExpr(ArithNonLinFloatExprType t0, int n0, int a0) : t(t0), a(allocate(n0)), n(n0), a_int(a0) {} /// Destructor ~ArithNonLinFloatExpr(void) override { - heap.free(a, n); + if (a != nullptr) + heap.free(a, n); } /// Post expression FloatVar diff --git a/gecode/minimodel/float-expr.cpp b/gecode/minimodel/float-expr.cpp index 528f4d8e5b..2aedf9e8fa 100644 --- a/gecode/minimodel/float-expr.cpp +++ b/gecode/minimodel/float-expr.cpp @@ -174,7 +174,7 @@ namespace Gecode { n->r->a == 1) { (void)n->l->sum.ne->post(home, &n->r->x_float); } else { - Float::Linear::Term* fts = r.alloc(n->n_float); + Float::Linear::Term* fts = (n->n_float > 0) ? r.alloc(n->n_float) : nullptr; FloatVal c = n->fill(home, fts); Float::Linear::post(home, fts, n->n_float, frt, -c); } @@ -206,7 +206,7 @@ namespace Gecode { } n->r->sum.ne->post(home, frt, n->c, b); } else { - Float::Linear::Term* fts = r.alloc(n->n_float); + Float::Linear::Term* fts = (n->n_float > 0) ? r.alloc(n->n_float) : nullptr; FloatVal c = n->fill(home, fts); Float::Linear::post(home, fts, n->n_float, frt, -c, b); } diff --git a/gecode/minimodel/int-expr.cpp b/gecode/minimodel/int-expr.cpp index 6b1d9dee17..5027f97c54 100644 --- a/gecode/minimodel/int-expr.cpp +++ b/gecode/minimodel/int-expr.cpp @@ -214,7 +214,8 @@ namespace Gecode { n->l->t == NT_NONLIN && n->r->a == 1) { (void)n->l->sum.ne->post(home, &n->r->x_int, ipls); } else { - Int::Linear::Term* its = r.alloc>(n->n_int); + Int::Linear::Term* its = + (n->n_int > 0) ? r.alloc>(n->n_int) : nullptr; int c = n->fill(home, ipls, its, nullptr); Int::Linear::post(home, its, n->n_int, irt, -c, (n->n_int > 2) ? ipls.linear() : ipls.linear2()); } @@ -276,7 +277,8 @@ namespace Gecode { } n->r->sum.ne->post(home, irt, n->c, b, ipls); } else { - Int::Linear::Term* its = r.alloc>(n->n_int); + Int::Linear::Term* its = + (n->n_int > 0) ? r.alloc>(n->n_int) : nullptr; int c = n->fill(home, ipls, its, nullptr); Int::Linear::post(home, its, n->n_int, irt, -c, b, (n->n_int > 2) ? ipls.linear() : ipls.linear2()); } diff --git a/test/float/arithmetic.cpp b/test/float/arithmetic.cpp index a50d530852..9abf6e8096 100644 --- a/test/float/arithmetic.cpp +++ b/test/float/arithmetic.cpp @@ -1676,6 +1676,45 @@ namespace Test { namespace Float { MinNaryShared min_s_nary; MaxNary max_nary; MaxNaryShared max_s_nary; + + /// Space without variables for minimum and maximum over no variables + class EmptyFixture : public Gecode::Space { + public: + /// Create fixture + EmptyFixture(void) = default; + /// Constructor for cloning \a fixture + EmptyFixture(EmptyFixture& fixture) : Gecode::Space(fixture) {} + /// Copy during cloning + Gecode::Space* + copy(void) override { + return new EmptyFixture(*this); + } + }; + + /// %Test that minimum and maximum expressions over no variables throw at posting + class MinMaxEmpty : public Base { + public: + /// Create and register test + MinMaxEmpty(void) : Base("Float::Arithmetic::MinMax::Empty") {} + /// Test whether posting \a e throws TooFewArguments + static bool + throws_too_few(const Gecode::LinFloatExpr& e) { + EmptyFixture home; + try { + (void)Gecode::expr(home, e); + } catch (Gecode::Float::TooFewArguments&) { + return true; + } + return false; + } + /// Perform test + bool + run(void) override { + Gecode::FloatVarArgs x; + return throws_too_few(Gecode::min(x)) && throws_too_few(Gecode::max(x)); + } + }; + MinMaxEmpty min_max_empty; //@} } // namespace Arithmetic diff --git a/test/float/linear.cpp b/test/float/linear.cpp index 48e006d3d0..723c845245 100644 --- a/test/float/linear.cpp +++ b/test/float/linear.cpp @@ -189,6 +189,55 @@ namespace Test { namespace Float { } }; + /// Space with a float and a Boolean variable for sums without terms + class EmptyFixture : public Gecode::Space { + public: + /// Right-hand side variable + Gecode::FloatVar y; + /// Reification variable + Gecode::BoolVar b; + /// Create fixture + EmptyFixture(void) : y(*this, -2, 2), b(*this, 0, 1) {} + /// Constructor for cloning \a fixture + EmptyFixture(EmptyFixture& fixture) : Gecode::Space(fixture) { + y.update(*this, fixture.y); + b.update(*this, fixture.b); + } + /// Copy during cloning + Gecode::Space* + copy(void) override { + return new EmptyFixture(*this); + } + /// Test whether the space is solved with \a b equal to \a val + bool + is_solved_with_b(int val) { + return (status() == Gecode::SS_SOLVED) && b.assigned() && (b.val() == val); + } + }; + + /// %Test linear constraints over no variables + class Empty : public Base { + public: + /// Create and register test + Empty(void) : Base("Float::Linear::Empty") {} + /// Perform test + bool + run(void) override { + using namespace Gecode; + FloatVarArgs x; + FloatValArgs a; + EmptyFixture eq, lq, rhs, reified, pmi; + linear(eq, x, FRT_EQ, 0); + linear(lq, a, x, FRT_LQ, -1); + linear(rhs, x, FRT_EQ, rhs.y); + linear(reified, x, FRT_EQ, 1, Reify(reified.b)); + linear(pmi, a, x, FRT_GQ, 0, Reify(pmi.b, RM_PMI)); + return (eq.status() == SS_SOLVED) && (lq.status() == SS_FAILED) && (rhs.status() == SS_SOLVED) && + rhs.y.assigned() && (rhs.y.val() == 0) && reified.is_solved_with_b(0) && pmi.is_solved_with_b(1); + } + }; + Empty empty; + /// Help class to create and register tests class Create { public: diff --git a/test/float/mm-lin.cpp b/test/float/mm-lin.cpp index a2529057cc..e02ef0a49b 100644 --- a/test/float/mm-lin.cpp +++ b/test/float/mm-lin.cpp @@ -2269,6 +2269,45 @@ namespace Test { namespace Float { &li396[0], &li397[0], &li398[0], &li399[0], }; + /// Space with a Boolean variable for sums without terms + class EmptyFixture : public Gecode::Space { + public: + /// Reification variable + Gecode::BoolVar b; + /// Create fixture + EmptyFixture(void) : b(*this, 0, 1) {} + /// Constructor for cloning \a fixture + EmptyFixture(EmptyFixture& fixture) : Gecode::Space(fixture) { + b.update(*this, fixture.b); + } + /// Copy during cloning + Gecode::Space* + copy(void) override { + return new EmptyFixture(*this); + } + }; + + /// %Test linear relations over sums without variables + class LinRelEmpty : public Base { + public: + /// Create and register test + LinRelEmpty(void) : Base("Float::MiniModel::LinRel::Empty") {} + /// Perform test + bool + run(void) override { + using namespace Gecode; + FloatVarArgs x; + FloatValArgs a; + EmptyFixture eq, gq, reified; + rel(eq, sum(x) == 0); + rel(gq, sum(a, x) >= 1); + rel(reified, (sum(x) == 1) == reified.b); + return (eq.status() == SS_SOLVED) && (gq.status() == SS_FAILED) && (reified.status() == SS_SOLVED) && + reified.b.assigned() && (reified.b.val() == 0); + } + }; + LinRelEmpty lin_rel_empty; + /// Help class to create and register tests class Create { public: diff --git a/test/int/linear.cpp b/test/int/linear.cpp index 4cfbcc8f45..8735485527 100644 --- a/test/int/linear.cpp +++ b/test/int/linear.cpp @@ -264,6 +264,59 @@ namespace Test { namespace Int { } }; + /// Space with an integer and a Boolean variable for sums without terms + class EmptyFixture : public Gecode::Space { + public: + /// Right-hand side variable + Gecode::IntVar y; + /// Reification variable + Gecode::BoolVar b; + /// Create fixture + EmptyFixture(void) : y(*this, -2, 2), b(*this, 0, 1) {} + /// Constructor for cloning \a fixture + EmptyFixture(EmptyFixture& fixture) : Gecode::Space(fixture) { + y.update(*this, fixture.y); + b.update(*this, fixture.b); + } + /// Copy during cloning + Gecode::Space* + copy(void) override { + return new EmptyFixture(*this); + } + /// Test whether the space is solved with \a b equal to \a val + bool + is_solved_with_b(int val) { + return (status() == Gecode::SS_SOLVED) && b.assigned() && (b.val() == val); + } + }; + + /// %Test linear constraints over no variables + class Empty : public Base { + public: + /// Create and register test + Empty(void) : Base("Int::Linear::Empty") {} + /// Perform test + bool + run(void) override { + using namespace Gecode; + IntVarArgs x; + BoolVarArgs bx; + IntArgs a; + EmptyFixture eq, nq, bool_lq, bool_y, reified, imp, pmi; + linear(eq, x, IRT_EQ, 0, IPL_DOM); + linear(nq, a, x, IRT_NQ, 0); + linear(bool_lq, a, bx, IRT_LQ, -1); + linear(bool_y, bx, IRT_EQ, bool_y.y); + linear(reified, x, IRT_EQ, 1, Reify(reified.b)); + linear(imp, a, bx, IRT_GQ, 1, Reify(imp.b, RM_IMP)); + linear(pmi, bx, IRT_LQ, 0, Reify(pmi.b, RM_PMI)); + return (eq.status() == SS_SOLVED) && (nq.status() == SS_FAILED) && (bool_lq.status() == SS_FAILED) && + (bool_y.status() == SS_SOLVED) && bool_y.y.assigned() && (bool_y.y.val() == 0) && + reified.is_solved_with_b(0) && imp.is_solved_with_b(0) && pmi.is_solved_with_b(1); + } + }; + Empty empty; + /// Help class to create and register tests class Create { public: diff --git a/test/int/mm-bool.cpp b/test/int/mm-bool.cpp index 702eb66332..bb89bdbe35 100644 --- a/test/int/mm-bool.cpp +++ b/test/int/mm-bool.cpp @@ -1954,6 +1954,45 @@ namespace Test { namespace Int { } }; + /// Space with an index variable for element over no Boolean variables + class EmptyFixture : public Gecode::Space { + public: + /// Index variable + Gecode::IntVar y; + /// Create fixture + EmptyFixture(void) : y(*this, 0, 1) {} + /// Constructor for cloning \a fixture + EmptyFixture(EmptyFixture& fixture) : Gecode::Space(fixture) { + y.update(*this, fixture.y); + } + /// Copy during cloning + Gecode::Space* + copy(void) override { + return new EmptyFixture(*this); + } + }; + + /// %Test that element over no Boolean variables throws at posting + class BoolElementEmpty : public Base { + public: + /// Create and register test + BoolElementEmpty(void) : Base("Int::MiniModel::BoolElement::Empty") {} + /// Perform test + bool + run(void) override { + using namespace Gecode; + BoolVarArgs x; + EmptyFixture home; + try { + (void)expr(home, element(x, home.y)); + } catch (Gecode::Int::TooFewArguments&) { + return true; + } + return false; + } + }; + BoolElementEmpty bool_element_empty; + /// Help class to create and register tests class Create { public: diff --git a/test/int/mm-lin.cpp b/test/int/mm-lin.cpp index bd91a7ab55..c20f0ac614 100644 --- a/test/int/mm-lin.cpp +++ b/test/int/mm-lin.cpp @@ -2548,6 +2548,52 @@ namespace Test { namespace Int { &li396[0], &li397[0], &li398[0], &li399[0], }; + /// Space with an integer and a Boolean variable for sums without terms + class EmptyFixture : public Gecode::Space { + public: + /// Right-hand side variable + Gecode::IntVar y; + /// Reification variable + Gecode::BoolVar b; + /// Create fixture + EmptyFixture(void) : y(*this, -2, 2), b(*this, 0, 1) {} + /// Constructor for cloning \a fixture + EmptyFixture(EmptyFixture& fixture) : Gecode::Space(fixture) { + y.update(*this, fixture.y); + b.update(*this, fixture.b); + } + /// Copy during cloning + Gecode::Space* + copy(void) override { + return new EmptyFixture(*this); + } + }; + + /// %Test linear relations over sums without variables + class LinRelEmpty : public Base { + public: + /// Create and register test + LinRelEmpty(void) : Base("Int::MiniModel::LinRel::Empty") {} + /// Perform test + bool + run(void) override { + using namespace Gecode; + IntVarArgs x; + BoolVarArgs bx; + IntArgs a; + EmptyFixture eq, gr, bool_eq, constant, reified; + rel(eq, sum(x) == 0); + rel(gr, sum(a, x) > 0); + rel(bool_eq, sum(bx) == 1); + rel(constant, sum(a) == 0); + rel(reified, (sum(x) == 1) == reified.b); + return (eq.status() == SS_SOLVED) && (gr.status() == SS_FAILED) && (bool_eq.status() == SS_FAILED) && + (constant.status() == SS_SOLVED) && (reified.status() == SS_SOLVED) && reified.b.assigned() && + (reified.b.val() == 0); + } + }; + LinRelEmpty lin_rel_empty; + /// Help class to create and register tests class Create { public: From d2308431dab51b2c2bb068b145e40b4bc36aaf18 Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Sun, 1 Nov 2026 21:49:27 +0100 Subject: [PATCH 6/7] Skip empty buffers in open-list distinct Zdev-Change-Id: Z2f16dcb32eaf668fab7f831f2cb1ce8bdafd7f6b7b30fa28158406da084ad476 --- gecode/int/distinct/open.hpp | 4 +++ test/int/open-var-list.cpp | 50 ++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/gecode/int/distinct/open.hpp b/gecode/int/distinct/open.hpp index c86d2159ca..4aa4d5869f 100644 --- a/gecode/int/distinct/open.hpp +++ b/gecode/int/distinct/open.hpp @@ -133,6 +133,8 @@ namespace Gecode { namespace Int { namespace Distinct { x.subscribe(home, *this, PC_INT_VAL, false); } processed = size; + if (active_count == 0) + return closed() ? home.ES_SUBSUMED(*this) : ES_FIX; Region region; const int capacity = active_count; @@ -190,6 +192,8 @@ namespace Gecode { namespace Int { namespace Distinct { static void post(Home home, OpenViewList list) { + if (list.length().max() == 0) + return; (void)new (home) Open(home, list); } }; diff --git a/test/int/open-var-list.cpp b/test/int/open-var-list.cpp index 299b34d835..a25b210eeb 100644 --- a/test/int/open-var-list.cpp +++ b/test/int/open-var-list.cpp @@ -1157,8 +1157,58 @@ namespace Test { namespace Int { namespace OpenList { } }; + /// Space posting distinct on an initially empty open list + class EmptyDistinctSpace : public Gecode::Space { + public: + Gecode::OpenIntVarList list; + + EmptyDistinctSpace(int max, Gecode::IntPropLevel ipl) : list(*this, Gecode::IntSet(0, 2), max) { + distinct(*this, list, ipl); + } + + EmptyDistinctSpace(EmptyDistinctSpace& s) : Gecode::Space(s) { + list.update(*this, s.list); + } + + Gecode::Space* + copy(void) override { + return new EmptyDistinctSpace(*this); + } + }; + + /// Distinct on a closed empty list posts nothing; an open one still grows + class DistinctEmpty : public ::Test::Base { + public: + DistinctEmpty(void) : ::Test::Base("Int::OpenList::DistinctEmpty") {} + + bool + run(void) override { + using namespace Gecode; + for (IntPropLevel ipl : {IPL_DEF, IPL_DOM}) { + EmptyDistinctSpace closed(0, ipl); + if ((PropagatorGroup::all.size(closed) != 0) || (closed.status() != SS_SOLVED)) + return false; + + EmptyDistinctSpace source(Gecode::Int::Limits::max, ipl); + if (source.status() != SS_SOLVED) + return false; + std::unique_ptr duplicate(static_cast(source.clone())); + source.list.append(source, IntVar(source, 1, 1)); + source.list.append(source, IntVar(source, 0, 1)); + if ((source.status() == SS_FAILED) || !source.list[1].assigned() || (source.list[1].val() != 0)) + return false; + duplicate->list.append(*duplicate, IntVar(*duplicate, 2, 2)); + duplicate->list.append(*duplicate, IntVar(*duplicate, 2, 2)); + if (duplicate->status() != SS_FAILED) + return false; + } + return true; + } + }; + GrowableIntSet growable_int_set; DistinctWaves distinct_waves; + DistinctEmpty distinct_empty; class GroupSpace : public Gecode::Space { public: From 1336d07e21de5093b52ef3a33ca6f90411c58409 Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Sun, 1 Nov 2026 22:43:03 +0100 Subject: [PATCH 7/7] Audit empty-array posting and record the fixes Zdev-Change-Id: Z1b18cdeb9e1612dbc04fc22e901b19d940719bb2db0fa18fa22cfc611f6454e7 --- changelog.in | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/changelog.in b/changelog.in index 2e4c3e594c..f3fd7b477d 100644 --- a/changelog.in +++ b/changelog.in @@ -125,6 +125,38 @@ Rank: minor Post nothing for no_overlap with zero rectangles. Debug builds aborted with a failed assertion in the memory manager. +[ENTRY] +Module: int +What: bug +Rank: minor +[DESCRIPTION] +Decide count with an array of counts (global cardinality) and bin_packing +with no variables or items when posting: every count and load becomes 0. +Count with variables but no values fails. Branching with LDSB symmetries, +for integer, Boolean, and set variables, ignores symmetries over no +variables or values and accepts an empty Symmetries. Debug builds aborted +with failed assertions. Distinct over an open list with maximum length 0 +also posts nothing. + +[ENTRY] +Module: set +What: bug +Rank: minor +[DESCRIPTION] +Post nothing for channel_sorted, channel with Boolean variables, and union +or disjoint-union element over constant sets when the array is empty, and +make the set variables empty. Debug builds aborted with failed assertions. +Element over set variables, rel with union or disjoint union and a constant +set, and weights also post nothing for empty arrays. + +[ENTRY] +Module: search +What: bug +Rank: minor +[DESCRIPTION] +Post nothing for relax with empty variable arrays. Debug builds aborted with +a failed assertion. + [ENTRY] Module: other What: performance