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 543c60f69a..6fa1624f0e 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 @@ -538,6 +546,31 @@ 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: 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: int +What: bug +Rank: minor +[DESCRIPTION] +Fix signed overflow when posting cumulative with a single task. + [ENTRY] Module: set What: bug @@ -570,6 +603,24 @@ 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: 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 @@ -578,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 @@ -706,6 +766,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); 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/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/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/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/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()); 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/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/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/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 { diff --git a/test/int/unary.cpp b/test/int/unary.cpp index 6a85ad9783..d376d561b1 100644 --- a/test/int/unary.cpp +++ b/test/int/unary.cpp @@ -267,6 +267,104 @@ 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; + + /** + * \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: 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 { 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: 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: