Searching

Searching is very important in AI and appears everywhere.

Once we have a good representation for a problem we need to search for a solution.

We will look at the following methods:

- Some Path
- Depth-first
- Hill climbing
- Breadth-first
- Best-first
- Optimal Path
- British museum
- Branch and bound
- A*
- Games
- Minimax
- Alpha-beta pruning
- Progressive Deepening
- Heuristic pruning
- Heuristic continuation

e.g. Find a way out of a maze or

Find the quickest route from Auckland to Wellington.

For the first, any route will do but for the second we want a near-optimal route.

We have a semantic net.

Easy solution: explore all possible paths.

We must avoid any repetition in the solution.

e.g. Auckland -> Hamilton -> Tauranga -> Taupo -> Hamilton

Bad Idea

The net is now a tree. For a net with n nodes the tree can have no more than n levels.

If all that is required is a solution then the tree can be searched by looking at all the terminal nodes(those at the bottom) one at a time.

O1 / \ O2 O /|\ |\ O3O O O O /| | |\ |\ O4O9O O O O O / \ | / \ |\ O5 O6O O O O O / \ / \ O7 O8 O O

Order that nodes are visited for depth first search.

Search Procedure:

- Form a one element queue consisting of the root node.
- Until the queue is empty or the root node has been reached, determine if the first element in the queue is the goal node.
- If it is the goal then do nothing
- otherwise: Remove the first element from the queue and add its children (if any) to the front of the queue.
- If the goal has been found then success otherwise failure.

If we can order the choices so that the most promising are explored first then the search will be more efficient.

Need a **Heuristic**

A heuristic is a measure of how close the solution is. For example, distance from Wellington or distance from maze exit would be good heuristics.

To Hill Climb:

- Form a one element queue consisting of the root node.
- Until the queue is empty or the goal has been reached, determine if the first element in the queue is the goal node.
- If it is the goal then do nothing
- otherwise: Remove the first element from the queue,
*sort the first element's children (if any) by estimated remaining distance,*and add to the front of the queue. - If the goal has been found then success otherwise failure.

e.g. find the highest point in Auckland city(OK).

or find the tallest building in Auckland city(not OK).

Two problems:

- Escaping from local solutions:
- There is no hill to climb:

/\ __ / \ _/ \___/ \ /

____ | | ___________| |______

O1 / \ O2 O3 /|\ |\ O4O5O6O7O8 /| | |\ |\ O9O O O O O O / \ | / \ |\ O O O O O O O / \ / \ O O O O

Search Procedure:

- Form a one element queue consisting of the root node.
- Until the queue is empty or the goal has been reached, determine if the first element in the queue is the goal node.
- If it is the goal then do nothing
- otherwise: Remove the first element from the queue and
add its children (if any) to the
*back*of the queue. - If the goal has been found then success otherwise failure.

Use a heuristic to decide which node to look at next.

Search Procedure:

- Form a one element queue consisting of the root node.
- Until the queue is empty or the goal has been reached, determine if the first element in the queue is the goal node.
- If it is the goal then do nothing
- otherwise: Remove the first element from the queue and
add its children (if any) to the queue,
*and sort the entire queue by estimated remaining distance*. - If the goal has been found then success otherwise failure.

In practice the queue need not be sorted, a hash table can be used.

Comparison of various search methods:

- Depth First: Good if tree does not get too deep.
- Breadth First: Good is the number of alternatives at each node is small.
- Hill Climbing: Good if a good heuristic can be found and good choices often lead to other good choices.
- Best First:Good when there is a natural measure of goal distance and a good path may still lead away from a solution.

Now we need to find not just a solution, but the best solution. Assume there is a cost associated with every node.

Search all possible paths. Use Breadth or Depth first but continue after a solution has been found. Choose the solution with the lowest cost.

Impractical except for trivial problems.

Instead of searching all possible paths, search in order of lowest cost. The first path found will be optimal.

Search Procedure:

- Form a queue of partial paths. Let the initial queue consist of the zero length, zero step path from the root node to nowhere.
- Until the queue is empty or the goal has been reached, determine if the first path in the queue reaches the goal node.
- If the first path reaches the goal node then do nothing
- otherwise:
- Remove the first path from the queue.
- Form new paths from the removed path by extending one step.
- Add the new paths to the queue.
- Sort the queue by total cost, with least cost at the front.
- If the goal has been found then success otherwise failure.

As well as a cost associated with each path, there may be a minimum possible distance to the goal. For example consider the following:

T---4----G--4--C /\ | W / \ 5 / 3 5 | 3 / \ | / A---4----H-2--B--4--P

Problem: find the optimum path from A to W

The minimum possible distance from any node to W is the straight line distance to W. We can use this to help the branch and bound algorithm.

Instead of using the cost, use the cost + minimum distance left. This will discourage steps away from the solution.

Some paths may be redundant e.g. A->T->H is a valid path but A-> H has a lower cost and so the former is redundant.

The A* procedure is a branch and bound search with the inclusion of a lower-bound estimate of remaining cost and removal of redundant paths.

Search Procedure:

- Form a queue of partial paths. Let the initial queue consist of the zero length, zero step path from the root node to nowhere.
- Until the queue is empty or the goal has been reached, determine if the first path in the queue reaches the goal node.
- If the first path reaches the goal node then do nothing
- otherwise:
- Remove the first path from the queue.
- Form new paths from the removed path by extending one step.
- Add the new paths to the queue.
- Sort the queue by the sum of total cost
*and a lower-bound estimate of the cost remaining*, with least cost at the front. - If two or more paths reach a common node, delete all those paths except for the one that reaches the common node with minimum cost.
- If the goal has been found then success otherwise failure.