SOLVING LOGIC GRID PUZZLE USING RECURSIVE CONSTRAINT SATISFACTION ALGORITHM Samuel Lukas*, Arnold Aribowo**, Aristianto* * Informatics Engineering, Computer Science Faculty, Universitas Pelita Harapan ** Computer Engineering, Computer Science Faculty, Universitas Pelita Harapan UPH Tower, Lippo Karawaci, Tangerang, 15811 [email protected], [email protected], [email protected]

ABSTRACT Logic puzzle is a kind of problem that gives us a set of premises and then we are asked to draw a conclusion using logical deduction or reasoning. Some types of logic puzzles include Knights and Knaves, Sudoku, Nonogram, and “logic grid” puzzle. If the puzzle is simple and contains only a small number of premises, it can be solved manually in a short time. But if the puzzle is complex and contains many premises we will need a systematic approach. This paper proposes a systematic approach to solve “logic grid” puzzles. This approach can be used to solve all “logic grid” puzzles. The puzzle is translated into a specification language which will be processed by the software to generate a solution. To convert the specification into a solution we will use backtracking and recursive function calling. How we specify the puzzle will affect the performance of the program. The aim of this paper is to create a specification language that is easy to understand and flexible enough to accommodate all “logic grid” puzzles. It does not deal with any optimization issues, complicated syntax checking, etc. More features can be added to the language in the future should the need arise. The name of the specification language is FasyLogic. Keywords : logic puzzle, constraint satisfaction

1

INTRODUCTION

Nowadays, the use of computers can be found in all fields. Computers help us to accomplish many things. The technology is developing rapidly, both in hardware and software. This paper will focus on the software. In order to be useful for humans, computers must have a certain level of “intelligence”; that is, computers must be able to think like humans. Since their intelligence is created by humans, it is called artificial intelligence. One topic in artificial intelligence is Constraint Satisfaction Problems (CSP). In a CSP, there are variables and constraints, for which each variable has some possible values [1]. The solution to a CSP is the combination of values for each variable where all constraints are satisfied. Some examples of CSP are map coloring problem, linear

programming, and logic puzzle. This paper focuses on the latter, because there is a high correlation between logic puzzle and logical deduction, which is very important in computer programming. Logical deduction is very important because computers are machines; they cannot understand human languages. Instructions to computers must be translated to a machine language. This translation requires systematic and logical thinking. To master the art of logical thinking, one must practice a lot. This practice will be more interesting if it is in the form of a game, such as logic puzzle and Sudoku. This paper proposes a specification language, FasyLogic (Fast and Easy Logic), to translate a “logic grid” puzzle into a program which is then processed by the interpreter to find a solution. By doing this, one can hone his skill in formulating a problem. The solution that is found by the interpreter can be compared to the real solution to verify the correctness of the FasyLogic program. It is assumed that the input program is always valid; that is, it conforms to the grammar of FasyLogic and all elements in the program are unique. Elements are possible values for each variable in the problem. This input program will then be processed by the interpreter to find the solution.

2

BASIC THEORIES

2.1 Constraint Satisfaction Algorithm In the field of artificial intelligence, a CSP is defined by a set or variables, X1, X2, ..., Xn, and a set of constraints, C1, C2, ..., Cm. Each variable Xi has some possible values for that particular variable. Each constraint Ci involves one or more variables and all constraints must be satisfied by the solution. A problem can be solved by enumerating each possible state and testing it against the constraints. A state is a combination of values for each variable in the system, {Xi= vi, Xj= vj, ...}. In other words, each value is assigned to one variable. If this assignment does not contradict the constraints, it is called consistent assignment or legal

assignment. When all variables already have values assigned to them and there exists no contradiction, a solution is found. A common strategy to do this enumeration is Depth-First Search (DFS). In DFS, each node in one level will always be expanded first before expanding the next node in the same level. In other words, the process goes for the “depth”, not the “breadth”, of the tree.

The man who keeps horses lives next to the man who smokes Dunhill. The owner who smokes Blue Master drinks beer. The German smokes Prince. The Norwegian lives next to the blue house. The man who smokes Blend has a neighbor who drinks water.

It is known that there are five different houses with different colors. The houses are inhabited by five different persons with different drinks, pets, and cigarettes. From the facts, the reader is asked to find out who has fish as his/her pet.

3

PROBLEM FORMULATION

In a “logic grid” puzzle, there are always m types and each type has n unique elements. Usually there are some statements (constraints) which connect the elements together. It is assumed that all elements are unique.

Figure 1. Depth-first Search Diagram

For the arrangement of nodes in Figure 1, the search will go in this order: A-B-D-F-C-G-E. It means the first possible state is A-B-D, but this is not the solution, so the process “backtracks” to the previous node. Now the state is A-B-F-, but this is not the solution either, so the process backtracks again. The whole process continues until a solution is found.

2.2 Logic Puzzle Logic puzzle is a kind of problem that gives the reader a set of premises and then he is asked to draw a conclusion using logical deduction or reasoning [2]. One example of logic puzzle is as follows:

There are many types of logic puzzle, for example: Knights and Knaves, Sudoku, Nonogram, logic mazes, and “logic grid” puzzle. This paper will deal with the latter. One example for “logic grid” puzzle is known as Zebra Puzzle or Einstein’s Puzzle [3]. There are some variations of this puzzle, one of which is given below: The The The The The The The The The The

Brit lives in the red house. Swede keeps dogs as pets. Dane drinks tea. green house is on the left of the white house. green house owner drinks coffee. person who smokes Pall Mall rears birds. owner of the yellow house smokes Dunhill. man living in the house right in the center drinks milk. Norwegian lives in the first house. man who smokes Blend lives next to the one who keeps cats.

The solution can be formulated using a matrix K = [kij] with the order of m x n; for each kpq = krs then p=r and q=s:

kij

where i = 1, 2, …, m and j = 1, 2, …, n for each kpq = krs then p=r and q=s

All elements must satisfy the given constraints. This concept can be better explained through an example [4]: Last Saturday, all six of the horses in Johnson Stables were rented to children for the day, all of them regulars. Each of the horses (whose names are Boris, Hunter, Lady, Ranger, Santa Fe, and Topper) lives in one of the six stalls, numbered one to six from west to east. The children included three boys (Brian, Curtis, and Roy) and three girls (Lily, Michelle, and Theresa), each a different age (15, 14, 12, 10, 9, 7 years old). Some facts about the horses and the children who rode them that day: 1. Topper lives two or more stalls to the east of Theresa's horse. 2. The nine-year-old's horse lives somewhere to the west of Brian's horse. 3. Three horses in consecutive stalls, from west to east, are Boris, Brian's horse, and the 12-year-old's horse. 4. The child who rode Topper is three years older than the one who rode the horse in stall 4, while Roy is three years older than Michelle. (These are 4 different children). 5. Ranger's rider is three years older than Lily, who in turn is two years older than the girl who rode Lady. 6. Santa Fe lives somewhere to the west of Curtis's horse. 7. Brian is just one year older than Theresa. 8. Roy didn't ride the horse in stall 6. Can you determine each horse's stall number, and the name and age of the child who rode him or her that day?

From the above problem, we know there are four types: Index, Kids, Age, and Horse and each type has six unique elements.

The solution is a matrix K with the order of 4 x 6: Index Kids Age Horse

k11 k21 k31 k41

k12 k22 k32 k42

k13 k23 k33 k43

k14 k24 k34 k44

k15 k25 k35 k45

k16 k26 k36 k46

k1j is an element of type Index, which values are 1, 2, 3, 4, 5, 6. k2j is an element of type Kids, which values are brian, curtis, roy, lily, michelle, theresa. k3j is an element of type Age, which values are 15, 14, 12, 10, 9, 7. K4j is an element of type Horse, which values are boris, hunter, lady, ranger, santa_fe, topper. The arrangement of all these elements must satisfy all given constraints. There are two kinds of constraints: fixed constraints (Facts) and flexible constraints (Conditions). Fixed constraints define the relation between two elements that are located in the same column and the values of both elements are fixed. Flexible constraints define the relation between elements, either in the same column or not, and the values of these elements may change. In the example above, the fixed constraints are not obvious. There are six different ages: 15, 14, 12, 10, 9, 7. It is known that brian is one year older than theresa (this statement is a flexible constraint, because the exact values are not known). From this, a conclusion can be made that brian is 15 or 10 years old (this statement is a fixed constraint, because the exact values are known).

4

THE FASYLOGIC LANGUAGE

A “logic grid” puzzle can be specified in FasyLogic using three kinds of components: type definition, fixed constraints, and flexible constraints. Each consists of one or more lines in arbitrary order. A line that begins with “#” is treated as a comment and will not be processed by the interpreter.

4.1 Type Definition This component defines the types and elements that are involved in the program. A type [name] is used to reference an element based on another element [or itself] which is located in the same column. The syntax definition of this component: TYPELINES Æ LINETYPE | TYPELINES NEWLINE TYPELINES NEWLINE Æ ’\n’ LINETYPE Æ TYPENAME COLON ELEMENTS TYPENAME Æ BIGLETTER | BIGLETTER STRINGBIG STRINGBIG Æ STRINGBIGPART | STRINGBIG STRINGBIGPART STRINGBIGPART Æ BIGLETTER | LETTER | DIGIT | SPECIAL COLON Æ ’:’ ELEMENTS Æ ELEMENT | ELEMENTS COMMA ELEMENTS

ELEMENT Æ STRING | DIGITS | MIN DIGITS STRING Æ STRINGPART | STRING STRINGPART STRINGPART Æ LETTER | DIGIT | SPECIAL BIGLETTER Æ A | B | ... | Z LETTER Æ a | b | ... | z DIGITS Æ DIGIT | DIGITS DIGIT DIGIT Æ 0 | 1 | ... | 9 COMMA Æ , MIN Æ SPECIAL Æ _

It can be seen from the syntax definition that TYPELINES consists of one or more LINETYPE in arbitrary order, separated by a newline character. For the example stated in section 3, the type definition is: Index: 1, 2, 3, 4, 5, 6 Kids: brian, curtis, roy, lily, michelle,theresa Age: 15, 14, 12, 10, 9, 7 Horse: boris, hunter, lady, ranger, santa_fe, topper

It means there are four types: Index, Kids, Age, and Horse. Each has six unique elements (the elements are separated by a comma). The type name must begin with an uppercase character and consists of alphanumeric characters and/or underscores. There is a reserved word, Index, which is used to assign values to the partial solution matrix before the interpreter starts finding a solution. All elements with type Index will be inserted into the matrix in the order of their definition.

4.2 Fixed Constraints This component specifies which elements are located in the same column. The syntax definition of this component: FACTLINES Æ LINEFACT | FACTLINES NEWLINE FACTLINES NEWLINE Æ ’\n’ LINEFACT Æ KNOWNFACT | LINEFACT B LINEFACT B Æ ’|’ KNOWNFACT Æ ELEMENT A ELEMENT A Æ ^ ELEMENT Æ STRING | DIGITS | MIN DIGITS STRING Æ STRINGPART | STRING STRINGPART STRINGPART Æ LETTER | DIGIT | SPECIAL LETTER Æ a | b | ... | z DIGITS Æ DIGIT | DIGITS DIGIT DIGIT Æ 0 | 1 | ... | 9 SPECIAL Æ _ MIN Æ -

It can be seen from the syntax definition that FACTLINES consists of one or more LINEFACT in arbitrary order, separated by a newline character. LINEFACT consists of one or more KNOWNFACT that are separated by the OR operator (“|”). This concept will be made clear by looking at the following fixed constraints definition for the example in section 3: brian ^ 15 | brian ^ 10 theresa ^ 14 | theresa ^ 9 topper ^ 15 | topper ^ 12 | topper ^ 10 ranger ^ 15 | ranger ^ 12

4 ^ 12 | 4 ^ 9 | 4 ^ 7 lily ^ 12 | lily ^ 9 roy ^ 15 | roy ^ 12 | roy ^ 10 michelle ^ 12 | michelle ^ 9 | michelle ^ 7 lady ^ 12 | lady ^ 10 | lady ^ 7

For the first line, it can be said that brian is 15 or 10 years old. The “^” operator is used to combine two elements that are known to be in the same column, while the “|” operator is used to branch the possibility. Likewise, for the last line, there are three possibilities for the age of lady: 12, 10, and 7.

4.3 Flexible Constraints This component defines the conditions that must be satisfied by the solution. In other words, it specifies all relations that exist between two elements, but the exact elements are not known yet. This relation is defined using a comparison operator and the involved elements are defined using the format ElementName.TypeName. For instance, it is known that roy is three years older than michelle. In this case the “older” relation can be defined using the type Age. It means roy’s age is written as roy.Age and michelle’s as michelle.Age. So the whole statement becomes roy.Age == michelle.Age + 3. The syntax definition of this component: CONDITIONLINES Æ LINECONDITION | CONDITIONLINES NEWLINE CONDITIONLINES NEWLINE Æ ’\n’ LINECONDITION Æ CONDITION | LINECONDITION C CONDITION CONDITION Æ E O E | ( CONDITION ) C Æ ’&’ | ’|’ O Æ ’==’ | ’=’ E Æ F | F A F F Æ ELEMENT | ELEMENTTYPE A Æ + | - | * | / | % ELEMENT Æ STRING | DIGITS | MIN DIGITS STRING Æ STRINGPART | STRING STRINGPART STRINGPART Æ LETTER | DIGIT | SPECIAL LETTER Æ a | b | ... | z DIGIT Æ 0 | 1 | ... | 9 DIGITS Æ DIGIT | DIGITS DIGIT SPECIAL Æ _ MIN Æ ELEMENTTYPE Æ ELEMENT DOT TYPENAME TYPENAME Æ BIGLETTER | BIGLETTER STRINGBIG STRINGBIG Æ STRINGBIGPART | STRINGBIG STRINGBIGPART STRINGBIGPART Æ BIGLETTER | LETTER | DIGIT | SPECIAL BIGLETTER Æ A | B | ... | Z DOT Æ .

CONDITIONLINES consists of one or more LINECONDITION in arbitrary order, separated by a newline character. LINECONDITION consists of one or more CONDITION that are separated by logical operators

(AND and OR). Each CONDITION consists of the left-handside, a comparison operator, and the right-handside. The example in section 3 gives more insight: brian.Age == theresa.Age + 1 curtis.Index > 1

brian.Index > 1 topper.Index > 2 12.Index > 2 roy.Index 6 boris.Index < 6 topper.Index >= theresa.Index + 2 9.Index < brian.Index & 9.Index < 4 santa_fe.Index < curtis.Index boris.Index == brian.Index - 1 & brian.Index == 12.Index - 1 topper.Age == 4.Age + 3 roy.Age == michelle.Age + 3 ranger.Age == lily.Age + 3 & lily.Age == lady.Age + 2 topper.Kids roy.Kids & 4.Kids roy.Kids & topper.Kids michelle.Kids & 4.Kids michelle.Kids lady.Kids == lily.Kids | lady.Kids == michelle.Kids | lady.Kids == theresa.Kids

The first line means brian is one year older than theresa. The second line means curtis’ horse does not live in the first stall. In all cases, the “&” is used as an AND operator and the “|” as an OR operator.

5

FINDING THE SOLUTION

When processing a FasyLogic program, it is assumed that the input program is always correct (conforms to the language specification and is a representation of the actual problem). If the interpreter encounters an error due to incorrect input, it will display an error message and the user is expected to correct the program. The algorithm to find the solution is rather complex but it can be outlined in the pseudocode below: In line 1, two variables with Stack data type are defined. colFacts is used to hold all fixed constraints and colConditions the flexible constraints. One line in the input program will become one element in colFacts or colConditions. In line 2, two variables with Matrix data type are defined. The Matrix data type is similar to a two-dimensional array. In this case, the size is the same as the size of the solution (number of rows = number of types, number of columns = number of elements for each type). K will be used as the partial solution matrix. E is an array to hold the enumeration of possible values for the empty elements in K. 1 2 3

colFacts: Stack, colConditions: Stack K: Matrix, E: array of Matrix i: Integer, n: Integer

4 5

colFacts = list of fixed constraints colConditions = list of flexible constraints

6

n = 1

7

WHILE(n == 1 | there exists untried colFacts combination) IF colFacts is empty THEN K = empty matrix ELSE K = the n-th valid combination of colFacts END-IF

8 9 10 11 12 13

E = enumeration of possible values for the empty elements in K

14

i = 1

15 16

WHILE(i

ABSTRACT Logic puzzle is a kind of problem that gives us a set of premises and then we are asked to draw a conclusion using logical deduction or reasoning. Some types of logic puzzles include Knights and Knaves, Sudoku, Nonogram, and “logic grid” puzzle. If the puzzle is simple and contains only a small number of premises, it can be solved manually in a short time. But if the puzzle is complex and contains many premises we will need a systematic approach. This paper proposes a systematic approach to solve “logic grid” puzzles. This approach can be used to solve all “logic grid” puzzles. The puzzle is translated into a specification language which will be processed by the software to generate a solution. To convert the specification into a solution we will use backtracking and recursive function calling. How we specify the puzzle will affect the performance of the program. The aim of this paper is to create a specification language that is easy to understand and flexible enough to accommodate all “logic grid” puzzles. It does not deal with any optimization issues, complicated syntax checking, etc. More features can be added to the language in the future should the need arise. The name of the specification language is FasyLogic. Keywords : logic puzzle, constraint satisfaction

1

INTRODUCTION

Nowadays, the use of computers can be found in all fields. Computers help us to accomplish many things. The technology is developing rapidly, both in hardware and software. This paper will focus on the software. In order to be useful for humans, computers must have a certain level of “intelligence”; that is, computers must be able to think like humans. Since their intelligence is created by humans, it is called artificial intelligence. One topic in artificial intelligence is Constraint Satisfaction Problems (CSP). In a CSP, there are variables and constraints, for which each variable has some possible values [1]. The solution to a CSP is the combination of values for each variable where all constraints are satisfied. Some examples of CSP are map coloring problem, linear

programming, and logic puzzle. This paper focuses on the latter, because there is a high correlation between logic puzzle and logical deduction, which is very important in computer programming. Logical deduction is very important because computers are machines; they cannot understand human languages. Instructions to computers must be translated to a machine language. This translation requires systematic and logical thinking. To master the art of logical thinking, one must practice a lot. This practice will be more interesting if it is in the form of a game, such as logic puzzle and Sudoku. This paper proposes a specification language, FasyLogic (Fast and Easy Logic), to translate a “logic grid” puzzle into a program which is then processed by the interpreter to find a solution. By doing this, one can hone his skill in formulating a problem. The solution that is found by the interpreter can be compared to the real solution to verify the correctness of the FasyLogic program. It is assumed that the input program is always valid; that is, it conforms to the grammar of FasyLogic and all elements in the program are unique. Elements are possible values for each variable in the problem. This input program will then be processed by the interpreter to find the solution.

2

BASIC THEORIES

2.1 Constraint Satisfaction Algorithm In the field of artificial intelligence, a CSP is defined by a set or variables, X1, X2, ..., Xn, and a set of constraints, C1, C2, ..., Cm. Each variable Xi has some possible values for that particular variable. Each constraint Ci involves one or more variables and all constraints must be satisfied by the solution. A problem can be solved by enumerating each possible state and testing it against the constraints. A state is a combination of values for each variable in the system, {Xi= vi, Xj= vj, ...}. In other words, each value is assigned to one variable. If this assignment does not contradict the constraints, it is called consistent assignment or legal

assignment. When all variables already have values assigned to them and there exists no contradiction, a solution is found. A common strategy to do this enumeration is Depth-First Search (DFS). In DFS, each node in one level will always be expanded first before expanding the next node in the same level. In other words, the process goes for the “depth”, not the “breadth”, of the tree.

The man who keeps horses lives next to the man who smokes Dunhill. The owner who smokes Blue Master drinks beer. The German smokes Prince. The Norwegian lives next to the blue house. The man who smokes Blend has a neighbor who drinks water.

It is known that there are five different houses with different colors. The houses are inhabited by five different persons with different drinks, pets, and cigarettes. From the facts, the reader is asked to find out who has fish as his/her pet.

3

PROBLEM FORMULATION

In a “logic grid” puzzle, there are always m types and each type has n unique elements. Usually there are some statements (constraints) which connect the elements together. It is assumed that all elements are unique.

Figure 1. Depth-first Search Diagram

For the arrangement of nodes in Figure 1, the search will go in this order: A-B-D-F-C-G-E. It means the first possible state is A-B-D, but this is not the solution, so the process “backtracks” to the previous node. Now the state is A-B-F-, but this is not the solution either, so the process backtracks again. The whole process continues until a solution is found.

2.2 Logic Puzzle Logic puzzle is a kind of problem that gives the reader a set of premises and then he is asked to draw a conclusion using logical deduction or reasoning [2]. One example of logic puzzle is as follows:

There are many types of logic puzzle, for example: Knights and Knaves, Sudoku, Nonogram, logic mazes, and “logic grid” puzzle. This paper will deal with the latter. One example for “logic grid” puzzle is known as Zebra Puzzle or Einstein’s Puzzle [3]. There are some variations of this puzzle, one of which is given below: The The The The The The The The The The

Brit lives in the red house. Swede keeps dogs as pets. Dane drinks tea. green house is on the left of the white house. green house owner drinks coffee. person who smokes Pall Mall rears birds. owner of the yellow house smokes Dunhill. man living in the house right in the center drinks milk. Norwegian lives in the first house. man who smokes Blend lives next to the one who keeps cats.

The solution can be formulated using a matrix K = [kij] with the order of m x n; for each kpq = krs then p=r and q=s:

kij

where i = 1, 2, …, m and j = 1, 2, …, n for each kpq = krs then p=r and q=s

All elements must satisfy the given constraints. This concept can be better explained through an example [4]: Last Saturday, all six of the horses in Johnson Stables were rented to children for the day, all of them regulars. Each of the horses (whose names are Boris, Hunter, Lady, Ranger, Santa Fe, and Topper) lives in one of the six stalls, numbered one to six from west to east. The children included three boys (Brian, Curtis, and Roy) and three girls (Lily, Michelle, and Theresa), each a different age (15, 14, 12, 10, 9, 7 years old). Some facts about the horses and the children who rode them that day: 1. Topper lives two or more stalls to the east of Theresa's horse. 2. The nine-year-old's horse lives somewhere to the west of Brian's horse. 3. Three horses in consecutive stalls, from west to east, are Boris, Brian's horse, and the 12-year-old's horse. 4. The child who rode Topper is three years older than the one who rode the horse in stall 4, while Roy is three years older than Michelle. (These are 4 different children). 5. Ranger's rider is three years older than Lily, who in turn is two years older than the girl who rode Lady. 6. Santa Fe lives somewhere to the west of Curtis's horse. 7. Brian is just one year older than Theresa. 8. Roy didn't ride the horse in stall 6. Can you determine each horse's stall number, and the name and age of the child who rode him or her that day?

From the above problem, we know there are four types: Index, Kids, Age, and Horse and each type has six unique elements.

The solution is a matrix K with the order of 4 x 6: Index Kids Age Horse

k11 k21 k31 k41

k12 k22 k32 k42

k13 k23 k33 k43

k14 k24 k34 k44

k15 k25 k35 k45

k16 k26 k36 k46

k1j is an element of type Index, which values are 1, 2, 3, 4, 5, 6. k2j is an element of type Kids, which values are brian, curtis, roy, lily, michelle, theresa. k3j is an element of type Age, which values are 15, 14, 12, 10, 9, 7. K4j is an element of type Horse, which values are boris, hunter, lady, ranger, santa_fe, topper. The arrangement of all these elements must satisfy all given constraints. There are two kinds of constraints: fixed constraints (Facts) and flexible constraints (Conditions). Fixed constraints define the relation between two elements that are located in the same column and the values of both elements are fixed. Flexible constraints define the relation between elements, either in the same column or not, and the values of these elements may change. In the example above, the fixed constraints are not obvious. There are six different ages: 15, 14, 12, 10, 9, 7. It is known that brian is one year older than theresa (this statement is a flexible constraint, because the exact values are not known). From this, a conclusion can be made that brian is 15 or 10 years old (this statement is a fixed constraint, because the exact values are known).

4

THE FASYLOGIC LANGUAGE

A “logic grid” puzzle can be specified in FasyLogic using three kinds of components: type definition, fixed constraints, and flexible constraints. Each consists of one or more lines in arbitrary order. A line that begins with “#” is treated as a comment and will not be processed by the interpreter.

4.1 Type Definition This component defines the types and elements that are involved in the program. A type [name] is used to reference an element based on another element [or itself] which is located in the same column. The syntax definition of this component: TYPELINES Æ LINETYPE | TYPELINES NEWLINE TYPELINES NEWLINE Æ ’\n’ LINETYPE Æ TYPENAME COLON ELEMENTS TYPENAME Æ BIGLETTER | BIGLETTER STRINGBIG STRINGBIG Æ STRINGBIGPART | STRINGBIG STRINGBIGPART STRINGBIGPART Æ BIGLETTER | LETTER | DIGIT | SPECIAL COLON Æ ’:’ ELEMENTS Æ ELEMENT | ELEMENTS COMMA ELEMENTS

ELEMENT Æ STRING | DIGITS | MIN DIGITS STRING Æ STRINGPART | STRING STRINGPART STRINGPART Æ LETTER | DIGIT | SPECIAL BIGLETTER Æ A | B | ... | Z LETTER Æ a | b | ... | z DIGITS Æ DIGIT | DIGITS DIGIT DIGIT Æ 0 | 1 | ... | 9 COMMA Æ , MIN Æ SPECIAL Æ _

It can be seen from the syntax definition that TYPELINES consists of one or more LINETYPE in arbitrary order, separated by a newline character. For the example stated in section 3, the type definition is: Index: 1, 2, 3, 4, 5, 6 Kids: brian, curtis, roy, lily, michelle,theresa Age: 15, 14, 12, 10, 9, 7 Horse: boris, hunter, lady, ranger, santa_fe, topper

It means there are four types: Index, Kids, Age, and Horse. Each has six unique elements (the elements are separated by a comma). The type name must begin with an uppercase character and consists of alphanumeric characters and/or underscores. There is a reserved word, Index, which is used to assign values to the partial solution matrix before the interpreter starts finding a solution. All elements with type Index will be inserted into the matrix in the order of their definition.

4.2 Fixed Constraints This component specifies which elements are located in the same column. The syntax definition of this component: FACTLINES Æ LINEFACT | FACTLINES NEWLINE FACTLINES NEWLINE Æ ’\n’ LINEFACT Æ KNOWNFACT | LINEFACT B LINEFACT B Æ ’|’ KNOWNFACT Æ ELEMENT A ELEMENT A Æ ^ ELEMENT Æ STRING | DIGITS | MIN DIGITS STRING Æ STRINGPART | STRING STRINGPART STRINGPART Æ LETTER | DIGIT | SPECIAL LETTER Æ a | b | ... | z DIGITS Æ DIGIT | DIGITS DIGIT DIGIT Æ 0 | 1 | ... | 9 SPECIAL Æ _ MIN Æ -

It can be seen from the syntax definition that FACTLINES consists of one or more LINEFACT in arbitrary order, separated by a newline character. LINEFACT consists of one or more KNOWNFACT that are separated by the OR operator (“|”). This concept will be made clear by looking at the following fixed constraints definition for the example in section 3: brian ^ 15 | brian ^ 10 theresa ^ 14 | theresa ^ 9 topper ^ 15 | topper ^ 12 | topper ^ 10 ranger ^ 15 | ranger ^ 12

4 ^ 12 | 4 ^ 9 | 4 ^ 7 lily ^ 12 | lily ^ 9 roy ^ 15 | roy ^ 12 | roy ^ 10 michelle ^ 12 | michelle ^ 9 | michelle ^ 7 lady ^ 12 | lady ^ 10 | lady ^ 7

For the first line, it can be said that brian is 15 or 10 years old. The “^” operator is used to combine two elements that are known to be in the same column, while the “|” operator is used to branch the possibility. Likewise, for the last line, there are three possibilities for the age of lady: 12, 10, and 7.

4.3 Flexible Constraints This component defines the conditions that must be satisfied by the solution. In other words, it specifies all relations that exist between two elements, but the exact elements are not known yet. This relation is defined using a comparison operator and the involved elements are defined using the format ElementName.TypeName. For instance, it is known that roy is three years older than michelle. In this case the “older” relation can be defined using the type Age. It means roy’s age is written as roy.Age and michelle’s as michelle.Age. So the whole statement becomes roy.Age == michelle.Age + 3. The syntax definition of this component: CONDITIONLINES Æ LINECONDITION | CONDITIONLINES NEWLINE CONDITIONLINES NEWLINE Æ ’\n’ LINECONDITION Æ CONDITION | LINECONDITION C CONDITION CONDITION Æ E O E | ( CONDITION ) C Æ ’&’ | ’|’ O Æ ’==’ | ’=’ E Æ F | F A F F Æ ELEMENT | ELEMENTTYPE A Æ + | - | * | / | % ELEMENT Æ STRING | DIGITS | MIN DIGITS STRING Æ STRINGPART | STRING STRINGPART STRINGPART Æ LETTER | DIGIT | SPECIAL LETTER Æ a | b | ... | z DIGIT Æ 0 | 1 | ... | 9 DIGITS Æ DIGIT | DIGITS DIGIT SPECIAL Æ _ MIN Æ ELEMENTTYPE Æ ELEMENT DOT TYPENAME TYPENAME Æ BIGLETTER | BIGLETTER STRINGBIG STRINGBIG Æ STRINGBIGPART | STRINGBIG STRINGBIGPART STRINGBIGPART Æ BIGLETTER | LETTER | DIGIT | SPECIAL BIGLETTER Æ A | B | ... | Z DOT Æ .

CONDITIONLINES consists of one or more LINECONDITION in arbitrary order, separated by a newline character. LINECONDITION consists of one or more CONDITION that are separated by logical operators

(AND and OR). Each CONDITION consists of the left-handside, a comparison operator, and the right-handside. The example in section 3 gives more insight: brian.Age == theresa.Age + 1 curtis.Index > 1

brian.Index > 1 topper.Index > 2 12.Index > 2 roy.Index 6 boris.Index < 6 topper.Index >= theresa.Index + 2 9.Index < brian.Index & 9.Index < 4 santa_fe.Index < curtis.Index boris.Index == brian.Index - 1 & brian.Index == 12.Index - 1 topper.Age == 4.Age + 3 roy.Age == michelle.Age + 3 ranger.Age == lily.Age + 3 & lily.Age == lady.Age + 2 topper.Kids roy.Kids & 4.Kids roy.Kids & topper.Kids michelle.Kids & 4.Kids michelle.Kids lady.Kids == lily.Kids | lady.Kids == michelle.Kids | lady.Kids == theresa.Kids

The first line means brian is one year older than theresa. The second line means curtis’ horse does not live in the first stall. In all cases, the “&” is used as an AND operator and the “|” as an OR operator.

5

FINDING THE SOLUTION

When processing a FasyLogic program, it is assumed that the input program is always correct (conforms to the language specification and is a representation of the actual problem). If the interpreter encounters an error due to incorrect input, it will display an error message and the user is expected to correct the program. The algorithm to find the solution is rather complex but it can be outlined in the pseudocode below: In line 1, two variables with Stack data type are defined. colFacts is used to hold all fixed constraints and colConditions the flexible constraints. One line in the input program will become one element in colFacts or colConditions. In line 2, two variables with Matrix data type are defined. The Matrix data type is similar to a two-dimensional array. In this case, the size is the same as the size of the solution (number of rows = number of types, number of columns = number of elements for each type). K will be used as the partial solution matrix. E is an array to hold the enumeration of possible values for the empty elements in K. 1 2 3

colFacts: Stack, colConditions: Stack K: Matrix, E: array of Matrix i: Integer, n: Integer

4 5

colFacts = list of fixed constraints colConditions = list of flexible constraints

6

n = 1

7

WHILE(n == 1 | there exists untried colFacts combination) IF colFacts is empty THEN K = empty matrix ELSE K = the n-th valid combination of colFacts END-IF

8 9 10 11 12 13

E = enumeration of possible values for the empty elements in K

14

i = 1

15 16

WHILE(i