Implementation of Strategies For Solving Constraint Satisfaction Problems
Implementation of Strategies For Solving Constraint Satisfaction Problems
Implementation of Strategies For Solving Constraint Satisfaction Problems
Submitted to the
Institute of Graduate Studies and Research
in partial fulfillment of the requirements for the Degree of
Master of Science
in
Applied Mathematics and Computer Science
I certify that this thesis satisfies the requirements as a thesis for the degree of Master
of Science in Applied Mathematics and Computer Science.
We certify that we have read this thesis and that in our opinion it is fully adequate in
scope and quality as a thesis for the degree of Master of Science in Applied
Mathematics and Computer Science.
Examining Committee
This thesis investigates the different strategies for solving constraint satisfaction
problems. The basic properties of constraint satisfaction problems are discussed. The
different types of constraints are given. The constraint graph and cryptarithmetic
backtracking search, local search, and constraint propagation for solving constraint
solved.
iii
ÖZ
iv
ACKNOWLEDGMENT
First of all, I thank God for every minute of my life in this world. I would like to
for his support during my work on this thesis. I thank him for every good advice,
I thank God for a wonderful family I have. I want to express my appreciation and
Abdulrazaq for supporting me in each step I take. The special thanks to my brother
Karwan for being by my side and helping me. I thank all my sisters, brothers,
v
TABLE OF CONTENTS
ABSTRACT………………………………………………………………………..iii
ÖZ..............................................................................................................................iv
ACKNOWLEDGMENT..……………………………………………………….......v
LIST OF FIGURES.................................................................................................viii
1 INTRODUCTION....................................................................................................1
SATISFACTION PROBLEMS..................................................................................5
SATISFACTION PROBLEMS................................................................................22
vi
4.8 Sudoku as a CSP..............................................................................................35
5 CONCLUSION......................................................................................................39
REFERENCES..........................................................................................................40
vii
LIST OF FIGURES
Figure 1: Binary constraint graph and matrix for binary constraint ..........................13
Figure 2: Initial (uncolored) and final (colored) map coloring CSP problem...........14
Figure 6: First step in 4-queens problem with one queen on chessboard and
unattacked cells...........................................................................................................25
Figure 7: Second step in 4-queens problem with two queens on chessboard and
unattacked cells...........................................................................................................26
Figure 8: Third step in 4-queens problem with three queens on chessboard and
unattacked cells...........................................................................................................27
Figure 10: The sequence of steps for the possible solution of 4-queens problem…..28
Figure 11: The possible arrangements for first chess queen in 8-queens
problem.......................................................................................................................30
Figure 12: The possible arrangements for two chess queens in 8-queens problem…31
Figure 13: The possible arrangements for three chess queens in 8-queens
problem.......................................................................................................................32
Figure 15: The first state in Sudoku game filled with digits from 1 to 9....................37
viii
Figure 16: The second state in Sudoku game without repetition of digits in the first
grid .............................................................................................................................37
ix
Chapter 1
INTRODUCTION
Artificial Intelligence (AI) is a special area of computer science that aims to create
intelligent machines. AI has become an basic part of the technology industry. Alan
Turing is a founder of AI, and in 1950 he made a simple machine to solve the
mathematical problems (called the Turing Machine). Based on this idea, Turing
wondered that if a computer’s reply was indistinguishable from a human, then the
data mining, medical diagnosis, industry and many other areas [2].
The concept of constraint satisfaction problem (CSP) was proposed in 1970s. CSP is
defined as a set of objects whose state must satisfy a number of constraints. CSP has
been used in many fields to represent various problems like N-queen game, the
graph-coloring problem, design issues such as VLSI, the industrial application such
resources.
A set of variables and a set of constraints are components of CSP. A finite and
1
A CSP can be considered as a search problem with the following incremental
formulation:
- Initial state: no value is assigned to the variables, i.e. empty assignment {};
- Successor function: the value is assigned to one of the unassigned variables with
should take place between unassigned variable and the variables which have been
assigned previously;
- Goal: the process of assigning the values to the variables is complete, i.e. all the
- Past cost: every step should be with a constant cost which is irrelevant.
make sure that each arc is consistent. The values that can never be the parts of any
as a graph in which each node depicts a variable, and each arc depicts a constraint
2
- Higher-order constraints consist of three or more variables to be represented by the
constraint hypergraph;
- Soft constraints can represent the preference of one variable over another one by
indicating the costs assigned to each of these variables. Soft constraints are useful in
constraints can be violated during the process that solution of the problem is found.
In Max-CSP all the constraints are to be equally important, and the purpose is to find
the assignment which is able to maximize the satisfied constraints. Several real world
warehouse location problem and max-cut problems. In order to solve the Max-CSP, a
value for each variable from each domain must be assigned in such a way that the
scientists [5].
There are various search algorithms in AI, and the backtracking algorithm is the
basic uninformed search algorithm for solving CSP. This algorithm uses constraint
3
propagation technique. In this algorithm, the variables and their values are chosen by
applying the heuristic approach. The variables are represented in some order. The
unassigned variable is chosen and the value to this variable is given which should be
consistent with all the variables. The values should be assigned to the variables one-
by-one. This process continues until no more assignment is made, i.e. every variable
has a value, then it is decided that the solution is found or the backtracking to the
coloring) problems.
the entire search space can’t be successful for the real-world CSPs.
are distributed among multiple agents, and the agents must communicate with each
other to satisfy all constraints. The agents must assign values to their variables so that
all the constraints are satisfied [6-7]. The multi-agent systems can be used for
4
Chapter 2
discussed. The algorithm of QCSP with discrete non-Boolean domains is given. The
problem is solved by expanding the search algorithm from standard CSPs to QCSP.
but at the same time the complexity of the reasoning task raises from NP-complete to
implementation in game playing and planning. This algorithm shows how the value
[9] is about probabilistic algorithm for solving k-SAT and CSP. The algorithm
randomly creates an initial task and then leads by those conditions that are not
satisfied to select random literal from such a condition and try to find a satisfying
task by flipping the conformable bit. If a satisfying task is not available after using
O(n) steps, so it will start again. This is the best known algorithm of 3-SAT known
until now.
5
The combination of global and local searches in which the abstraction of a constraint
satisfaction problem is created by local search, and afterwards the global search tries
to exploit it [10]. It is proved that the cluster-based abstraction provides better results
in searching problems.
In [11] a machine called Turing machine with atoms (TMA) is determined, and this
to characterize the classes of structures to make available from words with atoms.
applications. Two new algorithms called the Iterative Distributed Breakout (IDB)
and the Synchronous Branch and Bound (SBB) are discussed. Both algorithms are
tested. The result shows that SBB is better when the optimal solution is looked for,
and IDB is more suitable for the cases when one wants to get an optimal solution
In [13] proposed filtering techniques are necessary to solve the constraint satisfaction
problems. These techniques can sharply minimize the search particularly on hard and
large problems. One of the most useful filtering techniques is arc consistency,
because this technique easily removes the values which are suitable for neither
6
solution. The filtering techniques which are more meaningful than arc consistency in
cases when the set of constraints do not change, are studied and compared.
offered in [14], and it is capable of bounding the worst-case performance better than
pseudo-tree search.
Many techniques exist for solving the constraint satisfaction problem. The modified
satisfaction problem in a problem of a map coloring. The adjacent areas in the map
must not be of the same color. The branch and bound algorithm uses back jumping
when it faces a dead-end in the search, and the variable ordering is used to help the
searching process. In comparison with backjumping, the branch and bound algorithm
The tractable classes of CSPs are investigated in [16]. In order to solve constraint
and is used for constraint satisfaction problems. In [17] the basis for the extraction
7
a proposed basis. The domain values can be exploited by a proposed backtrack
procedure. The experimental results show that a proposed approach is accurate for a
problem. The polynomial-time algorithm is given which is used to determine the core
structures.
them, and this type of CSP is called weighted CSP (WCSP) which is discussed in
[19]. One of the complete techniques used to solve such problems is a bucket
heuristic method which is applied in case when the memory for the application of
bucket elimination is very high. A memetic algorithm for weighted CSP is presented
which provides better solutions in large instances than classical algorithms. The
hybrid form of bucket elimination (BE) and mini-bucket (MB) with memetic
algorithm is presented.
In case constraint satisfaction problem has any solution, this is NP-complete task.
There are many filtering techniques, and one of the useful of them is arc-consistency.
8
dynamic CSP, the algorithm is proposed. The advantages of the proposed algorithm
are given.
In [21] the proposed method reuses the previous solution of constraint satisfaction
problem by local changing to produce a new solution for CSP. The idea and
algorithm are given. The experimental results are compared to classical backtrack
and dynamic backtracking methods, and show the effectiveness of this method.
algorithm is suggested for above problem that uses JOIN operation which is applied
two types of errors called false negative and false positive errors. The affect of such
errors to the propose algorithm is analyzed. The error probabilities can be decreased
The combination of base algorithms which are described in terms of forward and
backward moves is given in [23]. The forward move of one algorithm can be
successfully combined with backward move of another algorithm and versus visa.
The combination of tree search algorithms can be useful for some other algorithms.
9
Chapter 3
variable 𝐴 i . Every constraint 𝐵 j limits the values that variables can take, for
some or all variables. An assignment does not break the constraints. A complete
1) Variables: 𝐴 = 𝐴 1 , 𝐴 2 , 𝐴 3 , … , 𝐴 m .
2) Domains: 𝑌, integers.
3) Constraints: 𝐵 1 , 𝐵 2 , 𝐵 3 , … , 𝐵 n Y n . .
1 i n.
10
There should be the connections between variables, for example: d ≠ e, d >
Assume there are three variables {a, b, c}, and for the variable a we have the set of
values {1,2,3,4}, and for variable 𝑏 we have the set of values {2,3,4} and for variable
The constraint a + b < c is defined as {(1,2), (1,3), (1,4), (2,2), (2,3), (2,4), (3,3)} .
1) Discrete variables:
2) Continuous variables:
The class schedule, the airline schedule are the examples of continuous variables.
11
3.2 Types of constraint satisfaction problem
P ≠ red;
D(X): X = 2;
D(Y): Y > 5.
2) A binary CSP: all constraints are binary. Every unary (non-binary) CSP can be
converted into a binary CSP by entering an additional variable. A binary CSP can be
represented as a constraint graph that has a node for each variable and an arc between
two nodes. The binary constraints are the edges between nodes.
graph. The constraint graph is the graph represents the constraint, which
communication between variables in the problem. This graph relies heavily on the
impersonation that picks for a specific problem. Constraint graph has a node for each
variable and an arc between two nodes. The constraints are the edges between every
node. Constraint graphs are more beneficial when whole constraint propagation is
In Figure 1 the binary constraint graph and the matrix for binary constraints are
represented.
12
Figure 1: Binary constraint graph and matrix for binary constraint
Map coloring is another famous problem of CSP. The problem can be defined in the
following form:
Variables (countries): {Germany (GE), Poland (PL), Czech Republic (CZ), Austria
(AT), Switzerland (SUI), Hungary (HU), Slovakia (SK), Italy (IT), Slovenia (SL)}.
Constraints: the adjacent areas must have different colors (e.g.: color of (GE) ≠ color
of PL)).
13
A CSP is solved if the complete assignment satisfying all the constraints is
determined.
Figure 2 shows the initial (uncolored) and final (colored) map coloring CSP problem.
Figure 2: Initial (uncolored) and final (colored) map coloring CSP problem
The map coloring problem can also be represented as a constraint graph. The nodes
of the graph represent the variables, and the arcs represent the binary constraints or
constraints.
14
Formulation for constraint graph:
- Node = variable;
- Arc = constraint;
- Successor state: select a value to one of the variables without a color or value;
15
Another example for map-coloring is the following: color this map by using three
colors (green, red, yellow), and no adjacent areas have the same color:
Variables: 𝐴, 𝐵, 𝐶, 𝐷, 𝐸, 𝐹, 𝐺.
Constraints: A ≠ B, A ≠ C, B ≠ D, B ≠ C, C ≠ D, C ≠ E, C ≠ F, D ≠ F, E ≠ F, G ≠ F.
(Figure 4).
16
Figure 5: Hypergraph coloring
B = {{1}, {2}, {3}, {4}, {5}, {6}, {1,2}, {1,3}, {1,5}, {2,4}, {3,6}, {4,5},
In cryptarithmetic problem, each letter stands for a different digit (or each letter
could be one digit), 0 through 9. The purpose of the problem is to determine which
TOM
+ NAG
GOAT
17
Variables: {T, O, M, N, A, G)
Domain: {0,1,2,…,9}
Number of variables: 6
Constraints:
𝐵1 =∶ {T, O, M, N, A, G) R 6 │0 ≤ T, … , G ≤ 9}
B2 = {(T, O, M, N, A, G) R 6 │100 ∗ T + 10 ∗ O + M +
100 ∗ N + 10 ∗ A + G =
1000 ∗ G + 100 ∗ O + 10 ∗ A + T =
B3 = {(T, O, M, N, A, G) R 6 │T ≠ 0}
B4 = {(T, O, M, N, A, G) R 6 │N ≠ 0}
Solution: (7, 0, 6, 3, 5, 1) R 6
706
+ 351
1057
HERE
+ SHE
COMES
18
Variables: {H, E, R, S, C, O, M}
Number of variables: 7
Constraints:
B1 = {(H, E, R, S, C, O, M) R 7 │0 ≤ H, … . , M ≤ 9}
100 ∗ S + 10 ∗ H + E =
B3 = {(H, E, R, S, C, O, M) R 7 │H ≠ 0}
B4 = {(H, E, R, S, C, O, M) R 7 │S ≠ 0}
Solution: (9, 4, 5, 8, 1, 0, 3) R 7
9454
+ 894
10348
The following is a timetable scheduling example. There are three computer courses
and three instructors who are able to teach these courses. The main problem is to
arrange the courses and classes in order that the same teacher will not teach more
Time:
19
Course 3: from 9:30am – 10:30am
Course 1: {B};
Course 1≠Course 2;
Course 2≠Course 3.
The following shows which course(s) can be taught by which instructor(s) (according
to the constraints):
Course 1: {B};
20
There are some possible solutions for this problem, and below two of these solutions
are given:
First solution:
Course 1= B;
Course 2= A;
Course 3= B.
Second solution:
Course 1= B;
Course 2= C;
Course 3= A.
21
Chapter 4
It is an algorithm to find all solutions for computational problems. After trying all
not found, it is necessary to come back and take another way until the correct
solution is found. The backtracking search is useful for solving CSPs, for example,
Solving a constraint satisfaction problem commonly means that one or more different
ways to assign the value to each of the variables must be found so as no constraint is
search, and is a regular search step for solving constraint satisfaction problems.
The backtracking algorithm usually considers the variables in some finite order.
Beginning with the first variable, the backtracking algorithm assigns a temporary
value for every variable in the role so long as every assigned value is found to be
regular with values assigned in the last. In case the algorithm marks a variable for
22
whatever none of the values in accordance with the precedent assignments, a dead-
The algorithm halts when the solution is found, or no solution can be found after the
two queens would be able to attack each other. There must be no any two queens
positioned in the same diagonal, row, and column. Otherwise, if any two queens are
in the same diagonal, row, and column, they will threaten each other.
Domain: 𝑋 i = {1, 2, 3, … , n };
Constraints: V i ≠ V j .
Vi − V j ≠ i − j
Vi − V j ≠ j − i
23
4.3 Search for 4-queens problem
A trick: create step by step and use one queen for each step.
B(r1, r3) = {(1,2), (1,4), (2,1), (2,3), (3,2), (3,4), (4,1), (4,3)}
B(r1, r4) = {(1,2), (1,3), (2,1), (2,3), (2,4), (3,1), (3,2), (3,4), (4,2), (4,3)}
B(r2, r4) = {(1,2), (1,4), (2,1), (2,3), (3,2), (3,4), (4,1), (4,3)}
24
x1 = {1, 2, 3, 4}
x2 = {1, 2, 3, 4}
x3 = {1, 2, 3, 4}
x4 = {1, 2, 3, 4}
- First step:
The first queen is put onto the cell 2 in column x1. Afterward the following cells are
excluded for the next queens to make the first queen unattacked.
x1 = 2, so eliminate {x1 = 1, x1 = 3, x1 = 4, x2 = 1, x2 = 2, x2 = 3, x3 = 2,
onto).
Figure 6: First step in 4-queens problem with one queen on chessboard and
unattacked cells
- Second step:
25
After the first queen is put, there is only cell [4] in column x2 to put the second
queen. So second queen is put onto the cell [4], and so the following cells must be
excluded for the next queens to make the first two queens unattacked.
x1 = 2, x2 = 4, so eliminate {x1 = 1, x1 = 3, x1 = 4, x2 = 1, x2 = 2, x2 =
3, x3 = 2, x3 = 3, x3 = 4, x4 = 2, x4 = 4} (Figure 7).
Figure 7: Second step in 4-queens problem with two queens on chessboard and
unattacked cells
- Third step:
There is only cell 1 of the column x3 to put the third queen. So the third queen is put
on. After the third queen is put, the following cells must be excluded for the last
fourth queen:
x1 = 2, x2 = 4, x3 = 1, so eliminate {x1 = 1, x1 = 3, x1 = 4, x2 = 1, x2 =
2, x2 = 3, x3 = 2, x3 = 3, x3 = 4, x4 = 1, x4 = 2, x4 = 4} (Figure 8).
26
Figure 8: Third step in 4-queens problem with three queens on chessboard and
unattacked cells
- Fourth step:
The only cell to put the last fourth queen on is the cell 3 of the column x4. So the
Figure 9.
The Figure 10 represents the sequence of steps (from starting to target) for a possible
27
Figure 10: The sequence of steps for the possible solution
of 4-queens problem
Finding all the solutions of n-queens problem can be reached just by using
backtracking.
The backtracking search can be also used to solve the 8-queens problem, where the
any another queen. So, the arrangement of queens should be carried out in the form
that no queen will be attacked by any another queen on the chessboard. It can be
easily calculated that there are 4,426,165,368 (8 8 ) possible ways to arrange queens
on 8×8 chessboard, but there are only 92 possible solutions of this problem. In
backtracking, any incomplete solution that contains any two queens attacking each
other, should be rejected and it is needed to go back to take another way in order to
1) At the first column and first row start with the first queen.
28
2) Determine which position the first queen will attack.
3) Go on with the second queen to be put at first row and second column.
7) Go back to step 4.
Goal: all eight queens on a chessboard and no queen attacks any another queen on
the chessboard.
First, there are eight possible cells to put the queen in the first row. It is shown in
Figure 11.
29
Figure 11: The possible arrangements for first chess queen in 8-queens problem
In the next stage, for each queen, there are many cells to be put, but the second queen
must be put on those cells which will not be attacked by any another queen (Figure
12).
30
Figure 12: The possible arrangements for two chess queens in 8-queens problem
A possible position for the third queen can be arranged on a chessboard in the form
31
Figure 13: The possible arrangements for three chess queens in 8-queens problem
easy to see that none of any 8 queens is attacked by any another queen. The solution
The local search is a helpful way for some optimization problems and consistency.
The local search techniques are unfinished satisfiability algorithms which can get a
solution of a problem.
32
Figure 14: One of the possible solutions of 8-queens problem
The idea of a local search is looking into the range of full tasks of values to variables,
33
4.6 Constraint propagation
problem (CSP). The main idea of using constraint propagation is to find a feasible
area of a point meaning that all the variables must satisfy the constraints. In another
way, they are methods that impose a form of consistency which is a requirement
propagation has different uses. First, it becomes a problem that is similar but easier
happen in general. Every time it happens for some certain kinds of problems and for
It is similar to backtracking, but also deals with the future variable. Forward
On the other hand, a forward checking it totally different from backtracking, and it is
easier than backtracking. Because forward checking is checking the same nodes as
backtracking, but a loss of time occurs to assign next nodes that might has never
34
been used. If there's no either useful value for present variable, back-track occurs to
the past variable to define another value. A useful value for the present variable is a
Nevertheless, whole arc consistency graph traverse is not needed to execute forward
checking. Finally, the formation of the constraint graph can be utilized to facilitate
Unfortunately, forward checking does not provide the premature disclosure for all
the mistakes. In particular, it does not reveal fail between any two unassigned
variables.
Sudoku is a puzzle which is based on logic, and the numbers are placed in a
particular order.
A Sudoku is a matrix of 9×9 grid, divided into nine 3×3 sub-grids, and each sub-grid
must contain the digits from 1 to 9, and also no column, row, and block must contain
repeating digits from 1 to 9 (it must be distinct in every vertical line, horizontal line
and 3×3 square). Firstly, some cells of a matrix 9×9 are filled with some digits, and
the target is to fill the empty cells. It is required to use all possible outcomes of
35
Variable: Y11, Y22, . . . . . . , Y99.
Constraints:
1. Mini Sudoku;
2. Alphabetical Sudoku;
3. Akshara Sudoku;
4. Hypersudoku.
An example of Sudoku problem as a CSP is given below: some cells are filled with
One of 3×3 grids is filled with red numbers, and this grid contains all the numbers
from 1 to 9 with no repetition of any number in each grid, column and row (Figure
16).
36
Figure 15: The first state in Sudoku game filled with digits from 1 to 9
Figure 16: The second state in Sudoku game without repetition of digits
in the first grid
37
Figure 17: Complete solution to Sudoku game
38
Chapter 5
CONCLUSION
In this thesis, some strategies for solving constraint satisfaction problems are
described. These problems have several possible solutions, and each problem has a
a subset of the set of variables. In other words, the constraint satisfaction problem
consists of a set of variables, a set of the values domain, and a set of constraints. In
addition, the CSP can be viewed as a search problem with the initial state, successor
A unary constraint, binary constraint, higher-order constraint and soft constraint are
types of constraint satisfaction problem. CSPs are used in many fields such as graph-
others. There are some search techniques such as backtracking search, local search,
and constraint propagation for solving CSP. The backtracking search is used to solve
39
REFERENCES
Press LLC.
[4] http://en.wikipedia.org/wiki/Constraint_satisfaction_problem.
[5] Ettaouil, M., Loqman, C., Haddouch, K., & Hami, Y. (2013). Maximal Constraint
[6] Bessiere, C., Maestre, A., Brito, I., & Meseguer, P. (2005). Asynchronous
Backtracking without Adding Links: A new Member in the ABT Family. Artificial
[7] Yokoo, M., Durfee, E., Ishida, T., & Kuwabara, K. (1998). Distributed constraint
40
[8] Mamoulis, N., & Stergiou, K. (2004). Algorithms for Quantified Constraint
[10] Susan L., & Xingjian Li. (2009). Cluster Graphs as Abstractions for Constraint
[11] Klin, B., Lasota, S., Ochremiak, J., & Torunczyk, S. (2014). Turing Machines
Computer Science Logic (CSL) and the Twenty-Ninth Annual ACM/IEEE Symposium
[12] Hirayama, K., & Yokoo, M. (1997). Distributed Partial Constraint Satisfaction
[13] Debruyne, R., & Bessiere, C. (1997). Some Practicable Filtering Techniques for
41
[14] Roberto J. B., & Daniel P. M. (1995). On the Space-Time Trade-off in Solving
[15] Mohamed, A., Yusoff, M., Mohtar, I., A., Mutalib, S., & Rahman, S., A. (2008).
[16] El Mouelhi, A., Jegou, P., Terrioux, C., & Zanuttini, B. (2012). On the
[18] Larose, B., Loten, C., & Tardif, C. (2007). A characterisation of First-Order
[19] Jose E., Cotta, C., & Antonio J. (2009). Solving Weighted Constraint
42
[20] Bessière, C. (1991). Arc-Consistency in Dynamic Constraint Satisfaction
[21] Verfaillie, G., & Schiex, T. (1994). Solution Reuse in Dynamic Constraint
[22] Dantsin, E., & Wolpert, A. (2002). Solving Constraint Satisfaction Problems
[23] Prosse, P. (1993). Hybrid Algorithms for the Constraint Satisfaction Problem.
43