Simon Fraser University School of Computing Science Lecture #7: Constructive Search Methods ☞ Introduction • Historical perspective from Artificial Intelligence.

• AI = Knowledge-based Search.

• Nondeterministic programming (PLANNER, ABSYS, Conniver, Prolog)

2Remember a binary CSP is equivalent to satisfying ∃ ∈ , ..., ∃ ∈ a1 D1 an Dn ∧ ... ∧ ∧ ∧ ∧ ∧ P1(a1) Pn(an) P12(a1,a2) P13(a1,a3) ... Pn-1,n(an-1,an). • Constructing a solution is an obvious approach.

October 2, 1998 Copyright © 1997 by Bill Havens 1 of 52

Simon Fraser University School of Computing Science Constructive Methods for solving CSPs: 1. British Museum Algorithm 2. Generate and Test 3. Chronological 4. Intelligent Backtracking (various) 5. Network Consistency Algorithms (various) 6. Hybrid approaches (various)

October 2, 1998 Copyright © 1997 by Bill Havens 2 of 52

Simon Fraser University School of Computing Science British Museum Algorithm • Worst search algorithm possible

• Works but never used!

• Not guaranteed to terminate (incomplete).

• Apocryphal organization to British Museum collection. BritishMuseum (D)

For some random tuple (a1,..., an) ∈ D1 × ... × Dn, If P1(a1) ∧ ... ∧ Pn(an) ∧ P12(a1,a2) ∧ P13(a1,a3) ∧ ... ∧ Pn-1,n(an-1,an) then Halt (a1,..., an) end. 2What’s wrong with this algorithm?

October 2, 1998 Copyright © 1997 by Bill Havens 3 of 52 Simon Fraser University School of Computing Science Generate and Test Approach ☞ Overview • Better search algorithm

• Works fine and frequently used! (for small problems) n • Complexity: O[d ] where d = |Di| and n = |V|. • Very simple to implement (nested “for-loops” approach)

• Algorithm . . .

October 2, 1998 Copyright © 1997 by Bill Havens 4 of 52 Simon Fraser University School of Computing Science

Generate&Test (V, D)

For some value a1 ∈ D1 do, For some value a2 ∈ D2 do, . .

For some value an ∈ Dn do, If P1(a1) ∧ ... ∧ Pn(an) ∧ P12(a1,a2) ∧ P13(a1,a3) ∧ ... ∧ Pn-1,n(an-1,an) then Halt (a1, a2, ... , an) end.

2The C-hackers favourite search algorithm !

October 2, 1998 Copyright © 1997 by Bill Havens 5 of 52 Simon Fraser University School of Computing Science Chronological Backtracking ☞ Overview • Most popular search algorithm (Prolog + most Expert Systems Shells)

• More formally Depth-First Search (more later).

• Also slow!

• Complexity: O[kn] for k < d

• AB-Satisfy called initially: AB-Satisfy(V, D, ())

• Algorithm . . .

October 2, 1998 Copyright © 1997 by Bill Havens 6 of 52 Simon Fraser University School of Computing Science

AB-Satisfy (V, D, X) If null(V) then Halt(X)

Let Vi = first(V) Let Di = first(D) For each value ai ∈ Di do, if P1(a1)∧ ... ∧P1i(a1,ai)∧ ... ∧Pji(aj,ai) for j < i then

AB-Satisfy (rest(V), rest(D), cons(ai, X)) Return false. end.

2So why is Chronological Backtracking used in modern applications (e.g. Prolog)?

October 2, 1998 Copyright © 1997 by Bill Havens 7 of 52 Simon Fraser University School of Computing Science Backtracking Example ☞ Map Colouring again red green 4 blue ≠ ≠ red 3 red green ≠ green blue 1 blue ≠ ≠ 2 red green blue

m Can AB-Satisfy colour this map? • Transcript of recursive calls . . .

October 2, 1998 Copyright © 1997 by Bill Havens 8 of 52 Simon Fraser University School of Computing Science Transcript

nV D X Vi ai Comments

0{V1V2V3V4}{D1D2D3D4}{} V1 r Initial call

1{V2V3V4}{D2D3D4} {r} V2 r ~(V1=r, V2=r) ≠ 1V2 gV1 V2

2{V3V4}{D3D4} {g,r} V3 r ~(V1=r, V3=r)

2V3 g ~(V2=g, V3=g) ≠ ≠ 2V3 bV1 V2 V3

3{V4}{D4} {b,g,r} V4 r ~(V1=r, V4=r) ≠ ≠ 3V4 gV1 V4, V3 V4 4 {} {} {g,b,g,r} done

m Note: only shallow backtracking in this example.

October 2, 1998 Copyright © 1997 by Bill Havens 9 of 52 Simon Fraser University School of Computing Science Search Algorithms ☞ Introduction • Search problems can frequently be visualized as searching a tree of nodes.

• Each node represents a problem state

• Each branch represents a search choice.

• The root represents the start node of the search.

• Some leaves of the tree represent goal states.

• All other leaf nodes are failures.

October 2, 1998 Copyright © 1997 by Bill Havens 10 of 52 Simon Fraser University School of Computing Science Example:

October 2, 1998 Copyright © 1997 by Bill Havens 11 of 52 Simon Fraser University School of Computing Science

• Map Colouring example again as tree search

·

V1 = r V1 = g V1 = b · · · · · · ··················

V2 = r V2 = g V2 = b ··· V3 = r ··

V3 = r V3 = g V3 = b ··· V4 = r ·· V4 = rV4 = gV4 = b

October 2, 1998 Copyright © 1997 by Bill Havens 12 of 52 Simon Fraser University School of Computing Science Generality of Tree Search ☞ Many problems can be represented as Tree Search • Map Colouring (above)

• Route Planning (eg- Travelling Salesman Problems)

• Resource Allocation (scheduling)

• Puzzles (eg- Crosswords)

m CSPs in general can be represented as search trees as follows: • The root of the tree corresponds to zero variables being instantiated.

• Each level in the tree corresponds to another variable being instantiated.

October 2, 1998 Copyright © 1997 by Bill Havens 13 of 52 Simon Fraser University School of Computing Science

• The branches of each node correspond to value assignments to the variable.

• The leaves of the tree correspond to consistent assignments of all variables (and hence solutions).

2Note: the chronological backtrack procedure called AB-Satisfy constructs and searches a tree of variable assignments as above.

October 2, 1998 Copyright © 1997 by Bill Havens 14 of 52 Simon Fraser University School of Computing Science Generalized Tree Search Algorithm ☞ Overview • Tree search can be viewed as variations on a general method.

• L = an initial list of nodes to be search (usually one root node). GeneralSearch(L) If null(L) then return(failure). Let n = some node removed from L. If GoalNode(n) then return(n) else GeneralSearch(append(children(n), L))). end.

October 2, 1998 Copyright © 1997 by Bill Havens 15 of 52 Simon Fraser University School of Computing Science

• Note that GeneralSearch is an iterative algorithm (c.f. AB- Satisfy)

• Variations in search method focus on selection of which node to examine in step 2 above.

• We will consider both Blind and Heuristic Search Methods. ☞ Blind Search Methods: 1. Breadth-First Search 2. Depth-First Search 3. Iterative Deepening

Reference: M. Ginsberg (1993) Essentials of Artificial Intelligence, Morgan Kauf- mann, San Francisco.

October 2, 1998 Copyright © 1997 by Bill Havens 16 of 52 Simon Fraser University School of Computing Science Breadth-First Search ☞ Introduction • A basic type of blind search (i.e. no heuristic information available).

• Explores the search tree uniformly one level at a time.

• Every node at depth d is explored before any node at depth d+1.

• Initially called with a list L of start nodes.

October 2, 1998 Copyright © 1997 by Bill Havens 17 of 52 Simon Fraser University School of Computing Science Algorithm Breadth-FirstSearch(L) If null(L) then return(failure). Let n = first(L). Let L = rest(L). If GoalNode(n) then return(n) else Breadth-FirstSearch(append(L, children(n)))). end.

• Note that every node already on list L will be explored before any of the newly added children to L.

October 2, 1998 Copyright © 1997 by Bill Havens 18 of 52 Simon Fraser University School of Computing Science Breadth-First Search Order • Given the search tree below [from Ginsberg]

Figure 3.1

• Breadth-FirstSearch provides the following search order (assuming the children of a node are expanded left-to-right)

Figure 3.2

October 2, 1998 Copyright © 1997 by Bill Havens 19 of 52 Simon Fraser University School of Computing Science

☞ Complexity • Let b = branching factor of the tree (for CSPs, b = domain size)

• Let d = depth of the tree (to a goal node).

• Space Complexity is O[bd/2] since list L must contain on average 1/2 of the complete fringe of the tree constructed to depth d.

• Time Complexity is likewise approximately O[bd/2] since the algorithm must spend time constructing the fringe of the tree. ☞ Properties • Sound procedure for searching arbitrary (infinite trees). Why?

• Finds shortest solution path possible.

October 2, 1998 Copyright © 1997 by Bill Havens 20 of 52 Simon Fraser University School of Computing Science Depth-First Search ☞ Overview • Most popular form of blind search (e.g. AB-Satisfy above).

• Search tree is examined from the top-down.

• Every descendent of a node is explored before any of its siblings.

• Initially called with a list L of start nodes.

October 2, 1998 Copyright © 1997 by Bill Havens 21 of 52 Simon Fraser University School of Computing Science Algorithm Depth-FirstSearch(L) If null(L) then return(failure). Let n = first(L). Let L = rest(L). If GoalNode(n) then return(n) else Depth-FirstSearch(append(children(n), L))). end.

October 2, 1998 Copyright © 1997 by Bill Havens 22 of 52 Simon Fraser University School of Computing Science Depth-First Search Order • The search order for the previous search tree is:

Figure 3.4

☞ Complexity of Depth-First Search • Space Complexity is O[d] since the algorithm need only remember the path from the root to the current node at depth d.

• Time Complexity is O[bd/2] since the algorithm on average will explore 1/2 of the entire search space.

• See Ginsberg[93] for a more detailed discussion.

October 2, 1998 Copyright © 1997 by Bill Havens 23 of 52 Simon Fraser University School of Computing Science Properties of Depth-First Search • Cannot search infinite trees.

• Cannot find shortest path solution.

• Much better space complexity than Breadth-First Search.

October 2, 1998 Copyright © 1997 by Bill Havens 24 of 52 Simon Fraser University School of Computing Science Iterative Deepening Search m Is it possible to have best properties of Breadth- First and Depth-First Search methods? ☞ Overview • Space requirements of Depth-First Search.

• Performance of Breadth-First Search (shortest path and infinite trees).

2Basic Idea: Use Breadth-First method but do not save fringe nodes of search tree.

October 2, 1998 Copyright © 1997 by Bill Havens 25 of 52 Simon Fraser University School of Computing Science Details • Assume it is more expensive to store the interior nodes of the search tree than to regenerate them as needed.

• Storing requires O[bd] space.

• Regenerating requires O[d] space.

• Define a depth cut-off “c” which is iteratively deepened to realize the Breadth-First Search but do not store the fringe of the tree at depth c.

• IterDeepSearch(nil, L0, 0) is called with a list L0 of initial nodes and a cutoff c=0.

October 2, 1998 Copyright © 1997 by Bill Havens 26 of 52 Simon Fraser University School of Computing Science Algorithm

IterDeepSearch(L, L0, c) if null(L)

then IterDeepSearch(L0, L0, c+1) else Let n = first(L). Let L = rest(L). if goal(n) then halt(n) if depth(n) < c then IterDeepSearch(append(children(n), L), L0, c) else

IterDeepSearch(L, L0, c) end.

October 2, 1998 Copyright © 1997 by Bill Havens 27 of 52 Simon Fraser University School of Computing Science Iterative Deepening Search Order

Figure 3.10

m Check this search order against my implementation of Iterative Deepening.

October 2, 1998 Copyright © 1997 by Bill Havens 28 of 52 Simon Fraser University School of Computing Science Performance of Iterative Deepening • Returns shortest path to goal from start node(s) like Breadth- First Search.

• Uses space like Depth-First Search

• Often search method of choice for Tree Search Problems. ☞ Complexity of Iterative Deepening • From above arguments:

• Space complexity is O[d]

• Time complexity is O[bd] for large branching factors.

October 2, 1998 Copyright © 1997 by Bill Havens 29 of 52 Simon Fraser University School of Computing Science Heuristic Search Methods ☞ Overview m Basic Idea: Search the tree in an order more likely to find solutions than a random order. • Complexity of a depth-first search of a tree where the goal node is the left-most branch at depth d is only O[d]. Why?

• Definition: a Search Heuristic is an ordering of the search tree such that nodes earlier in the order are more likely to be goal nodes.

• Identifying “good” heuristics is hard. Identifying “perfect” heuristics is impossible. Why? (NP-completeness)

2In search, heuristics often estimate the distance to a goal node from some current node.

October 2, 1998 Copyright © 1997 by Bill Havens 30 of 52 Simon Fraser University School of Computing Science Example: the 8-Puzzle

Figure 4.1

• Given initial state (on left) find goal state (on right). ☞ 8-Puzzle can be expressed as a search tree. 1. Start node is initial configuration. 2. Goal node is final configuration 3. Interior nodes are other reachable configurations. 4. Branches are legal moves of the blank tile (black).

October 2, 1998 Copyright © 1997 by Bill Havens 31 of 52 Simon Fraser University School of Computing Science

m What is the branching factor and possible depth of this tree ? ☞ Heuristic-1: • The value of the current node is the number of numbered tiles which are out of place.

• Distance to goal node is at least this far.

• Start node has 3 tiles out of place. (How many actual moves required?) ☞ Heuristic-2: • The value of the current node is the “Manhattan Distance” that every tile must move to reach a goal state.

• Much better estimate but still most probably low.

• Manhattan distance of start node is also 3.

October 2, 1998 Copyright © 1997 by Bill Havens 32 of 52 Simon Fraser University School of Computing Science Maze Example of Heuristics ☞ Overview • Mazes are similar to problems in route planning and graph searching.

• Mazes can be represented as a Tree Search Problem.

• Given the maze below, what is a good heuristic?

Figure 4.2

October 2, 1998 Copyright © 1997 by Bill Havens 33 of 52 Simon Fraser University School of Computing Science Heuristic: • The distance to the goal from any node is the Manhattan distance from the node to the exit of the maze.

• The hapless wanderer must move at least this far to escape.

m Does this heuristic work well for mazes? • Consider the start node of the search tree. Start Down Straight ·· • What are the heuristic values for moves Down and Straight?

• Does this heuristic work well for mazes? Why/Why not?

Note: maze designers are perverse people!

October 2, 1998 Copyright © 1997 by Bill Havens 34 of 52 Simon Fraser University School of Computing Science

☞ Goal Functions • Consider search as the problem of maximizing a function of many variables over a search space.

• Solution is the a point with a maximum value of the function.

• May be many such maximal points.

m Analogy to a blind hiker looking for the summit of a mountain. • How can we realize such a search method? ...

October 2, 1998 Copyright © 1997 by Bill Havens 35 of 52 Simon Fraser University School of Computing Science Simple ☞ Overview • Hill Climbing is the general method of realizing search as iterative function maximization.

• Definition: an evaluation function f(p) returns the value of the point p in the search space of the problem. Higher values of f correspond to points which are closer to a solution to the given problem.

• Heuristics are often used as evaluation functions (e.g. Manhattan Distance).

October 2, 1998 Copyright © 1997 by Bill Havens 36 of 52 Simon Fraser University School of Computing Science Geometric Analogy

October 2, 1998 Copyright © 1997 by Bill Havens 37 of 52 Simon Fraser University School of Computing Science Hill Climbing Algorithm • Let L be an initial list of nodes sorted by expected distance f from the goal node. HillClimb(L) if null(L) then return(failure). Let n = first(L). Let L = rest(L). if goal(n) then return(n) else Let C = children(n) sorted by function f. HillClimb(append(C, L) end.

Note: HillClimb has a depth-first flavour. Why?

October 2, 1998 Copyright © 1997 by Bill Havens 38 of 52 Simon Fraser University School of Computing Science Problems with Hill Climbing • Consider these situations

Figure 4.3

• What can go wrong?

October 2, 1998 Copyright © 1997 by Bill Havens 39 of 52 Simon Fraser University School of Computing Science Best-First Search ☞ Overview • Similar to Hill Climbing but does not assume a single locale as the fringe of the search.

• Analogous to Breadth-First Search

• Given initially a list L of nodes to search.

October 2, 1998 Copyright © 1997 by Bill Havens 40 of 52 Simon Fraser University School of Computing Science Algorithm BestFirst(L) if null(L) then return(failure). Let n = node on L with highest evaluation f(n). Let L = L less node n. if goal(n) then return(n) else BestFirst(append(L, children(n))) end.

October 2, 1998 Copyright © 1997 by Bill Havens 41 of 52 Simon Fraser University School of Computing Science The Amazing A* Algorithm ☞ Introduction • Often finding the shortest path solution is the desired for a given CSP.

• Examples: puzzles, mazes, , routing, ...

• Breadth-First Search naturally finds a shortest # branches (moves).

• Equivalent to shortest path only if each branch in the search tree has unit cost / value / length.

• Not generally true (e.g. travelling salesman, minimum distance problems).

m How can we modify Best-First type search to solve arbitrary path problems?

October 2, 1998 Copyright © 1997 by Bill Havens 42 of 52 Simon Fraser University School of Computing Science Basic Idea 1. Keep track of the distance (depth, cost) covered in the search so far for each node. 2. Estimate the cost from each node to the goal. 3. Compare these values to decide which node to expand next.

October 2, 1998 Copyright © 1997 by Bill Havens 43 of 52 Simon Fraser University School of Computing Science Example

Figure 4.8 of Ginsberg

• Hill-Climbing and Best-First Search fail to find an optimal.

• Nodes labelled with estimate to goal.

• Search order is in parentheses.

• Finds left goal node instead of closer right goal node.

m Gamblers Folly: always willing to invest a little more for a likely winner.

October 2, 1998 Copyright © 1997 by Bill Havens 44 of 52 Simon Fraser University School of Computing Science Definitions • Let h’(n) = minimum distance from node n to a goal.

• Let g(n) = the distance (cost) of the node n from the root.

• The estimated distance f(n) from the root through node n to the goal is therefore f(n) = g(n) + h’(n).

• We expand the node n on the fringe of the graph with minimun f(n).

October 2, 1998 Copyright © 1997 by Bill Havens 45 of 52 Simon Fraser University School of Computing Science Algorithm A* • Initially L is a list of starting nodes. A*(L) if null(L) then return(failure). Let n = node on L with lowest f(n) = g(n) + h’(n). Let L = L less node n. if goal(n) then return(n) else A*(append(L, children(n))) end.

October 2, 1998 Copyright © 1997 by Bill Havens 46 of 52 Simon Fraser University School of Computing Science Example • Consider previous example again

Figure 4.9 of Ginsberg

• What is the order of search using A* ?

October 2, 1998 Copyright © 1997 by Bill Havens 47 of 52 Simon Fraser University School of Computing Science Admissibility • Let h(n) = the actual distance of node n from the goal.

• Definition: a heuristic is admissible if its estimate h’(n) ≤ h(n).

• Theorem: If h’(n) is admissible for every node n in the search tree, then any solution found by A* is optimal.

• A* is a combination of Breadth-First search (yielding an optimal solution) and Best-First search (yielding heuristic search).

Note: If h’(n) = 0 then it is always admissible and A* = Breadth-First. ☞ Examples of Admissible Heuristics • For the 8-puzzle, h’(n) = # tiles misplaced ≤ Manhattan distance ≤ h(n).

• Both are admissible.

October 2, 1998 Copyright © 1997 by Bill Havens 48 of 52 Simon Fraser University School of Computing Science Type Search ☞ Overview • Can optimize a CSP by solving a sequence of more constrained CSPs.

• Similar to traditional Branch&Bound algorithm.

• Actually used in CLP implementations.

October 2, 1998 Copyright © 1997 by Bill Havens 49 of 52 Simon Fraser University School of Computing Science Method • Given a CSP P = (V, D, C) apply the following algorithm.

• Assume a distinguished variable f ∈ V that represents the value of the solution to P.

• Definitions: Given a CSP P = (V, D, C) and f’ is the value of the previous solution S’.

• Use any CSP solver algorithm.

• Initial call is B&BSearch(P, nil, 0.0)

October 2, 1998 Copyright © 1997 by Bill Havens 50 of 52 Simon Fraser University School of Computing Science Algorithm B&BSearch (P, S’, f’) Attempt to solve P yielding solution S with cost f. If unsatisfiable P then return S’ with cost f’ else Create a new instance P1 of P. Add a new constraint (f’ < f) to P1. B&BSearch(P1, S, f). end.

October 2, 1998 Copyright © 1997 by Bill Havens 51 of 52 Simon Fraser University School of Computing Science Discussion • Iteratively finds better-and-better solutions.

• Can be programmed to prune partial solutions (consider the search for a solution for P’ with a new constraint).

• Special purpose bounding functions required in host CLP language.

• Example: prune if lower_bound(partial_solution) ≥ cost

October 2, 1998 Copyright © 1997 by Bill Havens 52 of 52