59
Set theory in constraint programming Robert ˚ Akemalm π 03 [email protected] August 27, 2009

Set theory in constraint programming

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Set theory in constraint programming

Set theory in constraint programming

Robert Akemalm π − [email protected]

August 27, 2009

Page 2: Set theory in constraint programming
Page 3: Set theory in constraint programming

Acknowledgement

I would not have been able to do this work as good as I did without the helpfrom other people and it is only right that they get the thanks they shouldhave.

First of all Krzysztof Kuchcinski for introducing me to constraint program-ming and for always taking time to discuss and explain the pros and cons fordifferent kind of implementations.

Louise Johansson for discussing and proofreading the report and for alwayssupporting me and finally Jonas Eriksson and Linus Hjertqvist for commentsand proofreading.

Nedless to say, the responsability for any errors or imperfections in this re-port is entirely mine and I would be grateful if comments and findings oferrors was e-mailed to me.

Page 4: Set theory in constraint programming
Page 5: Set theory in constraint programming

Preface

I still remember my first contact with constraint programming. I was amazedby how different it was from the programming I was used to. Instead of bru-tally solving the problem one should describe the problem in a mathematicalway, set the boundaries for an expected solution and then the computer wasable to find the solutions to the problem. It was so easy to change the modelor the boundary if one wanted a different kind of solution.

The combination of mathematics and computer science was very interestingbut I was a bit annoyed over how unnecessary complicated some models hadto be.

When Krzysztof Kuchcinski told me about how other constraint program-ming solvers supported the usage of sets in the models I felt that imple-menting that would be a great way to understand more about constraintprogramming and making it possible to create less complicated models forsome problems.

This report is appropriate for people on a tertiary level with a mathemati-cal interest. It explains how one could implement set theory in a constraintprogramming language. It is advantageous, but not necessary, for the readerto have some basic knowledge in set theory. The reader will have easier tounderstand the theory if it is used to reading mathematical formulas and isfamiliar to most of the symbols in the Abbreviations and symbols section.

Page 6: Set theory in constraint programming
Page 7: Set theory in constraint programming

Abstract

Some constraint satisfaction problems are unnecessary difficult to model andsolve with finite domain variables in a constraint programming language likeJava Constraint Programming solver(JaCoP). The usage of set domain vari-ables could help to simplify these models and sometimes even make the solv-ing faster and more efficient.

The most important part to make our implementation of sets for constraintprograming efficient is the usage of set intervals to represent sets, with setintervals it is possible to make operations fast and the memory usage low. Itis also important to have optimized and usefull constraints if one would liketo solve harder problems efficiently.

In this report we present the theory behind our implementation of set inter-vals and eleven constraints that is usefull for set based problems are statedand explained. Our implementation of sets with set intervals and the elevenconstraints can be found on the official JaCoP homepage [5].

With set intervals and constraints implemented it is possible to solve a widefield of problems in a fast and easy-to-read way. A lot of problems can bemodeled in a shorter way and our evaluation shows that the solving of harderproblems like the social golfer problem could be greatly improved with sets.

The combination of set domain variables and finite domain variables shouldmake problems easier to model and solve.

Page 8: Set theory in constraint programming
Page 9: Set theory in constraint programming

Abbreviations and symbols

Abbreviations

JaCoP Java Constraint Programming solverCSP Constraint Satisfaction Problemglb Greatest Lower Boundlub Least Upper BoundDFS Depth First SearchCP Constraint ProgrammingSDV Set Domain VariableFDV Finite Domain Variable

Symbols

= Equality6= Not equality∈ Belongs to/∈ Not belongs to{; } Classifier∅ Empty setU Universal set∪ Union∩ Intersection\ Set differencec Complementx Convex closure of x⊆ Subset of or equal to⊂ Subset of⇔ Equivalence⇒ Implies<> Disjoint∼ Equivalence relation# Cardinality

Page 10: Set theory in constraint programming
Page 11: Set theory in constraint programming

Contents

1 Introduction and Aims of the Study 1

2 Theoretical Background - Set theory 3

3 Basics of Constraint Programming 5

4 Theory 104.1 Set intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104.2 Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

4.2.1 Primitive constraints . . . . . . . . . . . . . . . . . . . 154.2.2 Non-primitive constraints . . . . . . . . . . . . . . . . 18

4.3 Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244.4 Implementation in JaCoP . . . . . . . . . . . . . . . . . . . . 26

5 Evaluation 295.1 Steiner problem . . . . . . . . . . . . . . . . . . . . . . . . . . 29

5.1.1 Problem statement . . . . . . . . . . . . . . . . . . . . 295.1.2 Differences in solutions . . . . . . . . . . . . . . . . . . 295.1.3 Comparsion between domains . . . . . . . . . . . . . . 30

5.2 Social golfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315.2.1 Problem statement . . . . . . . . . . . . . . . . . . . . 315.2.2 Differences in solutions . . . . . . . . . . . . . . . . . . 315.2.3 Comparsion between domains . . . . . . . . . . . . . . 33

5.3 N-knight’s tour problem . . . . . . . . . . . . . . . . . . . . . 345.3.1 Problem statement . . . . . . . . . . . . . . . . . . . . 345.3.2 Differences in solutions . . . . . . . . . . . . . . . . . . 345.3.3 Comparsion between domains . . . . . . . . . . . . . . 34

6 Future Work 35

7 Conclusions 36

References 37

Page 12: Set theory in constraint programming

A Sudoku solver I

B Steiner problem IV

B.1 Solution in set domain . . . . . . . . . . . . . . . . . . . . . . IV

B.2 Solution in finite domain . . . . . . . . . . . . . . . . . . . . . V

C Social golfer problem VI

C.1 Solution in set domain . . . . . . . . . . . . . . . . . . . . . . VI

C.2 Solution in finite domain . . . . . . . . . . . . . . . . . . . . . VII

D N-knigth’s tour VIII

D.1 Possible moves . . . . . . . . . . . . . . . . . . . . . . . . . . VIII

D.2 Solution in set domain . . . . . . . . . . . . . . . . . . . . . . IX

D.3 Solution in finite domain . . . . . . . . . . . . . . . . . . . . . X

Page 13: Set theory in constraint programming

1 INTRODUCTION AND AIMS OF THE STUDY

1 Introduction and Aims of the Study

The main contribution of this work is a new package to Java Constraint Pro-gramming solver (JaCoP) allowing set based Constraint Satisfaction Prob-lems (CSPs) to be modeled and solved.

To understand the theory behind the implementation one must have somebasic knowledge in set theory and constraint programming. Chapter 2 willgive the reader the basics of set theory with definitions and examples on thebasic symbols and operations.

In Chapter 3 we will explain the basics of Constraint Programming (CP).In CP variables has a domain of possible values and constraints are used toprune this domains in order to get a solution. CP uses constraints in orderto model problems and search for solutions to the problems. The basics ofCP is mainly explained by analyzing the popular Sudoku puzzle.

The Set and SetDomain will be introduced as extensions of the IntervalDo-main and BoundDomain1 in JaCoP. A Set is a collection of distinct arbitraryintegers. If the Set does not contain any values it is said to be empty.

This work does not handle Sets of depth greater than 1, i.e. {{vi}} is not acorrect Set. A SetDomain is defined as a set interval specified with a greatestlower bound (glb) and a least upper bound (lub).

A Set is represented as {x1, . . . , xn}, with the special representation {} foran empty Set.A SetDomain is represented as {{xi, . . . , xn} . . .{x1, . . . , xm}} or {glb . . . lub}where glb and lub are Sets. A SetDomain is called a singelton if it only con-tain one element i.e. glb = lub.

The theory behind the implementation of set constraints is presented inChapter 4 starting with a discussion on set intervals followed by the defini-tions of set constraints and how they can be implemented to prune domainsin an efficient way. The constraints for set theory implemented in JaCoP aretheoretically explained and the pruning is showed with some basic examples.

The chapter is then ended by a discussion on how to use DFS to search forsolutions in set domains followed by an explanation on how set constraintswas implemented as a part of JaCoP.

1Intervaldomain and BoundDomain are the domains that finite domain variables usesin JaCoP.

1

Page 14: Set theory in constraint programming

1 INTRODUCTION AND AIMS OF THE STUDY

The Set and SetDomain are then used to solve CSPs. A CSP is described bya collection of variables ranging over a set of possible values (SetDomains)and a collection of constraints applied to the variables.

Solving of CSPs over SetDomains uses different kinds of consistency or prop-agation techniques to prune the search space. The search methods used forfinding solutions to the CSPs are the standard search methods implementedin JaCoP like the Depth First Search (DFS) or the credit search.

Selected evaluations of the implementation are presented in Chapter 5 bysolving some standard problems in set theory. The results from these testswill be used to determine the efficiency of our implementation compared tosolutions in a finite domain.

Some thoughts and ideas about future work is presented in Chapter 6 andthe report is ended in Chapter 7 where the conclusions of the work arediscussed.

2

Page 15: Set theory in constraint programming

2 THEORETICAL BACKGROUND - SET THEORY

2 Theoretical Background - Set theory

Since this work implements set theory in a programming language we willneed to know the basics of set theory.

To explain the basics of set theory we will start with some basic symbolsthat the reader should already be familiar to:

= Equality∈ Belongs to{; } Classifier

Clarification:

a = b means that a and b are equal or different names forthe same object. If a and b are not equal then a 6= b.

a ∈ A means that a is an element of the set A.If a is not an element of A we write a /∈ A.

A = {x ; x > 0} means that A is a set that consists of all elements thatare greater than 0.

From this we can conclude that a set is completely determined from it’selements. Thus if two sets are equal they will have to consist of the sameelements.

The classifier is used to define sets, for example the empty set is defined as∅ = {x ; x 6= x}.

We will for short use {a, b, c, . . . } instead of the long form{x ; x = a or x = b or x = c or . . . }. In the same way we will use {} insteadof {x ; x 6= x} for the empty set.

In set theory, there are three basic binary operations:

The Union of two sets A and B, denoted A ∪ B or {x ; x ∈ A or x ∈ B},is a set whose elements are also elements of at least one of A or B.Example:A = {1, 2}, B = {3, 4} ⇒ A ∪ B = {1, 2, 3, 4}.

The Intersection of two sets A and B, denoted A ∩ B or {x ; x ∈ A andx ∈ B}, is a set whose elements are also elements of both A and B.Example:A = {1, 2, 3}, B = {3, 4, 5} ⇒ A ∩ B = {3}.

3

Page 16: Set theory in constraint programming

2 THEORETICAL BACKGROUND - SET THEORY

Figure 1: A and B Figure 2: A ∪ B Figure 3: A ∩ B

The Set difference of two sets A and B, denoted A\B or{x ; x ∈ A and x /∈ B}, is a set whose elements are also elements of A butare not elements of B.Example:A = {1, 2, 3, 4}, B = {2, 3} ⇒ A\B = {1, 4}.

This operation is also used to define the complement of a set:Let U be the the set that that consists of all elements, the universal set, thenthe Complement of a set A, denoted Ac, is defined as Ac = U\A.

Figure 4: A\B Figure 5: A and Ac

With this definition of the complement we can rewrite the definition of setdifference as A\B = A ∩ Bc.

4

Page 17: Set theory in constraint programming

3 BASICS OF CONSTRAINT PROGRAMMING

3 Basics of Constraint Programming

CP uses methods from a wide range of different fields such as mathematics,Artificial Intelligence, geometry and operations research. CP is used to solveCSPs. A CSP is a problem of finding values for variables that satisfy a givenset of constraints.

In CP a variable has a domain of possible values.

Example:X = 1..3 means that one of the following statements is true: X = 1, X = 2or X = 3. X is said to have the domain 1, 2 and 3 or shortly 1..3.

To solve a problem a model is built by setting domains for variables andimposing constraints over the variables.

Example:A constraint could be XeqC(X, 2) which imposes that the variable X mustbe equal to 2 (X = 2).

There has to be a way to check if the constraints are satisfied so all con-straints needs to implement a satisfied -function. This can be used in orderto remove constraints that are allready satisfied and speed up the search.

Example:A constraint XeqC(X, 2) is only satisfied if the domain of X is 2, but it isnot satisfied if the domain is 1..3.

JaCoP uses two different kinds of constraints, Constraints and Primitive con-straints. The difference is that a primitive constraint also needs to have acorresponding negated constraint. The primitive constraints needs to imple-ment both a satisfied- and a notSatisfied-function and can therefor be usedas arguments to other constraints such as reified, And, Or. The primitiveconstraints can also be used during search in order to make decisions in thesearch tree, the two search trees below uses the primitive constraint XeqC insuch a way that for the left search tree X = C is imposed and for the rightsearch tree the negated constraint X 6= C is imposed. Both the constraintsand primitive constraints are used when a problem is modelled.

A solution to the problem is found by a search method, the search tries pos-sible assignments of values to the variables and a solution is found when allvariables has been assigned a value and non of the imposed constraints areviolated.

5

Page 18: Set theory in constraint programming

3 BASICS OF CONSTRAINT PROGRAMMING

A CSP example - Sudoku2

Sudoku is a number placement puzzle usually of size 9× 9 divided into nine3 × 3 boxes.

Each element in the puzzle shall have a value between 1 and 9. Math-ematically we will say that the value of the element lies in the domain{1, 2, 3, 4, 5, 6, 7, 8, 9} or shortly the element has the (value)domain {1..9}.

A solution must comply with three simple rules:

1. Each row shall contain all the digits from 1 to 9 exactly one time.

2. Each column shall contain all the digits from 1 to 9 exactly one time.

3. Each box shall contain all the digits from 1 to 9 exactly one time.

Each of these rules can be translated to a constraint.

Figure 6: Sudoku puzzle Figure 7: Sudoku puzzle solution

A puzzle has a partially completed grid from the beginning, each place thathas a value imposes a new constraint. In the puzzle above we have that asolution must have the value 5 for the element (a, 1).

When all constraints and domains are set for a problem the constraint pro-gram uses consistency to prune the domains of the elements according to theconsistency rules of each constraint.

2The puzzle and the solution is taken from [7]

6

Page 19: Set theory in constraint programming

3 BASICS OF CONSTRAINT PROGRAMMING

For the Sudoku puzzle it is sufficient to use only two types of constraints,Alldifferent and XeqC. Alldifferent prunes the domain of the supplied vari-ables so that all the supplied variables get unique values and XeqC prunesthe domain of variable X so that it is equal to the constant C (X=C).3

Alldifferent takes an array of variables and when consistency is imposed itremoves the value, assigned to a variable, from the domains of the othervariables in the array.

XeqC takes a variable X and a integer C and when consistency is imposed itsimply removes every value except C from the domain of X.

To solve our Sudoku puzzle it is enough to impose consistency on Alldifferentand XeqC.

In pseudocode the problem can be solved like this:4

//Define the domain for all variables

Variable[][] all = new Variable[9][9]

for(int i = 0 ; i < 9 ; i++)

for(int j = 0 ; j < 9 ; i++)

all[i][j] = new Variable(new domain(1,9))

//Define Alldifferent for each row

for(int i = 0 ; i < 9 ; i++)

Alldifferent(all[i])

//Define Alldifferent for each column

.

.

//Define Alldifferent for each box

.

.

impose_consistency()

If consistency is not enough to solve the problem the program will begin tosearch for a solution. The search can be made in different ways depending onthe problem, usually we will use DFS but if one for example is interested infinding the minimum cost in a problem then a credit search should be usedinstead.

3The reader can find out more about different kind of constraints in [4]4A complete program to solve the Sudoku puzzle can be found in Appendix A.

7

Page 20: Set theory in constraint programming

3 BASICS OF CONSTRAINT PROGRAMMING

For the Sudoku puzzle a DFS will work fine. At each node the DFS makesa decision (imposes a new constraint) that an element ei is equal to a valuevj in its domain. If this choice fails, i.e. some constraint is inconsistent, thesearch backtracks and vj is removed from the domain (the constraint ei 6= vj

is imposed).

The efficiency of the search can be tweaked by applying different rules onhow the variables and the values from the domain are to be chosen. In thefollowing search tree the variables are chosen in numerical order and thevalues from the domains are chosen as the minimum value.

e1 = 1..4

e2 = 1..3

...

e1=1 e1 6=1

e1 = 1

e2 = 1..3

...

e2=1 e2 6=1

e1 = 2..4

e2 = 1..3

...

e1=2 e1 6=2

e1 = 1

e2 = 1

...

e1 = 1

e2 = 2..3

...

e1 = 2

e2 = 1..3

...

e1 = 3..4

e2 = 1..3

...

It is important to analyze the problem and if possible experiment on differentsearch alternatives in order to find the search method that is best suited forthe problem. For our sudoku puzzle another, perhaps more efficient, way tochoose variables could be to choose the variable with smallest domain andto choose the value from the domain randomly:

8

Page 21: Set theory in constraint programming

3 BASICS OF CONSTRAINT PROGRAMMING

e1 = 1..4

e2 = 1..3

...

e2=2 e2 6=2

e1 = 1..4

e2 = 2

...

e1=3 e1 6=3

e1 = 1..4

e2 = 1, 3

...

e2=1 e2 6=1

e1 = 3

e2 = 2

...

e1 = 1..2, 4

e2 = 2

...

e1 = 1..4

e2 = 1

...

e1 = 1..4

e2 = 3

...

The key to performing an efficient search is to choose variables and valuesin an efficient way. We can save a lot of time if a wrong decision makes thesearch fail as early as possible, then we will not have to continue the searchin the subtrees.

In our example with the sudoku puzzle it seems logical to always choosethe variable with the smallest domain since consistency of the alldifferentconstraint implemented in JaCoP prunes the domains when a variable be-comes a singleton. The reader should note that this is true for the alldifferentconstraint in JaCoP, but other solvers might have other ways to prune thedomain for the alldifferent constraint and JaCoP does in fact not prune likethis for the alldistinct constraint that is similar to alldifferent. The sudokupuzzle might not be the best example when analyzing search since searchis only required for the most difficult puzzles, for most of the puzzles it isenough to impose consistency.

9

Page 22: Set theory in constraint programming

4 THEORY

4 Theory

4.1 Set intervals

In the implementation we will use set intervals proposed by the author of [3]to describe the SetDomains. If we have a collection of arbitrary sets, thenthe smallest convex set that contains the collection of sets is a set interval.

The smallest convex set is determined by ”The convex closure”-operation:

If x = {A1, . . . , An} the convex closure of x is defined asx = [∩Ai∈xAi,∪Ai∈xAi]

5

The SetDomain for x is written as {glb(x)..lub(x)} where glb(x) is ∩Ai∈xAi

and lub(x) is ∪Ai∈xAi.

ExampleFor the collection of sets [{3}, {3, 5}, {1, 3, 6}], the convex closure is the setinterval [{3}..{1, 3, 5, 6}]. For this collection of sets we will write the SetDo-main as {{3}..{1, 3, 5, 6}} and we say that {3} is the glb and that {1, 3, 5, 6}is the lub.

The glb contains all elements that are definite elements of the set and the lubcontains all possible (and definite) elements of the set.

Example:Let the SetDomain SD be {{1, 3}..{1, 3, 7..9}}, then for a set s ∈ SD wehave that 1 and 3 are definite elements of s and 7, 8, 9 are possible elementsof s.

5For set intervals Ai will be a set

10

Page 23: Set theory in constraint programming

4 THEORY 4.1 Set intervals

Why use set intervals?

The reason that we use set intervals is that, with set intervals we do not haveto handles ”holes” in the domain or create large set domains.Example:

[{1}, {1, 4}]∪ [{2}, {2, 3}, {2, 5}]=

[{1, 2}, {1, 2, 3}, {1, 2, 4}, {1, 2, 5}, {1, 2, 3, 4}, {1, 2, 4, 5}]

We can not write this in a short form so we will need to store this domainin a larger array. One easily understands that with a lot of SetDomains theamount of memory needed will quickly be large and the time needed to copythe elements between arrays will be longer, especially if there is a lot of unionoperations.

The advantage of set intervals is that we only need two sets to describe aSetDomain, with set intervals the last example can be written as:

{{1}..{1, 4}} ∪ {{2}..{2, 3, 5}] = {{1, 2}..{1, 2, 3, 4, 5}}.

That is, we will expand the domain of possible solutions with {1, 2, 3, 5} and{1, 2, 3, 4, 5}.

This might seem as a great disadvantage since we will have to test sets thatwe know can not be a part of a solution, but the gain of time and memoryin the commonly used operations ∪,∩, \ is far greater than the extra timeneeded to search for solutions.

Example:Without set intervals the creation of a new SetDomain as s = s1 ∪ s2 wouldrequire us to create a new array of sets, copy all elements from s1 and s2 intoit and order the array. With set intervals we will only have to set the glb andlub for s according to some simple rules.

11

Page 24: Set theory in constraint programming

4.1 Set intervals 4 THEORY

Is a set interval solution valid?

The convex closure operation has the following properties:P1. x ⊆ xP2. If x ⊆ y, then x ⊆ y

From these properties we see that P1 gives us that any element of x belongsto x (thus to glb(x)) and any element not in x (thus not in lub(x)) does notbelong to x. This gives us that any solution for x is also a solution for x.

Property P2 guarantees that the partial order is preserved in the convexclosure.

Set interval calculus

In order to perform calculus on set intervals and still get results that are setintervals we will have to change the way we perform the standard operations∪,∩, \.

For the ∪ and ∩ operations this is easily achieved with the convex closureoperation:

With the sets a, b, c, d and the set intervals [a, b], [c, d] we have that[a, b] ∪ [c, d] = [a ∪ c, b ∪ d] and [a, b] ∩ [c, d] = [a ∩ c, b ∩ d].Note: [a, b] ∪ [c, d] ⊆ [a ∪ c, b ∪ d] and [a, b] ∩ [c, d] ⊆ [a ∩ c, b ∩ d].

The \ operation [a, b]\[c, d] is a bit more tricky:We remember that a theoretical definition of the set difference is x\y =x ∩ yc where yc is the complement of y. [a, b]\[c, d] can then be writtenas [a, b]\[c, d] = [a, b] ∩ [c, d]c. The complement of the set interval [c, d] is[U\d, U\c] where U is the universal set mentioned in chapter 2.

Now [a, b]\[c, d] can be written as [a, b]\[c, d] = [a, b]∩ [U\d, U\c], but to geta result that is a set interval we will have to use the convex closure operation:

[a, b]\[c, d] ⊆ [a, b]\[c, d] = [a, b] ∩ [U\d, U\c] = [a ∩ (U\d), b ∩ (U\c)]= [(a ∩ U)\d, (b ∩ U)\c] = [a\d, b\c]

We will also need some consistency properties:With the sets a, b, c, d and the SetDomains {a, b}, {c, d} we say that thedomain {a, b} is consistent if a ⊆ b and we say that the calculus domainis consistent if [a, b] ⊆ [c, d] ⇔ a ⊆ c, b ⊆ d.

12

Page 25: Set theory in constraint programming

4 THEORY 4.1 Set intervals

Graduated functions

Problem:x = {{1}..{1..3}}, y = {1..2}Find the best solution for x that satisfies the constraint x ⊆ y.By solving the problem we get the solutions x = {1} and x = {1, 2},which solution is the best?

In order to search for an optimal solution we will need a way to comparedifferent solutions. One way to compare solutions is to use graduated func-tions. In our implementation we will use two graduated functions, presentedas constraints, namely the cardinality and the weighted sum.

A graduated function f : U → N is a function that maps an elementx ∈ U to a positive integer m ∈ N such that f(x) = m ands1 ⊂ s2 ⇒ f(s1) < f(s2).

For a set interval we will have to use the convex closure of the graduatedfunction:

The convex closure for a graduated function is, for set intervals, definedas:6

f([a, b]) = [f(a), f(b)]Note: If s ∈ [a, b] then f(s) ∈ f([a, b])

Example on convex closure for graduated functions (Cardinality)The cardinality of a set s, denoted #s, is simply the number of elementscontained in the set s.

Example:#{1, 3, 5} = 3 #{} = 0

The cardinality of a SetDomain SD, denoted #SD, is the bound domain[#glb(SD), #lub(SD)].

Example:#{{}..{1, 3, 5}} = [#{}, #{1, 3, 5}] = [0, 3]

6The cardinality

13

Page 26: Set theory in constraint programming

4.2 Constraints 4 THEORY

4.2 Constraints

In order to find a solution to a set problem we will need to prune the domainof each variable, this is done with different kind of constraints.

We have implemented the following constraints for set constraints in JaCoP:

• Element in Set - c1 ∈ S1.

• Equality - S1 = S2.

• Subset - S1 ⊆ S2.

• Union - S1 ∪ S2 = S3.

• Intersection - S1 ∩ S2 = S3.

• Set difference - S1\S2 = S3.

• Disjoint - S1 <> S2.

• Match - S1 ∼ X.

• Cardinality - #S1 = X.

• WeightedSum -∑

< S, W > = X.

• ElementSet - Set[X] = Y .

All of these constraints works in different ways in order to prune the domainsof Set Domain Variables (SDVs). The pruning is made by restricting thedomains for the SDVs in the constraint according to some specific rules for theconstraint. After applying the rules the SDVs will get new smaller domains.

The first three constraints are called primitive constraints meaning that theyhave corresponding negated constraints c1 6∈ S1, S1 6= S2 and S1 6⊆ S2. It ispossible to implement all of the constraints above as primitive constraints,but it is hard or even impossible to implement an efficient pruning on the non-primitive constraints and the usefullness of the negated constraints are low.Most of the negated constraints can instead be imposed as two constraints,i.e. S1 ∪ S2 6= S3 ⇔ S1 ∪ S2 = S4, S4 6= S3.

On the following pages we will take some time to discuss how to performpruning on the domains according to the constraints. Each constraint willbe discussed and some rules on how to do pruning is presented. There aretwo examples, to each constraint, that shows how the pruning is made. The

14

Page 27: Set theory in constraint programming

4 THEORY 4.2 Constraints

first example is consistent and the second example is inconsistent and resultsin an fail7. A setDomain is inconsistent if glb 6⊆ lub.

4.2.1 Primitive constraints

Element in SetThe element in set constraint, e1 ∈ S1, restricts the domains for the SDV S1.If the domain for the SDV is

S1.dom = d1 = {d1.glb .. d1.lub}

then we restrict the domain according to the following rules.

r18

d′

1.glb = d1.glb ∪ e1

d′

1.lub = d1.lub

Examples:S1.dom = {{1}..{1..3}} 2 ∈ S1 ⇒ S1.dom = {{1..2}..{1..3}}

S1.dom = {{1}..{1..3}} 4 ∈ S1 ⇒ S1.dom = {{1, 4}..{1..3}}

The negated constraint is e1 6∈ S1 and the restriction rules are

r2

d′

1.glb = d1.glbd

1.lub = d1.lub\e1

Examples:S1.dom = {{1}..{1..3}} 2 6∈ S1 ⇒ S1.dom = {{1}..{1, 3}}

S1.dom = {{1}..{1..3}} 1 6∈ S1 ⇒ S1.dom = {{1}..{2..3}}

7More about how a fail is handled can be read in the search section (4.3)8These rules are used in our implementation and the index of the rule is mentioned asa comment in the code where the rule is used.

15

Page 28: Set theory in constraint programming

4.2 Constraints 4 THEORY

EqualityThe equality constraint, S1 = S2, restricts the domains for the SDVs S1 andS2. If the domains for the SDVs are

S1.dom = d1 = {d1.glb .. d1.lub}S2.dom = d2 = {d2.glb .. d2.lub}

then we can restrict the domains according to the following rules.Note that the same rule can be applied for d2.

r3

d′

1.glb = d1.glb ∪ d2.glbd

1.lub = d1.lub ∩ d2.lub

Examples:S1.dom = {{1}..{1..3}} S1 = S2 ⇒ S1.dom = {{1..2}..{1..3}}S2.dom = {{2}..{0..4}} S2.dom = {{1..2}..{1..3}}

S1.dom = {{1}..{1..3}} S1 = S2 ⇒ S1.dom = {{1, 4}..{1..3}}S2.dom = {{4}..{0..4}} S2.dom = {{1, 4}..{1..3}}

The negated constraint is S1 6= S2 and the restriction rules are:

r4

If S1 and S2 are singletons a fail will be thrown if S1 = S2

Examples:S1.dom = {{1}..{1..3}} S1 6= S2 ⇒ S1.dom = {{1}..{1..3}}S2.dom = {{2}..{0..4}} S2.dom = {{2}..{0..4}}

S1.dom = {1..3} S1 6= S2 ⇒ S1.dom = {1..3}S2.dom = {1..3} S2.dom = {1..3}

16

Page 29: Set theory in constraint programming

4 THEORY 4.2 Constraints

SubsetThe subset constraint, S1 ⊆ S2, restricts the domains for the SDVs S1 andS2. If the domains for the SDVs are

S1.dom = d1 = {d1.glb .. d1.lub}S2.dom = d2 = {d2.glb .. d2.lub}

then we can restrict the domains according to the following rules.

r5

d′

1.glb = d1.glbd

1.lub = d1.lub ∩ d2.lub

r6

d′

2.glb = d2.glb ∪ d1.glbd

2.lub = d2.lub

Examples:S1.dom = {{1}..{1..3}} S1 ⊆ S2 ⇒ S1.dom = {{1}..{1..2}}S2.dom = {{2}..{0..2}} S2.dom = {{1..2}..{0..2}}

S1.dom = {{3}..{1..3}} S1 ⊆ S2 ⇒ S1.dom = {{3}..{1..2}}S2.dom = {{2}..{0..2}} S2.dom = {{2..3}..{0..2}}

The negated constraint is S1 6⊆ S2 and the restriction rules are:

r7

If S1.lub ⊆ S2.glb a fail is thrown.

r8

The constraint is satisfied if S1.glb 6⊆ S2.lub.

Examples:S1.dom = {{3}..{1..3}} S1 6⊆ S2 ⇒ S1.dom = {{3}..{1..3}}S2.dom = {{2}..{0..2}} S2.dom = {{2}..{0..2}}

S1.dom = {{3}..{1..3}} S1 6⊆ S2 ⇒ S1.dom = {{3}..{1..3}}S2.dom = {{0..4}..{0..6}} S2.dom = {{0..4}..{0..6}}

17

Page 30: Set theory in constraint programming

4.2 Constraints 4 THEORY

4.2.2 Non-primitive constraints

UnionThe union constraint, S1 ∪ S2 = S3, restricts the domains for the SDVs S1,S2 and S3. If the domains for the SDVs are

Si.dom = di = {di.glb .. di.lub}, i = 1, 2, 3

then we can restrict the domains according to the following rules. Note thatthere exist a rule for d2 that is similar to the one for d1.

r9

d′

1.glb = d1.glb ∪ (d3.glb\d2.lub)d

1.lub = d1.lub ∩ d3.lub

r10

d′

3.glb = d3.glb ∪ d1.glb ∪ d2.glbd

3.lub = d3.lub ∩ (d1.lub ∪ d2.lub)

Examples:S1.dom = {{1}..{1..3}} S1 ∪ S2 = S3 ⇒ S1.dom = {{1}..{1..3}}S2.dom = {{3}..{3..4}} S2.dom = {3..4}S3.dom = {{4}..{1..5}} S3.dom = {{1, 3..4}..{1..4}}

S1.dom = {{1}..{1..3}} S1 ∪ S2 = S3 ⇒ S1.dom = {{1}..{2..3}}S2.dom = {{3}..{3..4}} S2.dom = {3..4}S3.dom = {{4}..{2..5}} S3.dom = {{1, 3..4}..{2..4}}

IntersectionThe intersection constraint, S1∩S2 = S3, restricts the domains for the SDVsS1, S2 and S3. If the domains for the SDVs are

Si.dom = di = {di.glb .. di.lub}, i = 1, 2, 3

then we can restrict the domains according to the following rules. Note thatthere exist a rule for d2 that is similar to the one for d1.

r11

d′

1.glb = d1.glb ∪ d3.glbd

1.lub = d1.lub\((d1.lub ∩ d2.lub)\d3.lub)

r12

d′

3.glb = d3.glb ∪ (d1.glb ∩ d2.glb)d

3.lub = d3.lub ∩ d1.lub ∩ d2.lub

18

Page 31: Set theory in constraint programming

4 THEORY 4.2 Constraints

Examples:S1.dom = {{1}..{1..3}} S1 ∩ S2 = S3 ⇒ S1.dom = {{1..2}..{1..3}}S2.dom = {{3}..{2..4}} S2.dom = {{2..3}..{2..4}}S3.dom = {{2}..{1..5}} S3.dom = {{2}..{2..3}}

S1.dom = {{1}..{1..3}} S1 ∩ S2 = S3 ⇒ S1.dom = {{1..2}..{1..3}}S2.dom = {{3}..{3..4}} S2.dom = {{2..3}..{3..4}}S3.dom = {{2}..{1..5}} S3.dom = {{2}..{3}}

Set differenceThe set difference constraint, S1\S2 = S3, restricts the domains for the SDVsS1, S2 and S3. If the domains for the SDVs are

Si.dom = di = {di.glb .. di.lub}, i = 1, 2, 3

then we can restrict the domains according to the following rules.

r13

d′

1.glb = d1.glb ∪ d3.glbd

1.lub = d1.lub\(d1.lub\(d3.lub ∪ d2.lub))

r14

d′

2.glb = d2.glbd

2.lub = d2.lub\d3.glb

r15

d′

3.glb = d3.glb ∪ (d1.glb\d2.lub)d

3.lub = d3.lub ∩ (d1.lub\d2.glb)

Examples:S1.dom = {{1}..{1..3}} S1\S2 = S3 ⇒ S1.dom = {{1..2}..{1..3}}S2.dom = {{3}..{2..4}} S2.dom = {{3}..{3..4}}S3.dom = {{2}..{1..5}} S3.dom = {1..2}

S1.dom = {{1}..{1..3}} S1\S2 = S3 ⇒ S1.dom = {{1, 3}..{1..3}}S2.dom = {{3}..{2..4}} S2.dom = {{3}..{2, 4}}S3.dom = {{3}..{1..5}} S3.dom = {{1, 3}..{1..2}}

19

Page 32: Set theory in constraint programming

4.2 Constraints 4 THEORY

DisjointThe disjoint constraint, S1 <> S3, restricts the domains for the SDVs S1

and S2. If the domains for the SDVs are

S1.dom = d1 = {d1.glb .. d1.lub}S2.dom = d2 = {d2.glb .. d2.lub}

then we can restrict the domains according to the following rules.

r16

d′

1.glb = d1.glbd

1.lub = d1.lub\d2.glb

r17

d′

2.glb = d2.glbd

2.lub = d2.lub\d1.glb

Examples:S1.dom = {{1}..{1..3}} S1 <> S2 ⇒ S1.dom = {{1}..{1, 3}}S2.dom = {{2}..{0..2}} S2.dom = {{2}..{0, 2}}

S1.dom = {{1}..{1..3}} S1 <> S2 ⇒ S1.dom = {{1}..{3}}S2.dom = {{1..2}..{0..2}} S2.dom = {{1..2}..{0, 2}}

MatchThe match constraint, S ∼ X, matches the elements of a SDV to a collectionof Finite Domain Variables (FDVs) and restricts the domains for the variablesS and X, where a FDV is a collection of integers. If the domains for thevariables are

S.dom = d = {d.glb .. d.lub}X = [a1, ..., an] where ai is a FDVai.dom = di

then we can restrict the domain according to the following rules.

r18

d′

.glb =⋃

di ∪ d.glb for all di that are singleton.d

.lub =⋃

di ∪ d.lub for all di.

r19

If #d.glb = #X then:d

= glb = v1..vn

d′

i = vi

20

Page 33: Set theory in constraint programming

4 THEORY 4.2 Constraints

r20

If #d.lub = #X then:d

= lub = v1..vn

d′

i = vi

r21

d′

i = di ∩ d.lub ∩ mini ∩ maxi wheremini = [min(di.min, di+1.min − 1), min(di.max, di+1.max − 1)]9

maxi = [max(di.min, di−1.min + 1), max(di.max, di−1.max + 1)]10

Examples:S1.dom = {{2}..{1..10}} S1 ∼ X ⇒ S1.dom = {{2..3}..{2..5}}X = [[0..2], [3], [0..5]] X = [[2], [3], [4..5]]

S1.dom = {{6}..{1..10}} S1 ∼ X ⇒ S1.dom = {{3, 6}..{1..5}}X = [[0..2], [3], [0..5]] X = [[1], [3], [ ]]

CardinalityThe cardinality constraint, #S = X, restricts the domains for the variablesS and X. If the domains for the variables are

S.dom = d = {d.glb .. d.lub}X.dom = [m, n] = m..n

then we can restrict the domain according to the following rules.

9If i = n then mini = di

10If i = 0 then maxi = di

21

Page 34: Set theory in constraint programming

4.2 Constraints 4 THEORY

r22

[m′, n′] = [max(m, #d.glb), min(n, #d.lub)]

r23

d′

.glb = d.lub if #d.lub = md

.lub = d.glb if #d.glb = n

Examples:S1.dom = {{1..2}..{1..3}} #S1 = X ⇒ S1.dom = {1..2}X = [1, 2] X = 2

S1.dom = {{1..3}..{1..4}} #S1 = X ⇒ S1.dom = {{1..3}..{1..4}}X = [1, 2] X = [3, 2]

Weighted sumThe weighted sum constraint,

< S, W > = X, applies a weight to eachelement in the set S.lub and restricts the variables S and X in such a waythat the sum of the weights for the elements in S is equal to X.If the domainsfor the variables are

S.dom = d = {d.glb .. d.lub}X = [m, n] = m..n

then we can restrict the domain according to the following rules:

r24

[m′, n′] = [max(m, glbSum), min(n, lubSum)]Where glbSum is the sum of the weights for all elements in d.glb andlubSum is the sum of the weights for all elements in d.lub.

r25

d′

.glb = d.glbd

.lub = d.lub\⋃

ei for all elements ei such that glbSum + ei.weight > n

Examples:Weights: < 1, 5 >, < 2, 7 >, < 3, 2 >, < 4, 15 >S.dom = {{1..2}..{1..4}}

< S, W > = X ⇒ S.dom = {{1..2}..{1..3}}X = [10, 20] X = [12, 20]

Weights: < 1, 5 >, < 2, 7 >, < 3, 2 >S.dom = {{1..2}..{1..3}}

< S, W > = X ⇒ S.dom = {{1..2}..{1..3}}X = [15, 20] X = [15, 14]

22

Page 35: Set theory in constraint programming

4 THEORY 4.2 Constraints

Element setThe element set constraint, Set[X] = Y , takes a FDV X, a SDV Y and anarray of Sets Z. The constraint restricts the variables X and Y in such away that the set domain variable Y is equal to set of Z on index X, i.e.Z[X] = Y . If the domains for the variables are

X.dom = [m, n]Y.dom = d = {d.glb .. d.lub}Z is a static array

then we can restrict the domain according to the following rules:

r26

[m′, n′] = {xi | Z[xi] ⊆ d.lub and d.glb ⊆ Z[xi] and xi ∈ X.dom}

r27

d′

.glb = d.lub ∩⋂

Z[i] for all i ∈ X.domd

.lub =⋃

Z[i] for all i ∈ X.dom

Examples:Z = [{1..3}, {1, 3..4}, {1, 3}]Y.dom = {{}..{1..4}} Z[X] = Y ⇒ Y.dom = {{1, 3}..{1..4}}X = 1..3 X = 1..3

Z = [{1..3}, {1, 3..4}, {1, 3}]Y.dom = {{2}..{1..4}} Z[X] = Y ⇒ Y.dom = {{1, 3}..{1, 3..4}}X = 2..3 X = ∅

23

Page 36: Set theory in constraint programming

4.3 Search 4 THEORY

4.3 Search

A search with set domains must be altered a bit from the standard searchmethod mentioned in chapter 3. The standard search method gives an vari-able a grounded value on one subtree and removes this value from the vari-ables domain in the other subtree.

It is simple to give a SDV a grounded value, but we will not be able to removethis value from the variables domain in the other subtree since we work withset intervals. If we would like to implement the search in this way, we wouldneed to keep track on which sets we could and could not use and we wouldbe back at the problem with efficiency that we discussed when we introducedset intervals in chapter 4.1.

A search like this could look something like this:

[

e1 = {{1..2}..{1..4}}

e2 = {{}..{1..2}}

]

e1={1..2} e1 6={1..2}

[

e1 = {1..2}

e2 = {{}..{1..2}}

]

[

e1 = {{1..2}..{1..4}} e1 6= {1..2}

e2 = {{}..{1..2}}

]

To get around this problem we will alter the search method. Instead ofimposing the constraint ei = vj and the negated constraint ei 6= vj for thesubtrees of a node, as we did in chapter 3, we will instead use our newconstraint vj ∈ ei and the negated constraint vj /∈ ei. The search would thenlook like this:

[

e1 = {{1..2}..{1..4}}

e2 = {{}..{1..2}}

]

3∈e1 3/∈e1

[

e1 = {{1..3}..{1..4}}

e2 = {{}..{1..2}}

]

[

e1 = {{1..2}..{1..2, 4}}

e2 = {{}..{1..2}}

]

24

Page 37: Set theory in constraint programming

4 THEORY 4.3 Search

The search normally starts with the left subtree vj ∈ ei, if the search inthis subtree fails, i.e. some constraint is inconsistence, it will backtrack andcontinue the search in the right subtree vj /∈ ei. This can however be changedin JaCoP so that the search starts with searching the right subtree vj /∈ ei

instead.

As for the standard search method, we will be interested in different ways tochoose variables and values in order to tweak the efficiency of the search.

Since the domains of FDVs and SDVs differs, we will have to come up withnew, more relevant ways to choose variables and values.

Our implementation will use the following options for choosing variables:

MinCardDiffChooses the variable that has the smallest difference in cardinality betweenlub and glb.

MaxCardDiffChooses the variable that has the greatest difference in cardinality betweenlub and glb.

MinGlbCardChooses the variable that has the glb with the smallest cardinality.

MaxGlbCardChooses the variable that has the glb with the greatest cardinality.

MinLubCardChooses the variable that has the lub with the smallest cardinality.

MaxLubCardChooses the variable that has the lub with the greatest cardinality.

The ways to choose values from the domain are the same as for the finitedomain11. The only difference is that the values are not chosen from the entiredomain. Instead we will choose the values from the ground set lub\glb. Inthis way we will get the values to use in the constraints vj ∈ ei and vj /∈ ei.

11More about value selection methods for JaCoP can be found in [4]

25

Page 38: Set theory in constraint programming

4.4 Implementation in JaCoP 4 THEORY

4.4 Implementation in JaCoP

The set package is implemented as an extension to JaCoP, it builds on ex-isting structure and uses a lot of the existing functions.

The diagram below explains how setDomain and set constraints are inte-grated in JaCoP, the diagram is supposed to give the reader an idea of howthe integration is made so the diagram is somewhat simplified, meaning thatit does not contain all the classes or all the methods.

The new classes are highligthed with gray background.

Store

+vars

+changed

+level

+removeLevel()

Var iable

+domain

+min()

+max()

+domain()

+in()

0..*

<<Abstract>>

Domain

+modelConstraints

+level: int

+min()

+max()

+domain()

+removeLevel()

1

<<Abstract>>

Constraint

+consistecy()

+satisfied()

+queueVariable()

+removeLevel()

BooleanDomain

+min()

+max()

+domain()

BoundDomain

+min: int

+max: int

+min()

+max()

+domain()

In te rva lDomain

+interval: Interval[]

+min()

+max()

+domain()

SetDomain

+glb: Set

+lub: Set

+glb()

+lub()

+domain()

Set

+interval: Interval[]

+min()

+max()

+domain()

+card()

2

modelConstraints

0..*Changed

0..*

<<Abstract>>

Primit iveConstra int

+notConsistency()

+notSatisfied()

Alldi f ferent

+List: Variable[]

GCC

+X: Variable[]

+Y: Variable[]

Union

+S1: Variable

+S2: Variable

+S3: Variable

Subset

+S1: Variable

+S2: Variable

ElementSet

+X: Variable

+Y: Variable

+Z: Set[]

Element in Set

+c1: Int

+S1: Variable

XeqY

+X: Variable

+Y: Variable

XplusYeqZ

+X: Variable

+Y: Variable

+Z: Variable

.

.

.

.

.

.

.

.

.

.

.

.. . .

1

11

1

26

Page 39: Set theory in constraint programming

4 THEORY 4.4 Implementation in JaCoP

The IntervalDomain class has pretty much all the functions that we need forour set operations, but there are some functions that we would like to havefor sets that does not have a explanation for IntervalDomain. This led us toextend the IntervalDomain to a set class with some new set functions such ascardinality and lexicographical ordering. These functions are highly usefullfor the setDomain, but does not have any interpretation for IntervalDomainsince all FDVs should be interpreted as single values.

Another disadvantage with InterValDomain is that the empty domain (emptyset) is interpreted as an error for FDVs.

card() - Returns the cardinality of the set.lex(Set s) - Returns the lexicographical

ordering between the sets.elementsGreaterThan(int i) - Returns the number of elements

that are greater than i in the set.elementsSmallerThan(int i) - Returns the number of elements

that are smaller than i in the set.

Some functions from the IntervalDomain has been altered a bit in order touse Set, as argument and/or return value, instead of IntervalDomain. Thesefunctions still works in the same way as for IntervalDomain and it is onlythe argument and/or the return value that has been changed. More abouthow these functions work can be found on the JaCoP homepage [5].

* = All instances of this function is changed.boolean isIntersecting(Set set)Set cloneLight()Set shift(int shift)boolean contains(Set set)Set complement()boolean eq(Set s)Set intersect(*) And new argument Set sSet subtract(*) And new argument Set sSet union(*) And new argument Set ssetDomain(Set s)

Set is an extension of the domain IntervalDomain, but it is not used as andomain. We are for example never interested in pruning the domain of aset or in getting a grounded value for the domain. Set is instead used todescribe the boundaries (glb and lub) for the domain SetDomain, that is animplementation of the abstract class domain in JaCoP.

27

Page 40: Set theory in constraint programming

4.4 Implementation in JaCoP 4 THEORY

The constraints are, as in the rest of JaCoP, implementations of the abstractclass Constraint or of its extension, the abstract class PrimitiveConstraint.

Constraint rulesAs we mentioned in the constraint chapter, 4.2, the domains are restrictedaccording to some simple rules. In JaCoP this is done when the consistencymethod is called for that constraint.

Example (Disjoint):public void consistency(Store store) {

SetDomain s1 = (SetDomain) S1.dom().cloneLight();

SetDomain s2 = (SetDomain) S2.dom().cloneLight();

//r16

s1.lub = s1.lub.subtract(s2.glb);

//r17

s2.lub = s2.lub.subtract(s1.glb);

store.newPropagation = false;

S1.domain.in(store.level, S1, s1);

S2.domain.in(store.level, S2, s2);

}

The rules mentioned in chapter 4.2 is easy to write in JaCoP since the In-tervalDomain already have the basic operations union, intersection and setdifference.12

Each domain in JaCoP has an accompanying class, the ValueEnumeration,that is used to list all values in the domain. The value enumerator for set,SetValueEnumeration, is basically the same as the one for IntervalDomain.The value enumeration for setDomains is a bit more complicated, it usesthe structure of Pascal’s triangle in order to enumerate the values in the do-main in an lexicographical order. The setDomain {{}..{1..3}} is for exampleordered as {}, {1}, {2}, {3}, {1..2}, {1, 3}, {2..3}, {1..3}13.

12Set difference is called subtract in IntervalDomain.13The enumeration is explained as a comment in the SetDomainValueEnumeration-class.

28

Page 41: Set theory in constraint programming

5 EVALUATION

5 Evaluation

We have determined the efficiency of our implementation by solving someproblems14. The evaluation was made with an Acer Aspire 3651WLMi laptopwith 1.46 GHz IntelR© CeleronR© processor and 1 MB L2 cache. The computeruses the Ubuntu 8.10 OS and Eclipse to solve the problems that we evaluate.The efficiency is determined by comparing a solution in the set domain to asolution in the finite domain based on the following keyvalues:

The number of searchnodesThe number of decisionsThe number of wrong decisionsThe number of backtracksThe maximal depth of the searchTime

The efficient models to the problems below are to complicated and long tobe fully presented in this report, instead more simple models will be used toexplain the basics behind solving the problems.15

5.1 Steiner problem

5.1.1 Problem statement16

A ternary Steiner system of order n is a set of T = n(n − 1)/6 triples ofdistinct elements in {1, . . . , n} such that any two triples have at most oneelement in common. The mathematical properties of this problem prove thatn modulo 6 has to be equal to 1 or 3. One solution of Steiner 7 is for example

{1, 2, 6}, {1, 3, 5}, {2, 3, 4}, {3, 6, 7}, {2, 5, 7}, {1, 4, 7}, {4, 5, 6}

5.1.2 Differences in solutions

The most obvious difference between the SDV-model and the FDV-modelis the simplicity. The SDV-model is written in 17 lines of codes while theFDV-model needs 40 lines.The FDV-model seems to be a bit faster and more efficient than the SDV-model, but the most interesting thing is that the use of lexical ordering17

seems to improve the models greatly for harder problems like the Steiner(9).The FDV- and SDV-models can be found as pseudo codes in Appendix B.

14The problems where found in [3], [8], [9]15The interrested reader can find all the solvers on http://www.solvers.akemalm.se16This statement is taken from [3]17The Lex-constraint used here is a very simple model. Since it is not optimized it is

not a part of JaCoP but it is availible at http://www.constraints.akemalm.se

29

Page 42: Set theory in constraint programming

5.1 Steiner problem 5 EVALUATION

5.1.3 Comparsion between domains

One solution

Steiner(7) SDVs FDVs SDVs + Lex FDVs + LexSearchnodes 26 26 26 16Decisions 20 20 20 15Wrong decisions 6 6 6 1Backtracks 0 0 0 0Maximal depth 20 20 20 15Time(ms) 121 246 106 322

All solutions (All:151200, Unique:30)

Steiner(7) SDVs FDVs SDVs + Lex FDVs + LexSearchnodes 2962955 2962955 50101 21197Decisions 1557077 1557077 25065 10613Wrong decisions 1405878 1405878 25036 10584Backtracks 1557077 1557077 25065 10613Maximal depth 31 34 30 22Time(ms) 500572 222713 6533 1883

One solution

Steiner(9) SDVs FDVs SDVs + Lex FDVs + LexSearchnodes 9066 9056 2062 1074Decisions 4545 4540 1043 549Wrong decisions 4521 4516 1019 525Backtracks 4501 4496 999 518Maximal depth 49 48 47 32Time(ms) 2571 1467 765 633

All unique solutions (840)

Steiner(9) SDVs + Lex FDVs + LexSearchnodes 279727487 97936413Decisions 139864163 48968626Wrong decisions 139863324 48967787Backtracks 139864163 48968626Maximal depth 64 42Time 15h 4h 10min

30

Page 43: Set theory in constraint programming

5 EVALUATION 5.2 Social golfer

5.2 Social golfer

5.2.1 Problem statement18

The coordinator of a local golf club has come to you with the following prob-lem. In her club, there are 32 social golfers, each of whom play golf oncea week, and always in groups of 4. She would like you to come up with aschedule of play for these golfers, to last as many weeks as possible, suchthat no golfer plays in the same group as any other golfer on more than oneoccasion.

The problem can easily be generalized to that of scheduling m groups of ngolfers over p weeks, such that no golfer plays in the same group as anyother golfer twice (i.e. maximum socialization is achieved).

5.2.2 Differences in solutions

The social golfer is more complicated than the Steiner problem and to get anefficient model it is very important to break as many symmetries as possible.It is however possible to solve some of the social golfer problems even witha very simple model. A more advanced model could for example use lexio-graphical ordering between groups in order to break even more symmetrices.

A first notice is that a model with plain FDVs has massive problems to solveeven the more simple instances of the social golfer problem like the 2-4-4 forexample. A model that uses numerical ordering, that sets got built-in, insideeach group is much better and more relevant to compare to the SDV-model.

Our results, presented in chapter 5.2.3, shows that the FDV-model is slightlybetter for the most simple problems like the 2-4-4, but for the somewhatmore difficult problem like the 2-5-4 problem it is quite obvious that theSDV-model is superior. For even more difficult problems like the 5-8-3, thatis solved in a few seconds with the SDV-model, the FDV-model needs morethan 6 hours to find the solution.

The greatest difference when it comes to writing the models is how to writethe condition such that no golfer plays in the same group as any other golferon more than one occasion in an efficient way. For the SDV-model this isquite simple:

18This statement is taken from [8]

31

Page 44: Set theory in constraint programming

5.2 Social golfer 5 EVALUATION

int players = golfers*groups;

for(int i = 0 ; i < groups*weeks-1 ; i++){

for(int j = i + 1 ; j < groups*weeks){

Variable inters = new variable(new SetDomain(1,players));

XintersectYeqZ(vars[i], vars[j], inters);

Card(inters,0,1);

}

}

The same condition in the FDV-model:

int players = golfers*groups;

Variable[] cmp_gr = new Variable[golfers];

Variable[] gr = new Variable[golfers];

Variable match;

for(int i = 0 ; i < weeks-1 ; i++){

for(int j = 0 ; j < groups ; j++){

for(int k = 0 ; k < golfers ; k++){

gr[k] = vars[(i*players+j*golfers)+k];

}

for(int l = i + 1 ; l < weeks ; l++){

for(int m = 0 ; m < groups ; m++){

Variable[] reif = new Variable[golfers*golfers];

for(int k = 0 ; k < golfers ; k++){

cmp_gr[k] = vars[l*players+m*golfers+k];

for(int n = 0;n < golfers ; n++){

reif[k*golfers+n] = new Variable(0,1);

Reified(XeqY(gr[n],cmp_gr[k]),reif[k*golfers+n]);

}

}

match = new Variable(new BoundDomain(0,golfers));

Sum(reif,match);

XlteqC(match,1);

}

}

}

}

The SDV-model is easier to read, has fewer lines of codes19 and our results inchapter 5.2.3 shows that it is much more efficient for more difficult instancesof the social golfer problem. The FDV- and SDV-models can be found aspseudo codes in Appendix C.

1922 rows for the SDV-model compared to 38 for the FDV-model.

32

Page 45: Set theory in constraint programming

5 EVALUATION 5.2 Social golfer

5.2.3 Comparsion between domains

Golfer(2-4-4) Golfer(2-5-4)SDV FDV SDV FDV

Searchnodes 27 24 35 41140Decisions 27 24 35 20586Wrong decisions 0 0 0 20554Backtracks 0 0 0 20548Maximal depth 27 24 35 44Time(ms) 434 372 243 8544

Golfer(3-6-4) Golfer(3-7-4)SDV FDV SDV FDV

Searchnodes 26975 408224 85081 869714Decisions 13518 204140 42579 434892Wrong decisions 13457 204084 42502 434822Backtracks 13449 204068 42495 434809Maximal depth 76 81 93 94Time 64s 6m 4m 20s 18m 16s

Golfer(4-5-4) Golfer(4-9-4)SDV FDV SDV FDV

Searchnodes 7323 390772 3523 265376Decisions 3691 195414 1826 132749Wrong decisions 3632 195358 1697 132627Backtracks 3626 195343 1689 132604Maximal depth 72 91 139 154Time 26s 12m 27s 19s 12m 32s

Golfer(5-5-3) Golfer(5-8-3)SDV FDV SDV FDV

Searchnodes 492 1228 583 6230542Decisions 275 640 343 3115320Wrong decisions 217 588 240 3115222Backtracks 207 577 228 3115195Maximal depth 72 68 116 135Time 2s 2s 4s 6h 15m

33

Page 46: Set theory in constraint programming

5.3 N-knight’s tour problem 5 EVALUATION

5.3 N-knight’s tour problem

5.3.1 Problem statement20

The problem is to find a tour of a knight on a n by n board. Only valid movesof a knight are allowed and every square of the board must be visited exactlyonce. If it is possible to use a single valid move to get from the last visitedsquare to the first, then the tour is said to be re-entrant.

We will find a re-entrant solution for this problem with n = 6. With this nwe can encode the chessboard as:21

1 2 3 4 5 67 8 9 10 11 1213 14 15 16 17 1819 20 21 22 23 2425 26 27 28 29 3031 32 33 34 35 36

One re-entrant solution for this problem is:1 9 5 16 3 7 15 2 10 6 17 30 34 26 13 21 25 33 29 18 22 11 24 35 27 31 20 2832 19 8 4 12 23 36 14

5.3.2 Differences in solutions

This problem is not really a set problem, so we would expect the FDV-modelto be better and it seems as the FDV-model is better at every point exceptthe number of lines needed to write it. Both models are pretty simple to writeand read, the FDV-model is written 28 lines and the SDV-model consists of26 lines of code.

The FDV- and SDV-models can be found as pseudo codes in Appendix D.

5.3.3 Comparsion between domains

N-knight, n=6 SDV FDVSearchnodes 668844 7685Decisions 334436 3849Wrong decisions 334408 3836Backtracks 334314 3825Maximal depth 138 25Time 31s 4s

20This statement is taken from [9]21A table over possible next steps from each square ca be found in Appendix D.

34

Page 47: Set theory in constraint programming

6 FUTURE WORK

6 Future Work

The work with set domains in a CLP-solver like JaCoP is far from over, thiswork is only a beginning to show the useage of set domains.

A natural next step is to make set domains a natural part of JaCoP. Search-functions could be implemented to deal with SDVs and FDVs simultaneous.With this implemented it would be possible to get easy-to-read results forproblems modeled with a mixture of set domain variables and FDVs.

This work has mainly focused on making a stable implementation of setdomains in JaCoP so there is still be some work left to optimize the imple-mentation.

Using set domains to solve problems gives ideas for new constraints to im-plement. There is for example an article [2] about the social golfer problemthat suggests the use of lexicographic order constraint to break more sym-metries and another article [6] that suggest the similar length-lexicographicorder constraint.

[1] has chosen to save the cardinality of a SDV as an extra domain in setdomain. The cardinality is updated when the set domain changes, in thisway one can save a lot of time in constraints like the cardinality constraintand the length-lexicographic constraint suggested above. The disadvantagein saving the cardinality is the extra time needed to update it every time theset domain changes and of course it needs some extra memory to save theinformation. It would be good to have an evaluation of how to deal with thecardinality information in JaCoP since it could make some problems easierto solve.

Finally one should use a more universal solver input language like MiniZincor FlatZinc to make a thorough comparison to other solvers. In this wayone could use the same model in different solvers and the results of the com-parison would be more accurate. The usage of input languages for JaCoPwas not implemented when this work started, but during this work KrzysztofKuchcinski began the work to make input languages possible in JaCoP.

35

Page 48: Set theory in constraint programming

7 CONCLUSIONS

7 Conclusions

One of the most important parts that makes our set constraint implementa-tion efficient in JaCoP is the usage of set intervals to represent the setDo-main. This simple representation of setDomains makes common operationsas union, intersection and set difference really fast and still keeps the amountof memory needed at a low level.

Our work explains the theory behind SDVs and set constraints with set in-tervals and shows how we implemented eight regular constraints and threeprimitve constraints for sets. These constraints makes it possible to solvemany problems, from a wide variety of fields, in an easier way. The Steiner(7)problem is for example modeled with 7 SDVs while the same problem needs21 FDVs to be modeled.

To be able to solve harder problems, like the Social golfer problem, it is im-portant to break as much symmetry as possible. SDVs have the built-in char-acteristic to break some symmetry and will therefore need fewer constraintsto solve problems. This together with the easier modeling mentioned abovemakes a SDV-modeled problem much shorter and easier to read comparedwith the same problem modeled with FDVs.

Even though SDVs makes the problems easier to model, our evaluation showsthat it does not necessarily makes the solver faster, a problem will sometimesbe faster to solve with FDVs and sometimes be faster with SDVs. This iswhy the usage of FDVs and SDVs combined is such an interesting field. Ourevaluation suggests that this combination could be used to solve some harderproblems in a faster and more efficient way.

While this report has come to an end I hope that it has left the reader cu-rious for more and I encourage the reader to try JaCoP and the set theorypackage22 in order to solve different kind of problems.

22The set theory package can be found at JaCoPs homepage [5] or atwww.set.akemalm.se

36

Page 49: Set theory in constraint programming

REFERENCES REFERENCES

References

[1] Francisco Azevedo,Cardinal: A Finite Sets Constraint Solver In Constraints(2007).

[2] Nicolas Barnier and Pascal Brisset,Solving the Kirkman’s Schoolgirl Problem in a Few Seconds.

[3] Carmen Gervet,Interval Propagation to Reason about Sets: Definition and Implementa-tion of a Practical Language. In Constraint, An International Journal 1(1997).

[4] Krzysztof Kuchcinski and Radoslaw Szymanek,JaCoP Library User’s GuideVersion 2.3, (2009).

[5] JaCoP homepagehttp://www.jacop.eu,(2009).

[6] Justin Yip and Pascal Van Hentenryck,Length-Lex Bound Consistency for Knapsack Constraints, (2009).

[7] Wikipedia, Sudoku,http://en.wikipedia.org/wiki/Sudoku,(2009-07-09).

[8] CSPLib : a problem library for constraints (2009),http://www.csplib.org/,(2009-07-09).

[9] Information on the n Knight’s Tour Problem,http://theory.cs.uvic.ca/inf/misc/Knight.html,(2009-07-20).

37

Page 50: Set theory in constraint programming

A Sudoku solver

(Working Code)

import JaCoP.constraints.*;

import JaCoP.core.*;

import JaCoP.search.*;

}

public class Sudoku {

public static void main(String[] args) {

Store store = new Store();

//Set domain for all variables.

Variable[][] all = new Variable[9][9];

Variable[] allVariables = new Variable[81];

String rowname = "abcdefghi";

for(int i = 0 ; i<9 ; i++){

for(int j = 1 ; j<=9 ; j++){

all[i][j-1] = new Variable(store,""+rowname.charAt(i)+

""+j,new IntervalDomain(1,9));

allVariables[i*9+(j-1)] = all[i][j-1];

}

}

//Set constraints

Variable[] v = new Variable[9];

for(int i = 0 ; i < 9 ; i++){

//Each row shall contain all the digits from 1 to 9

//exactly one time.

store.impose(new Alldifferent(all[i]));

//Each column shall contain all the digits from 1 to 9

//exactly one time.

for(int j = 0 ; j < 9 ; j++)

v[j] = all[j][i];

store.impose(new Alldifferent(v));

}

//Each box shall contain all the digits from 1 to 9 exactly

//one time.

Variable[][] boxes = {{all[0][0],all[0][1],all[0][2],

all[1][0],all[1][1],all[1][2],all[2][0],all[2][1],

all[2][2]},{all[0][3],all[0][4],all[0][5],all[1][3],

all[1][4],all[1][5],all[2][3],all[2][4],all[2][5]},

I

Page 51: Set theory in constraint programming

{all[0][6],all[0][7],all[0][8],all[1][6],all[1][7],

all[1][8],all[2][6],all[2][7],all[2][8]},{all[3][0],

all[3][1],all[3][2],all[4][0],all[4][1],all[4][2],

all[5][0],all[5][1],all[5][2]},{all[3][3],all[3][4],

all[3][5],all[4][3],all[4][4],all[4][5],all[5][3],

all[5][4],all[5][5]},{all[3][6],all[3][7],all[3][8],

all[4][6],all[4][7],all[4][8],all[5][6],all[5][7],

all[5][8]},{all[6][0],all[6][1],all[6][2],all[7][0],

all[7][1],all[7][2],all[8][0],all[8][1],all[8][2]},

{all[6][3],all[6][4],all[6][5],all[7][3],all[7][4],

all[7][5],all[8][3],all[8][4],all[8][5]},{all[6][6],

all[6][7],all[6][8],all[7][6],all[7][7],all[7][8],

all[8][6],all[8][7],all[8][8]}};

for(Variable[] box : boxes)

store.impose(new Alldifferent(box));

//Each place that has a value imposes a constraint

int[][] values = { {0,0,5},{0,1,3},{0,4,7},

{1,0,6},{1,3,1},{1,4,9},{1,5,5},

{2,1,9},{2,2,8},{2,7,6},

{3,0,8},{3,4,6},{3,8,3},

{4,0,4},{4,3,8},{4,5,3},{4,8,1},

{5,0,7},{5,4,2},{5,8,6},

{6,1,6},{6,6,2},{6,7,8},

{7,3,4},{7,4,1},{7,5,9},{7,8,5},

{8,4,8},{8,7,7},{8,8,9}};

for(int[] e : values)

store.impose(new XeqC(all[e[0]][e[1]],e[2]));

boolean result;

if(!store.consistency()){

Search label;

SelectChoicePoint select;

label = new DepthFirstSearch();

ComparatorVariable cv = null;

select = new SimpleSelect(allVariables, cv,

new IndomainMin());

label.setSolutionListener(new SimpleSolutionListener());

label.setAssignSolution(false);

label.getSolutionListener().recordSolutions(true);

II

Page 52: Set theory in constraint programming

result = label.labeling(store, select);

}else{

result = true;

}

if(result)

for(Variable[] i : all){

for(Variable j : i)

System.out.print(j.value()+" ");

System.out.println();

}

}

}

III

Page 53: Set theory in constraint programming

B Steiner problem

B.1 Solution in set domain

(Pseudo code)

Steiner(int n){

Variable[] vars = new variable[n]

for(int i = 0 ; i < n ; i++){

vars[i] = new Variable(new setDomain({},{1,n}))

Card(vars[i],3)

for(int j = i+1 ; j < n ; j++){

Variable v = new Variable(new setDomain({},{1,n}))

XintersectYeqZ(vars[i],vars[j],v)

Card(v,1)

/* Lex(vars[i],vars[j]) */

}

}

}

IV

Page 54: Set theory in constraint programming

B.2 Solution in finite domain(Pseudo code)Steiner(int n){

Variable[] vars = new Variable[n*3];

for(int i=0 ; i<t ; i++){

for(int j=0 ; j<3 ; j++){

vars[i*3+j] = new Variable(new BoundDomain(1,n));

}

Alldifferent(vars[i*3], vars[i*3+1],vars[i*3+2]);

}

Variable[] reif = new Variable[9];

Variable[] sums = new Variable[3];

/*Variable[] totsums = new Variable[t]; */

for(int i=0 ; i<t ; i++){

for(int p=0 ; p<3 ; p++){

sums[p] = new Variable(new BoundDomain(1,987));

XmulCeqZ(vars[i*3+p],10^p,sums[p]);

}

Variable sum = new Variable(new BoundDomain(1,987));

Sum(sums,sum);

/* totsums[i] = sum; */

XltY(vars[i*3],vars[i*3+1]);

XltY(vars[i*3+1],vars[i*3+2]);

for(int j=i+1 ; j<t ; j++){

for(int k=0 ; k<9 ; k++){

reif[k] = new Variable(0, 1);

}

for(int k=0 ; k<3 ; k++){

for(int l=0 ; l<3 ; l++){

Reified(XeqY(vars[i*3+k],vars[j*3+l]),reif[k*3+l]);

}

}

Variable summa = new Variable(new BoundDomain(0,n));

Sum(reif,summa);

XlteqC(summa,1);

}

}

/*for(int i = 0 ; i < t-1 ; i++){

XltY(totsums[i],totsums[i+1]);

} */

}

V

Page 55: Set theory in constraint programming

C Social golfer problem

C.1 Solution in set domain

(Pseudo code)

Golfer(int weeks, int groups, int golfers){

int players = golfers*groups;

int gw = groups*weeks;

//Create all groups for all weeks

vars = new Variable[groups*weeks];

for(int i=0 ; i<weeks ; i++){

for(int j=0 ; j<groups ; j++){

vars[i*groups+j] = new Variable(new SetDomain(1,players);

Card(vars[i*groups+j],golfers);

//Every player must play each week

for(int k = 0 ; k < j ; k++){

DisjointSets(vars[i*groups+k],vars[i*groups+j]);

}

}

}

//Each golfer plays in the same group as any other golfer

//maximum once

for(int i = 0 ; i < groups*weeks-1 ; i++){

for(int j = i + 1 ; j < groups*weeks){

Variable inters = new variable(new SetDomain(1,players));

XintersectYeqZ(vars[i], vars[j], inters);

Card(inters,0,1);

}

}

}

VI

Page 56: Set theory in constraint programming

C.2 Solution in finite domain(Pseudo code)Golfer(int weeks, int groups, int golfers){

int players = golfers*groups;

//Create all groups for all weeks

vars = new Variable[players*weeks];

Variable[] week = new Variable[players];

for(int i=0 ; i<weeks ; i++){

for(int j=0 ; j<groups ; j++)

for(int k=0 ; k<golfers ; k++){

int pos = i*players+j*golfers+k;

vars[pos] = new Variable(new IntervalDomain(1,players);

week[j*golfers+k] = vars[pos];

if(k>0)

XltY(vars[pos-1],vars[pos]);

}

Alldifferent(week);

}

//Each golfer plays with any other golfer maximum once

Variable[] gr = new Variable[golfers];

Variable[] cmp_gr = new Variable[golfers];

for(int i = 0 ; i < weeks-1 ; i++)

for(int j = 0 ; j < groups ; j++){

for(int k = 0 ; k < golfers ; k++)

gr[k] = vars[(i*players+j*golfers)+k];

for(int l = i + 1 ; l < weeks ; l++)

for(int m = 0 ; m < groups ; m++){

Variable[] reif = new Variable[golfers*golfers];

for(int k = 0 ; k < golfers ; k++){

cmp_gr[k] = vars[l*players+m*golfers+k];

for(int n = 0;n < golfers ; n++){

reif[k*golfers+n] = new Variable(0,1);

Reified(XeqY(gr[n],cmp_gr[k]),reif[k*golfers+n]);

}

}

Variable match = new Variable(0,golfers);

Sum(reif,match);

XlteqC(match,1);

}

}

}

VII

Page 57: Set theory in constraint programming

D N-knigth’s tour

D.1 Possible moves

Possible moves for the N-knight’s problem:

1 {9, 14}2 {10, 13, 15}3 {7, 11, 14, 16}4 {8, 12, 15, 17}5 {9, 16, 18}6 {10, 17}7 {3, 15, 20}8 {4, 16, 19, 21}9 {1, 5, 13, 17, 20, 22}10 {2, 6, 14, 18, 21, 23}11 {3, 15, 22, 24}12 {4, 16, 23}13 {2, 9, 21, 26}14 {1, 3, 10, 22, 25, 27}15 {2, 4, 7, 11, 19, 23, 26, 28}16 {3, 5, 8, 12, 20, 24, 27, 29}17 {4, 6, 9, 21, 28, 30}18 {5, 10, 22, 29}

19 {8, 15, 27, 32}20 {7, 9, 16, 28, 31, 33}21 {8, 10, 13, 17, 25, 29, 32, 34}22 {9, 11, 14, 18, 26, 30, 33, 35}23 {10, 12, 15, 27, 34, 36}24 {11, 16, 28, 35}25 {14, 21, 33}26 {13, 15, 22, 34}27 {14, 16, 19, 23, 31, 35}28 {15, 17, 20, 24, 32, 36}29 {16, 18, 21, 33}30 {17, 22, 34}31 {20, 27}32 {19, 21, 28}33 {20, 22, 25, 29}34 {21, 23, 26, 30}35 {22, 24, 27}36 {23, 28}

Neighbour matrix used in the FDV and SDV models:

int[][] neighbs = {{9, 14}, {10, 13, 15}, {7, 11, 14, 16},

{8, 12, 15, 17}, {9, 16, 18}, {10, 17}, {3, 15, 20},

{4, 16, 19, 21}, {1, 5, 13, 17, 20, 22},

{2, 6, 14, 18, 21, 23}, {3, 15, 22, 24}, {4, 16, 23},

{2, 9, 21, 26}, {1, 3, 10, 22, 25, 27},

{2, 4, 7, 11, 19, 23, 26, 28}, {3, 5, 8, 12, 20, 24, 27, 29},

{4, 6, 9, 21, 28, 30}, {5, 10, 22, 29}, {8, 15, 27, 32},

{7, 9, 16, 28, 31, 33}, {8, 10, 13, 17, 25, 29, 32, 34},

{9, 11, 14, 18, 26, 30, 33, 35}, {10, 12, 15, 27, 34, 36},

{11, 16, 28, 35},{14, 21, 33}, {13, 15, 22, 34},

{14, 16, 19, 23, 31, 35}, {15, 17, 20, 24, 32, 36},

{16, 18, 21, 33}, {17, 22, 34}, {20, 27}, {19, 21, 28},

{20, 22, 25, 29}, {21, 23, 26, 30}, {22, 24, 27}, {23, 28}};

VIII

Page 58: Set theory in constraint programming

D.2 Solution in set domain

(Pseudo code)

knight(int[][] neighbs){

int nn = 36;

Variable[] elements = new Variable[nn];

for(int i=0 ; i<nn-1 ; i++)

elements[i] = new Variable(new SetDomain(1, nn));

/** Set neighbours **/

Set[] neighbours = new Set[nn];

for(int i = 0 ; i < neighbs.length ; i++){

neighbours[i] = new Set();

for(int j = 0 ; j < neighbs[i].length ; j++){

neighbours[i].addDom(neighbs[i][j]);

}

}

Variable[] vars = new Variable[nn];

for (int i=0; i<vars.length; i++)

vars[i] = new Variable(new IntervalDomain(1, nn));

/** First and last step **/

XeqC(vars[0], 1);

XeqC(vars[1], 9);

XeqC(vars[35], 14);

/** One number/square **/

for(int i=0 ; i<nn-1 ; i++){

for(int j=i+1 ; j<nn ; j++){

XneqY(vars[i], vars[j]);

}

}

for(int i=0 ; i<34 ; i++){

ElementSet(vars[i], neighbours, elements[34-i]);

XinY(vars[i+1], elements[34-i]);

}

}

IX

Page 59: Set theory in constraint programming

D.3 Solution in finite domain

(Pseudo code)

knight(int[][] neighbs){

int nn = 36;

/** Set neighbours **/

Variable[] tour = new Variable[nn];

IntervalDomain id;

for(int i = 0 ; i < neighbs.length ; i++){

id = new IntervalDomain();

for(int j = 0 ; j < neighbs[i].length ; j++){

id.addDom(neighbs[i][j],neighbs[i][j]);

}

tour[i] = new Variable(id);

}

Variable[] vars = new Variable[nn];

for (int i=0; i<vars.length; i++)

vars[i] = new Variable(new IntervalDomain(1, nn));

/** First and last step **/

XeqC(vars[0], 1);

XeqC(vars[1], 9);

XeqC(vars[35], 14);

/** One number/square **/

for(int i=0 ; i<nn-1 ; i++){

for(int j=i+1 ; j<nn ; j++){

XneqY(vars[i], vars[j]);

}

}

Variable[] elements = new Variable[nn];

for(int i=0 ; i<nn-1 ; i++)

elements[i] = new Variable(new BoundDomain(1, nn));

for(int i=0 ; i<35 ; i++){

Element(vars[i], tour, elements[i]);

XeqY(vars[i+1],elements[i]);

}

}

X