Constraint Propagation and Search Heuristics in Constraint Programming

Author

Your Name

Published

February 5, 2025

Introduction

This document summarizes the lecture on constraint propagation and search heuristics in constraint programming. The primary objective is to understand how to efficiently solve Constraint Satisfaction Problems (CSPs) and Constraint Optimization Problems (COPs). We will explore techniques such as path labeling trees, search heuristics, and restart strategies. Furthermore, we will demonstrate how to model problems using the MiniZinc language and how to fine-tune solver behavior through various parameters and annotations. This lecture emphasizes the importance of declarative programming for problem modeling, as well as the necessity of understanding underlying search mechanisms to optimize the solution process.

Constraint Propagation and Labeling Trees

Constraint propagation is a deterministic process that reduces the domains of variables by removing values that cannot be part of any solution. This process is applied at each node of the search tree. The overall search process combines constraint propagation with non-deterministic choices, such as variable and value selection.

  • Propagation in Each Node: At every node of the search tree, constraint propagation is applied to reduce the domains of variables. This step aims to prune the search space by eliminating inconsistent values.

  • Variable Selection: A variable is chosen for assignment. Its domain is then reduced either by assigning a specific value or by splitting the domain into smaller parts.

  • Backtracking: If the current assignment leads to a conflict or a dead end, the algorithm backtracks to a previous decision point and explores alternative choices.

Labeling Strategies

Labeling strategies define how values are assigned to variables during the search process.

  • Labeling: Assign values sequentially, trying each value in the domain one by one (e.g., \(A_1\), \(A_2\), ..., \(A_n\)).

  • Domain Enumeration: Select a specific value from the domain and explore the consequences of that choice, leaving other values for backtracking.

  • Domain Bisection: Split the domain into two halves and explore each half separately. This strategy is particularly useful for integer domains.

Impact of Search Heuristics

The choice of search heuristics significantly impacts the shape of the search tree and the path taken to reach solutions. Different heuristics can lead to the same solutions but in a different order and with varying efficiency.

  • Example: Consider a simple CSP with the constraint \(X < Y\), where \(X \in \{1, 2\}\) and \(Y \in \{1, 2, 3\}\). If we choose to assign \(X\) first, the search tree will have a different shape compared to when we assign \(Y\) first. This difference in tree shape can affect the order in which solutions are found.

  • Goal: The primary goal is to identify heuristics that guide the search towards solutions as quickly as possible, reducing the overall search time.

Search trees for the CSP (X < Y), with (X {1, 2}) and (Y {1, 2, 3}). The left tree shows the search when X is chosen first, and the right tree shows the search when Y is chosen first.

Search Heuristics in MiniZinc

This section details the search heuristics available in MiniZinc, which allow users to fine-tune the search process for solving CSPs and COPs.

Declarative vs. Procedural Programming

In constraint programming, it is crucial to distinguish between declarative and procedural programming approaches.

  • Declarative Programming: Focuses on modeling the problem by specifying the constraints that must be satisfied (e.g., X[i] != X[j]). The programmer describes what the problem is, rather than how to solve it.

  • Procedural Programming: Focuses on the search process, detailing the steps the solver should take to find a solution. This approach is less common in constraint programming, where the solver typically handles the search process.

Variable Selection Strategies

Variable selection strategies determine the order in which variables are assigned values during the search. Different strategies can significantly impact the efficiency of the search process.

  • Input Order: Variables are labeled in the order they appear in the list. This strategy is simple and has a constant time implementation, making it very fast.

  • Occurrence: Selects the variable that appears in the most constraints. This strategy uses static information that can be preprocessed, making it relatively efficient.

  • First Fail (FF): Chooses the variable with the smallest domain. This strategy is dynamic, as the domains of variables change during the search, and thus it is not constant time.

  • Anti First Fail: Chooses the variable with the largest domain.

  • Most Constrained: Combines First Fail and Occurrence. It selects the variable with the smallest domain, and among those, the one that is involved in the most constraints. This strategy is dynamic.

  • Wdeg (Weighted Degree): Selects the variable with the largest domain divided by the number of attached constraints, weighted by how often these constraints have caused failures. This strategy is costly to implement because it requires dynamic updates during the search.

  • Impact: Chooses the variable based on the number of propagations initiated from it. This is a dynamic measure and can be costly to compute.

  • Max Regret: Selects the variable with the largest difference between the two smallest values in its domain. This strategy aims to reduce the uncertainty in the variable assignment.

Value Choice Strategies

Value choice strategies determine the order in which values are assigned to a selected variable. These strategies can also significantly influence the search efficiency.

  • In Domain Min: Assigns values in ascending order, starting with the smallest value in the domain.

  • In Domain Interval: Bisects the domain, trying the first interval and then the other. This is useful for integer domains.

  • In Domain Median: Assigns the median value of the current domain.

  • In Domain Middle: Assigns the value closest to the arithmetic average of the current domain.

  • In Domain Random: Assigns a random value from the current domain.

  • In Domain Max: Assigns the largest value in the current domain.

  • Bisect Exclude Lower/Upper: Bisects the domain and excludes the lower or upper half first, respectively.

  • Bisect Random: Bisects the domain and chooses one half randomly.

  • Exclude Largest/Middle/Smallest: Leaves the largest, middle, or smallest value for backtracking, trying the other values first.

MiniZinc Language

This section provides an overview of the MiniZinc language, focusing on its syntax and key features for modeling constraint satisfaction and optimization problems.

Parameters and Variables

MiniZinc distinguishes between parameters (constants) and variables, which are essential for modeling problems.

  • Parameters: Parameters are constants that are defined at the beginning of the model. They are declared using the par keyword (though it is optional) and can be assigned only once. For example, par int N = 4; declares an integer parameter N with a value of
  • Variables: Variables are declared using the var keyword and represent the unknowns in the problem. For example, var int: x; declares an integer variable x.

Types

MiniZinc supports several basic data types for variables and parameters.

  • int: Represents integer variables with a finite domain.

  • bool: Represents Boolean variables, which can be either true or false.

  • float: Represents real numbers.

  • string: Represents strings of characters.

Domains

Each variable in a CSP has an associated domain, which specifies the set of possible values that the variable can take. Domains are typically defined as intervals or sets.

  • Interval: An interval specifies a range of integer values. For example, var 0..100: v; declares an integer variable v with a domain of integers from 0 to 100.

  • Set: A set specifies a collection of values. For example, var set of 1..8: s; declares a variable s that can take any subset of the integers from 1 to 8.

Arrays

MiniZinc supports multi-dimensional arrays, which are useful for representing collections of variables or parameters.

  • One-dimensional: A one-dimensional array is declared using the array keyword with a single index set. For example, array[0..2] of var 1..5: d; declares an array d of three variables, each with a domain from 1 to 5.

  • Multi-dimensional: Multi-dimensional arrays are declared using multiple index sets. For example, array[1..5, 1..5] of var 0..2: matrix; declares a 5x5 matrix of variables, each with a domain from 0 to 2.

Constraints

Constraints are defined using the constraint keyword and specify the relationships that must hold between variables.

  • Arithmetic: Arithmetic constraints specify relationships between arithmetic expressions. For example, constraint a + b < 100; states that the sum of variables a and b must be less than 100.

  • Boolean: Boolean constraints specify relationships between Boolean expressions. For example, constraint a b; states that at least one of the Boolean variables a or b must be true.

  • Global Constraints: Global constraints are built-in constraints that express common patterns in CSPs. For example, constraint all_different(list_of_variables); states that all variables in the list must have different values.

  • For Expressions: For expressions are used to define constraints over a range of indices. For example, constraint forall(i, j in 1..3 where i < j) (b[i] != b[j]); states that all pairs of variables b[i] and b[j] must have different values, where i and j are indices from 1 to 3 and i is less than j.

Aggregates

Aggregates are built-in constraints that perform operations on collections of variables, such as min, sum, and max.

  • Example: The constraint s = min(i in 1..n)(vec[i]); assigns the minimum value of the elements in the array vec to the variable s.

  • Note: Aggregates are not simple assignments; they are constraints that support bidirectional propagation. This means that if the value of s is known, it can be used to infer information about the values of the elements in vec, and vice versa.

Advanced Search Techniques

This section introduces advanced search techniques that can be used to improve the efficiency of solving complex CSPs and COPs. These techniques include the use of sequences of search annotations and restart strategies.

Sequence of Search Annotations

MiniZinc allows the use of a sequence of search annotations, enabling different search strategies to be applied to different sets of variables. This is particularly useful when dealing with problems where different parts of the model may benefit from different search heuristics.

  • Example: You can use the most_constrained variable selection strategy for one set of variables and the in_domain_min value choice strategy for another set of variables. This allows for a more tailored approach to the search process.

Restart Strategies

Restart strategies involve interrupting the current search process and restarting it from the beginning after a certain number of nodes have been visited or a certain amount of time has elapsed. This can help to avoid getting stuck in unproductive parts of the search space.

  • Linear Restart: The search is restarted after a fixed number of nodes, \(n\), have been visited. Subsequent restarts occur after \(2n\), \(3n\), and so on. The node limits for restarts form an arithmetic sequence: \(n, 2n, 3n, \dots\).

  • Geometric Restart: The search is restarted after a number of nodes that increases geometrically. The first restart occurs after \(n\) nodes, the second after \(n\alpha\) nodes, the third after \(n\alpha^2\) nodes, and so on. The node limits for restarts form a geometric sequence: \(n, n\alpha, n\alpha^2, \dots\), where \(\alpha\) is a constant greater than 1.

  • Luby Restart: The search is restarted based on the Luby sequence. The Luby sequence is a sequence of integers that is defined as follows:

    Let \(L(k)\) be the \(k\)-th element of the Luby sequence.

    • \(L(1) = 1\)

    • If \(k = 2^m\) for some integer \(m\), then \(L(k) = 2^m\).

    • If \(2^m < k < 2^{m+1}\), then \(L(k) = L(k - 2^m)\).

    The first few elements of the Luby sequence are: 1, 1, 2, 1, 1, 2, 4, 1, 1, 2, 1, 1, 2, 4, 8, ...

    The search is restarted after \(n \cdot L(k)\) nodes, where \(n\) is a base number of nodes and \(L(k)\) is the \(k\)-th element of the Luby sequence.

Conclusion

This lecture provided a comprehensive overview of constraint propagation, search heuristics, and advanced techniques such as restart strategies in constraint programming. We explored how to model problems using the MiniZinc language and how to fine-tune the solver’s behavior using various parameters and annotations. Key takeaways include the importance of selecting appropriate variable and value selection strategies, understanding the trade-offs between different heuristics, and leveraging restart mechanisms for tackling large and complex problems.

  • Constraint Propagation: A deterministic process that reduces variable domains.

  • Search Heuristics: Strategies for variable and value selection that impact search efficiency.

  • MiniZinc Language: A declarative language for modeling CSPs and COPs.

  • Advanced Techniques: Sequences of search annotations and restart strategies for complex problems.

For further exploration, consider the following follow-up questions and topics for the next lecture:

  1. Real-World Modeling: How can we effectively model real-world problems, such as the hospital scheduling problem, using MiniZinc?

  2. No Free Lunch Theorem: What are the practical implications of the "no free lunch" theorem in the context of choosing search heuristics? How does this theorem influence our approach to problem-solving?

  3. Restart Strategies Evaluation: How can we implement and evaluate the performance of different restart strategies? What are the criteria for selecting the most appropriate restart strategy for a given problem?

  4. Global Constraints: What are some examples of global constraints beyond all_different, and how are they implemented? How do these constraints contribute to the efficiency of constraint solvers?

  5. Hybrid Approaches: How can we combine constraint programming with other optimization techniques, such as local search or machine learning? What are the potential benefits and challenges of these hybrid approaches?

The next lecture will delve deeper into modeling with MiniZinc and provide hands-on examples to solidify these concepts. We will explore practical applications and further refine our understanding of constraint programming techniques.