1.7 Classifying problems
2.1.6 Tree search with no trees
To proceed, it would be handy to introduce the concepts of state,state space, feasible stateandcontracted state. Statemeans any grounding of domain values
to all decision variables, thestate space is given by all groundings of domain values to all decision variables, afeasible stateis a state for which all constraints are satisfied, acontracted state means any grounding of domain values tosome decision variables12.
The goal of anyProlog (andCLP) program is to satisfy aquery that is the head of some rule. The Prolog (CLP) compiler contains an inference system thatsearches thestate spacefor afeasible statethat will satisfy the query. This is done by generating sequences ofcontracted statesleading to the feasible state, provided a feasible state exists. If so,Yesis followed by some detailed messages. If no feasible state exists,No will be printed. AllProlog (and CLP) programs discussed farther will always have the query top; this makes for convenient testing.
Search denotes the following sequence of steps:
1. Selecting adecision variable from the body of the rule defining the query; 2. Grounding the selecteddecision variable, i.e. assigning to it a value from its domain. Thereby a contracted state is generated and the selected de- cision variable is termedgrounded;
3. Spreading the value of the grounded decision variable to all its instances in the body of the rule;
4. Testing the satisfaction of all predicates in the body of the rule using unification:
• if this is not possible because some predicates are not grounded, steps 1, 2 and 3 are repeated for the next nearest variable or for the rule defining this predicate, until eventually all predicates are grounded and satisfied;
• if all predicates in the body of the query are grounded and satisfied, the query is satisfied and the variable values used for grounding are displayed as the program solution;
• if some predicate in the body of the query fails, the latest selected variable isdegrounded, a return is performed leftwards to the nearest tested predicate with variables not yet grounded to some values from 12The concept of state is - to the best knowledge of the Author - not particularly en
vogue in theCLP community. The Author, because of his control-engineering and dynamic system background, is missing it from ever since, and uses this opportunity to show its broad usefulness while discussingCLP.
their domains, and one of the variables is regrounded. While return- ing, all variables that have been successfully grounded between the said nearest tested predicate and the failed predicate, are degrounded as well. The return, the degrounding, and the regrounding is named backtracking, and the predicate with variables of yet untested values to which the return was performed, is namedchoice point.
It should be emphasized that any variable grounded to some value may be grounded to another value only as the result of backtracking.
This is illustrated by the following simpleProlog program2_1_search.pl:
/*1*/ a(X,Y) :- /*2*/ b(X), /*3*/ c(X,Y). /*4*/ b(1). /*5*/ b(&). /*6*/ c(&,"A").
The programs query isa(X,Y). This means that the program aims at find- ing such values for decision variablesXandYthat satisfya(X,Y). The program contains one rule (lines/*1*/,/*2*/, and/*3*/) and three facts (lines/*4*/, /*5*/, and/*6*/). The indentation for lines/*2*/and/*3*/is used to make the rule better readable. The domains for variablesXandYare defined implic- itly by the facts: the domain ofXis (1,&), the domain ofYis"A".
The rule states that in order to satisfy a(X,Y) such value for X has to be found that satisfies b(X), and such value for Y has to be found that together with the value forXsatisfiesc(X,Y). The conditions for the rule are queried in a top-down fashion, so the first value found forXisX=1. Because the domain ofX contains another value&, a choice point is created forb(X). Next, the valueX=1 is spread to line/*3*/resulting inc(1,Y), which does not unify withc(&,"A") from line/*6*/. Soc(1,Y)is unsatisfied,Xis degrounded from its value1and a return to the choice point forb(X)follows. NowXis regrounded with&, the regrounding is spread to line/*3*/resulting inc(&,Y)that successfully unifies withc(&,"A")from line /*6*/giving the solutionX = &,Y = "A".
The process described may be interpreted as running according to thesearch treefrom Figure 2.2 that reflects the program structure.
For obvious reasons the search from Figure 2.2 is known as top-down search ordepth-first search. The way returns are generated (as the result of violating
Figure 2.2: Search tree for simple Prolog program
some constraint) is known as standard backtracking. Therefore the full name of this search is Depth-First Backtracking Search or Top-Down Backtracking Search.
The search tree is defined by all states of the decision variables that consti- tute the leaves of the search tree. For this example they are(X,Y) = (1,"A") and(X,Y) = (&,"A"). The intermediate node (there is only one node for this example) of the search tree corresponds to the choice point, where the value of Xis chosen.
The amazing thing is that search trees are never explicitly present in its entirety, but simply generated piecewise, on-the-fly. In the discussed example the left-hand branch from Figure 2.2 is generated first, but after the failed unification in line /*3*/it is dropped save the choice point(1)- (1’), to be used for generating the right-hand branch. The mechanism of making search
trees with no trees present in its entirety is of great practical significance because it allows Prolog and CLP languages to deal with problems corresponding to search trees of exorbitant sizes.
An important regularity from the example deserves to be emphasized:
• groundingof free variables occurs in top-down search any time a predicate with free variables is encountered;
• degrounding of grounded variables occurs when the last grounding results in some constraint to be unsatisfied and a return to the nearest choice point is done, where this (or some other free variable) may beregrounded. There is no other way for grounded variables to change their values. It should be emphasized, that Prologs search and unifications constitute a complete inference method. It means that if a solution to a CSP modelled in Prolog exists, it will be determined13.