From a94f083dec99ba67bf798d9b576f38dd2cff1b19 Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Mon, 13 Jul 2026 16:50:31 +0200 Subject: [PATCH] Fix non-terminating sorted propagation --- Makefile.in | 1 + changelog.in | 10 +++++ cmake/GecodeSources.cmake | 1 + gecode/int/sorted/narrowing.hpp | 10 ++++- test/flatzinc/issue168.cpp | 75 +++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 test/flatzinc/issue168.cpp diff --git a/Makefile.in b/Makefile.in index 2762b72a40..be42d52c23 100755 --- a/Makefile.in +++ b/Makefile.in @@ -1142,6 +1142,7 @@ FLATZINCTESTSRC0 = \ test/flatzinc/int_set_as_type1.cpp \ test/flatzinc/int_set_as_type2.cpp \ test/flatzinc/issue166.cpp \ + test/flatzinc/issue168.cpp \ test/flatzinc/jobshop.cpp \ test/flatzinc/no_warn_empty_domain.cpp \ test/flatzinc/output_test.cpp \ diff --git a/changelog.in b/changelog.in index e584560f51..18991b0bf4 100755 --- a/changelog.in +++ b/changelog.in @@ -86,6 +86,16 @@ Fix domain-level global cardinality propagation losing solutions by counting assigned variables twice and retaining stale conflict marks when cardinality bounds shrink. +[ENTRY] +Module: int +What: bug +Rank: major +Issue: 168 +Thanks: Mats Carlsson +[DESCRIPTION] +Fix non-terminating propagation for sorted constraints when a matching +component contains no interval compatible with an input variable. + [ENTRY] Module: flatzinc What: new diff --git a/cmake/GecodeSources.cmake b/cmake/GecodeSources.cmake index 088aa12457..0c9defc0f1 100644 --- a/cmake/GecodeSources.cmake +++ b/cmake/GecodeSources.cmake @@ -294,6 +294,7 @@ set(GECODE_TEST_SOURCES test/flatzinc/int_set_as_type1.cpp test/flatzinc/int_set_as_type2.cpp test/flatzinc/issue166.cpp + test/flatzinc/issue168.cpp test/flatzinc/jobshop.cpp test/flatzinc/jobshop2x2.cpp test/flatzinc/knights.cpp diff --git a/gecode/int/sorted/narrowing.hpp b/gecode/int/sorted/narrowing.hpp index 2d21714432..b29bd04511 100644 --- a/gecode/int/sorted/narrowing.hpp +++ b/gecode/int/sorted/narrowing.hpp @@ -152,7 +152,10 @@ namespace Gecode { namespace Int { namespace Sorted { */ int start = sinfo[scclist[i]].leftmost; while (y[start].max() < xmin) { - start = sinfo[start].right; + int next = sinfo[start].right; + if (next == start) + return false; + start = next; } if (Perm) { @@ -184,7 +187,10 @@ namespace Gecode { namespace Int { namespace Sorted { */ start = sinfo[scclist[ptau]].rightmost; while (y[start].min() > xmax) { - start = sinfo[start].left; + int next = sinfo[start].left; + if (next == start) + return false; + start = next; } if (Perm) { diff --git a/test/flatzinc/issue168.cpp b/test/flatzinc/issue168.cpp new file mode 100644 index 0000000000..fb62394912 --- /dev/null +++ b/test/flatzinc/issue168.cpp @@ -0,0 +1,75 @@ +/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ +/* + * Main authors: + * Mikael Zayenz Lagerkvist + * + * Copyright: + * Mikael Zayenz Lagerkvist, 2026 + * + * This file is part of Gecode, the generic constraint + * development environment: + * http://www.gecode.dev + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "test/flatzinc.hh" + +namespace Test { namespace FlatZinc { + + namespace { + /// Check that propagation terminates after finding all solutions + bool check(const std::string& output) { + const std::string separator = "----------\n"; + int solutions = 0; + for (std::string::size_type p = 0; + (p = output.find(separator,p)) != std::string::npos; + p += separator.size()) + solutions++; + return (solutions == 3) && + (output.find("==========\n") != std::string::npos); + } + + /// Helper class to create and register test + class Create { + public: + /// Perform creation and registration + Create(void) { + (void) new FlatZincTest("Issue168", +"predicate fzn_sort(array [int] of var int: x, array [int] of var int: y);\n\ +var 2..4: D :: output_var;\n\ +var {2,4}: E :: output_var;\n\ +var {1,4}: F :: output_var;\n\ +var 3..4: G :: output_var;\n\ +var {2,4}: H :: output_var;\n\ +var 3..4: J :: output_var;\n\ +var {1,4}: K :: output_var;\n\ +constraint fzn_sort([F,K,G,D],[F,E,H,J]);\n\ +solve :: int_search([1,D,F,K,F,E,G,H,J],input_order,indomain,complete) satisfy;\n", + "", true, {"-a"}, check); + } + }; + + Create c; + } + +}} + +// STATISTICS: test-flatzinc