From b497289e0286ba1e953f486eeca5d4a0ea53a072 Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Wed, 2 Dec 2026 21:44:24 +0100 Subject: [PATCH 1/8] Fix set element union when the selector is also the result When the selector and result views alias, the SOT_UNION element propagators modified the selector before consuming an iterator over the result's lower bound, which could drop valid solutions (including from MiniZinc's roots(x, s, s)). Cache the lower-bound difference before modifying the selector for variable sets, and check the subset condition before modifying the selector for constant sets. --- changelog.in | 9 +++++ gecode/set/element/union.hpp | 4 +- gecode/set/element/unionConst.hpp | 10 ++--- test/set/element.cpp | 64 +++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 6 deletions(-) diff --git a/changelog.in b/changelog.in index 543c60f69a..9e7e48462c 100644 --- a/changelog.in +++ b/changelog.in @@ -570,6 +570,15 @@ Rank: minor Handle empty arrays when posting channel_sorted, Boolean channel, element, union/disjoint-union rel with a constant set, and weights. +[ENTRY] +Module: set +What: bug +Rank: minor +[DESCRIPTION] +Fix set element with SOT_UNION (for both variable and constant set arrays), +which missed solutions when the selector and result are the same variable. +This also fixes MiniZinc's roots(x, s, s). + [ENTRY] Module: set What: documentation diff --git a/gecode/set/element/union.hpp b/gecode/set/element/union.hpp index acf8f4daac..62cb94410f 100644 --- a/gecode/set/element/union.hpp +++ b/gecode/set/element/union.hpp @@ -275,12 +275,14 @@ namespace Gecode::Set::Element { Iter::Ranges::Union inter(b, s); Iter::Ranges::Diff, Iter::Ranges::Union> diff(x1lb, inter); if (diff()) { + // Cache before changing x0, which may be the same view as x1 + Iter::Ranges::Cache extra(r, diff); ModEvent me = (x0.include(home, iv[i].idx)); loop_var |= me_modified(me); GECODE_ME_CHECK(me); // candidate != extra - me = iv[i].view.include_i(home, diff); + me = iv[i].view.include_i(home, extra); loop_var |= me_modified(me); GECODE_ME_CHECK(me); } diff --git a/gecode/set/element/unionConst.hpp b/gecode/set/element/unionConst.hpp index 2cb925d9c0..92410190e7 100644 --- a/gecode/set/element/unionConst.hpp +++ b/gecode/set/element/unionConst.hpp @@ -252,14 +252,14 @@ namespace Gecode::Set::Element { Iter::Ranges::Union inter(b, s); Iter::Ranges::Diff, Iter::Ranges::Union> diff(x0lb, inter); if (diff()) { - ModEvent me = (x1.include(home, i)); - loop_var |= me_modified(me); - GECODE_ME_CHECK(me); - - // candidate != extra + // candidate != extra; check before changing x1, which may alias x0 IntSetRanges ivi(iv[i]); if (!Iter::Ranges::subset(diff, ivi)) GECODE_ME_CHECK(ME_SET_FAILED); + + ModEvent me = (x1.include(home, i)); + loop_var |= me_modified(me); + GECODE_ME_CHECK(me); } IntSetRanges iviub(iv[i]); diff --git a/test/set/element.cpp b/test/set/element.cpp index 81415793d5..946bebf413 100644 --- a/test/set/element.cpp +++ b/test/set/element.cpp @@ -94,6 +94,42 @@ namespace Test::Set::Element { }; ElementUnion _elementunion("Element::Union"); + static IntSet ds_02(0, 2); + + /// Return whether \a y is the union of the sets in \a x at the indices in \a z + static bool + is_union_at(const SetAssignment& x, int n, int y, int z) { + Gecode::IntArgs selected_values; + for (CountableSetValues selector(x.lub, x[z]); selector(); ++selector) { + if (selector.val() < 0 || selector.val() >= n) + return false; + for (CountableSetValues v(x.lub, x[selector.val()]); v(); ++v) + selected_values << v.val(); + } + Gecode::IntSet u(selected_values); + IntSetRanges ur(u); + CountableSetRanges yr(x.lub, x[y]); + return Iter::Ranges::equal(ur, yr); + } + + /// %Test for %ElementUnion constraint whose selector is also the result + class ElementUnionShared : public SetTest { + public: + /// Create and register test + ElementUnionShared(const char* t) : SetTest(t, 4, ds_02, false) {} + /// %Test whether \a x is solution + bool + solution(const SetAssignment& x) const override { + return is_union_at(x, 3, 3, 3); + } + /// Post constraint on \a x + void + post(Space& home, SetVarArray& x, IntVarArray&) override { + Gecode::element(home, SOT_UNION, SetVarArgs{x[0], x[1], x[2]}, x[3], x[3]); + } + }; + ElementUnionShared _elementunion_shared("Element::Union::Shared"); + /// %Test for %ElementUnionConst constraint class ElementUnionConst : public SetTest { private: @@ -148,6 +184,34 @@ namespace Test::Set::Element { }; ElementUnionConst _elementunionconst("Element::UnionConst"); + /// %Test for %ElementUnionConst constraint whose selector is also the result + class ElementUnionConstShared : public SetTest { + private: + const IntSetArgs xs{IntSet::empty, IntSet(2, 2), IntSet(0, 1)}; + + public: + /// Create and register test + ElementUnionConstShared(const char* t) : SetTest(t, 1, ds_02, false) {} + /// %Test whether \a x is solution + bool + solution(const SetAssignment& x) const override { + Gecode::IntArgs selected_values; + for (CountableSetValues selector(x.lub, x[0]); selector(); ++selector) + for (IntSetValues v(xs[selector.val()]); v(); ++v) + selected_values << v.val(); + Gecode::IntSet u(selected_values); + IntSetRanges ur(u); + CountableSetRanges sr(x.lub, x[0]); + return Iter::Ranges::equal(ur, sr); + } + /// Post constraint on \a x + void + post(Space& home, SetVarArray& x, IntVarArray&) override { + Gecode::element(home, SOT_UNION, xs, x[0], x[0]); + } + }; + ElementUnionConstShared _elementunionconst_shared("Element::UnionConst::Shared"); + /// %Test for %ElementInter constraint class ElementInter : public SetTest { public: From a5c3e71ac3ec9c6962315f0c98023ca101d07a1d Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Wed, 2 Dec 2026 21:44:24 +0100 Subject: [PATCH 2/8] Fix the integer/set channel with a single set Posting ChannelInt bounds every integer variable to 0..ys.size()-1. With a single set variable that assigns every x[i] to 0, which matches the initial cache, so propagate() never sees x[i] as modified and never includes i in y[0]. This let the channel accept invalid solutions and broke MiniZinc's roots when the largest image value is 0. Include i in ys[xs[i].val()] at posting whenever xs[i] is already assigned. --- changelog.in | 9 +++++++++ gecode/set/channel/int.hpp | 3 +++ test/set/channel.cpp | 6 ++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/changelog.in b/changelog.in index 9e7e48462c..b14b6183b7 100644 --- a/changelog.in +++ b/changelog.in @@ -579,6 +579,15 @@ Fix set element with SOT_UNION (for both variable and constant set arrays), which missed solutions when the selector and result are the same variable. This also fixes MiniZinc's roots(x, s, s). +[ENTRY] +Module: set +What: bug +Rank: minor +[DESCRIPTION] +Fix the channel between integer variables and a single set variable, which +accepted wrong solutions. This also fixes MiniZinc's roots when the largest +value its image can contain is 0. + [ENTRY] Module: set What: documentation diff --git a/gecode/set/channel/int.hpp b/gecode/set/channel/int.hpp index 826dc6a23d..8bf62b7ddf 100644 --- a/gecode/set/channel/int.hpp +++ b/gecode/set/channel/int.hpp @@ -77,6 +77,9 @@ namespace Gecode::Set::Channel { for (int i = xs.size(); i--;) { GECODE_ME_CHECK(xs[i].gq(home, 0)); GECODE_ME_CHECK(xs[i].le(home, static_cast(yssize))); + // With a single set, the initial cache equals {0}, so modified() is false + if (xs[i].assigned()) + GECODE_ME_CHECK(ys[xs[i].val()].include(home, i)); } (void)new (home) ChannelInt(home, xs, ys); diff --git a/test/set/channel.cpp b/test/set/channel.cpp index 5dd4531f04..091a23f28a 100644 --- a/test/set/channel.cpp +++ b/test/set/channel.cpp @@ -117,8 +117,8 @@ namespace Test::Set::Channel { public: /// Create and register test - ChannelInt(const char* t, const IntSet& d, int _ssize, int _isize) - : SetTest(TestTags::sweep(), t, _ssize, d, false, _isize), ssize(_ssize), isize(_isize) {} + ChannelInt(const char* t, const IntSet& d, int _ssize, int _isize, TestTags tags = TestTags::sweep()) + : SetTest(tags, t, _ssize, d, false, _isize), ssize(_ssize), isize(_isize) {} /// %Test whether \a x is solution bool solution(const SetAssignment& x) const override { @@ -149,6 +149,8 @@ namespace Test::Set::Channel { ChannelInt _channelint1("Channel::Int::1", d2, 2, 3); ChannelInt _channelint2("Channel::Int::2", d3, 3, 3); + /// A single set fixes every integer to 0 at posting + ChannelInt _channelint_single("Channel::Int::Single", d_12, 1, 2, TestTags::standard()); /// %Test for Boolean channel constraint class ChannelBool : public SetTest { From c224e726ac6aa9fe5cdd3d82be99bdff1c971949 Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Wed, 2 Dec 2026 21:44:25 +0100 Subject: [PATCH 3/8] Fix signed overflow in unary task arithmetic near the integer limits OmegaNode and OmegaLambdaNode summed task processing and earliest completion times in int. When task durations summed beyond the integer limits, the sum overflowed and unary overload detection (also used for cumulative constraints reduced to unary) missed the overload. Store processing and completion times in long long int, clamp completion times to [-Limits::infinity, Limits::infinity] when reading from the trees, and subtract in long long int when updating task start bounds. --- changelog.in | 9 +++++++ gecode/int/task.hh | 3 --- gecode/int/task/tree.hpp | 6 ----- gecode/int/unary.hh | 16 +++++++++---- gecode/int/unary/task.hpp | 8 +++---- gecode/int/unary/tree.hpp | 49 +++++++++++++++++++++++++-------------- test/int/unary.cpp | 39 +++++++++++++++++++++++++++++++ 7 files changed, 94 insertions(+), 36 deletions(-) diff --git a/changelog.in b/changelog.in index b14b6183b7..bbbcc5731c 100644 --- a/changelog.in +++ b/changelog.in @@ -538,6 +538,15 @@ Rank: minor Avoid zero-byte heap allocations for arity-zero TupleSets, and allow constructing an empty IntSet from an empty initializer list. +[ENTRY] +Module: int +What: bug +Rank: minor +[DESCRIPTION] +Fix signed overflow in unary propagation (also used for cumulative +constraints reduced to unary), which missed overload failure when the total +duration of the tasks exceeds the integer limits. + [ENTRY] Module: set What: bug diff --git a/gecode/int/task.hh b/gecode/int/task.hh index 5a77846f6e..d9a013afa4 100644 --- a/gecode/int/task.hh +++ b/gecode/int/task.hh @@ -351,9 +351,6 @@ namespace Gecode::Int { namespace Gecode::Int { - /// Safe addition in case \a x is -Int::Limits::infinity - int plus(int x, int y); - /// Safe addition in case \a x is -Int::Limits::llinfinity long long int plus(long long int x, long long int y); diff --git a/gecode/int/task/tree.hpp b/gecode/int/task/tree.hpp index 242bee4d6b..3346b1dcea 100644 --- a/gecode/int/task/tree.hpp +++ b/gecode/int/task/tree.hpp @@ -34,12 +34,6 @@ namespace Gecode::Int { - forceinline int - plus(int x, int y) { - assert(y != -Limits::infinity); - return (x == -Limits::infinity) ? x : x + y; - } - forceinline long long int plus(long long int x, long long int y) { assert(y != -Limits::llinfinity); diff --git a/gecode/int/unary.hh b/gecode/int/unary.hh index 0e712518fd..e564d1ab98 100644 --- a/gecode/int/unary.hh +++ b/gecode/int/unary.hh @@ -644,13 +644,19 @@ namespace Gecode::Int { namespace Gecode::Int::Unary { - /// Node for an omega tree + /** + * \brief Node for an omega tree + * + * Processing and completion times of task sets can exceed the integer + * limits, so they are kept as long long. The tree accessors clamp + * completion times with clamp_ect(). + */ class OmegaNode { public: /// Processing time for subtree - int p; + long long int p; /// Earliest completion time for subtree - int ect; + long long int ect; /// Initialize node from left child \a l and right child \a r void init(const OmegaNode& l, const OmegaNode& r); /// Update node from left child \a l and right child \a r @@ -686,9 +692,9 @@ namespace Gecode::Int::Unary { /// Undefined task static const int undef = -1; /// Processing times for subtree - int lp; + long long int lp; /// Earliest completion times for subtree - int lect; + long long int lect; /// Node which is responsible for lect int res_ect; /// Node which is responsible for lp diff --git a/gecode/int/unary/task.hpp b/gecode/int/unary/task.hpp index d2ed2d39a8..c2aee051b5 100644 --- a/gecode/int/unary/task.hpp +++ b/gecode/int/unary/task.hpp @@ -121,7 +121,7 @@ namespace Gecode::Int::Unary { } forceinline ModEvent ManFixPTask::ect(Space& home, int n) { - return _s.gq(home, n - _p); + return _s.gq(home, static_cast(n) - _p); } forceinline ModEvent ManFixPTask::lst(Space& home, int n) { @@ -129,7 +129,7 @@ namespace Gecode::Int::Unary { } forceinline ModEvent ManFixPTask::lct(Space& home, int n) { - return _s.lq(home, n - _p); + return _s.lq(home, static_cast(n) - _p); } forceinline ModEvent ManFixPTask::norun(Space& home, int e, int l) { @@ -283,7 +283,7 @@ namespace Gecode::Int::Unary { case TT_FIXED_END: return (n <= _p) ? ME_INT_NONE : ME_INT_FAILED; case TT_FIXED_PROCESSING: - return _s.gq(home, n - _p); + return _s.gq(home, static_cast(n) - _p); case TT_FIXED_START: return _s.gq(home, n); default: @@ -310,7 +310,7 @@ namespace Gecode::Int::Unary { case TT_FIXED_END: return (n >= _p) ? ME_INT_NONE : ME_INT_FAILED; case TT_FIXED_PROCESSING: - return _s.lq(home, n - _p); + return _s.lq(home, static_cast(n) - _p); case TT_FIXED_START: return _s.lq(home, n); default: diff --git a/gecode/int/unary/tree.hpp b/gecode/int/unary/tree.hpp index 279722f0da..de8e7b3457 100644 --- a/gecode/int/unary/tree.hpp +++ b/gecode/int/unary/tree.hpp @@ -34,6 +34,19 @@ namespace Gecode::Int::Unary { + /** + * \brief Clamp a completion time \a ect of a tree to the int range + * + * Every task time lies within the integer limits, so completion times + * beyond them compare and prune like \c Limits::infinity. The empty set + * maps to \c -Limits::infinity. + */ + forceinline int + clamp_ect(long long int ect) { + return static_cast(std::clamp(ect, -static_cast(Limits::infinity), + static_cast(Limits::infinity))); + } + /* * Omega tree */ @@ -41,7 +54,7 @@ namespace Gecode::Int::Unary { forceinline void OmegaNode::init(const OmegaNode&, const OmegaNode&) { p = 0; - ect = -Limits::infinity; + ect = -Limits::llinfinity; } forceinline void @@ -56,7 +69,7 @@ namespace Gecode::Int::Unary { : TaskTree(r, t) { for (int i = 0; i < tasks.size(); i++) { leaf(i).p = 0; - leaf(i).ect = -Limits::infinity; + leaf(i).ect = -Limits::llinfinity; } init(); } @@ -65,7 +78,7 @@ namespace Gecode::Int::Unary { forceinline void OmegaTree::insert(int i) { leaf(i).p = tasks[i].pmin(); - leaf(i).ect = tasks[i].est() + tasks[i].pmin(); + leaf(i).ect = static_cast(tasks[i].est()) + tasks[i].pmin(); update(i); } @@ -73,14 +86,14 @@ namespace Gecode::Int::Unary { forceinline void OmegaTree::remove(int i) { leaf(i).p = 0; - leaf(i).ect = -Limits::infinity; + leaf(i).ect = -Limits::llinfinity; update(i); } template forceinline int OmegaTree::ect() const { - return root().ect; + return clamp_ect(root().ect); } template @@ -88,13 +101,13 @@ namespace Gecode::Int::Unary { OmegaTree::ect(int i) const { // Check whether task i is in? OmegaTree& o = const_cast&>(*this); - if (o.leaf(i).ect != -Limits::infinity) { + if (o.leaf(i).ect != -Limits::llinfinity) { o.remove(i); - int ect = o.root().ect; + long long int ect = o.root().ect; o.insert(i); - return ect; + return clamp_ect(ect); } - return root().ect; + return clamp_ect(root().ect); } /* @@ -142,7 +155,7 @@ namespace Gecode::Int::Unary { // Enter all tasks into tree (omega = all tasks, lambda = empty) for (int i = 0; i < tasks.size(); i++) { leaf(i).p = leaf(i).lp = tasks[i].pmin(); - leaf(i).ect = leaf(i).lect = tasks[i].est() + tasks[i].pmin(); + leaf(i).ect = leaf(i).lect = static_cast(tasks[i].est()) + tasks[i].pmin(); leaf(i).res_ect = OmegaLambdaNode::undef; leaf(i).res_lp = OmegaLambdaNode::undef; } @@ -151,7 +164,7 @@ namespace Gecode::Int::Unary { // Enter no tasks into tree (omega = empty, lambda = empty) for (int i = 0; i < tasks.size(); i++) { leaf(i).p = leaf(i).lp = 0; - leaf(i).ect = leaf(i).lect = -Limits::infinity; + leaf(i).ect = leaf(i).lect = -Limits::llinfinity; leaf(i).res_ect = OmegaLambdaNode::undef; leaf(i).res_lp = OmegaLambdaNode::undef; } @@ -163,9 +176,9 @@ namespace Gecode::Int::Unary { forceinline void OmegaLambdaTree::shift(int i) { // That means that i is in omega - assert(leaf(i).ect > -Limits::infinity); + assert(leaf(i).ect > -Limits::llinfinity); leaf(i).p = 0; - leaf(i).ect = -Limits::infinity; + leaf(i).ect = -Limits::llinfinity; leaf(i).res_ect = i; leaf(i).res_lp = i; update(i); @@ -175,7 +188,7 @@ namespace Gecode::Int::Unary { forceinline void OmegaLambdaTree::insert_omega(int i) { leaf(i).p = tasks[i].pmin(); - leaf(i).ect = tasks[i].est() + tasks[i].pmin(); + leaf(i).ect = static_cast(tasks[i].est()) + tasks[i].pmin(); update(i); } @@ -183,7 +196,7 @@ namespace Gecode::Int::Unary { forceinline void OmegaLambdaTree::insert_lambda(int i) { leaf(i).lp = tasks[i].pmin(); - leaf(i).lect = tasks[i].est() + tasks[i].pmin(); + leaf(i).lect = static_cast(tasks[i].est()) + tasks[i].pmin(); leaf(i).res_ect = i; leaf(i).res_lp = i; update(i); @@ -193,7 +206,7 @@ namespace Gecode::Int::Unary { forceinline void OmegaLambdaTree::remove_lambda(int i) { leaf(i).lp = 0; - leaf(i).lect = -Limits::infinity; + leaf(i).lect = -Limits::llinfinity; leaf(i).res_ect = OmegaLambdaNode::undef; leaf(i).res_lp = OmegaLambdaNode::undef; update(i); @@ -214,13 +227,13 @@ namespace Gecode::Int::Unary { template forceinline int OmegaLambdaTree::ect() const { - return root().ect; + return clamp_ect(root().ect); } template forceinline int OmegaLambdaTree::lect() const { - return root().lect; + return clamp_ect(root().lect); } } // namespace Gecode::Int::Unary diff --git a/test/int/unary.cpp b/test/int/unary.cpp index 6a85ad9783..eadedca9e7 100644 --- a/test/int/unary.cpp +++ b/test/int/unary.cpp @@ -267,6 +267,45 @@ namespace Test::Int::Unary { } }; + /// Space with three tasks whose total duration exceeds the integer limits + class LongTasks : public Gecode::Space { + public: + /// Start times + Gecode::IntVarArray s; + /// Post unary on three tasks of length 10^9 that must end by 2*10^9 + explicit LongTasks(Gecode::IntPropLevel ipl) : s(*this, 3, 0, 1000000000) { + Gecode::unary(*this, s, Gecode::IntArgs({1000000000, 1000000000, 1000000000}), ipl); + } + /// Constructor for cloning \a home + LongTasks(LongTasks& home) : Gecode::Space(home) { + s.update(*this, home.s); + } + /// Copy during cloning + Gecode::Space* + copy() override { + return new LongTasks(*this); + } + }; + + /// %Test that overload detection sums durations without overflow + class LongTaskOverload : public Base { + public: + /// Create and register test + LongTaskOverload() : Base("Int::Unary::LongTaskOverload") {} + /// Run test + bool + run() override { + for (Gecode::IntPropLevel ipl : {Gecode::IPL_BASIC, Gecode::IPL_ADVANCED}) { + LongTasks home(ipl); + if (home.status() != Gecode::SS_FAILED) + return false; + } + return true; + } + }; + + LongTaskOverload long_task_overload; + /// Help class to create and register tests class Create { public: From 5408551dae5814991cf8d42b11155d7e7b0581dd Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Wed, 2 Dec 2026 21:44:25 +0100 Subject: [PATCH 4/8] Make unary not-first/not-last pruning independent of task order MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit not_last() reset j to -1 on every iteration of the outer loop, so among tasks with equal latest completion or start times only the first in storage order received the tighter bound. Because not_first_not_last() runs not_last() forward and backward, propagation could differ between equivalent spaces with different task orders. Keep j across iterations and insert tasks with equal latest start time together, following VilĂ­m's algorithm, in a shared not_last_bounds() helper used by both mandatory and optional tasks. --- changelog.in | 9 +++ gecode/int/unary/not-first-not-last.hpp | 77 ++++++++++++++----------- test/int/unary.cpp | 59 +++++++++++++++++++ 3 files changed, 112 insertions(+), 33 deletions(-) diff --git a/changelog.in b/changelog.in index bbbcc5731c..696d6869a6 100644 --- a/changelog.in +++ b/changelog.in @@ -547,6 +547,15 @@ Fix signed overflow in unary propagation (also used for cumulative constraints reduced to unary), which missed overload failure when the total duration of the tasks exceeds the integer limits. +[ENTRY] +Module: int +What: bug +Rank: minor +[DESCRIPTION] +Make not-first/not-last propagation for unary resources prune all tasks with +equal start or completion bounds, so propagation no longer depends on the +order in which the tasks are stored. + [ENTRY] Module: set What: bug diff --git a/gecode/int/unary/not-first-not-last.hpp b/gecode/int/unary/not-first-not-last.hpp index aecd4c70ac..d4d768d9b1 100644 --- a/gecode/int/unary/not-first-not-last.hpp +++ b/gecode/int/unary/not-first-not-last.hpp @@ -34,32 +34,59 @@ namespace Gecode::Int::Unary { - template - forceinline ExecStatus - not_last(Space& home, TaskViewArray& t) { - sort(t); - - Region r; - - OmegaTree o(r, t); - TaskViewIter q(r, t); - int* lct = r.alloc(t.size()); + /** + * \brief Compute in \a lct the not-last bounds for the tasks \a t + * + * The tasks \a t must be sorted by increasing latest completion time + * and \a q iterates the mandatory tasks by increasing latest start + * time. Tasks with equal latest completion or start times receive + * bounds that do not depend on their order in \a t, so the bounds are a + * function of the task domains alone. + */ + template + forceinline void + not_last_bounds(Region& r, TaskViewArray& t, Iter& q, int* lct) { + OmegaTree o(r, t); + int* tied = r.alloc(t.size()); for (int i = 0; i < t.size(); i++) lct[i] = t[i].lct(); + // The inserted task with the largest latest start time + int j = -1; for (int i = 0; i < t.size(); i++) { - int j = -1; while (q() && (t[i].lct() > t[q.task()].lst())) { - if ((j >= 0) && (o.ect() > t[q.task()].lst())) - lct[q.task()] = std::min(lct[q.task()], t[j].lst()); - j = q.task(); - o.insert(j); - ++q; + // Insert all tasks with the same latest start time at once + int lst = t[q.task()].lst(); + bool precedes_inserted = (j >= 0) && (o.ect() > lst); + int n = 0; + while (q() && (t[q.task()].lst() == lst)) { + tied[n++] = q.task(); + o.insert(q.task()); + ++q; + } + for (int k = 0; k < n; k++) + if (precedes_inserted) + lct[tied[k]] = std::min(lct[tied[k]], t[j].lst()); + else if ((n > 1) && (o.ect(tied[k]) > lst)) + lct[tied[k]] = std::min(lct[tied[k]], lst); + j = tied[n - 1]; } if ((j >= 0) && (o.ect(i) > t[i].lst())) lct[i] = std::min(lct[i], t[j].lst()); } + } + + template + forceinline ExecStatus + not_last(Space& home, TaskViewArray& t) { + sort(t); + + Region r; + + TaskViewIter q(r, t); + int* lct = r.alloc(t.size()); + not_last_bounds(r, t, q, lct); for (int i = 0; i < t.size(); i++) GECODE_ME_CHECK(t[i].lct(home, lct[i])); @@ -83,25 +110,9 @@ namespace Gecode::Int::Unary { Region r; - OmegaTree o(r, t); ManTaskViewIter q(r, t); int* lct = r.alloc(t.size()); - - for (int i = 0; i < t.size(); i++) - lct[i] = t[i].lct(); - - for (int i = 0; i < t.size(); i++) { - int j = -1; - while (q() && (t[i].lct() > t[q.task()].lst())) { - if ((j >= 0) && (o.ect() > t[q.task()].lst())) - lct[q.task()] = std::min(lct[q.task()], t[j].lst()); - j = q.task(); - o.insert(j); - ++q; - } - if ((j >= 0) && (o.ect(i) > t[i].lst())) - lct[i] = std::min(lct[i], t[j].lst()); - } + not_last_bounds(r, t, q, lct); { int n = t.size(); diff --git a/test/int/unary.cpp b/test/int/unary.cpp index eadedca9e7..d376d561b1 100644 --- a/test/int/unary.cpp +++ b/test/int/unary.cpp @@ -306,6 +306,65 @@ namespace Test::Int::Unary { LongTaskOverload long_task_overload; + /** + * \brief Space with a task that cannot be last and an optional task + * + * Mandatory tasks a, b and c have durations 3, 4 and 5 and start times in + * [3,15], [8,15] and [7,15]; optional task d has duration 3 and start time + * in [3,15]. As b and c cannot both end before 16, a must precede one of + * them and start by 12. Tasks a and d have equal latest completion times + * and are posted in either order. + */ + class TiedTasks : public Gecode::Space { + public: + /// Start times of a, b, c and d + Gecode::IntVarArray s; + /// Post the tasks with d before a if \a d_first + explicit TiedTasks(bool d_first) : s(*this, 4, 0, 15) { + using namespace Gecode; + rel(*this, s[0], IRT_GQ, 3); + rel(*this, s[1], IRT_GQ, 8); + rel(*this, s[2], IRT_GQ, 7); + rel(*this, s[3], IRT_GQ, 3); + BoolVar d(*this, 0, 1); + BoolVar one(*this, 1, 1); + if (d_first) + unary(*this, IntVarArgs({s[3], s[0], s[1], s[2]}), IntArgs({3, 3, 4, 5}), + BoolVarArgs({d, one, one, one}), IPL_ADVANCED); + else + unary(*this, IntVarArgs({s[0], s[3], s[1], s[2]}), IntArgs({3, 3, 4, 5}), + BoolVarArgs({one, d, one, one}), IPL_ADVANCED); + } + /// Constructor for cloning \a home + TiedTasks(TiedTasks& home) : Gecode::Space(home) { + s.update(*this, home.s); + } + /// Copy during cloning + Gecode::Space* + copy() override { + return new TiedTasks(*this); + } + }; + + /// %Test that not-last pruning does not depend on the order of tied tasks + class TiedNotLast : public Base { + public: + /// Create and register test + TiedNotLast() : Base("Int::Unary::TiedNotLast") {} + /// Run test + bool + run() override { + for (bool d_first : {false, true}) { + TiedTasks home(d_first); + if ((home.status() == Gecode::SS_FAILED) || (home.s[0].max() != 12)) + return false; + } + return true; + } + }; + + TiedNotLast tied_not_last; + /// Help class to create and register tests class Create { public: From e9d95efd6dcbb31fa762779fe6ae8df1c36a896a Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Wed, 2 Dec 2026 21:44:25 +0100 Subject: [PATCH 5/8] Make reified MiniZinc regular false on values outside the alphabet MiniZinc's reified construction for a native regular propagator failed on values outside 1..S instead of reifying them to false. Set fzn_regular_is_decomposed = true in fzn_regular.mzn so reified regular uses MiniZinc's standard decomposition (with an absorbing dead state), while non-reified regular keeps using gecode_regular. --- .github/ci/flatzinc-tools-smoke.sh | 13 +++++++++++++ changelog.in | 8 ++++++++ gecode/flatzinc/mznlib/fzn_regular.mzn | 4 ++++ 3 files changed, 25 insertions(+) diff --git a/.github/ci/flatzinc-tools-smoke.sh b/.github/ci/flatzinc-tools-smoke.sh index 1a67550376..e4b8728593 100644 --- a/.github/ci/flatzinc-tools-smoke.sh +++ b/.github/ci/flatzinc-tools-smoke.sh @@ -65,4 +65,17 @@ MZN ) [[ "$result" == *"capacity=3 empty-capacity=1 large-usage=0"* ]] + # Reified regular is false, not unsatisfiable, on a value outside 1..S. + result=$("$prefix/bin/mzn-gecode" - <<'MZN' +include "regular.mzn"; +var 2..3: x; +var bool: b; +constraint b <-> regular([x],1,2,[|1,1|],1,{1}); +constraint not b; +solve satisfy; +output ["regular-x=",show(x),"\n"]; +MZN + ) + [[ "$result" == *"regular-x=3"* ]] + fi diff --git a/changelog.in b/changelog.in index 696d6869a6..ab700dd658 100644 --- a/changelog.in +++ b/changelog.in @@ -742,6 +742,14 @@ Rank: minor Avoid zero-byte heap allocations for empty variable domains (var {}, var set of {}) and empty final-state intervals in regular. +[ENTRY] +Module: flatzinc +What: bug +Rank: minor +[DESCRIPTION] +Make reified MiniZinc regular over 1..S evaluate to false when a variable +takes a value outside 1..S, instead of making the problem unsatisfiable. + [ENTRY] Module: example What: bug diff --git a/gecode/flatzinc/mznlib/fzn_regular.mzn b/gecode/flatzinc/mznlib/fzn_regular.mzn index 891a300cd6..a34692a688 100644 --- a/gecode/flatzinc/mznlib/fzn_regular.mzn +++ b/gecode/flatzinc/mznlib/fzn_regular.mzn @@ -30,6 +30,10 @@ % % +% Keep the standard decomposition for reified regular; MiniZinc's native +% reification fails on values outside 1..S instead of reifying them to false. +fzn_regular_is_decomposed = true; + predicate gecode_regular(array[int] of var int: x, int: Q, int: S, array[int] of int: d, int: q0, set of int: F); From d4b8c2807b2c97cd7e433ea2a40da5a78764c0ec Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Wed, 2 Dec 2026 21:44:26 +0100 Subject: [PATCH 6/8] Fix overflow when posting cumulative with a single task When posting cumulative, Gecode checks whether the two smallest task usages exceed the capacity so it can reduce to unary. With a single task, the second-smallest usage stays INT_MAX, so min_u + min_u2 overflowed signed int when min_u was positive. Sum in long long int to avoid signed overflow; a single task now takes the unary branch. --- changelog.in | 7 +++++++ gecode/int/cumulative.cpp | 6 ++++-- test/int/cumulative.cpp | 29 +++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/changelog.in b/changelog.in index ab700dd658..021a23eed7 100644 --- a/changelog.in +++ b/changelog.in @@ -556,6 +556,13 @@ Make not-first/not-last propagation for unary resources prune all tasks with equal start or completion bounds, so propagation no longer depends on the order in which the tasks are stored. +[ENTRY] +Module: int +What: bug +Rank: minor +[DESCRIPTION] +Fix signed overflow when posting cumulative with a single task. + [ENTRY] Module: set What: bug diff --git a/gecode/int/cumulative.cpp b/gecode/int/cumulative.cpp index 3c3c50e3e0..9eff20b6e9 100644 --- a/gecode/int/cumulative.cpp +++ b/gecode/int/cumulative.cpp @@ -67,7 +67,8 @@ namespace Gecode::Int::Cumulative { } max_u = std::max(ui, max_u); } - bool disjunctive = (min_u > c.max() / 2) || (min_u2 > c.max() / 2 && min_u + min_u2 > c.max()); + bool disjunctive = + (min_u > c.max() / 2) || (min_u2 > c.max() / 2 && static_cast(min_u) + min_u2 > c.max()); if (disjunctive) { GECODE_ME_FAIL(c.gq(home, max_u)); unary(home, t, s, p, ipl); @@ -191,7 +192,8 @@ namespace Gecode::Int::Cumulative { } max_u = std::max(ui, max_u); } - bool disjunctive = (min_u > c.max() / 2) || (min_u2 > c.max() / 2 && min_u + min_u2 > c.max()); + bool disjunctive = + (min_u > c.max() / 2) || (min_u2 > c.max() / 2 && static_cast(min_u) + min_u2 > c.max()); if (disjunctive) { GECODE_ME_FAIL(c.gq(home, max_u)); unary(home, s, p, ipl); diff --git a/test/int/cumulative.cpp b/test/int/cumulative.cpp index b115799124..6795107557 100644 --- a/test/int/cumulative.cpp +++ b/test/int/cumulative.cpp @@ -503,6 +503,35 @@ namespace Test::Int::Cumulative { }; ManFixECumulative man_fix_e_cumulative; + /// A single task with usage at most half the capacity always fits + class SingleTaskCumulative : public Test { + private: + /// Whether to post with task types + bool typed; + + public: + /// Create and register test + SingleTaskCumulative(bool typed0) + : Test(std::string("Cumulative::SingleTask::") + (typed0 ? "Typed" : "Untyped"), 1, 0, 2, false), + typed(typed0) {} + /// %Test whether \a x is solution + bool + solution(const Assignment&) const override { + return true; + } + /// Post constraint on \a x + void + post(Gecode::Space& home, Gecode::IntVarArray& x) override { + if (typed) + Gecode::cumulative(home, 2, Gecode::TaskTypeArgs({Gecode::TT_FIXED_PROCESSING}), x, + Gecode::IntArgs({2}), Gecode::IntArgs({1}), ipl); + else + Gecode::cumulative(home, 2, x, Gecode::IntArgs({2}), Gecode::IntArgs({1}), ipl); + } + }; + SingleTaskCumulative single_task_cumulative(false); + SingleTaskCumulative single_task_cumulative_typed(true); + /// Check overload detection before search, including capacity wakeups // Complete assignments cannot distinguish basic from advanced propagation. class KnapsackAugmentedOverload : public Test::Base { From 3879a50bc794b3f6d3c8e876aab97e1485be8b28 Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Wed, 2 Dec 2026 21:44:26 +0100 Subject: [PATCH 7/8] Fix uninitialized read when creating or cloning trace views RangeList::copy() stored the incoming value of its output pointer r in its sentinel before overwriting it. Integer and set trace views pass an uninitialized RangeList* when created or cloned, so that dead store read uninitialized memory. Remove the dead sentinel.next(r) call. --- changelog.in | 8 ++++++++ gecode/kernel/range-list.hpp | 3 +-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/changelog.in b/changelog.in index 021a23eed7..410c6456c4 100644 --- a/changelog.in +++ b/changelog.in @@ -276,6 +276,14 @@ Rank: minor Avoid zero-byte heap allocations and null memcpy calls for empty Action, CHB, Archive, and Space::realloc from an empty block. +[ENTRY] +Module: kernel +What: bug +Rank: minor +[DESCRIPTION] +Fix an uninitialized read in RangeList::copy() when creating or cloning +integer and set trace views. + [ENTRY] Module: support What: new diff --git a/gecode/kernel/range-list.hpp b/gecode/kernel/range-list.hpp index de941d4176..5c8488b70a 100644 --- a/gecode/kernel/range-list.hpp +++ b/gecode/kernel/range-list.hpp @@ -90,7 +90,7 @@ namespace Gecode { /// \name Iterator operations //@{ - /// Create rangelist \a r from range iterator \a i + /// Create rangelist \a r from range iterator \a i, ignoring the old value of \a r template static void copy(Space& home, RangeList*& r, Iter& i); /// Overwrite rangelist \a r with ranges from range iterator \a i @@ -214,7 +214,6 @@ namespace Gecode { forceinline void RangeList::copy(Space& home, RangeList*& r, Iter& i) { RangeList sentinel; - sentinel.next(r); RangeList* p = &sentinel; while (i()) { RangeList* n = new (home) RangeList(i.min(), i.max()); From a214dc182fb2958e7c0a841fd6a1ac8b8225465f Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Wed, 2 Dec 2026 21:44:26 +0100 Subject: [PATCH 8/8] Bound the set relation auxiliary by the operands' upper bounds For the order relations SRT_LQ, SRT_LT, SRT_GQ, and SRT_GR, rel(home, x, op, y, r, z) created its auxiliary set variable for x op y over the full set universe. Because the set order propagator walks every value of the upper bound, posting these relations was extremely slow and memory-intensive. Bound the auxiliary set by the union of lub(x) and lub(y) for SOT_UNION and SOT_DISJOINT_UNION, their intersection for SOT_INTERSECTION, and lub(x) for SOT_MINUS. --- changelog.in | 9 ++++ gecode/set/rel-op/post.hpp | 26 ++++++++++- test/set/rel-op.cpp | 94 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) diff --git a/changelog.in b/changelog.in index 410c6456c4..6fa1624f0e 100644 --- a/changelog.in +++ b/changelog.in @@ -629,6 +629,15 @@ Rank: minor Document the exceptions thrown by sequence and integer-indexed element for empty arrays and out-of-limits sets. +[ENTRY] +Module: set +What: performance +Rank: minor +[DESCRIPTION] +Bound the auxiliary set created by rel(home, x, op, y, r, z) for SRT_LQ, +SRT_LT, SRT_GQ, and SRT_GR by the upper bounds of x and y instead of the +whole set universe, so the order propagator no longer walks the universe. + [ENTRY] Module: float What: change diff --git a/gecode/set/rel-op/post.hpp b/gecode/set/rel-op/post.hpp index 36ad26e76a..d79770d229 100644 --- a/gecode/set/rel-op/post.hpp +++ b/gecode/set/rel-op/post.hpp @@ -135,6 +135,29 @@ namespace Gecode::Set::RelOp { } } + /// Return a set variable whose upper bound contains every value of \a x \a op \a y + template + forceinline SetVar + rel_op_result(Home home, View0 x, SetOpType op, View1 y) { + LubRanges x_lub(x); + LubRanges y_lub(y); + switch (op) { + case SOT_UNION: + case SOT_DISJOINT_UNION: { + Iter::Ranges::Union, LubRanges> lub(x_lub, y_lub); + return SetVar(home, IntSet::empty, IntSet(lub)); + } + case SOT_INTERSECTION: { + Iter::Ranges::Inter, LubRanges> lub(x_lub, y_lub); + return SetVar(home, IntSet::empty, IntSet(lub)); + } + case SOT_MINUS: + return SetVar(home, IntSet::empty, IntSet(x_lub)); + default: + throw UnknownOperation("Set::rel"); + } + } + template forceinline void rel_op_post_nocompl(Home home, View0 x, SetOpType op, View1 y, SetRelType r, View2 z) { @@ -148,7 +171,8 @@ namespace Gecode::Set::RelOp { case SRT_LT: case SRT_GQ: case SRT_GR: { - SetVar tmp(home, IntSet::empty, Set::Limits::min, Set::Limits::max); + // The order propagator walks every value of the upper bound. + SetVar tmp = rel_op_result(home, x, op, y); rel_eq(home, x, op, y, tmp); rel_op_post_lex(home, tmp, r, z); } break; diff --git a/test/set/rel-op.cpp b/test/set/rel-op.cpp index 8821a9c519..e651ca2b10 100644 --- a/test/set/rel-op.cpp +++ b/test/set/rel-op.cpp @@ -200,6 +200,100 @@ namespace Test::Set::RelOp { Create c; + static IntSet ds_02(0, 2); + + /// Return whether \a a is less than or equal to \a b in the set order + static bool + set_lq(const IntSet& a, const IntSet& b) { + IntSetRanges ar(a), br(b); + Iter::Ranges::Diff a_minus_b(ar, br); + IntSetRanges ar2(a), br2(b); + Iter::Ranges::Diff b_minus_a(br2, ar2); + Iter::Ranges::Union, + Iter::Ranges::Diff> + sym_diff(a_minus_b, b_minus_a); + // The empty set is least, and the smallest differing value decides + return (a.size() == 0) || !sym_diff() || b.in(sym_diff.min()); + } + + /// %Test for order relation between a set operation and a set + class RelOrder : public SetTest { + private: + Gecode::SetOpType sot; + Gecode::SetRelType srt; + + public: + /// Create and register test + RelOrder(Gecode::SetOpType sot0, Gecode::SetRelType srt0) + : SetTest("RelOp::Order::" + str(sot0) + "::" + str(srt0), 3, ds_02, false), sot(sot0), srt(srt0) {} + /// %Test whether \a x is solution + bool + solution(const SetAssignment& x) const override { + CountableSetRanges xr0(x.lub, x[0]); + CountableSetRanges xr1(x.lub, x[1]); + IntSet r; + switch (sot) { + case SOT_DISJOINT_UNION: { + Iter::Ranges::Inter inter(xr0, xr1); + if (inter()) + return false; + CountableSetRanges yr0(x.lub, x[0]); + CountableSetRanges yr1(x.lub, x[1]); + Iter::Ranges::Union u(yr0, yr1); + r = IntSet(u); + } break; + case SOT_UNION: { + Iter::Ranges::Union u(xr0, xr1); + r = IntSet(u); + } break; + case SOT_INTERSECTION: { + Iter::Ranges::Inter i(xr0, xr1); + r = IntSet(i); + } break; + case SOT_MINUS: { + Iter::Ranges::Diff d(xr0, xr1); + r = IntSet(d); + } break; + default: + GECODE_NEVER; + } + CountableSetRanges zr(x.lub, x[2]); + IntSet z(zr); + switch (srt) { + case SRT_LQ: + return set_lq(r, z); + case SRT_LT: + return set_lq(r, z) && !set_lq(z, r); + case SRT_GQ: + return set_lq(z, r); + case SRT_GR: + return set_lq(z, r) && !set_lq(r, z); + default: + GECODE_NEVER; + } + return false; + } + /// Post constraint on \a x + void + post(Space& home, SetVarArray& x, IntVarArray&) override { + Gecode::rel(home, x[0], sot, x[1], srt, x[2]); + } + }; + + /// Help class to create and register order relation tests + class CreateOrder { + public: + /// Perform creation and registration + CreateOrder() { + using namespace Gecode; + for (SetOpTypes sots; sots(); ++sots) + for (SetRelType srt : {SRT_LQ, SRT_LT, SRT_GQ, SRT_GR}) + (void)new RelOrder(sots.sot(), srt); + } + }; + + CreateOrder co; + /// %Test for n-ary partition constraint class RelN : public SetTest { private: